/
unittest.c
138 lines (104 loc) · 3.68 KB
/
unittest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <unistd.h>
#include <lwip/api.h>
#include <lwip/inet.h>
#include "unittest.h"
#include <stdio.h>
#define TEST_SUCCESS NULL
#define TEST_ASSERT(message, test) do { if (!(test)) return message; } while (0)
#define TEST_RUN(test) do { char *message = test(); tests_run++; \
if (message == TEST_SUCCESS) \
tests_success++; \
else \
printf("%s failed : %s\n", #test, message); \
} while (0)
char* simple_test(void) {
ip_addr_t destination, self_ip;
printf("%s()\n", __FUNCTION__);
struct netconn *conn;
err_t err;
struct netbuf *buf;
void *data;
u16_t len;
const char *test_str = "data\n";
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
/* Sets the device we want to connect to. */
IP4_ADDR(&destination, 10, 0, 0, 2);
IP4_ADDR(&self_ip, 10, 0, 0, 3);
/* Bind connection to well known port number 7. */
netconn_bind(conn, &self_ip, 1235);
printf("Connecting...\n");
err = netconn_connect(conn, &destination, 1235);
TEST_ASSERT("TCP connection failed.", err == ERR_OK);
/* Don't send final \0 */
err = netconn_write(conn, test_str, strlen(test_str), NETCONN_NOCOPY);
TEST_ASSERT("Netconn write failed.\n", err == ERR_OK);
err = netconn_recv(conn, &buf);
TEST_ASSERT("Recv failed.", err == ERR_OK);
netbuf_data(buf, &data, &len);
TEST_ASSERT("Data is not echoed correctly", !strcmp(data, test_str));
netconn_close(conn);
return TEST_SUCCESS;
}
/** Tests the fragmented IP packets.
* @bug Doesn't pass yet.
*/
char* fragmented_packet_test(void) {
ip_addr_t destination, self_ip;
printf("%s()\n", __FUNCTION__);
struct netconn *conn;
err_t err;
struct netbuf *buf;
void *data;
u16_t len;
/* Payload longer than MTU, should get split. */
char test_str[2001];
test_str[0] = 0;
char data_str[2001];
data_str[0] = 0;
/* Fills the data pattern. */
while (strlen(test_str) < 1900)
strcat(test_str, "data");
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
/* Sets the device we want to connect to. */
IP4_ADDR(&destination, 10, 0, 0, 2);
IP4_ADDR(&self_ip, 10, 0, 0, 3);
/* Bind connection to well known port number 7. */
netconn_bind(conn, &self_ip, 1235);
printf("Connecting...\n");
err = netconn_connect(conn, &destination, 1235);
TEST_ASSERT("TCP connection failed.", err == ERR_OK);
/* Don't send final \0 */
err = netconn_write(conn, test_str, strlen(test_str), NETCONN_NOCOPY);
TEST_ASSERT("Netconn write failed.\n", err == ERR_OK);
/* Reads whole response. */
int sum=0;
while(sum < strlen(test_str) && (err = netconn_recv(conn, &buf)) == ERR_OK) {
do {
netbuf_data(buf, &data, &len);
strncat(data_str, data, len);
sum += len;
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
TEST_ASSERT("Data is not echoed correctly", !strcmp(data_str, test_str));
netconn_close(conn);
return TEST_SUCCESS;
}
void unit_test_run_all(void)
{
int tests_run = 0, tests_success = 0;
/* delays for one second, to avoid race conditions with server thread. */
sleep(1);
TEST_RUN(simple_test);
TEST_RUN(simple_test);
TEST_RUN(fragmented_packet_test);
if (tests_run == tests_success) {
printf("All tests succeeded (%d tests run).\n", tests_run);
exit(EXIT_SUCCESS);
} else {
printf("Some tests failed (failed : %d/%d)\n", tests_run - tests_success, tests_run);
exit(EXIT_FAILURE);
}
}