bc9785120b3cacc794b3ae0445e16de1c919b866
[ccan] / ccan / crypto / shachain / test / run-badhash.c
1 #include <ccan/crypto/shachain/shachain.h>
2 /* Include the C files directly. */
3 #include <ccan/crypto/shachain/shachain.c>
4 #include <ccan/tap/tap.h>
5
6 #define NUM_TESTS 1000
7
8 int main(void)
9 {
10         struct sha256 seed;
11         struct shachain chain;
12         size_t i;
13
14         plan_tests(NUM_TESTS);
15
16         memset(&seed, 0xFF, sizeof(seed));
17         shachain_init(&chain);
18
19         for (i = 0; i < NUM_TESTS; i++) {
20                 struct sha256 expect;
21                 unsigned int num_known = chain.num_valid;
22
23                 shachain_from_seed(&seed, i, &expect);
24                 /* Screw it up. */
25                 expect.u.u8[0]++;
26
27                 /* Either it should fail, or it couldn't derive any others. */
28                 if (shachain_add_hash(&chain, i, &expect)) {
29                         ok1(chain.num_valid == num_known + 1);
30                         /* Fix it up in-place */
31                         chain.known[num_known].hash.u.u8[0]--;
32                 } else {
33                         expect.u.u8[0]--;
34                         ok1(shachain_add_hash(&chain, i, &expect));
35                 }
36         }
37         return exit_status();
38 }