From 3d4ccb781ae5d74f0e16a63c89e08459d15cccf1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 25 Sep 2003 05:17:11 +0000 Subject: Refactor buffers; implement descriptors. 'buf_t' is now an opaque type defined in buffers.c . Router descriptors now include all keys; routers generate keys as needed on startup (in a newly defined "data directory"), and generate their own descriptors. Descriptors are now self-signed. Implementation is not complete: descriptors are never published; and upon receiving a descriptor, the directory doesn't do anything with it. At least "routers.or" and orkeygen are now obsolete, BTW. svn:r483 --- src/or/test.c | 173 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 99 insertions(+), 74 deletions(-) (limited to 'src/or/test.c') diff --git a/src/or/test.c b/src/or/test.c index e8a9c18d1..b3cc777c4 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -49,19 +49,19 @@ test_buffers() { char str[256]; char str2[256]; - char *buf; - int buflen, buf_datalen; + buf_t *buf; + buf_t *buf2; int s, i, j, eof; /**** * buf_new ****/ - if (buf_new(&buf, &buflen, &buf_datalen)) + if (!(buf = buf_new())) test_fail(); - test_eq(buflen, MAX_BUF_SIZE); - test_eq(buf_datalen, 0); + test_eq(buf_capacity(buf), MAX_BUF_SIZE); + test_eq(buf_datalen(buf), 0); /**** * read_to_buf @@ -75,52 +75,52 @@ test_buffers() { s = open("/tmp/tor_test/data", O_RDONLY, 0); eof = 0; - i = read_to_buf(s, 10, &buf, &buflen, &buf_datalen, &eof); - test_eq(buflen, MAX_BUF_SIZE); - test_eq(buf_datalen, 10); + i = read_to_buf(s, 10, buf, &eof); + test_eq(buf_capacity(buf), MAX_BUF_SIZE); + test_eq(buf_datalen(buf), 10); test_eq(eof, 0); test_eq(i, 10); - test_memeq(str, buf, 10); + test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* Test reading 0 bytes. */ - i = read_to_buf(s, 0, &buf, &buflen, &buf_datalen, &eof); - test_eq(buflen, MAX_BUF_SIZE); - test_eq(buf_datalen, 10); + i = read_to_buf(s, 0, buf, &eof); + test_eq(buf_capacity(buf), MAX_BUF_SIZE); + test_eq(buf_datalen(buf), 10); test_eq(eof, 0); test_eq(i, 0); /* Now test when buffer is filled exactly. */ - buflen = 16; - i = read_to_buf(s, 6, &buf, &buflen, &buf_datalen, &eof); - test_eq(buflen, 16); - test_eq(buf_datalen, 16); + buf2 = buf_new_with_capacity(6); + i = read_to_buf(s, 6, buf2, &eof); + test_eq(buf_capacity(buf2), 6); + test_eq(buf_datalen(buf2), 6); test_eq(eof, 0); test_eq(i, 6); - test_memeq(str, buf, 16); + test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6); + buf_free(buf2); /* Now test when buffer is filled with more data to read. */ - buflen = 32; - i = read_to_buf(s, 128, &buf, &buflen, &buf_datalen, &eof); - test_eq(buflen, 32); - test_eq(buf_datalen, 32); + buf2 = buf_new_with_capacity(32); + i = read_to_buf(s, 128, buf2, &eof); + test_eq(buf_capacity(buf2), 32); + test_eq(buf_datalen(buf2), 32); test_eq(eof, 0); - test_eq(i, 16); - test_memeq(str, buf, 32); + test_eq(i, 32); + buf_free(buf2); /* Now read to eof. */ - buflen = MAX_BUF_SIZE; - test_assert(buflen > 256); - i = read_to_buf(s, 1024, &buf, &buflen, &buf_datalen, &eof); - test_eq(i, (256-32)); - test_eq(buflen, MAX_BUF_SIZE); - test_eq(buf_datalen, 256); - test_memeq(str, buf, 256); + test_assert(buf_capacity(buf) > 256); + i = read_to_buf(s, 1024, buf, &eof); + test_eq(i, (256-32-10-6)); + test_eq(buf_capacity(buf), MAX_BUF_SIZE); + test_eq(buf_datalen(buf), 256-6-32); + test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */ test_eq(eof, 0); - i = read_to_buf(s, 1024, &buf, &buflen, &buf_datalen, &eof); + i = read_to_buf(s, 1024, buf, &eof); test_eq(i, 0); - test_eq(buflen, MAX_BUF_SIZE); - test_eq(buf_datalen, 256); + test_eq(buf_capacity(buf), MAX_BUF_SIZE); + test_eq(buf_datalen(buf), 256-6-32); test_eq(eof, 1); close(s); @@ -128,50 +128,59 @@ test_buffers() { /**** * find_on_inbuf ****/ + buf_free(buf); + buf = buf_new(); + s = open("/tmp/tor_test/data", O_RDONLY, 0); + eof = 0; + i = read_to_buf(s, 1024, buf, &eof); + test_eq(256, i); + close(s); - test_eq(((int)'d') + 1, find_on_inbuf("abcd", 4, buf, buf_datalen)); - test_eq(-1, find_on_inbuf("xyzzy", 5, buf, buf_datalen)); + test_eq(((int)'d') + 1, find_on_inbuf("abcd", 4, buf)); + test_eq(-1, find_on_inbuf("xyzzy", 5, buf)); /* Make sure we don't look off the end of the buffef */ - buf[256] = 'A'; - buf[257] = 'X'; - test_eq(-1, find_on_inbuf("\xff" "A", 2, buf, buf_datalen)); - test_eq(-1, find_on_inbuf("AX", 2, buf, buf_datalen)); + ((char*)_buf_peek_raw_buffer(buf))[256] = 'A'; + ((char*)_buf_peek_raw_buffer(buf))[257] = 'X'; + test_eq(-1, find_on_inbuf("\xff" "A", 2, buf)); + test_eq(-1, find_on_inbuf("AX", 2, buf)); /* Make sure we use the string length */ - test_eq(((int)'d')+1, find_on_inbuf("abcdX", 4, buf, buf_datalen)); + test_eq(((int)'d')+1, find_on_inbuf("abcdX", 4, buf)); /**** * fetch_from_buf ****/ memset(str2, 255, 256); - test_eq(246, fetch_from_buf(str2, 10, &buf, &buflen, &buf_datalen)); + test_eq(246, fetch_from_buf(str2, 10, buf)); test_memeq(str2, str, 10); - test_memeq(str+10,buf,246); - test_eq(buf_datalen,246); + test_memeq(str+10,(char*)_buf_peek_raw_buffer(buf),246); + test_eq(buf_datalen(buf),246); - test_eq(0, fetch_from_buf(str2, 246, &buf, &buflen, &buf_datalen)); + test_eq(0, fetch_from_buf(str2, 246, buf)); test_memeq(str2, str+10, 246); - test_eq(buflen,MAX_BUF_SIZE); - test_eq(buf_datalen,0); + test_eq(buf_capacity(buf),MAX_BUF_SIZE); + test_eq(buf_datalen(buf),0); /**** * write_to_buf ****/ - memset(buf, (int)'-', 256); - i = write_to_buf("Hello world", 11, &buf, &buflen, &buf_datalen); + memset((char *)_buf_peek_raw_buffer(buf), (int)'-', 256); + i = write_to_buf("Hello world", 11, buf); test_eq(i, 11); - test_eq(buf_datalen, 11); - test_memeq(buf, "Hello world", 11); - i = write_to_buf("XYZZY", 5, &buf, &buflen, &buf_datalen); + test_eq(buf_datalen(buf), 11); + test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello world", 11); + i = write_to_buf("XYZZY", 5, buf); test_eq(i, 16); - test_eq(buf_datalen, 16); - test_memeq(buf, "Hello worldXYZZY", 16); + test_eq(buf_datalen(buf), 16); + test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello worldXYZZY", 16); /* Test when buffer is overfull. */ +#if 0 buflen = 18; test_eq(-1, write_to_buf("This string will not fit.", 25, &buf, &buflen, &buf_datalen)); test_eq(buf_datalen, 16); test_memeq(buf, "Hello worldXYZZY--", 18); buflen = MAX_BUF_SIZE; +#endif /**** * flush_buf @@ -482,7 +491,8 @@ test_onion_handshake() { } /* from main.c */ -int dump_router_to_string(char *s, int maxlen, routerinfo_t *router); +int dump_router_to_string(char *s, int maxlen, routerinfo_t *router, + crypto_pk_env_t *ident_key); void dump_directory_to_string(char *s, int maxlen); /* from routers.c */ @@ -491,28 +501,30 @@ int compare_recommended_versions(char *myversion, char *start); void test_dir_format() { - - char buf[2048], buf2[2048]; - char *pk1_str = NULL, *pk2_str = NULL, *cp; - int pk1_str_len, pk2_str_len; + char buf[8192], buf2[8192]; + char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp; + int pk1_str_len, pk2_str_len, pk3_str_len; routerinfo_t r1, r2; - crypto_pk_env_t *pk1 = NULL, *pk2 = NULL; - routerinfo_t *rp1, *rp2; + crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL; + routerinfo_t *rp1 = NULL, *rp2 = NULL; struct exit_policy_t ex1, ex2; directory_t *dir1 = NULL, *dir2 = NULL; test_assert( (pk1 = crypto_new_pk_env(CRYPTO_PK_RSA)) ); test_assert( (pk2 = crypto_new_pk_env(CRYPTO_PK_RSA)) ); + test_assert( (pk3 = crypto_new_pk_env(CRYPTO_PK_RSA)) ); test_assert(! crypto_pk_generate_key(pk1)); test_assert(! crypto_pk_generate_key(pk2)); + test_assert(! crypto_pk_generate_key(pk3)); r1.address = "testaddr1.foo.bar"; r1.addr = 0xc0a80001u; /* 192.168.0.1 */ r1.or_port = 9000; r1.ap_port = 9002; r1.dir_port = 9003; - r1.pkey = pk1; - r1.signing_pkey = NULL; + r1.onion_pkey = pk1; + r1.identity_pkey = pk2; + r1.link_pkey = pk3; r1.bandwidth = 1000; r1.exit_policy = NULL; @@ -530,8 +542,9 @@ test_dir_format() r2.or_port = 9005; r2.ap_port = 0; r2.dir_port = 0; - r2.pkey = pk2; - r2.signing_pkey = pk1; + r2.onion_pkey = pk2; + r2.identity_pkey = pk1; + r2.link_pkey = pk2; r2.bandwidth = 3000; r2.exit_policy = &ex1; @@ -539,14 +552,23 @@ test_dir_format() &pk1_str_len)); test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str, &pk2_str_len)); - strcpy(buf2, "router testaddr1.foo.bar 9000 9002 9003 1000\n"); + test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str, + &pk3_str_len)); + + strcpy(buf2, "router testaddr1.foo.bar 9000 9002 9003 1000\nonion-key\n"); strcat(buf2, pk1_str); - strcat(buf2, "\n"); + strcat(buf2, "link-key\n"); + strcat(buf2, pk3_str); + strcat(buf2, "signing-key\n"); + strcat(buf2, pk2_str); + strcat(buf2, "router-signature\n"); memset(buf, 0, 2048); - test_assert(dump_router_to_string(buf, 2048, &r1)>0); + test_assert(dump_router_to_string(buf, 2048, &r1, pk1)>0); + buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same 2ce*/ test_streq(buf, buf2); - + + test_assert(dump_router_to_string(buf, 2048, &r1, pk1)>0); cp = buf; rp1 = router_get_entry_from_string(&cp); test_assert(rp1); @@ -555,16 +577,18 @@ test_dir_format() test_eq(rp1->ap_port, r1.ap_port); test_eq(rp1->dir_port, r1.dir_port); test_eq(rp1->bandwidth, r1.bandwidth); - test_assert(crypto_pk_cmp_keys(rp1->pkey, pk1) == 0); - test_assert(rp1->signing_pkey == NULL); + test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0); + test_assert(crypto_pk_cmp_keys(rp1->link_pkey, pk3) == 0); + test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0); test_assert(rp1->exit_policy == NULL); +#if 0 strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n"); strcat(buf2, pk2_str); strcat(buf2, "signing-key\n"); strcat(buf2, pk1_str); strcat(buf2, "accept *:80\nreject 18.*:24\n\n"); - test_assert(dump_router_to_string(buf, 2048, &r2)>0); + test_assert(dump_router_to_string(buf, 2048, &r2, pk2)>0); test_streq(buf, buf2); cp = buf; @@ -575,8 +599,8 @@ test_dir_format() test_eq(rp2->ap_port, r2.ap_port); test_eq(rp2->dir_port, r2.dir_port); test_eq(rp2->bandwidth, r2.bandwidth); - test_assert(crypto_pk_cmp_keys(rp2->pkey, pk2) == 0); - test_assert(crypto_pk_cmp_keys(rp2->signing_pkey, pk1) == 0); + test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0); + test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0); test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT); test_streq(rp2->exit_policy->string, "accept *:80"); test_streq(rp2->exit_policy->address, "*"); @@ -586,6 +610,7 @@ test_dir_format() test_streq(rp2->exit_policy->next->address, "18.*"); test_streq(rp2->exit_policy->next->port, "24"); test_assert(rp2->exit_policy->next->next == NULL); +#endif /* Okay, now for the directories. */ dir1 = (directory_t*) tor_malloc(sizeof(directory_t)); @@ -593,7 +618,7 @@ test_dir_format() dir1->routers = (routerinfo_t**) tor_malloc(sizeof(routerinfo_t*)*2); dir1->routers[0] = &r1; dir1->routers[1] = &r2; - test_assert(! dump_signed_directory_to_string_impl(buf, 2048, dir1, pk1)); + test_assert(! dump_signed_directory_to_string_impl(buf, 4096, dir1, pk1)); /* puts(buf); */ test_assert(! router_get_dir_from_string_impl(buf, &dir2, pk1)); -- cgit v1.2.3