aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_controller.c
diff options
context:
space:
mode:
authorYawning Angel <yawning@schwanenlied.me>2015-04-25 08:23:15 +0000
committerNick Mathewson <nickm@torproject.org>2015-04-28 10:19:08 -0400
commit915c7438a77edfaf3103b69cb494a4f069a79a0c (patch)
tree1aec80453d2aa691de928349d233a85374b4305c /src/test/test_controller.c
parent63a90f2df4dcd7fff862ca3849f3aa3b1dec7e84 (diff)
downloadtor-915c7438a77edfaf3103b69cb494a4f069a79a0c.tar.gz
tor-915c7438a77edfaf3103b69cb494a4f069a79a0c.zip
Add "ADD_ONION"/"DEL_ONION" and "GETINFO onions/*" to the controller.
These commands allow for the creation and management of ephemeral Onion ("Hidden") services that are either bound to the lifetime of the originating control connection, or optionally the lifetime of the tor instance. Implements #6411.
Diffstat (limited to 'src/test/test_controller.c')
-rw-r--r--src/test/test_controller.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/test/test_controller.c b/src/test/test_controller.c
new file mode 100644
index 0000000000..9039dc7677
--- /dev/null
+++ b/src/test/test_controller.c
@@ -0,0 +1,161 @@
+/* Copyright (c) 2015, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define CONTROL_PRIVATE
+#include "or.h"
+#include "control.h"
+#include "rendservice.h"
+#include "test.h"
+
+static void
+test_add_onion_helper_keyarg(void *arg)
+{
+ crypto_pk_t *pk = NULL;
+ crypto_pk_t *pk2 = NULL;
+ const char *key_new_alg = NULL;
+ char *key_new_blob = NULL;
+ char *err_msg = NULL;
+ char *encoded = NULL;
+ char *arg_str = NULL;
+
+ (void) arg;
+
+ /* Test explicit RSA1024 key generation. */
+ pk = add_onion_helper_keyarg("NEW:RSA1024", 0, &key_new_alg, &key_new_blob,
+ &err_msg);
+ tt_assert(pk);
+ tt_str_op(key_new_alg, OP_EQ, "RSA1024");
+ tt_assert(key_new_blob);
+ tt_assert(!err_msg);
+
+ /* Test "BEST" key generation (Assumes BEST = RSA1024). */
+ crypto_pk_free(pk);
+ tor_free(key_new_blob);
+ pk = add_onion_helper_keyarg("NEW:BEST", 0, &key_new_alg, &key_new_blob,
+ &err_msg);
+ tt_assert(pk);
+ tt_str_op(key_new_alg, OP_EQ, "RSA1024");
+ tt_assert(key_new_blob);
+ tt_assert(!err_msg);
+
+ /* Test discarding the private key. */
+ crypto_pk_free(pk);
+ tor_free(key_new_blob);
+ pk = add_onion_helper_keyarg("NEW:BEST", 1, &key_new_alg, &key_new_blob,
+ &err_msg);
+ tt_assert(pk);
+ tt_assert(!key_new_alg);
+ tt_assert(!key_new_blob);
+ tt_assert(!err_msg);
+
+ /* Test generating a invalid key type. */
+ crypto_pk_free(pk);
+ pk = add_onion_helper_keyarg("NEW:RSA512", 0, &key_new_alg, &key_new_blob,
+ &err_msg);
+ tt_assert(!pk);
+ tt_assert(!key_new_alg);
+ tt_assert(!key_new_blob);
+ tt_assert(err_msg);
+
+ /* Test loading a RSA1024 key. */
+ tor_free(err_msg);
+ pk = pk_generate(0);
+ tt_int_op(0, OP_EQ, crypto_pk_base64_encode(pk, &encoded));
+ tor_asprintf(&arg_str, "RSA1024:%s", encoded);
+ pk2 = add_onion_helper_keyarg(arg_str, 0, &key_new_alg, &key_new_blob,
+ &err_msg);
+ tt_assert(pk2);
+ tt_assert(!key_new_alg);
+ tt_assert(!key_new_blob);
+ tt_assert(!err_msg);
+ tt_assert(crypto_pk_cmp_keys(pk, pk2) == 0);
+
+ /* Test loading a invalid key type. */
+ tor_free(arg_str);
+ tor_asprintf(&arg_str, "RSA512:%s", encoded);
+ pk = add_onion_helper_keyarg(arg_str, 0, &key_new_alg, &key_new_blob,
+ &err_msg);
+ tt_assert(!pk);
+ tt_assert(!key_new_alg);
+ tt_assert(!key_new_blob);
+ tt_assert(err_msg);
+
+ /* Test loading a invalid key. */
+ tor_free(arg_str);
+ tor_free(err_msg);
+ encoded[strlen(encoded)/2] = '\0';
+ tor_asprintf(&arg_str, "RSA1024:%s", encoded);
+ pk = add_onion_helper_keyarg(arg_str, 0, &key_new_alg, &key_new_blob,
+ &err_msg);
+ tt_assert(!pk);
+ tt_assert(!key_new_alg);
+ tt_assert(!key_new_blob);
+ tt_assert(err_msg);
+
+ done:
+ crypto_pk_free(pk);
+ crypto_pk_free(pk2);
+ tor_free(key_new_blob);
+ tor_free(err_msg);
+ tor_free(encoded);
+ tor_free(arg_str);
+}
+
+static void
+test_rend_service_parse_port_config(void *arg)
+{
+ const char *sep = ",";
+ rend_service_port_config_t *cfg = NULL;
+ char *err_msg = NULL;
+
+ (void)arg;
+
+ /* Test "VIRTPORT" only. */
+ cfg = rend_service_parse_port_config("80", sep, &err_msg);
+ tt_assert(cfg);
+ tt_assert(!err_msg);
+
+ /* Test "VIRTPORT,TARGET" (Target is port). */
+ rend_service_port_config_free(cfg);
+ cfg = rend_service_parse_port_config("80,8080", sep, &err_msg);
+ tt_assert(cfg);
+ tt_assert(!err_msg);
+
+ /* Test "VIRTPORT,TARGET" (Target is IPv4:port). */
+ rend_service_port_config_free(cfg);
+ cfg = rend_service_parse_port_config("80,192.0.2.1:8080", sep, &err_msg);
+ tt_assert(cfg);
+ tt_assert(!err_msg);
+
+ /* Test "VIRTPORT,TARGET" (Target is IPv6:port). */
+ rend_service_port_config_free(cfg);
+ cfg = rend_service_parse_port_config("80,[2001:db8::1]:8080", sep, &err_msg);
+ tt_assert(cfg);
+ tt_assert(!err_msg);
+
+ /* XXX: Someone should add tests for AF_UNIX targets if supported. */
+
+ /* Test empty config. */
+ rend_service_port_config_free(cfg);
+ cfg = rend_service_parse_port_config("", sep, &err_msg);
+ tt_assert(!cfg);
+ tt_assert(err_msg);
+
+ /* Test invalid port. */
+ tor_free(err_msg);
+ cfg = rend_service_parse_port_config("90001", sep, &err_msg);
+ tt_assert(!cfg);
+ tt_assert(err_msg);
+
+ done:
+ rend_service_port_config_free(cfg);
+ tor_free(err_msg);
+}
+
+struct testcase_t controller_tests[] = {
+ { "add_onion_helper_keyarg", test_add_onion_helper_keyarg, 0, NULL, NULL },
+ { "rend_service_parse_port_config", test_rend_service_parse_port_config, 0,
+ NULL, NULL },
+ END_OF_TESTCASES
+};
+