Blame view

RIOT/tests/unittests/tests-gnrc_udp/tests-gnrc_udp.c 7.05 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
  /*
   * Copyright (C) 2016 Takuo Yonezawa <Yonezawa-T2@mail.dnp.co.jp>
   *
   * 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 <stdlib.h>
  
  #include "embUnit.h"
  
  #include "net/gnrc/udp.h"
  #include "net/ipv6/hdr.h"
  
  #include "unittests-constants.h"
  #include "tests-gnrc_udp.h"
  
  static gnrc_pktsnip_t zero_snip = {
      .users = 0,
      .next = NULL,
      .data = NULL,
      .size = 0,
      .type = GNRC_NETTYPE_UNDEF,
  };
  
  static void test_gnrc_udp__csum_null(void)
  {
      gnrc_pktsnip_t *hdr = NULL;
      gnrc_pktsnip_t *pseudo_hdr = NULL;
  
      TEST_ASSERT_EQUAL_INT(-EFAULT, gnrc_udp_calc_csum(hdr, pseudo_hdr));
  }
  
  static void test_gnrc_udp__csum_not_a_udp(void)
  {
      gnrc_pktsnip_t hdr = zero_snip;
      gnrc_pktsnip_t pseudo_hdr = zero_snip;
  
      TEST_ASSERT_EQUAL_INT(-EBADMSG, gnrc_udp_calc_csum(&hdr, &pseudo_hdr));
  }
  
  static void test_gnrc_udp__csum_not_a_ipv6(void)
  {
      gnrc_pktsnip_t payload = zero_snip;
      uint8_t payload_data[] = { 0 };
      gnrc_pktsnip_t hdr = zero_snip;
      udp_hdr_t hdr_data = (udp_hdr_t) {
          .src_port = byteorder_htons(0),
          .dst_port = byteorder_htons(0),
          .length = byteorder_htons(0),
          .checksum = byteorder_htons(0),
      };
      gnrc_pktsnip_t pseudo_hdr = zero_snip;
      ipv6_hdr_t pseudo_hdr_data = (ipv6_hdr_t) {
          .v_tc_fl = byteorder_htonl(0),
          .len = byteorder_htons((uint16_t) (sizeof(hdr_data) + sizeof(payload_data))),
          .nh = GNRC_NETTYPE_UDP,
          .hl = 0,
          .src = IPV6_ADDR_UNSPECIFIED,
          .dst = IPV6_ADDR_UNSPECIFIED,
      };
  
      pseudo_hdr.type = GNRC_NETTYPE_UNDEF; /* This should result in ENOENT */
      pseudo_hdr.data = &pseudo_hdr_data;
      pseudo_hdr.size = sizeof(pseudo_hdr_data);
      pseudo_hdr.next = &hdr;
  
      hdr.type = GNRC_NETTYPE_UDP;
      hdr.data = &hdr_data;
      hdr.size = sizeof(hdr_data);
      hdr.next = &payload;
  
      payload.data = payload_data;
      payload.size = 0;
  
      TEST_ASSERT_EQUAL_INT(-ENOENT, gnrc_udp_calc_csum(&hdr, &pseudo_hdr));
  }
  
  /**
   * @brief computes UDP checksum for given UDP payload and checksum.
   *
   * @param[in] payload_data UDP payload
   * @param[in] size         The size of the payload
   * @param[in] checksum     Checksum field of the UDP packet.
   *                         Will be overridden with the computed checksum.
   *
   * @return  0 on success
   * @return  non-zero on failure
   */
  static uint16_t _compute_checksum(uint8_t *payload_data, size_t size, uint16_t *checksum) {
      gnrc_pktsnip_t payload = zero_snip;
  
      gnrc_pktsnip_t hdr = zero_snip;
      udp_hdr_t hdr_data = (udp_hdr_t) {
          .src_port = byteorder_htons(0),
          .dst_port = byteorder_htons(0),
          .length = byteorder_htons(0),
          .checksum = byteorder_htons(*checksum),
      };
  
      gnrc_pktsnip_t pseudo_hdr = zero_snip;
      ipv6_hdr_t pseudo_hdr_data = (ipv6_hdr_t) {
          .v_tc_fl = byteorder_htonl(0),
          .len = byteorder_htons((uint16_t) (sizeof(hdr_data) + size)),
          .nh = GNRC_NETTYPE_UDP,
          .hl = 0,
          .src = IPV6_ADDR_UNSPECIFIED,
          .dst = IPV6_ADDR_UNSPECIFIED,
      };
  
      pseudo_hdr.type = GNRC_NETTYPE_IPV6;
      pseudo_hdr.data = &pseudo_hdr_data;
      pseudo_hdr.size = sizeof(pseudo_hdr_data);
      pseudo_hdr.next = &hdr;
  
      hdr.type = GNRC_NETTYPE_UDP;
      hdr.data = &hdr_data;
      hdr.size = sizeof(hdr_data);
      hdr.next = &payload;
  
      payload.data = payload_data;
      payload.size = size;
  
      int status = gnrc_udp_calc_csum(&hdr, &pseudo_hdr);
  
      *checksum = byteorder_ntohs(hdr_data.checksum);
  
      return status;
  }
  
  static void test_gnrc_udp__csum_simple1(void)
  {
      uint8_t payload_data[] = {
          0x00, 0x01, 0xFF, 0xE2,
      };
  
      uint16_t checksum = 0;
  
      int status = _compute_checksum(payload_data, sizeof(payload_data), &checksum);
  
      TEST_ASSERT_EQUAL_INT(0, status);
      TEST_ASSERT_EQUAL_INT((~0x0001) & 0xFFFF, checksum);
  }
  
  static void test_gnrc_udp__csum_simple2(void)
  {
      uint8_t payload_data[] = {
          0x00, 0x02, 0xFF, 0xE2,
      };
  
      uint16_t checksum = 0;
  
      int status = _compute_checksum(payload_data, sizeof(payload_data), &checksum);
  
      TEST_ASSERT_EQUAL_INT(0, status);
      TEST_ASSERT_EQUAL_INT((~0x0002) & 0xFFFF, checksum);
  }
  
  static void test_gnrc_udp__csum_applying_twice_yields_ffff(void)
  {
      uint8_t payload_data[] = {
          0x00, 0x02, 0xFF, 0xE2,
      };
  
      uint16_t checksum = 0;
  
      int status1 = _compute_checksum(payload_data, sizeof(payload_data), &checksum);
      int status2 = _compute_checksum(payload_data, sizeof(payload_data), &checksum);
  
      TEST_ASSERT_EQUAL_INT(0, status1);
      TEST_ASSERT_EQUAL_INT(0, status2);
      TEST_ASSERT_EQUAL_INT(0xFFFF, checksum);
  }
  
  static void test_gnrc_udp__csum_ffff(void)
  {
      uint8_t payload_data[] = {
          0x00, 0x00, 0xFF, 0xE2,
      };
  
      uint16_t checksum = 0;
  
      int status = _compute_checksum(payload_data, sizeof(payload_data), &checksum);
  
      TEST_ASSERT_EQUAL_INT(0, status);
      TEST_ASSERT_EQUAL_INT(0xFFFF, checksum);
  }
  
  static void test_gnrc_udp__csum_zero(void)
  {
      uint8_t payload_data[] = {
          0xFF, 0xFF, 0xFF, 0xE2,
      };
  
      uint16_t checksum = 0;
  
      int status = _compute_checksum(payload_data, sizeof(payload_data), &checksum);
  
      TEST_ASSERT_EQUAL_INT(0, status);
      /* https://tools.ietf.org/html/rfc2460#section-8.1
       * bullet 4
       * "if that computation yields a result of zero, it must be changed
       * to hex FFFF for placement in the UDP header."
       */
      TEST_ASSERT_EQUAL_INT(0xFFFF, checksum);
  }
  
  static void test_gnrc_udp__csum_all(void)
  {
      uint8_t payload_data[] = {
          0x00, 0x00, 0xFF, 0xE2,
      };
  
      for (uint32_t i = 0; i < 0x10000; i++) {
          payload_data[0] = i >> 8;
          payload_data[1] = i & 0xFF;
  
          uint16_t checksum = 0;
  
          int status = _compute_checksum(payload_data, sizeof(payload_data), &checksum);
  
          TEST_ASSERT_EQUAL_INT(0, status);
          if (i == 0xFFFF) {
              /* https://tools.ietf.org/html/rfc2460#section-8.1
               * bullet 4
               * "if that computation yields a result of zero, it must be changed
               * to hex FFFF for placement in the UDP header."
               */
              TEST_ASSERT_EQUAL_INT(0xFFFF, checksum);
          } else {
              TEST_ASSERT_EQUAL_INT(0xFFFF - i, checksum);
          }
      }
  }
  
  Test *tests_gnrc_udp_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_gnrc_udp__csum_null),
          new_TestFixture(test_gnrc_udp__csum_not_a_udp),
          new_TestFixture(test_gnrc_udp__csum_not_a_ipv6),
          new_TestFixture(test_gnrc_udp__csum_simple1),
          new_TestFixture(test_gnrc_udp__csum_simple2),
          new_TestFixture(test_gnrc_udp__csum_applying_twice_yields_ffff),
          new_TestFixture(test_gnrc_udp__csum_ffff),
          new_TestFixture(test_gnrc_udp__csum_zero),
          new_TestFixture(test_gnrc_udp__csum_all),
      };
  
      EMB_UNIT_TESTCALLER(gnrc_udp_tests, NULL, NULL, fixtures);
  
      return (Test *)&gnrc_udp_tests;
  }
  
  void tests_gnrc_udp(void)
  {
      TESTS_RUN(tests_gnrc_udp_tests());
  }
  /** @} */