]> git.ozlabs.org Git - ccan/blob - ccan/crc32c/test/run-crc32c.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / crc32c / test / run-crc32c.c
1 /* Test vectors from https://tools.ietf.org/html/rfc3720#appendix-B.4 */
2
3 /* Get access to sw version explicitly */
4 #include <ccan/crc32c/crc32c.c>
5 #include <ccan/tap/tap.h>
6 #include <string.h>
7
8 #define BSWAP_32(val)                                   \
9         ((((uint32_t)(val) & 0x000000ff) << 24)         \
10          | (((uint32_t)(val) & 0x0000ff00) << 8)                \
11          | (((uint32_t)(val) & 0x00ff0000) >> 8)                \
12          | (((uint32_t)(val) & 0xff000000) >> 24))
13
14 #if HAVE_LITTLE_ENDIAN
15 #define BE32_TO_CPU(le_val) BSWAP_32((uint32_t)le_val)
16 #else
17 #define BE32_TO_CPU(le_val) ((uint32_t)(le_val))
18 #endif
19
20 int main(void)
21 {
22         unsigned char m[48];
23
24         plan_tests(5);
25
26         /* 32 bytes of zeroes:
27
28      Byte:        0  1  2  3
29
30         0:       00 00 00 00
31       ...
32        28:       00 00 00 00
33
34       CRC:       aa 36 91 8a
35         */
36
37         memset(m, 0, 32);
38         ok1(crc32c_sw(0, m, 32) == BE32_TO_CPU(0xaa36918a));
39
40         /* 32 bytes of ones:
41
42      Byte:        0  1  2  3
43
44         0:       ff ff ff ff
45       ...
46        28:       ff ff ff ff
47
48       CRC:       43 ab a8 62
49         */
50         memset(m, 0xff, 32);
51         ok1(crc32c_sw(0, m, 32) == BE32_TO_CPU(0x43aba862));
52
53         /* 32 bytes of incrementing 00..1f:
54
55      Byte:        0  1  2  3
56
57         0:       00 01 02 03
58       ...
59        28:       1c 1d 1e 1f
60
61       CRC:       4e 79 dd 46
62         */
63         for (size_t i = 0; i < 32; i++)
64                 m[i] = i;
65         ok1(crc32c_sw(0, m, 32) == BE32_TO_CPU(0x4e79dd46));
66
67         /*  32 bytes of decrementing 1f..00:
68
69      Byte:        0  1  2  3
70
71         0:       1f 1e 1d 1c
72       ...
73        28:       03 02 01 00
74
75       CRC:       5c db 3f 11
76         */
77         for (size_t i = 0; i < 32; i++)
78                 m[i] = 31 - i;
79         ok1(crc32c_sw(0, m, 32) == BE32_TO_CPU(0x5cdb3f11));
80
81         /*  An iSCSI - SCSI Read (10) Command PDU
82     Byte:        0  1  2  3
83
84        0:       01 c0 00 00
85        4:       00 00 00 00
86        8:       00 00 00 00
87       12:       00 00 00 00
88       16:       14 00 00 00
89       20:       00 00 04 00
90       24:       00 00 00 14
91       28:       00 00 00 18
92       32:       28 00 00 00
93       36:       00 00 00 00
94       40:       02 00 00 00
95       44:       00 00 00 00
96
97      CRC:       56 3a 96 d9
98         */
99         memset(m, 0, sizeof(m));
100         m[0] = 0x01;
101         m[1] = 0xc0;
102         m[16] = 0x14;
103         m[22] = 0x04;
104         m[27] = 0x14;
105         m[31] = 0x18;
106         m[32] = 0x28;
107         m[40] = 0x02;
108         ok1(crc32c_sw(0, m, sizeof(m)) == BE32_TO_CPU(0x563a96d9));
109
110         return exit_status();
111 }