summaryrefslogtreecommitdiff
path: root/src/or/test.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2006-10-19 23:05:19 +0000
committerNick Mathewson <nickm@torproject.org>2006-10-19 23:05:19 +0000
commit96caedecc76592610565157417af5273af7b7625 (patch)
treea2fe960d5632ff36d17d4607e3412b221c54c540 /src/or/test.c
parent7551c44a5375c390b653c7b8f28d904a3156e1ae (diff)
downloadtor-96caedecc76592610565157417af5273af7b7625.tar.gz
tor-96caedecc76592610565157417af5273af7b7625.zip
r9275@Kushana: nickm | 2006-10-19 16:45:19 -0400
Split smartlist tests into their own function, and add a couple more. Re-enable strmap iteration tests. Remove tests for code that doesnt exist any more (parse_nickname_list, identity ciphers). svn:r8763
Diffstat (limited to 'src/or/test.c')
-rw-r--r--src/or/test.c389
1 files changed, 199 insertions, 190 deletions
diff --git a/src/or/test.c b/src/or/test.c
index 2b441aeecd..654bade87b 100644
--- a/src/or/test.c
+++ b/src/or/test.c
@@ -205,6 +205,11 @@ test_buffers(void)
}
#if 0
+ {
+ int s;
+ int eof;
+ int i;
+ buf_t *buf2;
/****
* read_to_buf
****/
@@ -264,6 +269,7 @@ test_buffers(void)
test_eq(buf_capacity(buf), MAX_BUF_SIZE);
test_eq(buf_datalen(buf), 256-6-32);
test_eq(eof, 1);
+ }
#endif
buf_free(buf);
@@ -324,20 +330,6 @@ test_crypto(void)
crypto_rand(data2, 100);
test_memneq(data1,data2,100);
-#if 0
- /* Try out identity ciphers. */
- env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
- test_neq(env1, 0);
- test_eq(crypto_cipher_generate_key(env1), 0);
- test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
- for (i = 0; i < 1024; ++i) {
- data1[i] = (char) i*73;
- }
- crypto_cipher_encrypt(env1, data2, data1, 1024);
- test_memeq(data1, data2, 1024);
- crypto_free_cipher_env(env1);
-#endif
-
/* Now, test encryption and decryption with stream cipher. */
data1[0]='\0';
for (i = 1023; i>0; i -= 35)
@@ -585,7 +577,6 @@ test_util(void)
{
struct timeval start, end;
struct tm a_time;
- smartlist_t *sl;
char timestr[RFC1123_TIME_LEN+1];
char buf[1024];
time_t t_res;
@@ -643,6 +634,153 @@ test_util(void)
test_eq(i,0);
test_eq(t_res, (time_t)1091580502UL);
+ /* Test tor_strstrip() */
+ strcpy(buf, "Testing 1 2 3");
+ test_eq(0, tor_strstrip(buf, ",!"));
+ test_streq(buf, "Testing 1 2 3");
+ strcpy(buf, "!Testing 1 2 3?");
+ test_eq(5, tor_strstrip(buf, "!? "));
+ test_streq(buf, "Testing123");
+
+ /* Test tor_strpartition() */
+ test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
+ TERMINATE_IF_EVEN));
+ test_streq(buf, "abc##def##g");
+ test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
+ ALWAYS_TERMINATE));
+ test_streq(buf, "abc##def##g##");
+ test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
+ TERMINATE_IF_EVEN));
+ test_streq(buf, "abc##def##ghi##");
+ test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
+ NEVER_TERMINATE));
+ test_streq(buf, "abc##def##ghi");
+
+ /* Test parse_addr_port */
+ cp = NULL; u32 = 3; u16 = 3;
+ test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
+ test_streq(cp, "1.2.3.4");
+ test_eq(u32, 0x01020304u);
+ test_eq(u16, 0);
+ tor_free(cp);
+ test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
+ test_streq(cp, "4.3.2.1");
+ test_eq(u32, 0x04030201u);
+ test_eq(u16, 99);
+ tor_free(cp);
+ test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
+ &cp, NULL, &u16));
+ test_streq(cp, "nonexistent.address");
+ test_eq(u16, 4040);
+ tor_free(cp);
+ test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
+ test_streq(cp, "localhost");
+ test_eq(u32, 0x7f000001u);
+ test_eq(u16, 9999);
+ tor_free(cp);
+ u32 = 3;
+ test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
+ test_eq(cp, NULL);
+ test_eq(u32, 0x7f000001u);
+ test_eq(u16, 0);
+ tor_free(cp);
+ test_eq(0, addr_mask_get_bits(0x0u));
+ test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
+ test_eq(16, addr_mask_get_bits(0xFFFF0000u));
+ test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
+ test_eq(1, addr_mask_get_bits(0x80000000u));
+
+ /* Test tor_parse_long. */
+ test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
+ test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
+
+ /* Test parse_line_from_str */
+ strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
+ "k2\n"
+ "k3 \n" "\n" " \n" "#comment\n"
+ "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
+ cp = buf;
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "k");
+ test_streq(v, "v");
+ test_assert(!strcmpstart(cp, " key value with"));
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "key");
+ test_streq(v, "value with spaces");
+ test_assert(!strcmpstart(cp, "keykey"));
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "keykey");
+ test_streq(v, "val");
+ test_assert(!strcmpstart(cp, "k2\n"));
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "k2");
+ test_streq(v, "");
+ test_assert(!strcmpstart(cp, "k3 \n"));
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "k3");
+ test_streq(v, "");
+ test_assert(!strcmpstart(cp, "\n \n"));
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "k4");
+ test_streq(v, "");
+ test_assert(!strcmpstart(cp, "k5#abc"));
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "k5");
+ test_streq(v, "");
+ test_assert(!strcmpstart(cp, "k6"));
+
+ cp = parse_line_from_str(cp, &k, &v);
+ test_streq(k, "k6");
+ test_streq(v, "val");
+ test_streq(cp, "");
+
+ /* Test for strcmpstart and strcmpend. */
+ test_assert(strcmpstart("abcdef", "abcdef")==0);
+ test_assert(strcmpstart("abcdef", "abc")==0);
+ test_assert(strcmpstart("abcdef", "abd")<0);
+ test_assert(strcmpstart("abcdef", "abb")>0);
+ test_assert(strcmpstart("ab", "abb")<0);
+
+ test_assert(strcmpend("abcdef", "abcdef")==0);
+ test_assert(strcmpend("abcdef", "def")==0);
+ test_assert(strcmpend("abcdef", "deg")<0);
+ test_assert(strcmpend("abcdef", "dee")>0);
+ test_assert(strcmpend("ab", "abb")<0);
+
+ {
+ char tmpbuf[INET_NTOA_BUF_LEN];
+ struct in_addr in;
+ tor_inet_aton("18.244.0.188",&in);
+ tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
+ test_streq(tmpbuf, "18.244.0.188");
+ }
+
+ /* Test 'escaped' */
+ test_streq("\"\"", escaped(""));
+ test_streq("\"abcd\"", escaped("abcd"));
+ test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
+ test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
+}
+
+static void
+test_smartlist(void)
+{
+ smartlist_t *sl;
+ char *cp;
+
+ /* XXXX test add_all, remove, string_remove, isin, string_isin,
+ * string_num_isin, overlap, intersect, subtract,
+ * sort_strings, sort_digests, uniq_strings, uniq_digests, bsearch,
+ * join_strings2
+ */
+
/* Test smartlist */
sl = smartlist_create();
smartlist_add(sl, (void*)1);
@@ -659,8 +797,16 @@ test_util(void)
test_eq_ptr((void*)3, smartlist_get(sl,3));
test_eq_ptr((void*)4, smartlist_get(sl,4));
test_eq_ptr((void*)555, smartlist_get(sl,5));
+ /* Try deleting in the middle. */
+ smartlist_del(sl, 1);
+ test_eq_ptr((void*)555, smartlist_get(sl, 1));
+ /* Try deleting at the end. */
+ smartlist_del(sl, 4);
+ test_eq(4, smartlist_len(sl));
+ /* Test split and join */
smartlist_clear(sl);
+ test_eq(0, smartlist_len(sl));
smartlist_split_string(sl, "abc", ":", 0, 0);
test_eq(1, smartlist_len(sl));
test_streq("abc", smartlist_get(sl, 0));
@@ -782,142 +928,21 @@ test_util(void)
test_streq(cp, "and");
tor_free(cp);
test_eq(smartlist_len(sl), 6);
+ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ smartlist_clear(sl);
- /* Test tor_strstrip() */
- strcpy(buf, "Testing 1 2 3");
- test_eq(0, tor_strstrip(buf, ",!"));
- test_streq(buf, "Testing 1 2 3");
- strcpy(buf, "!Testing 1 2 3?");
- test_eq(5, tor_strstrip(buf, "!? "));
- test_streq(buf, "Testing123");
-
- /* Test tor_strpartition() */
- test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
- TERMINATE_IF_EVEN));
- test_streq(buf, "abc##def##g");
- test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
- ALWAYS_TERMINATE));
- test_streq(buf, "abc##def##g##");
- test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
- TERMINATE_IF_EVEN));
- test_streq(buf, "abc##def##ghi##");
- test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
- NEVER_TERMINATE));
- test_streq(buf, "abc##def##ghi");
-
- /* Test parse_addr_port */
- cp = NULL; u32 = 3; u16 = 3;
- test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
- test_streq(cp, "1.2.3.4");
- test_eq(u32, 0x01020304u);
- test_eq(u16, 0);
- tor_free(cp);
- test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
- test_streq(cp, "4.3.2.1");
- test_eq(u32, 0x04030201u);
- test_eq(u16, 99);
- tor_free(cp);
- test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
- &cp, NULL, &u16));
- test_streq(cp, "nonexistent.address");
- test_eq(u16, 4040);
- tor_free(cp);
- test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
- test_streq(cp, "localhost");
- test_eq(u32, 0x7f000001u);
- test_eq(u16, 9999);
- tor_free(cp);
- u32 = 3;
- test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
- test_eq(cp, NULL);
- test_eq(u32, 0x7f000001u);
- test_eq(u16, 0);
+ /* Test uniq() */
+ smartlist_split_string(sl,
+ "noon,radar,a,man,a,plan,a,canal,panama,radar,noon",
+ ",", 0, 0);
+ smartlist_sort(sl, _compare_strs);
+ smartlist_uniq(sl, _compare_strs, NULL);
+ cp = smartlist_join_strings(sl, ",", 0, NULL);
+ test_streq(cp, "a,canal,man,noon,panama,plan,radar");
tor_free(cp);
- test_eq(0, addr_mask_get_bits(0x0u));
- test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
- test_eq(16, addr_mask_get_bits(0xFFFF0000u));
- test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
- test_eq(1, addr_mask_get_bits(0x80000000u));
-
- /* Test tor_parse_long. */
- test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
- test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
-
- /* Test parse_line_from_str */
- strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
- "k2\n"
- "k3 \n" "\n" " \n" "#comment\n"
- "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
- cp = buf;
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "k");
- test_streq(v, "v");
- test_assert(!strcmpstart(cp, " key value with"));
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "key");
- test_streq(v, "value with spaces");
- test_assert(!strcmpstart(cp, "keykey"));
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "keykey");
- test_streq(v, "val");
- test_assert(!strcmpstart(cp, "k2\n"));
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "k2");
- test_streq(v, "");
- test_assert(!strcmpstart(cp, "k3 \n"));
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "k3");
- test_streq(v, "");
- test_assert(!strcmpstart(cp, "\n \n"));
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "k4");
- test_streq(v, "");
- test_assert(!strcmpstart(cp, "k5#abc"));
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "k5");
- test_streq(v, "");
- test_assert(!strcmpstart(cp, "k6"));
-
- cp = parse_line_from_str(cp, &k, &v);
- test_streq(k, "k6");
- test_streq(v, "val");
- test_streq(cp, "");
-
- /* Test for strcmpstart and strcmpend. */
- test_assert(strcmpstart("abcdef", "abcdef")==0);
- test_assert(strcmpstart("abcdef", "abc")==0);
- test_assert(strcmpstart("abcdef", "abd")<0);
- test_assert(strcmpstart("abcdef", "abb")>0);
- test_assert(strcmpstart("ab", "abb")<0);
-
- test_assert(strcmpend("abcdef", "abcdef")==0);
- test_assert(strcmpend("abcdef", "def")==0);
- test_assert(strcmpend("abcdef", "deg")<0);
- test_assert(strcmpend("abcdef", "dee")>0);
- test_assert(strcmpend("ab", "abb")<0);
-
- {
- char tmpbuf[INET_NTOA_BUF_LEN];
- struct in_addr in;
- tor_inet_aton("18.244.0.188",&in);
- tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
- test_streq(tmpbuf, "18.244.0.188");
- }
-
- /* Test 'escaped' */
- test_streq("\"\"", escaped(""));
- test_streq("\"abcd\"", escaped("abcd"));
- test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
- test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
+ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ smartlist_clear(sl);
- /* XXXX test older functions. */
smartlist_free(sl);
}
@@ -1091,9 +1116,11 @@ static void
test_strmap(void)
{
strmap_t *map;
-// strmap_iter_t *iter;
-// const char *k;
+ strmap_iter_t *iter;
+ const char *k;
void *v;
+ char *visited;
+ smartlist_t *found_keys;
map = strmap_new();
v = strmap_set(map, "K1", (void*)99);
@@ -1121,30 +1148,31 @@ test_strmap(void)
strmap_set(map, "K6", (void*)105);
strmap_assert_ok(map);
-#if 0
+ /* Test iterator. */
iter = strmap_iter_init(map);
- strmap_iter_get(iter,&k,&v);
- test_streq(k, "K1");
- test_eq(v, (void*)10000);
- iter = strmap_iter_next(map,iter);
- strmap_iter_get(iter,&k,&v);
- test_streq(k, "K2");
- test_eq(v, (void*)10201);
- iter = strmap_iter_next_rmv(map,iter);
- strmap_iter_get(iter,&k,&v);
- test_streq(k, "K3");
- test_eq(v, (void*)10404);
- iter = strmap_iter_next(map,iter); /* K5 */
- test_assert(!strmap_iter_done(iter));
- iter = strmap_iter_next(map,iter); /* K6 */
- test_assert(!strmap_iter_done(iter));
- iter = strmap_iter_next(map,iter); /* done */
- test_assert(strmap_iter_done(iter));
+ found_keys = smartlist_create();
+ while (!strmap_iter_done(iter)) {
+ strmap_iter_get(iter,&k,&v);
+ smartlist_add(found_keys, tor_strdup(k));
+ test_eq_ptr(v, strmap_get(map, k));
+
+ if (!strcmp(k, "K2")) {
+ iter = strmap_iter_next_rmv(map,iter);
+ } else {
+ iter = strmap_iter_next(map,iter);
+ }
+ }
/* Make sure we removed K2, but not the others. */
test_eq_ptr(strmap_get(map, "K2"), NULL);
- test_eq_ptr(strmap_get(map, "K5"), (void*)10816);
-#endif
+ test_eq_ptr(strmap_get(map, "K5"), (void*)104);
+ /* Make sure we visited everyone once */
+ smartlist_sort_strings(found_keys);
+ visited = smartlist_join_strings(found_keys, ":", 0, NULL);
+ test_streq(visited, "K1:K2:K3:K4:K5:K6");
+ tor_free(visited);
+ SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
+ smartlist_free(found_keys);
strmap_assert_ok(map);
/* Clean up after ourselves. */
@@ -1232,25 +1260,6 @@ test_control_formats(void)
}
static void
-test_onion(void)
-{
-#if 0
- char **names;
- int i,num;
-
- names = parse_nickname_list(" foo bar\t baz quux ", &num);
- test_eq(num,4);
- test_streq(names[0],"foo");
- test_streq(names[1],"bar");
- test_streq(names[2],"baz");
- test_streq(names[3],"quux");
- for (i=0;i<num;i++)
- tor_free(names[i]);
- tor_free(names);
-#endif
-}
-
-static void
test_onion_handshake(void)
{
/* client-side */
@@ -1820,12 +1829,12 @@ main(int c, char**v)
puts("\n========================= Util ============================");
test_gzip();
test_util();
+ test_smartlist();
test_strmap();
test_control_formats();
test_pqueue();
test_mmap();
puts("\n========================= Onion Skins =====================");
- test_onion();
test_onion_handshake();
puts("\n========================= Directory Formats ===============");
test_dir_format();