Loading...
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 | /*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include <sys/crc.h>
#include "../../../lib/os/crc8_sw.c"
#include "../../../lib/os/crc16_sw.c"
#include "../../../lib/os/crc32_sw.c"
#include "../../../lib/os/crc32c_sw.c"
#include "../../../lib/os/crc7_sw.c"
void test_crc32c(void)
{
uint8_t test1[] = { 'A' };
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
uint8_t test3[] = { 'Z', 'e', 'p', 'h', 'y', 'r' };
/* Single streams */
zassert_equal(crc32_c(0, test1, sizeof(test1), true, true),
0xE16DCDEE, NULL);
zassert_equal(crc32_c(0, test2, sizeof(test2), true, true),
0xE3069283, NULL);
zassert_equal(crc32_c(0, test3, sizeof(test3), true, true),
0xFCDEB58D, NULL);
/* Continuous streams - test1, test2 and test3 are considered part
* of one big stream whose CRC needs to be calculated. Note that the
* CRC of the first string is passed over to the second crc calculation,
* second to third and so on.
*/
zassert_equal(crc32_c(0, test1, sizeof(test1), true, false),
0x1E923211, NULL);
zassert_equal(crc32_c(0x1E923211, test2, sizeof(test2), false, false),
0xB2983B83, NULL);
zassert_equal(crc32_c(0xB2983B83, test3, sizeof(test3), false, true),
0x7D4F9D21, NULL);
}
void test_crc32_ieee(void)
{
uint8_t test1[] = { 'A' };
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
uint8_t test3[] = { 'Z', 'e', 'p', 'h', 'y', 'r' };
zassert_equal(crc32_ieee(test1, sizeof(test1)), 0xD3D99E8B, NULL);
zassert_equal(crc32_ieee(test2, sizeof(test2)), 0xCBF43926, NULL);
zassert_equal(crc32_ieee(test3, sizeof(test3)), 0x20089AA4, NULL);
}
void test_crc16(void)
{
uint8_t test0[] = { };
uint8_t test1[] = { 'A' };
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
zassert_equal(crc16(test0, sizeof(test0), 0x1021, 0xffff, true),
0x1d0f, NULL);
zassert_equal(crc16(test1, sizeof(test1), 0x1021, 0xffff, true),
0x9479, NULL);
zassert_equal(crc16(test2, sizeof(test2), 0x1021, 0xffff, true),
0xe5cc, NULL);
}
void test_crc16_ansi(void)
{
uint8_t test0[] = { };
uint8_t test1[] = { 'A' };
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
zassert(crc16_ansi(test0, sizeof(test0)) == 0x800d, "pass", "fail");
zassert(crc16_ansi(test1, sizeof(test1)) == 0x8f85, "pass", "fail");
zassert(crc16_ansi(test2, sizeof(test2)) == 0x9ecf, "pass", "fail");
}
void test_crc16_ccitt(void)
{
uint8_t test0[] = { };
uint8_t test1[] = { 'A' };
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
uint8_t test3[] = { 'Z', 'e', 'p', 'h', 'y', 'r', 0, 0 };
uint16_t crc;
zassert_equal(crc16_ccitt(0, test0, sizeof(test0)), 0x0, NULL);
zassert_equal(crc16_ccitt(0, test1, sizeof(test1)), 0x538d, NULL);
zassert_equal(crc16_ccitt(0, test2, sizeof(test2)), 0x2189, NULL);
/* Appending the CRC to a buffer and computing the CRC over
* the extended buffer leaves a residual of zero.
*/
crc = crc16_ccitt(0, test3, sizeof(test3) - sizeof(uint16_t));
test3[sizeof(test3)-2] = (uint8_t)(crc >> 0);
test3[sizeof(test3)-1] = (uint8_t)(crc >> 8);
zassert_equal(crc16_ccitt(0, test3, sizeof(test3)), 0, NULL);
}
void test_crc16_ccitt_for_ppp(void)
{
/* Example capture including FCS from
* https://www.horo.ch/techno/ppp-fcs/examples_en.html
*/
uint8_t test0[] = {
0xff, 0x03, 0xc0, 0x21, 0x01, 0x01, 0x00, 0x17,
0x02, 0x06, 0x00, 0x0a, 0x00, 0x00, 0x05, 0x06,
0x00, 0x2a, 0x2b, 0x78, 0x07, 0x02, 0x08, 0x02,
0x0d, 0x03, 0x06, 0xa5, 0xf8
};
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
zassert_equal(crc16_ccitt(0xffff, test0, sizeof(test0)),
0xf0b8, NULL);
zassert_equal(crc16_ccitt(0xffff, test2, sizeof(test2)) ^ 0xFFFF,
0x906e, NULL);
}
void test_crc16_itu_t(void)
{
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
zassert_equal(crc16_itu_t(0, test2, sizeof(test2)),
0x31c3, NULL);
}
void test_crc8_ccitt(void)
{
uint8_t test0[] = { 0 };
uint8_t test1[] = { 'A' };
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
zassert(crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, test0,
sizeof(test0)) == 0xF3, "pass", "fail");
zassert(crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, test1,
sizeof(test1)) == 0x33, "pass", "fail");
zassert(crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, test2,
sizeof(test2)) == 0xFB, "pass", "fail");
}
void test_crc7_be(void)
{
uint8_t test0[] = { 0 };
uint8_t test1[] = { 'A' };
uint8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
zassert_equal(crc7_be(0, test0, sizeof(test0)), 0, NULL);
zassert_equal(crc7_be(0, test1, sizeof(test1)), 0xDA, NULL);
zassert_equal(crc7_be(0, test2, sizeof(test2)), 0xEA, NULL);
}
void test_crc8(void)
{
uint8_t fcs, expected;
uint8_t test0[] = { 0x00 };
uint8_t test1[] = { 0xBE, 0xEF };
uint8_t test2[] = { 0x07, 0x3F }; /* GSM 07.10 example */
uint8_t test3[] = { 0x07, 0x3F, 0x89 }; /* GSM 07.10 example */
uint8_t test4[] = { 0x03, 0x02, 0x0A, 0x38, 0x17, 0x00 };
uint8_t test5[] = { 0x03, 0x3f, 0x01, 0x1c }; /* Our GSM 07.10 calc */
fcs = crc8(test0, sizeof(test0), 0x00, 0x00, false);
expected = 0x00;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test0, sizeof(test0), 0x31, 0x00, false);
expected = 0x00;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test1, sizeof(test1), 0x07, 0x00, false);
expected = 0x1a;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test1, sizeof(test1), 0x31, 0xff, false);
expected = 0x92;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test1, sizeof(test1), 0x07, 0x00, false);
expected = 0x1a;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test2, sizeof(test2), 0x31, 0x00, false);
expected = 0x45;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test2, sizeof(test2), 0x31, 0xff, false);
expected = 0xc4;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test2, sizeof(test2), 0x07, 0x00, false);
expected = 0xd6;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test2, sizeof(test2), 0x07, 0xff, false);
expected = 0x01;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test2, sizeof(test2), 0xe0, 0xff, true);
expected = 0x76;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test3, sizeof(test3), 0xe0, 0xff, true);
expected = 0xcf;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test3, sizeof(test3), 0x07, 0xff, false);
expected = 0xb1;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test4, sizeof(test4), 0x31, 0x00, false);
expected = 0x3a;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test4, sizeof(test4), 0x07, 0x00, false);
expected = 0xaf;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test4, sizeof(test4), 0x9b, 0xff, false);
expected = 0xf0;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test4, sizeof(test4), 0x1d, 0xfd, false);
expected = 0x49;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
fcs = crc8(test5, sizeof(test5), 0xe0, 0xff, true);
expected = 0xcf;
zassert_equal(fcs, expected, "0x%02x vs 0x%02x", fcs, expected);
}
void test_main(void)
{
ztest_test_suite(test_crc,
ztest_unit_test(test_crc32c),
ztest_unit_test(test_crc32_ieee),
ztest_unit_test(test_crc16),
ztest_unit_test(test_crc16_ansi),
ztest_unit_test(test_crc16_ccitt),
ztest_unit_test(test_crc16_ccitt_for_ppp),
ztest_unit_test(test_crc16_itu_t),
ztest_unit_test(test_crc8_ccitt),
ztest_unit_test(test_crc7_be),
ztest_unit_test(test_crc8));
ztest_run_test_suite(test_crc);
}
|