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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* Ouroboros - Copyright (C) 2016 - 2026
*
* Flow PoA tests
*
* Dimitri Staessens <dimitri@ouroboros.rocks>
* Sander Vrijders <sander@ouroboros.rocks>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200809L
#endif
#include <test/test.h>
#include "poa/addr.c"
#ifdef HAVE_RAW_SOCKETS
#include "poa/eth.c"
#endif
#include <arpa/inet.h>
#include <stdio.h>
#ifdef HAVE_RAW_SOCKETS
static const uint8_t eth_our_mac[POA_MAC_SIZE] =
{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 };
static const uint8_t eth_far_mac[POA_MAC_SIZE] =
{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x02 };
static const uint8_t eth_bc_mac[POA_MAC_SIZE] =
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
#endif
/* PoA core stubs: the reader threads never run in this test. */
void poa_rx_pkt(struct poa * poa,
uint32_t eid,
struct ssm_pk_buff * spb)
{
(void) poa;
(void) eid;
(void) spb;
}
void poa_rx_mgmt(struct poa * poa,
const struct poa_addr * src,
const uint8_t * buf,
size_t len)
{
(void) poa;
(void) src;
(void) buf;
(void) len;
}
int poa_spb_reserve(struct ssm_pk_buff ** spb,
size_t len)
{
(void) spb;
(void) len;
return -1;
}
size_t poa_link_updown(int ifindex,
bool up)
{
(void) ifindex;
(void) up;
return 0;
}
bool poa_has_name(const uint8_t * hash)
{
(void) hash;
return false;
}
int poa_bcast_mgmt(const struct poa_addr * dst,
const uint8_t * buf,
size_t len)
{
(void) dst;
(void) buf;
(void) len;
return 0;
}
static void udp4_addr(struct poa_addr * addr,
const char * ip,
uint16_t port)
{
memset(addr, 0, sizeof(*addr));
addr->type = POA_UDP4;
addr->udp4.port = port;
inet_pton(AF_INET, ip, &addr->udp4.ip_addr);
}
static int test_poa_addr_cmp(void)
{
struct poa_addr a;
struct poa_addr b;
TEST_START();
udp4_addr(&a, "10.0.0.10", 3435);
udp4_addr(&b, "10.0.0.10", 3435);
if (poa_addr_cmp(&a, &b) != 0) {
printf("Identical addresses did not match.\n");
goto fail;
}
udp4_addr(&b, "10.0.0.10", 3436);
if (poa_addr_cmp(&a, &b) == 0) {
printf("Addresses with a different port matched.\n");
goto fail;
}
udp4_addr(&b, "10.0.0.11", 3435);
if (poa_addr_cmp(&a, &b) == 0) {
printf("Addresses with a different ip matched.\n");
goto fail;
}
b.type = POA_UDP6;
if (poa_addr_cmp(&a, &b) == 0) {
printf("Addresses of a different type matched.\n");
goto fail;
}
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
static int test_poa_mgmt_msg_qos(void)
{
struct poa_mgmt_msg msg;
qosspec_t qs;
TEST_START();
poa_mgmt_msg_ser(&msg, POA_FLOW_REQ, 1, 2, qos_stream, 0, 0);
if (msg.code != POA_FLOW_REQ) {
printf("Wrong code in management message.\n");
goto fail;
}
if (ntoh32(msg.s_eid) != 1 || ntoh32(msg.d_eid) != 2) {
printf("Wrong PoA ids in management message.\n");
goto fail;
}
memset(&qs, 0, sizeof(qs));
poa_mgmt_msg_qos(&msg, &qs);
if (memcmp(&qs, &qos_stream, sizeof(qs)) != 0) {
printf("QoS did not survive the management message.\n");
goto fail;
}
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
#ifdef HAVE_RAW_SOCKETS
static void eth_fake_priv(struct eth_priv * priv,
struct poa * e)
{
memset(priv, 0, sizeof(*priv));
memset(e, 0, sizeof(*e));
e->type = POA_ETH;
strcpy(e->local.eth.src.dev, "test0");
priv->poa = e;
priv->ethertype = htons(0xA000);
priv->mtu = 1500;
memcpy(priv->hw_addr, eth_our_mac, POA_MAC_SIZE);
}
static int test_eth_frame(void)
{
struct eth_priv priv;
struct poa e;
uint8_t buf[64];
struct eth_hdr * hdr = (struct eth_hdr *) buf;
const char * body = "hello";
size_t n;
size_t plen;
uint32_t eid;
TEST_START();
eth_fake_priv(&priv, &e);
eth_hdr_ser(&priv, hdr, eth_our_mac, 7, strlen(body));
memcpy(buf + ETH_HDR_TOT_SIZE, body, strlen(body));
n = ETH_HDR_TOT_SIZE + strlen(body);
if (frame_parse(&priv, buf, n, &eid, &plen) < 0) {
printf("Failed to parse a valid frame.\n");
goto fail;
}
if (eid != 7 || plen != strlen(body)) {
printf("Expected eid 7 len %zu, got %u len %zu.\n",
strlen(body), eid, plen);
goto fail;
}
hdr->poa.hcs ^= 0xFF;
if (frame_parse(&priv, buf, n, &eid, &plen) == 0) {
printf("Accepted a corrupt header checksum.\n");
goto fail;
}
hdr->poa.hcs ^= 0xFF;
hdr->ethertype ^= 0xFF;
if (frame_parse(&priv, buf, n, &eid, &plen) == 0) {
printf("Accepted a foreign Ethertype.\n");
goto fail;
}
hdr->ethertype ^= 0xFF;
if (frame_parse(&priv, buf, ETH_HDR_SIZE + 2, &eid, &plen) == 0) {
printf("Accepted a truncated frame.\n");
goto fail;
}
eth_hdr_ser(&priv, hdr, eth_far_mac, 7, strlen(body));
if (frame_parse(&priv, buf, n, &eid, &plen) == 0) {
printf("Accepted a frame for another host.\n");
goto fail;
}
eth_hdr_ser(&priv, hdr, eth_bc_mac, 7, strlen(body));
if (frame_parse(&priv, buf, n, &eid, &plen) < 0) {
printf("Rejected a broadcast frame.\n");
goto fail;
}
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
#endif /* HAVE_RAW_SOCKETS */
int poa_test(int argc,
char ** argv)
{
int ret = 0;
(void) argc;
(void) argv;
ret |= test_poa_addr_cmp();
ret |= test_poa_mgmt_msg_qos();
#ifdef HAVE_RAW_SOCKETS
ret |= test_eth_frame();
#endif
return ret;
}
|