Blame view

RIOT/tests/unittests/tests-checksum/tests-checksum-fletcher16.c 2.46 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright 2016 Ludwig KnΓΌpfer <ludwig.knuepfer@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.
   */
  
  #include <stdint.h>
  
  #include "embUnit/embUnit.h"
  
  #include "checksum/fletcher16.h"
  
  #include "tests-checksum.h"
  
  /* String is longer than 20 so it checks the wrap-around property of
   * fletcher16 */
  static const unsigned char wrap_around_data[] = "u9k89rm88z58f3hGxTfgG3BnyKnyRP";
  
  static int calc_and_compare_checksum(const unsigned char *buf, size_t len,
                                       uint16_t expected)
  {
      uint16_t result = fletcher16(buf, len);
  
      return result == expected;
  }
  
  static void test_checksum_fletcher16_empty(void)
  {
      unsigned char buf[] = "";
      uint16_t expect = 0xFFFF;
  
      TEST_ASSERT(calc_and_compare_checksum(buf, sizeof(buf) - 1, expect));
  }
  
  static void test_checksum_fletcher16_0to1_undetected(void)
  {
      /* fletcher cannot distinguish between all 0 and all 1 segments */
      unsigned char buf0[16] = {
          0xA1, 0xA1, 0xA1, 0xA1,
          0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00,
          0x1A, 0x1A, 0x1A, 0x1A,
      };
      uint32_t expect = fletcher16(buf0, sizeof(buf0));
      unsigned char buf1[16] = {
          0xA1, 0xA1, 0xA1, 0xA1,
          0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF,
          0x1A, 0x1A, 0x1A, 0x1A,
      };
  
      TEST_ASSERT(calc_and_compare_checksum(buf1, sizeof(buf1), expect));
  }
  
  static void test_checksum_fletcher16_atoe(void)
  {
      /* verified with http://www.nitrxgen.net/hashgen/ */
      unsigned char buf[] = "abcde";
      uint16_t expect = 0xc8f0;
  
      TEST_ASSERT(calc_and_compare_checksum(buf, sizeof(buf) - 1, expect));
  }
  
  static void test_checksum_fletcher16_wrap_around(void)
  {
      /* verified with http://www.nitrxgen.net/hashgen/ */
      uint16_t expect = 0xf122;
  
      TEST_ASSERT(calc_and_compare_checksum(wrap_around_data,
                                            sizeof(wrap_around_data) - 1, expect));
  }
  
  Test *tests_checksum_fletcher16_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_checksum_fletcher16_empty),
          new_TestFixture(test_checksum_fletcher16_0to1_undetected),
          new_TestFixture(test_checksum_fletcher16_atoe),
          new_TestFixture(test_checksum_fletcher16_wrap_around),
      };
  
      EMB_UNIT_TESTCALLER(checksum_fletcher16_tests, NULL, NULL, fixtures);
  
      return (Test *)&checksum_fletcher16_tests;
  }