1#include "modules/pico_dev_loop.c"
2#include "check.h"
3static int called = 0;
4static int fail = 0;
5
6Suite *pico_suite(void);
7
8int pico_device_init(struct pico_device __attribute__((unused)) *dev, const char __attribute__((unused)) *name, const uint8_t __attribute__((unused)) *mac)
9{
10    if (fail)
11        return -1;
12
13    return 0;
14}
15
16void pico_device_destroy(struct pico_device *dev)
17{
18    dev = dev;
19}
20
21int32_t pico_stack_recv(struct pico_device __attribute__((unused)) *dev, uint8_t __attribute__((unused)) *buffer, uint32_t __attribute__((unused)) len)
22{
23    called = 1;
24    return 1;
25}
26
27START_TEST(tc_pico_loop_send)
28{
29    uint8_t buf[LOOP_MTU + 1] = {};
30    fail_if(pico_loop_send(NULL, buf, LOOP_MTU + 1) != 0);
31
32    /* First send: OK */
33    fail_if(pico_loop_send(NULL, buf, LOOP_MTU) != LOOP_MTU);
34
35    /* Second: buffer busy */
36    fail_if(pico_loop_send(NULL, buf, LOOP_MTU) != 0);
37
38}
39END_TEST
40
41START_TEST(tc_pico_loop_poll)
42{
43    uint8_t buf[LOOP_MTU + 1] = {};
44    fail_if(pico_loop_poll(NULL, 0) != 0);
45    called = 0;
46    /* First send: OK */
47    fail_if(pico_loop_send(NULL, buf, LOOP_MTU) != LOOP_MTU);
48    fail_if(pico_loop_poll(NULL, 1) != 0);
49    fail_if(called == 0);
50}
51END_TEST
52
53START_TEST(tc_pico_loop_create)
54{
55
56#ifdef PICO_FAULTY
57    printf("Testing with faulty memory in pico_loop_create (1)\n");
58    pico_set_mm_failure(1);
59    fail_if(pico_loop_create() != NULL);
60#endif
61    fail = 1;
62    fail_if(pico_loop_create() != NULL);
63    fail = 0;
64    fail_if(pico_loop_create() == NULL);
65
66}
67END_TEST
68
69
70Suite *pico_suite(void)
71{
72    Suite *s = suite_create("PicoTCP");
73
74    TCase *TCase_pico_loop_send = tcase_create("Unit test for pico_loop_send");
75    TCase *TCase_pico_loop_poll = tcase_create("Unit test for pico_loop_poll");
76    TCase *TCase_pico_loop_create = tcase_create("Unit test for pico_loop_create");
77
78
79    tcase_add_test(TCase_pico_loop_send, tc_pico_loop_send);
80    suite_add_tcase(s, TCase_pico_loop_send);
81    tcase_add_test(TCase_pico_loop_poll, tc_pico_loop_poll);
82    suite_add_tcase(s, TCase_pico_loop_poll);
83    tcase_add_test(TCase_pico_loop_create, tc_pico_loop_create);
84    suite_add_tcase(s, TCase_pico_loop_create);
85    return s;
86}
87
88int main(void)
89{
90    int fails;
91    Suite *s = pico_suite();
92    SRunner *sr = srunner_create(s);
93    srunner_run_all(sr, CK_NORMAL);
94    fails = srunner_ntests_failed(sr);
95    srunner_free(sr);
96    return fails;
97}
98