From: John Hodge Date: Sat, 10 May 2014 02:36:37 +0000 (+0800) Subject: Tools/NetTest - Code cleanup in TCP tests X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=01401dc92068f781e6af6c601be7deba954b3444;p=tpg%2Facess2.git Tools/NetTest - Code cleanup in TCP tests --- diff --git a/Tools/NetTest_Runner/Makefile b/Tools/NetTest_Runner/Makefile index e9575793..59704c70 100644 --- a/Tools/NetTest_Runner/Makefile +++ b/Tools/NetTest_Runner/Makefile @@ -6,7 +6,7 @@ OBJ += link.o OBJ += test_arp.o test_tcp.o BIN := ../nettest_runner -CFLAGS := -Wall -std=c99 +CFLAGS := -Wall -std=c99 -g CPPFLAGS := -Iinclude LIBS := @@ -23,7 +23,7 @@ $(BIN): $(OBJ) @echo [CC] -o $@ @$(CC) $(LINKFLAGS) -o $@ $(OBJ) $(LIBS) -obj/%.o: %.c +obj/%.o: %.c Makefile @mkdir -p $(dir $@) @echo [CC] -c -o $@ @$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $< -MMD -MP diff --git a/Tools/NetTest_Runner/include/tests.h b/Tools/NetTest_Runner/include/tests.h index a96f3881..f6cc1356 100644 --- a/Tools/NetTest_Runner/include/tests.h +++ b/Tools/NetTest_Runner/include/tests.h @@ -10,6 +10,7 @@ extern bool Test_ARP_Basic(void); extern bool Test_TCP_Basic(void); +extern bool Test_TCP_Reset(void); extern bool Test_TCP_WindowSizes(void); #endif diff --git a/Tools/NetTest_Runner/main.c b/Tools/NetTest_Runner/main.c index b1d95c13..57ac080e 100644 --- a/Tools/NetTest_Runner/main.c +++ b/Tools/NetTest_Runner/main.c @@ -27,16 +27,19 @@ int main(int argc, char *argv[]) Test_ARP_Basic, Test_TCP_Basic, Test_TCP_WindowSizes, + Test_TCP_Reset, NULL }; + // Truncate the two output files // TODO: Move to stack.c - FILE *fp; - fp = fopen("stdout.txt", "w"); fclose(fp); - fp = fopen("stderr.txt", "w"); fclose(fp); + fclose( fopen("stdout.txt", "w") ); + fclose( fopen("stderr.txt", "w") ); Net_Open(0, "/tmp/acess2net"); + int n_pass = 0; + int n_fail = 0; for(int i = 0; tests[i]; i ++ ) { Stack_AddDevice("/tmp/acess2net", (char[]){TEST_MAC}); @@ -48,18 +51,22 @@ int main(int argc, char *argv[]) if( Net_Receive(0, 1, &argc, 1000) == 0 ) goto teardown; - if( tests[i]() ) - printf("%s: PASS\n", gsTestName); + bool result = tests[i](); + + printf("%s: %s\n", gsTestName, (result ? "PASS" : "FAIL")); + if(result) + n_pass ++; else - printf("%s: FAIL\n", gsTestName); + n_fail ++; teardown: Stack_Kill(); } Net_Close(0); unlink("/tmp/acess2net"); + printf("--- All tests done %i pass, %i fail\n", n_pass, n_fail); - return 0; + return n_fail; } void PrintUsage(const char *ProgName) diff --git a/Tools/NetTest_Runner/stack.c b/Tools/NetTest_Runner/stack.c index 7d5e2613..1a224e85 100644 --- a/Tools/NetTest_Runner/stack.c +++ b/Tools/NetTest_Runner/stack.c @@ -53,6 +53,7 @@ void Stack_AddInterface(const char *Name, int AddrType, const void *Addr, int Ma break; default: assert(AddrType == 4); + fprintf(stderr, "Stack_AddInterface: Bad address type %i\n", AddrType); exit(1); } } @@ -63,14 +64,24 @@ void Stack_AddRoute(int Type, const void *Network, int MaskBits, const void *Nex void Stack_AddArg(const char *Fmt, ...) { + if( giNumStackArgs == MAX_ARGS ) { + fprintf(stderr, "ERROR: Too many arguments to stack, %i max\n", MAX_ARGS); + return ; + } va_list args; + va_start(args, Fmt); size_t len = vsnprintf(NULL, 0, Fmt, args); va_end(args); + char *arg = malloc(len+1); + assert(arg); + va_start(args, Fmt); vsnprintf(arg, len+1, Fmt, args); va_end(args); + + assert( arg[len] == '\0' ); gasStackArgs[giNumStackArgs++] = arg; } @@ -123,6 +134,10 @@ void Stack_Kill(void) kill(giStack_PID, SIGTERM); giStack_PID = 0; } + + for( int i = 1; i < giNumStackArgs; i ++ ) + free(gasStackArgs[i]); + giNumStackArgs = 1; } int Stack_SendCommand(const char *Fmt, ...) diff --git a/Tools/NetTest_Runner/test_tcp.c b/Tools/NetTest_Runner/test_tcp.c index 36942a5a..4e4aeb47 100644 --- a/Tools/NetTest_Runner/test_tcp.c +++ b/Tools/NetTest_Runner/test_tcp.c @@ -13,6 +13,13 @@ #include "tcp.h" #include +#define RX_HEADER \ + size_t rxlen, ofs, len; \ + char rxbuf[MTU] +#define TEST_HEADER \ + TEST_SETNAME(__func__);\ + RX_HEADER + #define TEST_ASSERT_rx() TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) ) #define TEST_ASSERT_no_rx() TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 ) const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure) @@ -24,9 +31,7 @@ const size_t DACK_BYTES = 4096; // OS PARAM - Threshold for delayed ACKs bool Test_TCP_Basic(void) { - TEST_SETNAME(__func__); - size_t rxlen, ofs, len; - char rxbuf[MTU]; + TEST_HEADER; tTCPConn testconn = { .IFNum = 0, .AF = 4, @@ -206,20 +211,41 @@ bool Test_TCP_Basic(void) return true; } +bool Test_TCP_int_OpenConnection(tTCPConn *Conn) +{ + RX_HEADER; + // >> SYN + TCP_SendC(Conn, TCP_SYN, 0, NULL); + Conn->LSeq ++; + TEST_ASSERT_rx(); + // << SYN|ACK (save remote sequence number) + TCP_SkipCheck_Seq(true); + TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, Conn, TCP_SYN|TCP_ACK) ); + TEST_ASSERT_REL(len, ==, 0); + Conn->RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, Conn->AF) + 1; + // >> ACK + TCP_SendC(Conn, TCP_ACK, 0, NULL); + TEST_ASSERT_no_rx(); + return true; +} + +#if 0 bool Test_TCP_SYN_RECEIVED(void) { - TEST_SETNAME(__func__); + TEST_HEADER; + // 1. Get into SYN-RECEIVED + TCP_SendC(&testconn, TCP_SYN, 0, NULL); // 2. Send various non-ACK packets return false; } +#endif -bool Test_TCP_WindowSizes(void) +bool Test_TCP_Reset(void) { - TEST_SETNAME(__func__); - size_t rxlen, ofs, len; - char rxbuf[MTU]; + TEST_HEADER; + tTCPConn testconn = { .IFNum = 0, .AF = 4, @@ -231,20 +257,71 @@ bool Test_TCP_WindowSizes(void) .RSeq = 0x600, .Window = 128 }; - char testdata[152]; - memset(testdata, 0xAB, sizeof(testdata)); - + Stack_SendCommand("tcp_echo_server %i", testconn.RPort); - // > Open Connection + + // 1. Response in listen-based SYN-RECEIVED + // >> SYN TCP_SendC(&testconn, TCP_SYN, 0, NULL); testconn.LSeq ++; + // << SYN|ACK :: Now in SYN-RECEIVED TEST_ASSERT_rx(); TCP_SkipCheck_Seq(true); TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) ); TEST_ASSERT_REL(len, ==, 0); testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1; + // >> RST (not ACK) + TCP_SendC(&testconn, TCP_RST, 0, NULL); + // << nothing (connection should now be dead) + TEST_ASSERT_no_rx(); + // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above) TCP_SendC(&testconn, TCP_ACK, 0, NULL); + // << RST + TEST_ASSERT_rx(); + TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) ); + TEST_ASSERT_REL(len, ==, 0); + + // 2. Response in open-based SYN-RECEIVED? (What is that?) + TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED"); + + testconn.LPort += 1234; + // ESTABLISHED[RST] - RFC793:Pg70 + // 2. Response in ESTABLISHED + TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) ); + // >> RST + TCP_SendC(&testconn, TCP_RST, 0, NULL); + // << no response, connection closed TEST_ASSERT_no_rx(); + // >> ACK (LISTEN[ACK]) + TCP_SendC(&testconn, TCP_ACK, 0, NULL); + // << RST + TEST_ASSERT_rx(); + TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) ); + TEST_ASSERT_REL(len, ==, 0); + + return true; +} + +bool Test_TCP_WindowSizes(void) +{ + TEST_HEADER; + tTCPConn testconn = { + .IFNum = 0, + .AF = 4, + .LAddr = BLOB(HOST_IP), + .RAddr = BLOB(TEST_IP), + .LPort = 44359, + .RPort = 9, + .LSeq = 0x600, + .RSeq = 0x600, + .Window = 128 + }; + char testdata[152]; + memset(testdata, 0xAB, sizeof(testdata)); + + Stack_SendCommand("tcp_echo_server %i", testconn.RPort); + // > Open Connection + TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) ); // 1. Test remote respecting our transmission window (>=1 byte) // > Send more data than our RX window