commit afa421e1b6755545f9174c02c48b0f9961530ffd Author: Zack Weinberg zackw@panix.com Date: Mon Jul 18 13:59:29 2011 -0700
Factor out duplicated unittest_socks code with a test fixture. Fix some small things in unittest_crypt and unittest_obfs2 left over from previous patches. --- src/test/unittest_crypt.c | 6 +- src/test/unittest_obfs2.c | 5 +- src/test/unittest_socks.c | 370 ++++++++++++++++++++++----------------------- 3 files changed, 184 insertions(+), 197 deletions(-)
diff --git a/src/test/unittest_crypt.c b/src/test/unittest_crypt.c index 0e48a90..5a3e2aa 100644 --- a/src/test/unittest_crypt.c +++ b/src/test/unittest_crypt.c @@ -80,9 +80,9 @@ test_crypt_aes1(void *data) /* In-place encryption of the test vectors from http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf for AES128 in counter mode (section F.5.1) */ - const uchar key[16] = + static const uchar key[16] = "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c"; - const uchar iv[16] = + static const uchar iv[16] = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; struct testblock { const uchar counter[16]; @@ -90,7 +90,7 @@ test_crypt_aes1(void *data) const uchar plaintext[16]; const uchar ciphertext[16]; }; - const struct testblock testvec[4] = { + static const struct testblock testvec[4] = { { "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", "\xec\x8c\xdf\x73\x98\x60\x7c\xb0\xf2\xd2\x16\x75\xea\x9e\xa1\xe4", "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a", diff --git a/src/test/unittest_obfs2.c b/src/test/unittest_obfs2.c index 961bfd2..3ac4c42 100644 --- a/src/test/unittest_obfs2.c +++ b/src/test/unittest_obfs2.c @@ -150,9 +150,8 @@ setup_obfs2_state(const struct testcase_t *unused) return s;
end: - if (s) - cleanup_obfs2_state(NULL, s); - return 0; + cleanup_obfs2_state(NULL, s); + return NULL; }
static const struct testcase_setup_t obfs2_fixture = diff --git a/src/test/unittest_socks.c b/src/test/unittest_socks.c index 4d95666..db4f120 100644 --- a/src/test/unittest_socks.c +++ b/src/test/unittest_socks.c @@ -8,12 +8,58 @@ #define SOCKS_PRIVATE #include "../socks.h" #include "../crypt.h" +#include "../util.h"
#include <stdio.h> +#include <stdlib.h> #include <string.h>
#include <event2/buffer.h>
+/* All the tests below use this test environment. Some of them + do not need both evbuffers, but we give them two anyway. */ +struct test_socks_state +{ + struct evbuffer *dest; + struct evbuffer *source; + socks_state_t *state; +}; + +static int +cleanup_socks_state(const struct testcase_t *unused, void *data) +{ + struct test_socks_state *s = (struct test_socks_state *)data; + + if (s->dest) + evbuffer_free(s->dest); + if (s->source) + evbuffer_free(s->source); + if (s->state) + socks_state_free(s->state); + free(data); + return 1; +} + +static void * +setup_socks_state(const struct testcase_t *unused) +{ + struct test_socks_state *s = xzalloc(sizeof(struct test_socks_state)); + + s->dest = evbuffer_new(); + tt_assert(s->dest); + s->source = evbuffer_new(); + tt_assert(s->source); + s->state = socks_state_new(); + tt_assert(s->state); + return s; + + end: + cleanup_socks_state(NULL, s); + return NULL; +} + +static const struct testcase_setup_t socks_fixture = + { setup_socks_state, cleanup_socks_state };
/** This function tests the negotiation phase of the SOCKS5 protocol. @@ -24,13 +70,7 @@ static void test_socks_socks5_send_negotiation(void *data) { - struct evbuffer *dest = NULL; - struct evbuffer *source = NULL; - dest = evbuffer_new(); - source = evbuffer_new(); - - socks_state_t *state; - state = socks_state_new(); + struct test_socks_state *s = (struct test_socks_state *)data;
/* First test: Only one method: NOAUTH. @@ -39,13 +79,13 @@ test_socks_socks5_send_negotiation(void *data) req1[0] = 1; req1[1] = 0;
- evbuffer_add(source, req1, 2); + evbuffer_add(s->source, req1, 2);
- tt_int_op(SOCKS_GOOD, ==, socks5_handle_negotiation(source,dest,state)); - tt_int_op(0, ==, evbuffer_get_length(source)); + tt_int_op(SOCKS_GOOD, ==, socks5_handle_negotiation(s->source,s->dest,s->state)); + tt_int_op(0, ==, evbuffer_get_length(s->source));
uchar rep1[2]; - tt_int_op(2, ==, evbuffer_remove(dest,rep1,2)); + tt_int_op(2, ==, evbuffer_remove(s->dest,rep1,2)); tt_assert(rep1[0] == 5); tt_assert(rep1[1] == 0);
@@ -56,13 +96,13 @@ test_socks_socks5_send_negotiation(void *data) memset(req2+1,0x42,8); req2[9] = 0;
- evbuffer_add(source, req2, 10); + evbuffer_add(s->source, req2, 10);
- tt_int_op(SOCKS_GOOD, ==, socks5_handle_negotiation(source,dest,state)); - tt_int_op(0, ==, evbuffer_get_length(source)); + tt_int_op(SOCKS_GOOD, ==, socks5_handle_negotiation(s->source,s->dest,s->state)); + tt_int_op(0, ==, evbuffer_get_length(s->source));
uchar rep2[2]; - tt_int_op(2, ==, evbuffer_remove(dest,rep2,2)); + tt_int_op(2, ==, evbuffer_remove(s->dest,rep2,2)); tt_assert(rep2[0] == 5); tt_assert(rep2[1] == 0);
@@ -72,13 +112,13 @@ test_socks_socks5_send_negotiation(void *data) req3[0] = 99; memset(req3+1,0x42,99);
- evbuffer_add(source, req3, 100); + evbuffer_add(s->source, req3, 100);
- tt_int_op(SOCKS_BROKEN, ==, socks5_handle_negotiation(source,dest,state)); - tt_int_op(0, ==, evbuffer_get_length(source)); /* all data removed */ + tt_int_op(SOCKS_BROKEN, ==, socks5_handle_negotiation(s->source,s->dest,s->state)); + tt_int_op(0, ==, evbuffer_get_length(s->source)); /* all data removed */
uchar rep3[2]; - tt_int_op(2, ==, evbuffer_remove(dest,rep3,2)); + tt_int_op(2, ==, evbuffer_remove(s->dest,rep3,2)); tt_assert(rep3[0] == 5); tt_assert(rep3[1] == 0xff);
@@ -91,11 +131,11 @@ test_socks_socks5_send_negotiation(void *data) req4[0] = 4; memset(req4+1,0x0,3);
- evbuffer_add(source, req4, 4); + evbuffer_add(s->source, req4, 4);
- tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_negotiation(source,dest,state)); - tt_int_op(4, ==, evbuffer_get_length(source)); /* no bytes removed */ - evbuffer_drain(source, 4); + tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_negotiation(s->source,s->dest,s->state)); + tt_int_op(4, ==, evbuffer_get_length(s->source)); /* no bytes removed */ + evbuffer_drain(s->source, 4);
/* Fifth test: nmethods field = 3 but 4 actual methods. @@ -104,25 +144,18 @@ test_socks_socks5_send_negotiation(void *data) req5[0] = 3; memset(req5+1,0x0,4);
- evbuffer_add(source, req5, 5); + evbuffer_add(s->source, req5, 5);
- tt_int_op(SOCKS_GOOD, ==, socks5_handle_negotiation(source,dest,state)); - tt_int_op(1, ==, evbuffer_get_length(source)); /* 4 bytes removed */ - evbuffer_drain(source, 1); + tt_int_op(SOCKS_GOOD, ==, socks5_handle_negotiation(s->source,s->dest,s->state)); + tt_int_op(1, ==, evbuffer_get_length(s->source)); /* 4 bytes removed */ + evbuffer_drain(s->source, 1);
uchar rep4[2]; - tt_int_op(2, ==, evbuffer_remove(dest,rep4,2)); + tt_int_op(2, ==, evbuffer_remove(s->dest,rep4,2)); tt_assert(rep4[0] == 5); tt_assert(rep4[1] == 0);
- end: - if (state) - socks_state_free(state); - - if (source) - evbuffer_free(source); - if (dest) - evbuffer_free(dest); + end:; }
/** @@ -135,13 +168,7 @@ test_socks_socks5_send_negotiation(void *data) static void test_socks_socks5_request(void *data) { - struct evbuffer *dest = NULL; - struct evbuffer *source = NULL; - dest = evbuffer_new(); - source = evbuffer_new(); - - socks_state_t *state; - state = socks_state_new(); + struct test_socks_state *s = (struct test_socks_state *)data;
const uint32_t addr_ipv4 = htonl(0x7f000001); /* 127.0.0.1 */ const uint8_t addr_ipv6[16] = {0,13,0,1,0,5,0,14,0,10,0,5,0,14,0,0}; /* d:1:5:e:a:5:e:0 */ @@ -156,13 +183,13 @@ test_socks_socks5_request(void *data) req1[2] = 1; memcpy(req1+3,&addr_ipv4,4);
- evbuffer_add(source, "\x05", 1); - evbuffer_add(source, req1, 7); - tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_request(source,&pr1)); /* 0: want more data*/ + evbuffer_add(s->source, "\x05", 1); + evbuffer_add(s->source, req1, 7); + tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_request(s->source,&pr1)); /* 0: want more data*/
/* emptying source buffer before next test */ - size_t buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + size_t buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Second test: Broken FQDN req packet with no destport */ @@ -173,13 +200,13 @@ test_socks_socks5_request(void *data) req2[3] = 15; memcpy(req1+4,&addr_ipv4,3);
- evbuffer_add(source, "\x05", 1); - evbuffer_add(source, req2, 7); - tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_request(source,&pr1)); /* 0: want more data*/ + evbuffer_add(s->source, "\x05", 1); + evbuffer_add(s->source, req2, 7); + tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_request(s->source,&pr1)); /* 0: want more data*/
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Third test: Correct IPv4 req packet. */ @@ -190,15 +217,15 @@ test_socks_socks5_request(void *data) memcpy(req3+3,&addr_ipv4,4); memcpy(req3+7,&port,2);
- evbuffer_add(source, "\x05", 1); - evbuffer_add(source, req3, 9); - tt_int_op(SOCKS_GOOD, ==, socks5_handle_request(source,&pr1)); + evbuffer_add(s->source, "\x05", 1); + evbuffer_add(s->source, req3, 9); + tt_int_op(SOCKS_GOOD, ==, socks5_handle_request(s->source,&pr1)); tt_str_op(pr1.addr, ==, "127.0.0.1"); tt_int_op(pr1.port, ==, 80);
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Fourth test: Correct IPv6 req packet. */ @@ -210,14 +237,14 @@ test_socks_socks5_request(void *data) memcpy(req4+4,&addr_ipv6,16); memcpy(req4+20,&port,2);
- evbuffer_add(source,req4,22); - tt_int_op(SOCKS_GOOD, ==, socks5_handle_request(source,&pr1)); + evbuffer_add(s->source,req4,22); + tt_int_op(SOCKS_GOOD, ==, socks5_handle_request(s->source,&pr1)); tt_str_op(pr1.addr, ==, "d:1:5:e:a:5:e:0"); tt_int_op(pr1.port, ==, 80);
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Fifth test: Correct FQDN req packet. */ @@ -231,8 +258,8 @@ test_socks_socks5_request(void *data) strcpy((char *)req5+5,fqdn); memcpy(req5+5+strlen(fqdn),&port,2);
- evbuffer_add(source, req5, 24); - tt_int_op(SOCKS_GOOD, ==, socks5_handle_request(source,&pr1)); + evbuffer_add(s->source, req5, 24); + tt_int_op(SOCKS_GOOD, ==, socks5_handle_request(s->source,&pr1)); tt_str_op(pr1.addr, ==, "www.test.example"); tt_int_op(pr1.port, ==, 80);
@@ -243,12 +270,12 @@ test_socks_socks5_request(void *data) req6[1] = 1; req6[2] = 0;
- evbuffer_add(source,req6,3); - tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_request(source,&pr1)); + evbuffer_add(s->source,req6,3); + tt_int_op(SOCKS_INCOMPLETE, ==, socks5_handle_request(s->source,&pr1));
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Seventh test: Wrong Reserved field */ @@ -259,12 +286,12 @@ test_socks_socks5_request(void *data) req7[3] = 42; req7[4] = 42;
- evbuffer_add(source,req7,5); - tt_int_op(SOCKS_BROKEN, ==, socks5_handle_request(source,&pr1)); + evbuffer_add(s->source,req7,5); + tt_int_op(SOCKS_BROKEN, ==, socks5_handle_request(s->source,&pr1));
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Eigth test: Everything is dreamy... if only the requested command was CONNECT... */ @@ -275,20 +302,12 @@ test_socks_socks5_request(void *data) memcpy(req8+3,&addr_ipv4,4); memcpy(req8+7,&port,2);
- evbuffer_add(source, "\x05", 1); - evbuffer_add(source, req8, 9); + evbuffer_add(s->source, "\x05", 1); + evbuffer_add(s->source, req8, 9); /* '-2' means that we don't support the requested command. */ - tt_int_op(SOCKS_CMD_NOT_CONNECT, ==, socks5_handle_request(source,&pr1)); - - - end: - if (state) - socks_state_free(state); + tt_int_op(SOCKS_CMD_NOT_CONNECT, ==, socks5_handle_request(s->source,&pr1));
- if (source) - evbuffer_free(source); - if (dest) - evbuffer_free(dest); + end:; }
/** @@ -301,23 +320,19 @@ test_socks_socks5_request(void *data) static void test_socks_socks5_request_reply(void *data) { - struct evbuffer *reply_dest = NULL; - reply_dest = evbuffer_new(); + struct test_socks_state *s = (struct test_socks_state *)data;
- socks_state_t *state; - state = socks_state_new(); - - state->parsereq.af = AF_INET; - strcpy(state->parsereq.addr, "127.0.0.1"); - state->parsereq.port = 7357; + s->state->parsereq.af = AF_INET; + strcpy(s->state->parsereq.addr, "127.0.0.1"); + s->state->parsereq.port = 7357;
/* First test: We ask the server to send us a reply on an IPv4 request with succesful status. */ - socks5_send_reply(reply_dest,state, SOCKS5_SUCCESS); + socks5_send_reply(s->dest,s->state, SOCKS5_SUCCESS);
uchar rep1[255]; - evbuffer_remove(reply_dest,rep1,255); /* yes, this is dirty */ + evbuffer_remove(s->dest,rep1,255); /* yes, this is dirty */
tt_assert(rep1[3] == SOCKS5_ATYP_IPV4); /* check address */ @@ -325,20 +340,20 @@ test_socks_socks5_request_reply(void *data) /* check port */ tt_int_op(0, ==, memcmp(rep1+4+4,"\x1c\xbd",2));
- /* emptying reply_dest buffer before next test */ - size_t buffer_len = evbuffer_get_length(reply_dest); - tt_int_op(0, ==, evbuffer_drain(reply_dest, buffer_len)); + /* emptying s->dest buffer before next test */ + size_t buffer_len = evbuffer_get_length(s->dest); + tt_int_op(0, ==, evbuffer_drain(s->dest, buffer_len));
/* Second test: We ask the server to send us a reply on an IPv6 request with succesful status. */ - state->parsereq.af = AF_INET6; - strcpy(state->parsereq.addr, "d:1:5:e:a:5:e:0"); + s->state->parsereq.af = AF_INET6; + strcpy(s->state->parsereq.addr, "d:1:5:e:a:5:e:0");
- socks5_send_reply(reply_dest,state, SOCKS5_SUCCESS); + socks5_send_reply(s->dest,s->state, SOCKS5_SUCCESS);
uchar rep2[255]; - evbuffer_remove(reply_dest,rep2,255); + evbuffer_remove(s->dest,rep2,255);
tt_assert(rep2[3] = SOCKS5_ATYP_IPV6); /* Test returned address against inet_pton(d:1:5:e:a:5:e:0) */ @@ -348,21 +363,21 @@ test_socks_socks5_request_reply(void *data) 16)); tt_int_op(0, ==, memcmp(rep2+4+16, "\x1c\xbd", 2));
- /* emptying reply_dest buffer before next test */ - buffer_len = evbuffer_get_length(reply_dest); - tt_int_op(0, ==, evbuffer_drain(reply_dest, buffer_len)); + /* emptying dest buffer before next test */ + buffer_len = evbuffer_get_length(s->dest); + tt_int_op(0, ==, evbuffer_drain(s->dest, buffer_len));
/* Third test : We ask the server to send us a reply on an FQDN request with failure status. */ const char *fqdn = "www.test.example"; - state->parsereq.af = AF_UNSPEC; - strcpy(state->parsereq.addr, fqdn); + s->state->parsereq.af = AF_UNSPEC; + strcpy(s->state->parsereq.addr, fqdn);
- socks5_send_reply(reply_dest,state, SOCKS5_FAILED_GENERAL); + socks5_send_reply(s->dest, s->state, SOCKS5_FAILED_GENERAL);
uchar rep3[255]; - evbuffer_remove(reply_dest,rep3,255); + evbuffer_remove(s->dest,rep3,255);
tt_assert(rep3[3] == SOCKS5_ATYP_FQDN); tt_assert(rep3[4] == strlen(fqdn)); @@ -374,22 +389,17 @@ test_socks_socks5_request_reply(void *data) /* Fourth test: We ask the server while having an empty parsereq and with a SOCKS5_FAILED_UNSUPPORTED status. */ - memset(&state->parsereq,'\x00',sizeof(struct parsereq)); + memset(&s->state->parsereq,'\x00',sizeof(struct parsereq));
- socks5_send_reply(reply_dest,state, SOCKS5_FAILED_UNSUPPORTED); + socks5_send_reply(s->dest,s->state, SOCKS5_FAILED_UNSUPPORTED); uchar rep4[255]; - evbuffer_remove(reply_dest,rep4,255); + evbuffer_remove(s->dest,rep4,255);
tt_assert(rep4[3] == SOCKS5_ATYP_IPV4); tt_int_op(0, ==, memcmp(rep4+4,"\x00\x00\x00\x00",4)); tt_int_op(0, ==, memcmp(rep4+4+4, "\x00\x00", 2));
- end: - if (state) - socks_state_free(state); - - if (reply_dest) - evbuffer_free(reply_dest); + end:; }
/** @@ -402,37 +412,31 @@ test_socks_socks5_request_reply(void *data) static void test_socks_socks4_request(void *data) { - struct evbuffer *dest = NULL; - struct evbuffer *source = NULL; - dest = evbuffer_new(); - source = evbuffer_new(); + struct test_socks_state *s = (struct test_socks_state *)data;
const uint32_t addr = htonl(0x7f000001); /* 127.0.0.1 */ const uint16_t port = htons(80); /* 80 */
- socks_state_t *state; - state = socks_state_new(); - /* First test: Correct SOCKS4 req packet with nothing in the optional field. */ struct parsereq pr1; memset(&pr1, 0, sizeof(struct parsereq)); - state->parsereq = pr1; + s->state->parsereq = pr1; uchar req1[8]; req1[0] = 1; memcpy(req1+1,&port,2); memcpy(req1+3,&addr,4); req1[7] = '\x00';
- evbuffer_add(source,req1,8); + evbuffer_add(s->source,req1,8);
- tt_int_op(SOCKS_GOOD, ==, socks4_read_request(source,state)); - tt_str_op(state->parsereq.addr, ==, "127.0.0.1"); - tt_int_op(state->parsereq.port, ==, 80); + tt_int_op(SOCKS_GOOD, ==, socks4_read_request(s->source,s->state)); + tt_str_op(s->state->parsereq.addr, ==, "127.0.0.1"); + tt_int_op(s->state->parsereq.port, ==, 80);
/* emptying source buffer before next test */ - size_t buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + size_t buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Second test: Broken SOCKS4 req packet with incomplete optional field */ @@ -442,13 +446,13 @@ test_socks_socks4_request(void *data) memcpy(req2+3,&addr,4); strcpy(req2+7,"KO");
- evbuffer_add(source,req2,9); + evbuffer_add(s->source,req2,9);
- tt_int_op(SOCKS_INCOMPLETE, ==, socks4_read_request(source,state)); + tt_int_op(SOCKS_INCOMPLETE, ==, socks4_read_request(s->source,s->state));
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Third test: Correct SOCKS4 req packet with optional field. */ @@ -458,15 +462,15 @@ test_socks_socks4_request(void *data) memcpy(req3+3,&addr,4); strcpy(req3+7,"iamalive");
- evbuffer_add(source,req3,16); + evbuffer_add(s->source,req3,16);
- tt_int_op(SOCKS_GOOD, ==, socks4_read_request(source,state)); - tt_str_op(state->parsereq.addr, ==, "127.0.0.1"); - tt_int_op(state->parsereq.port, ==, 80); + tt_int_op(SOCKS_GOOD, ==, socks4_read_request(s->source,s->state)); + tt_str_op(s->state->parsereq.addr, ==, "127.0.0.1"); + tt_int_op(s->state->parsereq.port, ==, 80);
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Fourth test: Correct SOCKS4a req packet with optional field. */ @@ -478,15 +482,15 @@ test_socks_socks4_request(void *data) strcpy(req4+7,"iamalive"); strcpy(req4+16, "www.test.example");
- evbuffer_add(source,req4,33); + evbuffer_add(s->source,req4,33);
- tt_int_op(SOCKS_GOOD, ==, socks4_read_request(source,state)); - tt_str_op(state->parsereq.addr, ==, "www.test.example"); - tt_int_op(state->parsereq.port, ==, 80); + tt_int_op(SOCKS_GOOD, ==, socks4_read_request(s->source,s->state)); + tt_str_op(s->state->parsereq.addr, ==, "www.test.example"); + tt_int_op(s->state->parsereq.port, ==, 80);
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Fifth test: Broken SOCKS4a req packet with incomplete optional field. */ @@ -498,13 +502,13 @@ test_socks_socks4_request(void *data) strcpy(req5+16, "www.test.example");
/* Don't send it all. */ - evbuffer_add(source,req5,28); + evbuffer_add(s->source,req5,28);
- tt_int_op(SOCKS_INCOMPLETE, ==, socks4_read_request(source,state)); + tt_int_op(SOCKS_INCOMPLETE, ==, socks4_read_request(s->source,s->state));
/* emptying source buffer before next test */ - buffer_len = evbuffer_get_length(source); - tt_int_op(0, ==, evbuffer_drain(source, buffer_len)); + buffer_len = evbuffer_get_length(s->source); + tt_int_op(0, ==, evbuffer_drain(s->source, buffer_len));
/* Sixth test: Broken SOCKS4a req packet with a HUGE domain name. */ @@ -518,19 +522,12 @@ test_socks_socks4_request(void *data) memset(req6+16,'2', HUGE); req6[16+HUGE] = '\x00';
- evbuffer_add(source,req6,16+HUGE+1); + evbuffer_add(s->source,req6,16+HUGE+1);
- tt_int_op(SOCKS_BROKEN, ==, socks4_read_request(source,state)); + tt_int_op(SOCKS_BROKEN, ==, socks4_read_request(s->source,s->state)); #undef HUGE
- end: - if (state) - socks_state_free(state); - - if (source) - evbuffer_free(source); - if (dest) - evbuffer_free(dest); + end:; }
/** @@ -543,23 +540,19 @@ test_socks_socks4_request(void *data) static void test_socks_socks4_request_reply(void *data) { - struct evbuffer *reply_dest = NULL; - reply_dest = evbuffer_new(); + struct test_socks_state *s = (struct test_socks_state *)data;
- socks_state_t *state; - state = socks_state_new(); - - state->parsereq.af = AF_INET; - strcpy(state->parsereq.addr, "127.0.0.1"); - state->parsereq.port = 7357; + s->state->parsereq.af = AF_INET; + strcpy(s->state->parsereq.addr, "127.0.0.1"); + s->state->parsereq.port = 7357;
/* First test: We ask the server to send us a reply on an IPv4 request with succesful status. */ - socks4_send_reply(reply_dest,state, SOCKS4_SUCCESS); + socks4_send_reply(s->dest,s->state, SOCKS4_SUCCESS);
uchar rep1[255]; - evbuffer_remove(reply_dest,rep1,255); /* yes, this is dirty */ + evbuffer_remove(s->dest,rep1,255); /* yes, this is dirty */
tt_assert(rep1[0] == '\x00'); tt_assert(rep1[1] == SOCKS4_SUCCESS); @@ -568,21 +561,21 @@ test_socks_socks4_request_reply(void *data) /* check address */ tt_int_op(0, ==, memcmp(rep1+2+2,"\x7f\x00\x00\x01", 4));
- /* emptying reply_dest buffer before next test */ - size_t buffer_len = evbuffer_get_length(reply_dest); - tt_int_op(0, ==, evbuffer_drain(reply_dest, buffer_len)); + /* emptying dest buffer before next test */ + size_t buffer_len = evbuffer_get_length(s->dest); + tt_int_op(0, ==, evbuffer_drain(s->dest, buffer_len));
/* Second test : We ask the server to send us a reply on an FQDN request with failure status. */ const char *fqdn = "www.test.example"; - state->parsereq.af = AF_UNSPEC; - strcpy(state->parsereq.addr, fqdn); + s->state->parsereq.af = AF_UNSPEC; + strcpy(s->state->parsereq.addr, fqdn);
- socks4_send_reply(reply_dest,state, SOCKS4_FAILED); + socks4_send_reply(s->dest,s->state, SOCKS4_FAILED);
uchar rep2[255]; - evbuffer_remove(reply_dest,rep2,255); + evbuffer_remove(s->dest,rep2,255);
tt_assert(rep2[1] == SOCKS4_FAILED); /* check port */ @@ -590,22 +583,17 @@ test_socks_socks4_request_reply(void *data) /* check address */ /* tt_str_op(rep1+2+2, ==, "www.test.example"); */
- end: - if (state) - socks_state_free(state); - - if (reply_dest) - evbuffer_free(reply_dest); + end:; }
-#define T(name, flags) \ - { #name, test_socks_##name, (flags), NULL, NULL } +#define T(name) \ + { #name, test_socks_##name, 0, &socks_fixture, NULL }
struct testcase_t socks_tests[] = { - T(socks5_send_negotiation, 0), - T(socks5_request, 0), - T(socks5_request_reply, 0), - T(socks4_request, 0), - T(socks4_request_reply, 0), + T(socks5_send_negotiation), + T(socks5_request), + T(socks5_request_reply), + T(socks4_request), + T(socks4_request_reply), END_OF_TESTCASES };
tor-commits@lists.torproject.org