Merge remote-tracking branch 'origin/pr/47'
[ccan] / ccan / crypto / sha256 / test / run-types.c
1 #include <ccan/crypto/sha256/sha256.h>
2 /* Include the C files directly. */
3 #include <ccan/crypto/sha256/sha256.c>
4 #include <ccan/tap/tap.h>
5
6 static unsigned char arr[] = {
7         0x12,
8 #if HAVE_BIG_ENDIAN
9         /* u16 */
10         0x12, 0x34,
11         /* u32 */
12         0x12, 0x34, 0x56, 0x78,
13         /* u64 */
14         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
15 #else
16         /* u16 */
17         0x34, 0x12,
18         /* u32 */
19         0x78, 0x56, 0x34, 0x12,
20         /* u64 */
21         0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12,
22 #endif
23         /* le16 */
24         0x34, 0x12,
25         /* le32 */
26         0x78, 0x56, 0x34, 0x12,
27         /* le64 */
28         0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12,
29         /* be16 */
30         0x12, 0x34,
31         /* be32 */
32         0x12, 0x34, 0x56, 0x78,
33         /* be64 */
34         0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
35 };
36
37 int main(void)
38 {
39         struct sha256 h, expected;
40         struct sha256_ctx ctx;
41
42         /* This is how many tests you plan to run */
43         plan_tests(1);
44
45         sha256_init(&ctx);
46         sha256_u8(&ctx, 0x12);
47         sha256_u16(&ctx, 0x1234);
48         sha256_u32(&ctx, 0x12345678);
49         sha256_u64(&ctx, 0x123456789abcdef0ULL);
50         sha256_le16(&ctx, 0x1234);
51         sha256_le32(&ctx, 0x12345678);
52         sha256_le64(&ctx, 0x123456789abcdef0ULL);
53         sha256_be16(&ctx, 0x1234);
54         sha256_be32(&ctx, 0x12345678);
55         sha256_be64(&ctx, 0x123456789abcdef0ULL);
56         sha256_done(&ctx, &h);
57
58         sha256(&expected, arr, sizeof(arr));
59         ok1(memcmp(&h, &expected, sizeof(h)) == 0);
60
61         /* This exits depending on whether all tests passed */
62         return exit_status();
63 }