Back to index

tor  0.2.3.18-rc
test_pt.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 #define PT_PRIVATE
00008 #include "or.h"
00009 #include "transports.h"
00010 #include "circuitbuild.h"
00011 #include "test.h"
00012 
00013 static void
00014 reset_mp(managed_proxy_t *mp)
00015 {
00016   mp->conf_state = PT_PROTO_LAUNCHED;
00017   SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
00018   smartlist_clear(mp->transports);
00019 }
00020 
00021 static void
00022 test_pt_parsing(void)
00023 {
00024   char line[200];
00025 
00026   managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
00027   mp->conf_state = PT_PROTO_INFANT;
00028   mp->transports = smartlist_new();
00029 
00030   /* incomplete cmethod */
00031   strcpy(line,"CMETHOD trebuchet");
00032   test_assert(parse_cmethod_line(line, mp) < 0);
00033 
00034   reset_mp(mp);
00035 
00036   /* wrong proxy type */
00037   strcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999");
00038   test_assert(parse_cmethod_line(line, mp) < 0);
00039 
00040   reset_mp(mp);
00041 
00042   /* wrong addrport */
00043   strcpy(line,"CMETHOD trebuchet socks4 abcd");
00044   test_assert(parse_cmethod_line(line, mp) < 0);
00045 
00046   reset_mp(mp);
00047 
00048   /* correct line */
00049   strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
00050   test_assert(parse_cmethod_line(line, mp) == 0);
00051   test_assert(smartlist_len(mp->transports));
00052 
00053   reset_mp(mp);
00054 
00055   /* incomplete smethod */
00056   strcpy(line,"SMETHOD trebuchet");
00057   test_assert(parse_smethod_line(line, mp) < 0);
00058 
00059   reset_mp(mp);
00060 
00061   /* wrong addr type */
00062   strcpy(line,"SMETHOD trebuchet abcd");
00063   test_assert(parse_smethod_line(line, mp) < 0);
00064 
00065   reset_mp(mp);
00066 
00067   /* cowwect */
00068   strcpy(line,"SMETHOD trebuchy 127.0.0.1:1999");
00069   test_assert(parse_smethod_line(line, mp) == 0);
00070 
00071   reset_mp(mp);
00072 
00073   /* unsupported version */
00074   strcpy(line,"VERSION 666");
00075   test_assert(parse_version(line, mp) < 0);
00076 
00077   /* incomplete VERSION */
00078   strcpy(line,"VERSION ");
00079   test_assert(parse_version(line, mp) < 0);
00080 
00081   /* correct VERSION */
00082   strcpy(line,"VERSION 1");
00083   test_assert(parse_version(line, mp) == 0);
00084 
00085  done:
00086   tor_free(mp);
00087 }
00088 
00089 static void
00090 test_pt_protocol(void)
00091 {
00092   char line[200];
00093 
00094   managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
00095   mp->conf_state = PT_PROTO_LAUNCHED;
00096   mp->transports = smartlist_new();
00097   mp->argv = tor_malloc_zero(sizeof(char*)*2);
00098   mp->argv[0] = tor_strdup("<testcase>");
00099 
00100   /* various wrong protocol runs: */
00101 
00102   strcpy(line,"VERSION 1");
00103   handle_proxy_line(line, mp);
00104   test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
00105 
00106   strcpy(line,"VERSION 1");
00107   handle_proxy_line(line, mp);
00108   test_assert(mp->conf_state == PT_PROTO_BROKEN);
00109 
00110   reset_mp(mp);
00111 
00112   strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
00113   handle_proxy_line(line, mp);
00114   test_assert(mp->conf_state == PT_PROTO_BROKEN);
00115 
00116   reset_mp(mp);
00117 
00118   /* correct protocol run: */
00119   strcpy(line,"VERSION 1");
00120   handle_proxy_line(line, mp);
00121   test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
00122 
00123   strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
00124   handle_proxy_line(line, mp);
00125   test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
00126 
00127   strcpy(line,"CMETHODS DONE");
00128   handle_proxy_line(line, mp);
00129   test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
00130 
00131  done:
00132   tor_free(mp);
00133 }
00134 
00135 #define PT_LEGACY(name)                                               \
00136   { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
00137 
00138 struct testcase_t pt_tests[] = {
00139   PT_LEGACY(parsing),
00140   PT_LEGACY(protocol),
00141   END_OF_TESTCASES
00142 };
00143