Blame view

RIOT/tests/unittests/tests-ecc/tests-ecc.c 2.19 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
  /*
   * Copyright (C) 2015 Lucas Jenß
   *
   * 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.
   */
  
  /**
   * @ingroup     fs
   * @brief
   * @{
   *
   * @brief       Tests for Hamming Code implementation
   * @author      Lucas Jenß <lucas@x3ro.de>
   */
  #include <string.h>
  #include "embUnit.h"
  
  #include "ecc/hamming256.h"
  
  static void test_single(void)
  {
      uint8_t data[256];
      uint8_t ecc[3];
      uint8_t result;
  
      memset(data, 0xAB, 256);
  
      hamming_compute256x(data, 256, ecc);
      result = hamming_verify256x(data, 256, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_NONE, result);
  
      data[10] |= (2 << 3);
      result = hamming_verify256x(data, 256, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_SINGLEBIT, result);
  
      data[10] |= (2 << 3);
      data[20] |= (2 << 5);
      result = hamming_verify256x(data, 256, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_MULTIPLEBITS, result);
  
      memset(data, 0xAB, 256);
      ecc[1] ^= 1; // Flip first bit, corrupting the ECC
      result = hamming_verify256x(data, 256, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_ECC, result);
  }
  
  static void test_padding(void)
  {
      uint8_t data[203];
      uint8_t ecc[3];
      uint8_t result;
  
      memset(data, 0xAB, 203);
  
      hamming_compute256x(data, 203, ecc);
      result = hamming_verify256x(data, 203, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_NONE, result);
  
      data[10] |= (2 << 3);
      result = hamming_verify256x(data, 203, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_SINGLEBIT, result);
  
      data[10] |= (2 << 3);
      data[20] |= (2 << 5);
      result = hamming_verify256x(data, 203, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_MULTIPLEBITS, result);
  
      memset(data, 0xAB, 203);
      ecc[1] ^= 1; // Flip first bit, corrupting the ECC
      result = hamming_verify256x(data, 203, ecc);
      TEST_ASSERT_EQUAL_INT(Hamming_ERROR_ECC, result);
  }
  
  TestRef test_all(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_single),
          new_TestFixture(test_padding),
      };
  
      EMB_UNIT_TESTCALLER(EccTest, 0, 0, fixtures);
      return (TestRef) & EccTest;
  }
  
  void tests_ecc(void)
  {
      TESTS_RUN(test_all());
  }