Blame view

RIOT/tests/unittests/tests-gnrc_netif/tests-gnrc_netif.c 8.82 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  /*
   * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
   *
   * 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 <errno.h>
  #include <stdint.h>
  #include <stdlib.h>
  
  #include "embUnit/embUnit.h"
  #include "kernel_types.h"
  #include "net/gnrc/netif.h"
  
  #include "unittests-constants.h"
  #include "tests-gnrc_netif.h"
  
  static void set_up(void)
  {
      gnrc_netif_init();
  }
  
  static void test_gnrc_netif_add__KERNEL_PID_UNDEF(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(KERNEL_PID_UNDEF));
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(0, size);
  }
  
  static void test_gnrc_netif_add__memfull(void)
  {
      for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
          TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(TEST_UINT8 + i));
      }
  
      TEST_ASSERT_EQUAL_INT(-ENOMEM, gnrc_netif_add(TEST_UINT8 - 1));
  }
  
  static void test_gnrc_netif_add__success(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(TEST_UINT8));
  
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(1, size);
      TEST_ASSERT_EQUAL_INT(TEST_UINT8, ifs[0]);
  }
  
  static void test_gnrc_netif_add__duplicate_entry(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
  
      for (int i = 0; i < GNRC_NETIF_NUMOF + 4; i++) {
          TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(TEST_UINT8));
      }
  
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(1, size);
      TEST_ASSERT_EQUAL_INT(TEST_UINT8, ifs[0]);
  }
  
  static void test_gnrc_netif_remove__KERNEL_PID_UNDEF(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
  
      test_gnrc_netif_add__success();
  
      gnrc_netif_remove(KERNEL_PID_UNDEF);
  
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(1, size);
      TEST_ASSERT_EQUAL_INT(TEST_UINT8, ifs[0]);
  }
  
  static void test_gnrc_netif_remove__not_an_if(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
  
      test_gnrc_netif_add__success();
  
      gnrc_netif_remove(TEST_UINT8 + 1);
  
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(1, size);
      TEST_ASSERT_EQUAL_INT(TEST_UINT8, ifs[0]);
  }
  
  static void test_gnrc_netif_remove__success(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
  
      test_gnrc_netif_add__success();
  
      gnrc_netif_remove(TEST_UINT8);
  
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(0, size);
  }
  
  static void test_gnrc_netif_get__empty(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size = gnrc_netif_get(ifs);
  
      TEST_ASSERT_EQUAL_INT(0, size);
  }
  
  /* takes one out of the middle of the gnrc_netif list and checks if all interfaces
   * are gotten regardless */
  static void test_gnrc_netif_get__success_3_minus_one(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
      int count = 0;
  
      for (int i = 0; i < 3; i++) {
          TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(TEST_UINT8 + i));
      }
  
      gnrc_netif_remove(TEST_UINT8 + 1);
  
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(2, size);
  
      for (size_t i = 0; i < size; i++) {
          if ((ifs[i] == TEST_UINT8) || ifs[i] == (TEST_UINT8 + 2)) {
              count++;
          }
      }
  
      TEST_ASSERT_EQUAL_INT(size, count);
  }
  
  static void test_gnrc_netif_get__full(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      size_t size;
  
      for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
          TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(TEST_UINT8 + i));
      }
  
      size = gnrc_netif_get(ifs);
      TEST_ASSERT_EQUAL_INT(GNRC_NETIF_NUMOF, size);
  }
  
  static void test_gnrc_netif_exist(void)
  {
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(0));
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(1));
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(TEST_UINT8));
  
      for (int i = 0; i < UINT8_MAX; i++) {
          if ((i == 0) || (i == 1) || (i == TEST_UINT8)) {
              TEST_ASSERT(gnrc_netif_exist(i));
          }
          else {
              TEST_ASSERT(!gnrc_netif_exist(i));
          }
      }
  }
  
  static void test_gnrc_netif_addr_to_str__out_too_short(void)
  {
      static const uint8_t addr[] = { 0x05, 0xcd };
      char out[2];
  
      TEST_ASSERT_NULL(gnrc_netif_addr_to_str(out, sizeof(out), addr, sizeof(addr)));
  }
  
  static void test_gnrc_netif_addr_to_str__success(void)
  {
      static const uint8_t addr[] = { 0x05, 0xcd };
      char out[3 * sizeof(addr)];
  
      TEST_ASSERT_EQUAL_STRING("05:cd", gnrc_netif_addr_to_str(out, sizeof(out),
                                                               addr, sizeof(addr)));
  }
  
  static void test_gnrc_netif_addr_from_str__out_too_short(void)
  {
      static const char str[] = "05:cd";
      uint8_t out[1];
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str));
  }
  
  static void test_gnrc_netif_addr_from_str__omitted_delimitter(void)
  {
      static const char str[] = "4567:cd";
      uint8_t out[3];
  
      TEST_ASSERT_EQUAL_INT(3, gnrc_netif_addr_from_str(out, sizeof(out), str));
      TEST_ASSERT_EQUAL_INT(0x45, out[0]);
      TEST_ASSERT_EQUAL_INT(0x67, out[1]);
      TEST_ASSERT_EQUAL_INT(0xcd, out[2]);
  }
  
  static void test_gnrc_netif_addr_from_str__ill_formated2(void)
  {
      static const char str[] = TEST_STRING8;
      uint8_t out[sizeof(str)];
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str));
  }
  
  static void test_gnrc_netif_addr_from_str__dash_delimitter(void)
  {
      static const char str[] = "05-cd";
      uint8_t out[2];
  
      TEST_ASSERT_EQUAL_INT(2, gnrc_netif_addr_from_str(out, sizeof(out), str));
      TEST_ASSERT_EQUAL_INT(0x05, out[0]);
      TEST_ASSERT_EQUAL_INT(0xcd, out[1]);
  }
  
  static void test_gnrc_netif_addr_from_str__zero_omitted_back(void)
  {
      static const char str[] = "05:c";
      uint8_t out[2];
  
      TEST_ASSERT_EQUAL_INT(2, gnrc_netif_addr_from_str(out, sizeof(out), str));
      TEST_ASSERT_EQUAL_INT(0x05, out[0]);
      TEST_ASSERT_EQUAL_INT(0x0c, out[1]);
  }
  
  static void test_gnrc_netif_addr_from_str__zero_omitted_front(void)
  {
      static const char str[] = "5:cd";
      uint8_t out[2];
  
      TEST_ASSERT_EQUAL_INT(2, gnrc_netif_addr_from_str(out, sizeof(out), str));
      TEST_ASSERT_EQUAL_INT(0x05, out[0]);
      TEST_ASSERT_EQUAL_INT(0xcd, out[1]);
  }
  
  static void test_gnrc_netif_addr_from_str__ill_trailing_delimitter(void)
  {
      static const char str[] = "05:cd:";
      uint8_t out[sizeof(str)];
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str));
  }
  
  static void test_gnrc_netif_addr_from_str__ill_leading_delimitter(void)
  {
      static const char str[] = ":05:cd";
      uint8_t out[sizeof(str)];
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str));
  }
  
  static void test_gnrc_netif_addr_from_str__ill_extra_delimitter(void)
  {
      static const char str[] = "05::cd";
      uint8_t out[sizeof(str)];
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str));
  }
  
  static void test_gnrc_netif_addr_from_str__success(void)
  {
      static const char str[] = "05:cd";
      uint8_t out[2];
  
      TEST_ASSERT_EQUAL_INT(2, gnrc_netif_addr_from_str(out, sizeof(out), str));
      TEST_ASSERT_EQUAL_INT(0x05, out[0]);
      TEST_ASSERT_EQUAL_INT(0xcd, out[1]);
  }
  
  Test *tests_gnrc_netif_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_gnrc_netif_add__KERNEL_PID_UNDEF),
          new_TestFixture(test_gnrc_netif_add__memfull),
          new_TestFixture(test_gnrc_netif_add__success),
          new_TestFixture(test_gnrc_netif_add__duplicate_entry),
          new_TestFixture(test_gnrc_netif_remove__KERNEL_PID_UNDEF),
          new_TestFixture(test_gnrc_netif_remove__not_an_if),
          new_TestFixture(test_gnrc_netif_remove__success),
          new_TestFixture(test_gnrc_netif_get__empty),
          new_TestFixture(test_gnrc_netif_get__success_3_minus_one),
          new_TestFixture(test_gnrc_netif_get__full),
          new_TestFixture(test_gnrc_netif_exist),
          new_TestFixture(test_gnrc_netif_addr_to_str__out_too_short),
          new_TestFixture(test_gnrc_netif_addr_to_str__success),
          new_TestFixture(test_gnrc_netif_addr_from_str__out_too_short),
          new_TestFixture(test_gnrc_netif_addr_from_str__omitted_delimitter),
          new_TestFixture(test_gnrc_netif_addr_from_str__ill_formated2),
          new_TestFixture(test_gnrc_netif_addr_from_str__dash_delimitter),
          new_TestFixture(test_gnrc_netif_addr_from_str__zero_omitted_back),
          new_TestFixture(test_gnrc_netif_addr_from_str__zero_omitted_front),
          new_TestFixture(test_gnrc_netif_addr_from_str__ill_trailing_delimitter),
          new_TestFixture(test_gnrc_netif_addr_from_str__ill_leading_delimitter),
          new_TestFixture(test_gnrc_netif_addr_from_str__ill_extra_delimitter),
          new_TestFixture(test_gnrc_netif_addr_from_str__success),
      };
  
      EMB_UNIT_TESTCALLER(gnrc_netif_tests, set_up, NULL, fixtures);
  
      return (Test *)&gnrc_netif_tests;
  }
  
  void tests_gnrc_netif(void)
  {
      TESTS_RUN(tests_gnrc_netif_tests());
  }
  /** @} */