Blame view

RIOT/tests/unittests/tests-gnrc_mac_internal/tests-gnrc_mac_internal.c 10.8 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2016, 2016 Shuguo Zhuo <shuguo.zhuo@inria.fr>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   *
   * @file
   */
  #include <string.h>
  
  #include "embUnit.h"
  
  #include "net/gnrc/pkt.h"
  #include "net/gnrc/mac/internal.h"
  
  #include "unittests-constants.h"
  #include "tests-gnrc_mac_internal.h"
  
  static void set_up(void)
  {
      gnrc_pktbuf_init();
  }
  
  #if GNRC_MAC_TX_QUEUE_SIZE != 0
  /**
   * @brief This function test the `gnrc_mac_queue_tx_packet()`, to see whether it can
   *        correctly queue the packet to the corresponded priority packet queue.
   *
   *        In case when the `gnrc_mac_tx_neighbor_t` structure is in used (indicated by
   *        by `GNRC_MAC_NEIGHBOR_COUNT != 0`), `test_gnrc_mac_queue_tx_packet()` successively
   *        queues 4 packets, which are pkt1, pkt2, pkt3 and pkt_bcast, into a defined `tx`
   *        (type of `gnrc_mac_tx_t`). Pkt1, pkt2 have the same destination address of "0x76b6",
   *        , pkt3 is heading for "0x447e", while pkt_bcast is for broadcasting.
   *        Expected results: pkt1 and pkt2 should be queued to `tx::neighbors[1]::queue`,
   *        pkt3 should be queued to `tx::neighbors[2]::queue`, while pkt_bcast should be
   *        queued to `tx::neighbors[0]::queue`.
   *
   *        In case when the `gnrc_mac_tx_neighbor_t` structure is not in used (indicated by
   *        by `GNRC_MAC_NEIGHBOR_COUNT == 0`), `test_gnrc_mac_queue_tx_packet()` successively
   *        queues 4 packets, which are pkt1, pkt2, pkt3 and pkt_bcast, into a defined `tx`
   *        (type of `gnrc_mac_tx_t`). Pkt1, pkt2 have the same destination address of "0x76b6",
   *        , pkt3 is heading for "0x447e", while pkt_bcast is for broadcasting.
   *        Expected results: all packets should be queued to `tx::queue`, and ranking in
   *        `tx::queue` according to their priorities.
   *
   */
  static void test_gnrc_mac_queue_tx_packet(void)
  {
      gnrc_mac_tx_t tx = GNRC_MAC_TX_INIT;
      gnrc_pktsnip_t *hdr;
      gnrc_netif_hdr_t* netif_hdr;
      uint8_t dst_addr[2];
  
      dst_addr[0] = 0x76;
      dst_addr[1] = 0xb6;
  
      hdr = gnrc_netif_hdr_build(NULL, 0, NULL, 0);
      gnrc_pktsnip_t *pkt_bcast = gnrc_pktbuf_add(NULL, TEST_STRING12, sizeof(TEST_STRING12),
                                                  GNRC_NETTYPE_UNDEF);
      LL_APPEND(hdr, pkt_bcast);
      pkt_bcast = hdr;
  
      netif_hdr = hdr->data;
      netif_hdr->flags |= GNRC_NETIF_HDR_FLAGS_BROADCAST;
  
      hdr = gnrc_netif_hdr_build(NULL, 0, dst_addr, 2);
      gnrc_pktsnip_t *pkt1 = gnrc_pktbuf_add(NULL, TEST_STRING4, sizeof(TEST_STRING4),
                                             GNRC_NETTYPE_UNDEF);
      LL_APPEND(hdr, pkt1);
      pkt1 = hdr;
  
      hdr = gnrc_netif_hdr_build(NULL, 0, dst_addr, 2);
      gnrc_pktsnip_t *pkt2 = gnrc_pktbuf_add(NULL, TEST_STRING8, sizeof(TEST_STRING8),
                                             GNRC_NETTYPE_UNDEF);
      LL_APPEND(hdr, pkt2);
      pkt2 = hdr;
  
      dst_addr[0] = 0x44;
      dst_addr[1] = 0x7e;
  
      hdr = gnrc_netif_hdr_build(NULL, 0, dst_addr, 2);
      gnrc_pktsnip_t *pkt3 = gnrc_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16),
                                             GNRC_NETTYPE_UNDEF);
      LL_APPEND(hdr, pkt3);
      pkt3 = hdr;
  
  #if GNRC_MAC_NEIGHBOR_COUNT != 0
  
      gnrc_pktsnip_t *pkt_head;
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,1,pkt1));
      pkt_head = gnrc_priority_pktqueue_head(&tx.neighbors[1].queue);
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&tx.neighbors[1].queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->next->data);
  
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,0,pkt2));
      pkt_head = gnrc_priority_pktqueue_head(&tx.neighbors[1].queue);
      TEST_ASSERT(pkt_head == pkt2);
      TEST_ASSERT(2 == gnrc_priority_pktqueue_length(&tx.neighbors[1].queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, pkt_head->next->data);
  
      pkt_head = gnrc_priority_pktqueue_pop(&tx.neighbors[1].queue);
      TEST_ASSERT(pkt_head == pkt2);
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&tx.neighbors[1].queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, pkt_head->next->data);
  
      pkt_head = gnrc_priority_pktqueue_head(&tx.neighbors[1].queue);
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->next->data);
  
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,0,pkt3));
      pkt_head = gnrc_priority_pktqueue_head(&tx.neighbors[2].queue);
      TEST_ASSERT(pkt_head == pkt3);
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&tx.neighbors[2].queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING16, pkt_head->next->data);
  
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,0,pkt_bcast));
      pkt_head = gnrc_priority_pktqueue_head(&tx.neighbors[0].queue);
      TEST_ASSERT(pkt_head == pkt_bcast);
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&tx.neighbors[0].queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING12, pkt_head->next->data);
  
  #else
  
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,1,pkt1));
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&tx.queue));
      gnrc_pktsnip_t *pkt_head;
      pkt_head = gnrc_priority_pktqueue_head(&tx.queue);
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->next->data);
  
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,1,pkt2));
      TEST_ASSERT(2 == gnrc_priority_pktqueue_length(&tx.queue));
      pkt_head = gnrc_priority_pktqueue_head(&tx.queue);
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->next->data);
  
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,0,pkt3));
      TEST_ASSERT(3 == gnrc_priority_pktqueue_length(&tx.queue));
      pkt_head = gnrc_priority_pktqueue_head(&tx.queue);
      TEST_ASSERT(pkt_head == pkt3);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING16, pkt_head->next->data);
  
      TEST_ASSERT(gnrc_mac_queue_tx_packet(&tx,0,pkt_bcast));
      TEST_ASSERT(4 == gnrc_priority_pktqueue_length(&tx.queue));
      pkt_head = gnrc_priority_pktqueue_head(&tx.queue);
      TEST_ASSERT(pkt_head == pkt3);
  
      pkt_head = gnrc_priority_pktqueue_pop(&tx.queue);
      TEST_ASSERT(pkt_head == pkt3);
      TEST_ASSERT(3 == gnrc_priority_pktqueue_length(&tx.queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING16, pkt_head->next->data);
  
      pkt_head = gnrc_priority_pktqueue_pop(&tx.queue);
      TEST_ASSERT(pkt_head == pkt_bcast);
      TEST_ASSERT(2 == gnrc_priority_pktqueue_length(&tx.queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING12, pkt_head->next->data);
  
      pkt_head = gnrc_priority_pktqueue_pop(&tx.queue);
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&tx.queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->next->data);
  
      pkt_head = gnrc_priority_pktqueue_pop(&tx.queue);
      TEST_ASSERT(pkt_head == pkt2);
      TEST_ASSERT(0 == gnrc_priority_pktqueue_length(&tx.queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, pkt_head->next->data);
  
  #endif /* GNRC_MAC_NEIGHBOR_COUNT != 0 */
  }
  #endif /* GNRC_MAC_TX_QUEUE_SIZE != 0 */
  
  #if GNRC_MAC_RX_QUEUE_SIZE != 0
  /**
   * @brief This function test the `gnrc_mac_queue_rx_packet()`, to see whether it can
   *        correctly queue the packets to `rx::queue` according to their priorities.
   *
   *        `test_gnrc_mac_queue_tx_packet()` successively queues 3 packets, which are
   *        pkt1, pkt2, pkt3, into a defined `rx` (type of `gnrc_mac_rx_t`).
   *        Pkt1, pkt2 have the same priority of "1", while pkt3 has the priority of "0".
   *        Expected results: after all the packets are queued, in `rx::queue`, them should
   *        be ranked as (from high priority to low): pkt3, pkt1 and pkt2.
   *
   */
  static void test_gnrc_mac_queue_rx_packet(void)
  {
      gnrc_mac_rx_t rx = GNRC_MAC_RX_INIT;
      gnrc_pktsnip_t *pkt1 = gnrc_pktbuf_add(NULL, TEST_STRING4, sizeof(TEST_STRING4),
                                             GNRC_NETTYPE_UNDEF);
      gnrc_pktsnip_t *pkt2 = gnrc_pktbuf_add(NULL, TEST_STRING8, sizeof(TEST_STRING8),
                                             GNRC_NETTYPE_UNDEF);
      gnrc_pktsnip_t *pkt3 = gnrc_pktbuf_add(NULL, TEST_STRING16, sizeof(TEST_STRING16),
                                             GNRC_NETTYPE_UNDEF);
  
      TEST_ASSERT(gnrc_mac_queue_rx_packet(&rx,1,pkt1));
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&rx.queue));
  
      gnrc_pktsnip_t *pkt_head;
      pkt_head = gnrc_priority_pktqueue_head(&rx.queue);
  
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->data);
  
      TEST_ASSERT(gnrc_mac_queue_rx_packet(&rx,1,pkt2));
      TEST_ASSERT(2 == gnrc_priority_pktqueue_length(&rx.queue));
  
      pkt_head = gnrc_priority_pktqueue_head(&rx.queue);
  
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->data);
  
      TEST_ASSERT(gnrc_mac_queue_rx_packet(&rx,0,pkt3));
      TEST_ASSERT(3 == gnrc_priority_pktqueue_length(&rx.queue));
  
      pkt_head = gnrc_priority_pktqueue_head(&rx.queue);
  
      TEST_ASSERT(pkt_head == pkt3);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING16, pkt_head->data);
  
      pkt_head = gnrc_priority_pktqueue_pop(&rx.queue);
      TEST_ASSERT(pkt_head == pkt3);
      TEST_ASSERT(2 == gnrc_priority_pktqueue_length(&rx.queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING16, pkt_head->data);
  
      pkt_head = gnrc_priority_pktqueue_pop(&rx.queue);
      TEST_ASSERT(pkt_head == pkt1);
      TEST_ASSERT(1 == gnrc_priority_pktqueue_length(&rx.queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt_head->data);
  
      pkt_head = gnrc_priority_pktqueue_pop(&rx.queue);
      TEST_ASSERT(pkt_head == pkt2);
      TEST_ASSERT(0 == gnrc_priority_pktqueue_length(&rx.queue));
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, pkt_head->data);
  }
  #endif /* GNRC_MAC_RX_QUEUE_SIZE != 0 */
  
  #if GNRC_MAC_DISPATCH_BUFFER_SIZE != 0
  static void test_gnrc_mac_dispatch(void)
  {
      gnrc_mac_rx_t rx = GNRC_MAC_RX_INIT;
  
      for (size_t i = 0; i < GNRC_MAC_DISPATCH_BUFFER_SIZE; i++) {
          rx.dispatch_buffer[i] = gnrc_pktbuf_add(NULL, TEST_STRING4, sizeof(TEST_STRING4),
                                                  GNRC_NETTYPE_UNDEF);
      }
  
      gnrc_mac_dispatch(&rx);
  
      for (size_t i = 0; i < GNRC_MAC_DISPATCH_BUFFER_SIZE; i++) {
          TEST_ASSERT_NULL(rx.dispatch_buffer[i]);
      }
  }
  #endif /* GNRC_MAC_DISPATCH_BUFFER_SIZE != 0 */
  
  Test *tests_gnrc_mac_internal_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
  #if GNRC_MAC_TX_QUEUE_SIZE != 0
          new_TestFixture(test_gnrc_mac_queue_tx_packet),
  #endif /* GNRC_MAC_TX_QUEUE_SIZE != 0 */
  #if GNRC_MAC_RX_QUEUE_SIZE != 0
          new_TestFixture(test_gnrc_mac_queue_rx_packet),
  #endif /* GNRC_MAC_RX_QUEUE_SIZE != 0 */
  #if GNRC_MAC_DISPATCH_BUFFER_SIZE != 0
          new_TestFixture(test_gnrc_mac_dispatch),
  #endif /* GNRC_MAC_DISPATCH_BUFFER_SIZE != 0 */
      };
  
      EMB_UNIT_TESTCALLER(gnrc_mac_internal_tests, set_up, NULL, fixtures);
  
      return (Test *)&gnrc_mac_internal_tests;
  }
  
  void tests_gnrc_mac_internal(void)
  {
      TESTS_RUN(tests_gnrc_mac_internal_tests());
  }
  /** @} */