/
main.c
253 lines (215 loc) · 7.04 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#define BUFFER_SIZE 128
extern void socket_stub_input(void *buf_p, size_t size);
extern void socket_stub_output(void *buf_p, size_t size);
static char qoutbuf[BUFFER_SIZE];
static QUEUE_INIT_DECL(qout, qoutbuf, sizeof(qoutbuf));
static int read_until(char *buf_p, const char *pattern)
{
char c;
size_t length = 0;
size_t pattern_length = strlen(pattern);
while (length < BUFFER_SIZE - 1) {
chan_read(&qout, &c, sizeof(c));
#if defined(ARCH_LINUX)
std_printf(FSTR("%c"), c);
#endif
*buf_p++ = c;
length++;
*buf_p = '\0';
/* Compare to pattern. */
if (length >= pattern_length) {
if (!strcmp(&buf_p[-pattern_length], pattern)) {
return (1);
}
}
}
return (0);
}
static int test_host_by_ip_address(struct harness_t *harness_p)
{
struct inet_ip_addr_t address;
struct time_t round_trip_time, timeout;
uint8_t request[8];
uint8_t reply[28];
timeout.seconds = 1;
timeout.nanoseconds = 0;
/* Prepare the socket stub with the reply packet. The first 20
bytes in the reply are the IP header. */
reply[20] = 0;
reply[21] = 0;
reply[22] = 0xff;
reply[23] = 0xfe;
reply[24] = 0;
reply[25] = 0;
reply[26] = 0;
reply[27] = 1;
socket_stub_input(reply, sizeof(reply));
/* Perform the ping. */
address.number = 0x1;
BTASSERT(ping_host_by_ip_address(&address,
&timeout,
&round_trip_time) == 0);
/* Check the request send by the ping module. */
socket_stub_output(request, sizeof(request));
BTASSERT(request[0] == 8);
BTASSERT(request[1] == 0);
BTASSERT(request[2] == 0xf7);
BTASSERT(request[3] == 0xfe);
BTASSERT(request[4] == 0);
BTASSERT(request[5] == 0);
BTASSERT(request[6] == 0);
BTASSERT(request[7] == 1);
std_printf(FSTR("round trip time: %lu s %lu ns\r\n"),
round_trip_time.seconds,
round_trip_time.nanoseconds);
return (0);
}
static int test_bad_reply_crc(struct harness_t *harness_p)
{
struct inet_ip_addr_t address;
struct time_t round_trip_time, timeout;
uint8_t request[8];
uint8_t reply[28];
timeout.seconds = 1;
timeout.nanoseconds = 0;
/* Prepare the socket stub with the reply packet. The first 20
bytes in the reply are the IP header. */
reply[20] = 0;
reply[21] = 0;
reply[22] = 0xff;
reply[23] = 0xfe;
reply[24] = 0;
reply[25] = 1;
reply[26] = 0;
reply[27] = 1;
socket_stub_input(reply, sizeof(reply));
/* Perform the ping. */
address.number = 0x1;
BTASSERT(ping_host_by_ip_address(&address,
&timeout,
&round_trip_time) == -1);
/* Check the request send by the ping module. */
socket_stub_output(request, sizeof(request));
BTASSERT(request[0] == 8);
BTASSERT(request[1] == 0);
BTASSERT(request[2] == 0xf7);
BTASSERT(request[3] == 0xfd);
BTASSERT(request[4] == 0);
BTASSERT(request[5] == 1);
BTASSERT(request[6] == 0);
BTASSERT(request[7] == 1);
return (0);
}
static int test_cmd_ping(struct harness_t *harness_p)
{
uint8_t request[8];
uint8_t reply[28];
char buf[128];
/* Prepare the socket stub with the reply packet. The first 20
bytes in the reply are the IP header. */
reply[20] = 0;
reply[21] = 0;
reply[22] = 0xff;
reply[23] = 0xfc;
reply[24] = 0;
reply[25] = 2;
reply[26] = 0;
reply[27] = 1;
socket_stub_input(reply, sizeof(reply));
/* Perform the ping. */
strcpy(buf, "inet/ping/ping 1.1.1.1\r\n");
BTASSERT(fs_call(buf, NULL, &qout, NULL) == 0);
read_until(buf, "Successfully pinged '1.1.1.1' in 0 ms.\r\n");
/* Check the request send by the ping module. */
socket_stub_output(request, sizeof(request));
BTASSERT(request[0] == 8);
BTASSERT(request[1] == 0);
BTASSERT(request[2] == 0xf7);
BTASSERT(request[3] == 0xfc);
BTASSERT(request[4] == 0);
BTASSERT(request[5] == 2);
BTASSERT(request[6] == 0);
BTASSERT(request[7] == 1);
return (0);
}
static int test_cmd_ping_bad_reply(struct harness_t *harness_p)
{
uint8_t reply[28];
char buf[128];
/* Prepare the socket stub with a bad reply packet. The first 20
bytes in the reply are the IP header. */
reply[20] = 0;
reply[21] = 0;
reply[22] = 0xff;
reply[23] = 0xfe;
reply[24] = 0;
reply[25] = 2;
reply[26] = 0;
reply[27] = 2;
socket_stub_input(reply, sizeof(reply));
/* Perform the ping. */
strcpy(buf, "inet/ping/ping 1.1.1.1\r\n");
BTASSERT(fs_call(buf, NULL, &qout, NULL) == 0);
read_until(buf, "Failed to ping '1.1.1.1'.\r\n");
return (0);
}
static int test_cmd_ping_bad_input(struct harness_t *harness_p)
{
char buf[128];
/* Too few arguemnts. */
strcpy(buf, "inet/ping/ping\r\n");
BTASSERT(fs_call(buf, NULL, &qout, NULL) == -1);
read_until(buf, "Usage: inet/ping/ping <remote host>\r\n");
/* Bad ip address. */
strcpy(buf, "inet/ping/ping a.b.c.d\r\n");
BTASSERT(fs_call(buf, NULL, &qout, NULL) == -1);
read_until(buf, "Bad ip address 'a.b.c.d'.\r\n");
return (0);
}
int main()
{
struct harness_t harness;
struct harness_testcase_t harness_testcases[] = {
{ test_host_by_ip_address, "test_host_by_ip_address" },
{ test_bad_reply_crc, "test_bad_reply_crc" },
{ test_cmd_ping, "test_cmd_ping" },
{ test_cmd_ping_bad_reply, "test_cmd_ping_bad_reply" },
{ test_cmd_ping_bad_input, "test_cmd_ping_bad_input" },
{ NULL, NULL }
};
sys_start();
ping_module_init();
harness_init(&harness);
harness_run(&harness, harness_testcases);
return (0);
}