Back to index

tor  0.2.3.19-rc
test_config.c
Go to the documentation of this file.
00001 /* Copyright (c) 2001-2004, Roger Dingledine.
00002  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00003  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00004 /* See LICENSE for licensing information */
00005 
00006 #include "orconfig.h"
00007 #include "or.h"
00008 #include "config.h"
00009 #include "connection_edge.h"
00010 #include "test.h"
00011 
00012 static void
00013 test_config_addressmap(void *arg)
00014 {
00015   char buf[1024];
00016   char address[256];
00017   time_t expires = TIME_MAX;
00018   (void)arg;
00019 
00020   strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
00021           "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
00022           "MapAddress *.google.com *.torserver.exit\n"
00023           "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
00024           "MapAddress *.cn.com www.cnn.com\n"
00025           "MapAddress *.cnn.com www.cnn.com\n"
00026           "MapAddress ex.com www.cnn.com\n"
00027           "MapAddress ey.com *.cnn.com\n"
00028           "MapAddress www.torproject.org 1.1.1.1\n"
00029           "MapAddress other.torproject.org "
00030             "this.torproject.org.otherserver.exit\n"
00031           "MapAddress test.torproject.org 2.2.2.2\n"
00032           "MapAddress www.google.com 3.3.3.3\n"
00033           "MapAddress www.example.org 4.4.4.4\n"
00034           "MapAddress 4.4.4.4 7.7.7.7\n"
00035           "MapAddress 4.4.4.4 5.5.5.5\n"
00036           "MapAddress www.infiniteloop.org 6.6.6.6\n"
00037           "MapAddress 6.6.6.6 www.infiniteloop.org\n"
00038           , sizeof(buf));
00039 
00040   config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
00041   config_register_addressmaps(get_options());
00042 
00043   /* MapAddress .invalidwildcard.com .torserver.exit  - no match */
00044   strlcpy(address, "www.invalidwildcard.com", sizeof(address));
00045   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00046 
00047   /* MapAddress *invalidasterisk.com .torserver.exit  - no match */
00048   strlcpy(address, "www.invalidasterisk.com", sizeof(address));
00049   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00050 
00051   /* Where no mapping for FQDN match on top-level domain */
00052   /* MapAddress .google.com .torserver.exit */
00053   strlcpy(address, "reader.google.com", sizeof(address));
00054   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00055   test_streq(address, "reader.torserver.exit");
00056 
00057   /* MapAddress *.yahoo.com *.google.com.torserver.exit */
00058   strlcpy(address, "reader.yahoo.com", sizeof(address));
00059   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00060   test_streq(address, "reader.google.com.torserver.exit");
00061 
00062   /*MapAddress *.cnn.com www.cnn.com */
00063   strlcpy(address, "cnn.com", sizeof(address));
00064   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00065   test_streq(address, "www.cnn.com");
00066 
00067   /* MapAddress .cn.com www.cnn.com */
00068   strlcpy(address, "www.cn.com", sizeof(address));
00069   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00070   test_streq(address, "www.cnn.com");
00071 
00072   /* MapAddress ex.com www.cnn.com  - no match */
00073   strlcpy(address, "www.ex.com", sizeof(address));
00074   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00075 
00076   /* MapAddress ey.com *.cnn.com - invalid expression */
00077   strlcpy(address, "ey.com", sizeof(address));
00078   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00079 
00080   /* Where mapping for FQDN match on FQDN */
00081   strlcpy(address, "www.google.com", sizeof(address));
00082   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00083   test_streq(address, "3.3.3.3");
00084 
00085   strlcpy(address, "www.torproject.org", sizeof(address));
00086   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00087   test_streq(address, "1.1.1.1");
00088 
00089   strlcpy(address, "other.torproject.org", sizeof(address));
00090   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00091   test_streq(address, "this.torproject.org.otherserver.exit");
00092 
00093   strlcpy(address, "test.torproject.org", sizeof(address));
00094   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00095   test_streq(address, "2.2.2.2");
00096 
00097   /* Test a chain of address mappings and the order in which they were added:
00098           "MapAddress www.example.org 4.4.4.4"
00099           "MapAddress 4.4.4.4 7.7.7.7"
00100           "MapAddress 4.4.4.4 5.5.5.5"
00101   */
00102   strlcpy(address, "www.example.org", sizeof(address));
00103   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00104   test_streq(address, "5.5.5.5");
00105 
00106   /* Test infinite address mapping results in no change */
00107   strlcpy(address, "www.infiniteloop.org", sizeof(address));
00108   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00109   test_streq(address, "www.infiniteloop.org");
00110 
00111   /* Test we don't find false positives */
00112   strlcpy(address, "www.example.com", sizeof(address));
00113   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00114 
00115   /* Test top-level-domain matching a bit harder */
00116   addressmap_clear_configured();
00117   strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
00118           "MapAddress *.torproject.org 1.1.1.1\n"
00119           "MapAddress *.net 2.2.2.2\n"
00120           , sizeof(buf));
00121   config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
00122   config_register_addressmaps(get_options());
00123 
00124   strlcpy(address, "www.abc.com", sizeof(address));
00125   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00126   test_streq(address, "www.abc.torserver.exit");
00127 
00128   strlcpy(address, "www.def.com", sizeof(address));
00129   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00130   test_streq(address, "www.def.torserver.exit");
00131 
00132   strlcpy(address, "www.torproject.org", sizeof(address));
00133   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00134   test_streq(address, "1.1.1.1");
00135 
00136   strlcpy(address, "test.torproject.org", sizeof(address));
00137   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00138   test_streq(address, "1.1.1.1");
00139 
00140   strlcpy(address, "torproject.net", sizeof(address));
00141   test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
00142   test_streq(address, "2.2.2.2");
00143 
00144   /* We don't support '*' as a mapping directive */
00145   addressmap_clear_configured();
00146   strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
00147   config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
00148   config_register_addressmaps(get_options());
00149 
00150   strlcpy(address, "www.abc.com", sizeof(address));
00151   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00152 
00153   strlcpy(address, "www.def.net", sizeof(address));
00154   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00155 
00156   strlcpy(address, "www.torproject.org", sizeof(address));
00157   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
00158 
00159  done:
00160   ;
00161 }
00162 
00163 #define CONFIG_TEST(name, flags)                          \
00164   { #name, test_config_ ## name, flags, NULL, NULL }
00165 
00166 struct testcase_t config_tests[] = {
00167   CONFIG_TEST(addressmap, 0),
00168   END_OF_TESTCASES
00169 };
00170