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});
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)
break;
default:
assert(AddrType == 4);
+ fprintf(stderr, "Stack_AddInterface: Bad address type %i\n", AddrType);
exit(1);
}
}
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;
}
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, ...)
#include "tcp.h"
#include <string.h>
+#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)
bool Test_TCP_Basic(void)
{
- TEST_SETNAME(__func__);
- size_t rxlen, ofs, len;
- char rxbuf[MTU];
+ TEST_HEADER;
tTCPConn testconn = {
.IFNum = 0, .AF = 4,
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,
.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