Software APIs
crc32_functest.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/lib/base/crc32.h"
7 #include "sw/device/lib/testing/test_framework/check.h"
9 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
10 
11 #define LOG_TEST_PARAMS(x) \
12  LOG_INFO("[%s] Test params: input = 0x%!y, expected_crc32 = 0x%x", \
13  __FUNCTION__, x.input_len, x.input, x.expected_crc32);
14 
15 typedef struct test_params {
16  const char *input;
17  size_t input_len;
18  uint32_t expected_crc32;
20 
21 static const char kTestString1[] = "123456789";
22 static const char kTestString2[] =
23  "The quick brown fox jumps over the lazy dog";
24 static const char kTestString3[] = "\xfe\xca\xfe\xca\x02\xb0\xad\x1b";
25 
26 static const test_params_t kTestCases[] = {{
27  kTestString1,
28  sizeof(kTestString1) - 1,
29  0xcbf43926,
30  },
31  {
32  kTestString2,
33  sizeof(kTestString2) - 1,
34  0x414fa339,
35  },
36  {
37  kTestString3,
38  sizeof(kTestString3) - 1,
39  0x9508ac14,
40  }};
41 
42 static status_t crc32_test(void) {
43  for (size_t i = 0; i < ARRAYSIZE(kTestCases); ++i) {
44  LOG_TEST_PARAMS(kTestCases[i]);
45  TRY_CHECK(crc32(kTestCases[i].input, kTestCases[i].input_len) ==
46  kTestCases[i].expected_crc32);
47  }
48  return OK_STATUS();
49 }
50 
51 static status_t crc32_add_test(void) {
52  for (size_t i = 0; i < ARRAYSIZE(kTestCases); ++i) {
53  LOG_TEST_PARAMS(kTestCases[i]);
54  uint32_t ctx;
55  crc32_init(&ctx);
56  crc32_add(&ctx, kTestCases[i].input, kTestCases[i].input_len);
57  TRY_CHECK(crc32_finish(&ctx) == kTestCases[i].expected_crc32);
58  }
59  return OK_STATUS();
60 }
61 
62 static status_t crc32_misaligned_test(void) {
63  uint32_t kExpCrc = 0x414fa339;
64  alignas(uint32_t) char input[] =
65  ">The quick brown fox jumps over the lazy dog";
66  TRY_CHECK(crc32(&input[1], sizeof(input) - 2) == kExpCrc);
67 
68  uint32_t ctx;
69  crc32_init(&ctx);
70  crc32_add(&ctx, &input[1], sizeof(input) - 2);
71  TRY_CHECK(crc32_finish(&ctx) == kExpCrc);
72  return OK_STATUS();
73 }
74 
75 static status_t crc32_add8_test(void) {
76  for (size_t i = 0; i < ARRAYSIZE(kTestCases); ++i) {
77  LOG_TEST_PARAMS(kTestCases[i]);
78  uint32_t ctx;
79  crc32_init(&ctx);
80  for (size_t j = 0; j < kTestCases[i].input_len; ++j) {
81  crc32_add8(&ctx, kTestCases[i].input[j]);
82  }
83  TRY_CHECK(crc32_finish(&ctx) == kTestCases[i].expected_crc32);
84  }
85  return OK_STATUS();
86 }
87 
88 static status_t crc32_add32_test(void) {
89  uint32_t ctx;
90  crc32_init(&ctx);
91  const uint32_t kExpCrc = 0x9508ac14;
92 
93  crc32_add32(&ctx, 0xcafecafe);
94  OT_DISCARD(crc32_finish(&ctx));
95  crc32_add32(&ctx, 0x1badb002);
96 
97  TRY_CHECK(crc32_finish(&ctx) == kExpCrc);
98  return OK_STATUS();
99 }
100 
101 OTTF_DEFINE_TEST_CONFIG();
102 
103 bool test_main(void) {
104  status_t result = OK_STATUS();
105  EXECUTE_TEST(result, crc32_test);
106  EXECUTE_TEST(result, crc32_add_test);
107  EXECUTE_TEST(result, crc32_misaligned_test);
108  EXECUTE_TEST(result, crc32_add8_test);
109  EXECUTE_TEST(result, crc32_add32_test);
110  return status_ok(result);
111 }