1/*
2 * robust_deserialization_unit.c
3 *
4 */
5
6#include <assert.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include <roaring/roaring.h>
12
13
14#include "config.h"
15#include "test.h"
16
17
18long filesize(FILE* fp) {
19 fseek(fp, 0L, SEEK_END);
20 return ftell(fp);
21}
22
23char* readfile(FILE* fp, size_t * bytes) {
24 *bytes = filesize(fp);
25 char* buf = malloc(*bytes);
26 if(buf == NULL) return NULL;
27
28 rewind(fp);
29
30 size_t cnt = fread(buf, 1, *bytes, fp);
31 if(*bytes != cnt){
32 free(buf);
33 return NULL;
34 }
35 return buf;
36}
37
38int compare(char* x, char* y, size_t size) {
39 for (size_t i = 0; i < size; ++i) {
40 if (x[i] != y[i]) {
41 return (int)(i + 1);
42 }
43 }
44 return 0;
45}
46
47int test_deserialize(const char * filename) {
48 FILE* fp = fopen(filename, "rb");
49 if(fp == NULL) {
50 printf("Could not open %s, check your configuration. \n",filename);
51 assert_false(fp == NULL);
52 }
53 size_t bytes;
54 char* input_buffer = readfile(fp, &bytes);
55
56 if(input_buffer == NULL) {
57 printf("Could not read bytes from %s, check your configuration. \n",filename);
58 assert_false(input_buffer == NULL);
59 }
60 printf("Binary content read.\n");
61
62
63 roaring_bitmap_t* bitmap =
64 roaring_bitmap_portable_deserialize_safe(input_buffer, bytes);
65
66 if(bitmap == NULL) {
67 printf("Null bitmap loaded.\n");
68 free(input_buffer);
69 return 1; // this is the expected behavior
70 }
71 printf("Non-null bitmap loaded.\n");
72
73 size_t expected_size = roaring_bitmap_portable_size_in_bytes(bitmap);
74
75 char* output_buffer = malloc(expected_size);
76 size_t actual_size =
77 roaring_bitmap_portable_serialize(bitmap, output_buffer);
78
79
80 if(actual_size != expected_size) {
81 free(input_buffer);
82 free(output_buffer);
83 assert_int_equal(actual_size, expected_size);
84 }
85
86 int compare_result = compare(input_buffer, output_buffer, actual_size);
87
88 free(output_buffer);
89 free(input_buffer);
90 fclose(fp);
91
92 roaring_bitmap_free(bitmap);
93 assert_false(compare_result);
94
95 return compare_result;
96}
97
98
99
100void test_robust_deserialize1() {
101 char filename[1024];
102
103 strcpy(filename, TEST_DATA_DIR);
104 strcat(filename, "crashproneinput1.bin");
105
106 test_deserialize(filename);
107}
108
109
110void test_robust_deserialize2() {
111 char filename[1024];
112
113 strcpy(filename, TEST_DATA_DIR);
114 strcat(filename, "crashproneinput2.bin");
115
116 test_deserialize(filename);
117}
118
119
120void test_robust_deserialize3() {
121 char filename[1024];
122
123 strcpy(filename, TEST_DATA_DIR);
124 strcat(filename, "crashproneinput3.bin");
125
126 test_deserialize(filename);
127}
128
129void test_robust_deserialize4() {
130 char filename[1024];
131
132 strcpy(filename, TEST_DATA_DIR);
133 strcat(filename, "crashproneinput4.bin");
134
135 test_deserialize(filename);
136}
137
138void test_robust_deserialize5() {
139 char filename[1024];
140
141 strcpy(filename, TEST_DATA_DIR);
142 strcat(filename, "crashproneinput5.bin");
143
144 test_deserialize(filename);
145}
146
147void test_robust_deserialize6() {
148 char filename[1024];
149
150 strcpy(filename, TEST_DATA_DIR);
151 strcat(filename, "crashproneinput6.bin");
152
153 test_deserialize(filename);
154}
155
156void test_robust_deserialize7() {
157 char filename[1024];
158
159 strcpy(filename, TEST_DATA_DIR);
160 strcat(filename, "crashproneinput7.bin");
161
162 test_deserialize(filename);
163}
164
165void test_robust_deserialize8() {
166 char filename[1024];
167
168 strcpy(filename, TEST_DATA_DIR);
169 strcat(filename, "crashproneinput8.bin");
170
171 test_deserialize(filename);
172}
173
174int main() {
175 const struct CMUnitTest tests[] = {
176 cmocka_unit_test(test_robust_deserialize1),
177 cmocka_unit_test(test_robust_deserialize2),
178 cmocka_unit_test(test_robust_deserialize3),
179 cmocka_unit_test(test_robust_deserialize4),
180 cmocka_unit_test(test_robust_deserialize5),
181 cmocka_unit_test(test_robust_deserialize6),
182 cmocka_unit_test(test_robust_deserialize7),
183 };
184
185 return cmocka_run_group_tests(tests, NULL, NULL);
186}
187