updated to 64bit crc version of ccan crc
[httpd-crcsyncproxy.git] / ccan / crcsync / test / run-crash.c
blobc2561e5d69134862aca8bd651186d18a9cd5858a
1 /* This used to crash us on 64-bit; submitted by
2 Alex Wulms <alex.wulms@scarlet.be> */
3 #include "crcsync/crcsync.h"
4 #include "crcsync/crcsync.c"
5 #include "tap/tap.h"
6 #include <stdlib.h>
7 #include <stdbool.h>
9 /* FIXME: ccanize. */
10 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
12 typedef struct {
13 int block_count;
14 uint64_t *crcs;
15 } crc_info_t;
17 static void crcblocks(crc_info_t *crc_info, char *data, int datalen, int blocksize)
19 crc_info->block_count = (datalen+blocksize-1)/blocksize;
20 crc_info->crcs = malloc(sizeof(uint64_t)*(crc_info->block_count + 1));
21 crc_of_blocks(data, datalen, blocksize, 60, crc_info->crcs);
24 #define BLOCKSIZE 5
26 int main(int argc, char *argv[])
28 /* Divided into BLOCKSIZE blocks */
29 char *data1 =
30 "abcde" "fghij" "klmno" "pqrst" "uvwxy" "z ABC"
31 "DEFGH" "IJKLM" "NOPQR" "STUVW" "XYZ 0" "12345" "6789";
32 /* Divided into blocks that match. */
33 char *data2 =
34 /* NO MATCH */
35 "acde"
36 /* MATCH */
37 "fghij" "klmno"
38 /* NO MATCH */
39 "pqr-a-very-long-test-that-differs-between-two-invokations-of-the-same-page-st"
40 /* MATCH */
41 "uvwxy" "z ABC" "DEFGH" "IJKLM" "NOPQR" "STUVW" "XYZ 0" "12345"
42 "6789"
43 /* NO MATCH */
44 "ab";
46 int expected[] = { 4,
47 -2, -3,
48 77,
49 -5, -6, -7, -8, -9, -10, -11, -12,
50 -13,
51 2 };
52 crc_info_t crc_info1;
53 struct crc_context *crcctx;
54 long result;
55 size_t ndigested;
56 size_t offset = 0;
57 size_t len2 = strlen(data2);
58 size_t tailsize = strlen(data1) % BLOCKSIZE;
59 int expected_i = 0;
61 plan_tests(ARRAY_SIZE(expected) + 2);
62 crcblocks(&crc_info1, data1, strlen(data1), BLOCKSIZE);
64 crcctx = crc_context_new(BLOCKSIZE, 60, crc_info1.crcs, crc_info1.block_count,
65 tailsize);
66 while ( offset < len2)
68 ndigested = crc_read_block(crcctx, &result, data2+offset, len2 - offset);
69 offset += ndigested;
70 if (result < 0)
71 /* Match. */
72 ok1(result == expected[expected_i++]);
73 else {
74 /* Literal. */
75 ok1(result <= expected[expected_i]);
76 expected[expected_i] -= result;
77 if (!expected[expected_i])
78 expected_i++;
82 while ((result = crc_read_flush(crcctx)) != 0) {
83 if (result < 0)
84 /* Match. */
85 ok1(result == expected[expected_i++]);
86 else {
87 /* Literal. */
88 ok1(result <= expected[expected_i]);
89 expected[expected_i] -= result;
90 if (!expected[expected_i])
91 expected_i++;
94 ok1(expected_i == ARRAY_SIZE(expected));
96 return 0;