1 /*
2 * Copyright (c) 2022 Trackunit Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*************************************************************************************************/
8 /* Dependencies */
9 /*************************************************************************************************/
10 #include <zephyr/ztest.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/net/net_if.h>
13 #include <zephyr/net/net_pkt.h>
14 #include <zephyr/net/net_l2.h>
15 #include <zephyr/net/ppp.h>
16 #include <zephyr/sys/crc.h>
17 #include <string.h>
18
19 #include <zephyr/modem/ppp.h>
20 #include <modem_backend_mock.h>
21
22 #define TEST_MODEM_PPP_BUF_SIZE (16)
23 #define TEST_MODEM_PPP_TX_PKT_BUF_SIZE (5)
24 #define TEST_MODEM_PPP_MOCK_PIPE_RX_BUF_SIZE (4096)
25 #define TEST_MODEM_PPP_MOCK_PIPE_TX_BUF_SIZE (4096)
26
27 #define TEST_MODEM_PPP_IP_FRAME_SEND_MULT_N (5)
28 #define TEST_MODEM_PPP_IP_FRAME_SEND_LARGE_N (2048)
29 #define TEST_MODEM_PPP_IP_FRAME_RECEIVE_LARGE_N (2048)
30
31 /*************************************************************************************************/
32 /* Mock pipe */
33 /*************************************************************************************************/
34 static struct modem_backend_mock mock;
35 static uint8_t mock_rx_buf[TEST_MODEM_PPP_MOCK_PIPE_RX_BUF_SIZE];
36 static uint8_t mock_tx_buf[TEST_MODEM_PPP_MOCK_PIPE_TX_BUF_SIZE];
37 static struct modem_pipe *mock_pipe;
38
39 /*************************************************************************************************/
40 /* PPP frames */
41 /*************************************************************************************************/
42 static uint8_t ppp_frame_wrapped[] = {0x7E, 0xFF, 0x7D, 0x23, 0xC0, 0x21, 0x7D, 0x21, 0x7D,
43 0x21, 0x7D, 0x20, 0x7D, 0x24, 0xD1, 0xB5, 0x7E};
44
45 static uint8_t ppp_frame_unwrapped[] = {0xC0, 0x21, 0x01, 0x01, 0x00, 0x04};
46
47 static uint8_t ip_frame_wrapped[] = {
48 0x7E, 0xFF, 0x7D, 0x23, 0x7D, 0x20, 0x21, 0x45, 0x7D, 0x20, 0x7D, 0x20, 0x29, 0x87, 0x6E,
49 0x40, 0x7D, 0x20, 0xE8, 0x7D, 0x31, 0xC1, 0xE9, 0x7D, 0x23, 0xFB, 0x7D, 0x25, 0x20, 0x7D,
50 0x2A, 0x2B, 0x36, 0x26, 0x25, 0x7D, 0x32, 0x8C, 0x3E, 0x7D, 0x20, 0x7D, 0x35, 0xBD, 0xF3,
51 0x2D, 0x7D, 0x20, 0x7D, 0x2B, 0x7D, 0x20, 0x7D, 0x27, 0x7D, 0x20, 0x7D, 0x24, 0x7D, 0x20,
52 0x7D, 0x24, 0x7D, 0x2A, 0x7D, 0x20, 0x7D, 0x2A, 0x7D, 0x20, 0xD4, 0x31, 0x7E};
53
54 static uint8_t ip_frame_unwrapped[] = {
55 0x45, 0x00, 0x00, 0x29, 0x87, 0x6E, 0x40, 0x00, 0xE8, 0x11, 0xC1, 0xE9, 0x03, 0xFB,
56 0x05, 0x20, 0x0A, 0x2B, 0x36, 0x26, 0x25, 0x12, 0x8C, 0x3E, 0x00, 0x15, 0xBD, 0xF3,
57 0x2D, 0x00, 0x0B, 0x00, 0x07, 0x00, 0x04, 0x00, 0x04, 0x0A, 0x00, 0x0A, 0x00};
58
59 static uint8_t ip_frame_unwrapped_with_protocol[] = {
60 0x00, 0x21, 0x45, 0x00, 0x00, 0x29, 0x87, 0x6E, 0x40, 0x00, 0xE8, 0x11, 0xC1, 0xE9, 0x03,
61 0xFB, 0x05, 0x20, 0x0A, 0x2B, 0x36, 0x26, 0x25, 0x12, 0x8C, 0x3E, 0x00, 0x15, 0xBD, 0xF3,
62 0x2D, 0x00, 0x0B, 0x00, 0x07, 0x00, 0x04, 0x00, 0x04, 0x0A, 0x00, 0x0A, 0x00};
63
64 static uint8_t corrupt_start_end_ppp_frame_wrapped[] = {0x2A, 0x46, 0x7E, 0x7E, 0xFF, 0x7D, 0x23,
65 0xC0, 0x21, 0x7D, 0x21, 0x7D, 0x21, 0x7D,
66 0x20, 0x7D, 0x24, 0xD1, 0xB5, 0x7E};
67
68 /*************************************************************************************************/
69 /* Buffers */
70 /*************************************************************************************************/
71 static struct net_pkt *received_packets[12];
72 static size_t received_packets_len;
73 static uint8_t buffer[4096];
74 static uint8_t unwrapped_buffer[4096];
75 static uint8_t wrapped_buffer[4096];
76
77 /*************************************************************************************************/
78 /* Mock network interface */
79 /*************************************************************************************************/
80 static uint8_t test_net_link_addr[] = {0x00, 0x00, 0x5E, 0x00, 0x53, 0x01};
81
test_net_l2_recv(struct net_if * iface,struct net_pkt * pkt)82 static enum net_verdict test_net_l2_recv(struct net_if *iface, struct net_pkt *pkt)
83 {
84 /* Validate buffer not overflowing */
85 zassert_true(received_packets_len < ARRAY_SIZE(received_packets),
86 "Mock network interface receive buffer limit reached");
87
88 /* Store pointer to received packet */
89 received_packets[received_packets_len] = pkt;
90 received_packets_len++;
91 return NET_OK;
92 }
93
94 /* This emulates the layer two API */
95 static struct net_l2 test_net_l2 = {
96 .recv = test_net_l2_recv,
97 };
98
99 /* This emulates the network interface device which will receive unwrapped network packets */
100 static struct net_if_dev test_net_if_dev = {
101 .l2 = &test_net_l2,
102 .link_addr.addr = test_net_link_addr,
103 .link_addr.len = sizeof(test_net_link_addr),
104 .link_addr.type = NET_LINK_DUMMY,
105 .mtu = 1500,
106 .oper_state = NET_IF_OPER_UP,
107 };
108
109 /* This emulates the network interface which contains the network interface device */
110 static struct net_if test_iface = {
111 .if_dev = &test_net_if_dev,
112 };
113
114 /*************************************************************************************************/
115 /* Modem PPP */
116 /*************************************************************************************************/
117 /*
118 * The following initialization happens automatically when MODEM_PPP_DEFINE is used. However,
119 * since we are emulating the network interface, we can't use that macro, and have to initialize
120 * it manually here.
121 */
122 static uint8_t ppp_receive_buf[TEST_MODEM_PPP_BUF_SIZE];
123 static uint8_t ppp_transmit_buf[TEST_MODEM_PPP_BUF_SIZE];
124
125 static struct modem_ppp ppp = {
126 .iface = &test_iface,
127 .receive_buf = ppp_receive_buf,
128 .transmit_buf = ppp_transmit_buf,
129 .buf_size = TEST_MODEM_PPP_BUF_SIZE,
130 };
131
132 /*************************************************************************************************/
133 /* Modem PPP net device */
134 /*************************************************************************************************/
135 extern const struct ppp_api modem_ppp_ppp_api;
136 static const struct device ppp_net_dev = {.data = &ppp};
test_net_send(struct net_pkt * pkt)137 static int test_net_send(struct net_pkt *pkt)
138 {
139 return modem_ppp_ppp_api.send(&ppp_net_dev, pkt);
140 }
141
142 /*************************************************************************************************/
143 /* Helpers */
144 /*************************************************************************************************/
test_modem_ppp_prng_random(bool reset)145 static uint8_t test_modem_ppp_prng_random(bool reset)
146 {
147 static uint32_t prng_state = 1234;
148
149 if (reset == true) {
150 prng_state = 1234;
151 }
152
153 prng_state = (1103515245 * prng_state + 12345) % (1 << 31);
154 return (uint8_t)(prng_state & 0xFF);
155 }
156
test_modem_ppp_fill_net_pkt(struct net_pkt * pkt,size_t size)157 static size_t test_modem_ppp_fill_net_pkt(struct net_pkt *pkt, size_t size)
158 {
159 test_modem_ppp_prng_random(true);
160
161 for (size_t i = 0; i < size; i++) {
162 if (net_pkt_write_u8(pkt, test_modem_ppp_prng_random(false)) < 0) {
163 return i;
164 }
165 }
166
167 return size;
168 }
169
test_modem_ppp_unwrap(uint8_t * unwrapped,const uint8_t * wrapped,size_t wrapped_size)170 static size_t test_modem_ppp_unwrap(uint8_t *unwrapped, const uint8_t *wrapped, size_t wrapped_size)
171 {
172 size_t wrapped_pos = 4;
173 size_t unwrapped_pos = 0;
174
175 while (wrapped_pos < (wrapped_size - 1)) {
176 /* Escape byte */
177 if (wrapped[wrapped_pos] == 0x7D) {
178 unwrapped[unwrapped_pos] = wrapped[wrapped_pos + 1] ^ 0x20;
179 wrapped_pos += 2;
180 unwrapped_pos += 1;
181 continue;
182 }
183
184 /* Normal byte */
185 unwrapped[unwrapped_pos] = wrapped[wrapped_pos];
186 wrapped_pos += 1;
187 unwrapped_pos += 1;
188 }
189
190 /* Remove FCS */
191 unwrapped_pos -= 2;
192 return unwrapped_pos;
193 }
194
test_modem_ppp_validate_fill(const uint8_t * data,size_t size)195 static bool test_modem_ppp_validate_fill(const uint8_t *data, size_t size)
196 {
197 test_modem_ppp_prng_random(true);
198
199 for (size_t i = 0; i < size; i++) {
200 if (data[i] != test_modem_ppp_prng_random(false)) {
201 return false;
202 }
203 }
204
205 return true;
206 }
207
test_modem_ppp_generate_ppp_frame(uint8_t * frame,size_t size)208 static void test_modem_ppp_generate_ppp_frame(uint8_t *frame, size_t size)
209 {
210 uint8_t byte;
211 uint16_t fcs;
212
213 test_modem_ppp_prng_random(true);
214
215 byte = 0x03;
216 fcs = crc16_ccitt(0xFFFF, &byte, 0x01);
217
218 frame[0] = 0x00;
219 frame[1] = 0x21;
220
221 for (size_t i = 2; i < (size - 2); i++) {
222 byte = test_modem_ppp_prng_random(false);
223 frame[i] = byte;
224 }
225
226 fcs = crc16_ccitt(fcs, frame, size) ^ 0xFFFF;
227
228 frame[size - 2] = fcs >> 8;
229 frame[size - 1] = fcs;
230 }
231
test_modem_ppp_wrap_ppp_frame(uint8_t * wrapped,const uint8_t * frame,size_t size)232 static size_t test_modem_ppp_wrap_ppp_frame(uint8_t *wrapped, const uint8_t *frame, size_t size)
233 {
234 size_t wrapped_pos = 4;
235
236 wrapped[0] = 0x7E;
237 wrapped[1] = 0xFF;
238 wrapped[2] = 0x7D;
239 wrapped[3] = 0x23;
240
241 for (size_t i = 0; i < size; i++) {
242 if ((frame[i] == 0x7E) || (frame[i] == 0x7D) || (frame[i] < 0x20)) {
243 wrapped[wrapped_pos] = 0x7D;
244 wrapped[wrapped_pos + 1] = frame[i] ^ 0x20;
245 wrapped_pos += 2;
246 continue;
247 }
248
249 wrapped[wrapped_pos] = frame[i];
250 wrapped_pos += 1;
251 }
252
253 wrapped[wrapped_pos] = 0x7E;
254 wrapped_pos += 1;
255 return wrapped_pos;
256 }
257
258 /*************************************************************************************************/
259 /* Test setup */
260 /*************************************************************************************************/
test_modem_ppp_setup(void)261 static void *test_modem_ppp_setup(void)
262 {
263 /*
264 * Manually run internal init function which would normally be performed by kernel as
265 * result of using the macro MODEM_PPP_DEFINE()
266 */
267 zassert_true(modem_ppp_init_internal(&ppp_net_dev) == 0, "Failed to run internal init");
268 net_if_flag_set(modem_ppp_get_iface(&ppp), NET_IF_UP);
269
270 const struct modem_backend_mock_config mock_config = {
271 .rx_buf = mock_rx_buf,
272 .rx_buf_size = sizeof(mock_rx_buf),
273 .tx_buf = mock_tx_buf,
274 .tx_buf_size = sizeof(mock_tx_buf),
275 .limit = 8,
276 };
277
278 mock_pipe = modem_backend_mock_init(&mock, &mock_config);
279 zassert_true(modem_pipe_open(mock_pipe, K_SECONDS(10)) == 0, "Failed to open mock pipe");
280 modem_ppp_attach(&ppp, mock_pipe);
281 return NULL;
282 }
283
test_modem_ppp_before(void * f)284 static void test_modem_ppp_before(void *f)
285 {
286 /* Unreference packets */
287 for (size_t i = 0; i < received_packets_len; i++) {
288 net_pkt_unref(received_packets[i]);
289 }
290
291 /* Reset packets received buffer */
292 received_packets_len = 0;
293
294 /* Reset mock pipe */
295 modem_backend_mock_reset(&mock);
296 }
297
298 /*************************************************************************************************/
299 /* Tests */
300 /*************************************************************************************************/
ZTEST(modem_ppp,test_ppp_frame_receive)301 ZTEST(modem_ppp, test_ppp_frame_receive)
302 {
303 struct net_pkt *pkt;
304 size_t pkt_len;
305
306 /* Put wrapped frame */
307 modem_backend_mock_put(&mock, ppp_frame_wrapped, sizeof(ppp_frame_wrapped));
308
309 /* Give modem ppp time to process received frame */
310 k_msleep(1000);
311
312 /* Validate frame received on mock network interface */
313 zassert_true(received_packets_len == 1, "Expected to receive one network packet");
314
315 pkt = received_packets[0];
316 pkt_len = net_pkt_get_len(pkt);
317
318 /* Validate length of received frame */
319 zassert_true(pkt_len == sizeof(ppp_frame_unwrapped), "Received net pkt data len incorrect");
320
321 /* Validate data of received frame */
322 net_pkt_cursor_init(pkt);
323 net_pkt_read(pkt, buffer, pkt_len);
324
325 zassert_true(memcmp(buffer, ppp_frame_unwrapped, pkt_len) == 0,
326 "Recevied net pkt data incorrect");
327 }
328
ZTEST(modem_ppp,test_corrupt_start_end_ppp_frame_receive)329 ZTEST(modem_ppp, test_corrupt_start_end_ppp_frame_receive)
330 {
331 struct net_pkt *pkt;
332 size_t pkt_len;
333
334 /* Put wrapped frame */
335 modem_backend_mock_put(&mock, corrupt_start_end_ppp_frame_wrapped,
336 sizeof(corrupt_start_end_ppp_frame_wrapped));
337
338 /* Give modem ppp time to process received frame */
339 k_msleep(1000);
340
341 /* Validate frame received on mock network interface */
342 zassert_true(received_packets_len == 1, "Expected to receive one network packet");
343
344 /* Validate length of received frame */
345 pkt = received_packets[0];
346 pkt_len = net_pkt_get_len(pkt);
347 zassert_true(pkt_len == sizeof(ppp_frame_unwrapped), "Received net pkt data len incorrect");
348
349 /* Validate data of received frame */
350 net_pkt_cursor_init(pkt);
351 net_pkt_read(pkt, buffer, pkt_len);
352 zassert_true(memcmp(buffer, ppp_frame_unwrapped, pkt_len) == 0,
353 "Recevied net pkt data incorrect");
354 }
355
ZTEST(modem_ppp,test_ppp_frame_send)356 ZTEST(modem_ppp, test_ppp_frame_send)
357 {
358 struct net_pkt *pkt;
359 int ret;
360
361 /* Allocate net pkt */
362 pkt = net_pkt_alloc_with_buffer(&test_iface, 256, AF_UNSPEC, 0, K_NO_WAIT);
363
364 zassert_true(pkt != NULL, "Failed to allocate network packet");
365
366 /* Set network packet data */
367 net_pkt_cursor_init(pkt);
368 ret = net_pkt_write(pkt, ppp_frame_unwrapped, sizeof(ppp_frame_unwrapped));
369 zassert_true(ret == 0, "Failed to write data to allocated network packet");
370 net_pkt_set_ppp(pkt, true);
371
372 /* Send network packet */
373 zassert_true(test_net_send(pkt) == 0, "Failed to send PPP pkt");
374
375 /* Give modem ppp time to wrap and send frame */
376 k_msleep(1000);
377
378 /* Get any sent data */
379 ret = modem_backend_mock_get(&mock, buffer, sizeof(buffer));
380 zassert_true(ret == sizeof(ppp_frame_wrapped), "Wrapped frame length incorrect");
381 zassert_true(memcmp(buffer, ppp_frame_wrapped, ret) == 0,
382 "Wrapped frame content is incorrect");
383 }
384
ZTEST(modem_ppp,test_ip_frame_receive)385 ZTEST(modem_ppp, test_ip_frame_receive)
386 {
387 struct net_pkt *pkt;
388 size_t pkt_len;
389
390 /* Put wrapped frame */
391 modem_backend_mock_put(&mock, ip_frame_wrapped, sizeof(ip_frame_wrapped));
392
393 /* Give modem ppp time to process received frame */
394 k_msleep(1000);
395
396 /* Validate frame received on mock network interface */
397 zassert_true(received_packets_len == 1, "Expected to receive one network packet");
398 pkt = received_packets[0];
399 pkt_len = net_pkt_get_len(pkt);
400
401 /* Validate length of received frame */
402 zassert_true(pkt_len == sizeof(ip_frame_unwrapped_with_protocol),
403 "Received net pkt data len incorrect");
404
405 /* Validate data of received frame */
406 net_pkt_cursor_init(pkt);
407 net_pkt_read(pkt, buffer, pkt_len);
408 zassert_true(memcmp(buffer, ip_frame_unwrapped_with_protocol, pkt_len) == 0,
409 "Recevied net pkt data incorrect");
410 }
411
ZTEST(modem_ppp,test_ip_frame_send)412 ZTEST(modem_ppp, test_ip_frame_send)
413 {
414 struct net_pkt *pkt;
415 int ret;
416
417 /* Allocate net pkt */
418 pkt = net_pkt_alloc_with_buffer(&test_iface, 256, AF_UNSPEC, 0, K_NO_WAIT);
419 zassert_true(pkt != NULL, "Failed to allocate network packet");
420
421 /* Set network packet data */
422 net_pkt_cursor_init(pkt);
423 ret = net_pkt_write(pkt, ip_frame_unwrapped, sizeof(ip_frame_unwrapped));
424 zassert_true(ret == 0, "Failed to write data to allocated network packet");
425 net_pkt_set_family(pkt, AF_INET);
426
427 /* Send network packet */
428 test_net_send(pkt);
429
430 /* Give modem ppp time to wrap and send frame */
431 k_msleep(100);
432
433 /* Get any sent data */
434 ret = modem_backend_mock_get(&mock, buffer, sizeof(buffer));
435 zassert_true(ret == sizeof(ip_frame_wrapped), "Wrapped frame length incorrect");
436 zassert_true(memcmp(buffer, ip_frame_wrapped, ret) == 0,
437 "Wrapped frame content is incorrect");
438 }
439
ZTEST(modem_ppp,test_ip_frame_send_multiple)440 ZTEST(modem_ppp, test_ip_frame_send_multiple)
441 {
442 struct net_pkt *pkts[TEST_MODEM_PPP_IP_FRAME_SEND_MULT_N];
443 int ret;
444
445 /* Allocate net pkts */
446 for (uint8_t i = 0; i < TEST_MODEM_PPP_IP_FRAME_SEND_MULT_N; i++) {
447 pkts[i] = net_pkt_alloc_with_buffer(&test_iface, 256, AF_UNSPEC, 0, K_NO_WAIT);
448 zassert_true(pkts[i] != NULL, "Failed to allocate network packet");
449 net_pkt_cursor_init(pkts[i]);
450 ret = net_pkt_write(pkts[i], ip_frame_unwrapped, sizeof(ip_frame_unwrapped));
451 zassert_true(ret == 0, "Failed to write data to allocated network packet");
452 net_pkt_set_family(pkts[i], AF_INET);
453 }
454
455 /* Send net pkts */
456 for (uint8_t i = 0; i < TEST_MODEM_PPP_IP_FRAME_SEND_MULT_N; i++) {
457 test_net_send(pkts[i]);
458 }
459
460 k_msleep(100);
461
462 ret = modem_backend_mock_get(&mock, buffer, TEST_MODEM_PPP_MOCK_PIPE_RX_BUF_SIZE);
463 zassert_true(ret == (sizeof(ip_frame_wrapped) * TEST_MODEM_PPP_IP_FRAME_SEND_MULT_N),
464 "Incorrect data amount received");
465 }
466
ZTEST(modem_ppp,test_ip_frame_send_large)467 ZTEST(modem_ppp, test_ip_frame_send_large)
468 {
469 struct net_pkt *pkt;
470 size_t size;
471 int ret;
472
473 pkt = net_pkt_alloc_with_buffer(&test_iface, TEST_MODEM_PPP_IP_FRAME_SEND_LARGE_N,
474 AF_UNSPEC, 0, K_NO_WAIT);
475
476 net_pkt_cursor_init(pkt);
477 net_pkt_set_family(pkt, AF_INET);
478 size = test_modem_ppp_fill_net_pkt(pkt, TEST_MODEM_PPP_IP_FRAME_SEND_LARGE_N);
479 zassert_true(size == TEST_MODEM_PPP_IP_FRAME_SEND_LARGE_N, "Failed to fill net pkt");
480 test_net_send(pkt);
481 k_msleep(TEST_MODEM_PPP_IP_FRAME_SEND_LARGE_N * 2);
482
483 /* Data + protocol */
484 ret = modem_backend_mock_get(&mock, buffer, TEST_MODEM_PPP_MOCK_PIPE_RX_BUF_SIZE);
485 size = test_modem_ppp_unwrap(unwrapped_buffer, buffer, ret);
486 zassert_true(size == (TEST_MODEM_PPP_IP_FRAME_SEND_LARGE_N + 2),
487 "Incorrect data amount received");
488
489 /* Validate protocol */
490 zassert_true(unwrapped_buffer[0] == 0x00, "Incorrect protocol");
491 zassert_true(unwrapped_buffer[1] == 0x21, "Incorrect protocol");
492
493 /* Validate data */
494 zassert_true(test_modem_ppp_validate_fill(&unwrapped_buffer[2], (size - 2)) == true,
495 "Incorrect data received");
496 }
497
ZTEST(modem_ppp,test_ip_frame_receive_large)498 ZTEST(modem_ppp, test_ip_frame_receive_large)
499 {
500 struct net_pkt *pkt;
501 size_t size;
502 size_t pkt_len;
503
504 test_modem_ppp_generate_ppp_frame(buffer, TEST_MODEM_PPP_IP_FRAME_RECEIVE_LARGE_N);
505 size = test_modem_ppp_wrap_ppp_frame(wrapped_buffer, buffer,
506 TEST_MODEM_PPP_IP_FRAME_RECEIVE_LARGE_N);
507
508 zassert_true(size > TEST_MODEM_PPP_IP_FRAME_RECEIVE_LARGE_N, "Failed to wrap data");
509 modem_backend_mock_put(&mock, wrapped_buffer, size);
510
511 k_msleep(TEST_MODEM_PPP_IP_FRAME_RECEIVE_LARGE_N * 2);
512
513 zassert_true(received_packets_len == 1, "Expected to receive one network packet");
514 pkt = received_packets[0];
515 pkt_len = net_pkt_get_len(pkt);
516
517 /* FCS is removed from packet data */
518 zassert_true(pkt_len == (TEST_MODEM_PPP_IP_FRAME_RECEIVE_LARGE_N - 2),
519 "Incorrect length of net packet received");
520 }
521
522 ZTEST_SUITE(modem_ppp, NULL, test_modem_ppp_setup, test_modem_ppp_before, NULL, NULL);
523