64b1c7f44b9f7fa32bcf651f48945ce40c379a91
[ccan] / ccan / crypto / shachain / test / run-8bit.c
1 #define shachain_index_t uint8_t
2
3 #include <ccan/crypto/shachain/shachain.h>
4 /* Include the C files directly. */
5 #include <ccan/crypto/shachain/shachain.c>
6 #include <ccan/tap/tap.h>
7
8 #include <stdio.h>
9
10 #define NUM_TESTS 255
11
12 int main(void)
13 {
14         struct sha256 seed;
15         struct shachain chain;
16         struct sha256 expect[NUM_TESTS];
17         size_t i, j;
18
19         /* This is how many tests you plan to run */
20         plan_tests(NUM_TESTS * 3 + NUM_TESTS * (NUM_TESTS + 1));
21
22         memset(&seed, 0, sizeof(seed));
23         /* Generate a whole heap. */
24         for (i = 0; i < NUM_TESTS; i++) {
25                 shachain_from_seed(&seed, i, &expect[i]);
26                 if (i == 0)
27                         ok1(memcmp(&expect[i], &seed, sizeof(expect[i])));
28                 else
29                         ok1(memcmp(&expect[i], &expect[i-1], sizeof(expect[i])));
30         }
31
32         shachain_init(&chain);
33
34         for (i = 0; i < NUM_TESTS; i++) {
35                 struct sha256 hash;
36
37                 ok1(shachain_add_hash(&chain, i, &expect[i]));
38                 for (j = 0; j <= i; j++) {
39                         ok1(shachain_get_hash(&chain, j, &hash));
40                         ok1(memcmp(&hash, &expect[j], sizeof(hash)) == 0);
41                 }
42                 ok1(!shachain_get_hash(&chain, i+1, &hash));
43                 if (chain.num_valid == 8) {
44                         printf("%zu: num_valid %u\n", i, chain.num_valid);
45                         for (j = 0; j < 8; j++)
46                                 printf("chain.known[%zu] = 0x%02x\n",
47                                        j, chain.known[j].index);
48                 }
49         }
50
51         return exit_status();
52 }