1 // SPDX-License-Identifier: BSD-2-Clause-Patent
3 * test-csv.c - test our csv parser
19 test_parse_csv_line_size_0(void)
22 char *columns
[] = { "a", "b", "c", "d" };
23 char *test_columns
[] = { NULL
, NULL
, NULL
, NULL
};
27 test_columns
[3] = columns
[3];
29 parse_csv_line(s0
, 0, &n_columns
, (const char **)columns
);
31 assert_equal_return(s0
[0], '\0', -1, "got %#hhx expected %#hhx\n");
32 assert_equal_return(n_columns
, 0, -1, "got %#hhx expected %#hhx\n");
33 for (i
= 0; i
< 4; i
++) {
34 assert_equal_return(test_columns
[i
], columns
[i
], -1,
35 "expected %p got %p for column %d\n",
42 test_parse_csv_line_size_1(void)
45 char *columns
[] = { "a", "b", "c", "d" };
46 char *test_columns
[] = { "", NULL
, NULL
, NULL
};
51 test_columns
[3] = columns
[3];
53 parse_csv_line(s0
, max
, &n_columns
, (const char **)columns
);
55 assert_equal_return(s0
[0], '\0', -1, "got %#hhx expected %#hhx\n");
56 assert_equal_return(n_columns
, 1, -1, "got %#hhx expected %#hhx\n");
57 for (i
= 0; i
< 4; i
++) {
58 assert_equal_return(test_columns
[i
], columns
[i
], -1,
59 "expected %p got %p for column %d\n",
66 test_parse_csv_line_comma_size_1(void)
69 char *columns
[] = { "a", "b", "c", "d" };
70 char *test_columns
[] = { "", NULL
, NULL
, "d" };
76 * For reasons unknown, when I do this the normal way with:
78 * gcc is putting it in .rodata,
79 * *** AND combining it with the "," from delims from parse_csv_line***.
85 parse_csv_line(s0
, max
, &n_columns
, (const char **)columns
);
87 assert_equal_return(s0
[0], '\0', -1, "got %#hhx expected %#hhx\n");
88 assert_equal_return(n_columns
, 1, -1, "got %#hhx expected %#hhx\n");
89 // for (i = 0; i < 4; i++) {
90 // printf("columns[%d]:%p:\"%s\"\n", i, columns[i], columns[i]);
92 for (i
= 0; i
< 1; i
++) {
93 assert_equal_return(strcmp(test_columns
[i
], columns
[i
]), 0, -1,
94 "expected %d got %d for column %d\n", i
);
96 for (i
= 1; i
< 3; i
++) {
97 assert_equal_return(test_columns
[i
], columns
[i
], -1,
98 "expected %p got %p for column %d\n",
101 for (i
= 3; i
< 4; i
++) {
102 assert_equal_return(strcmp(test_columns
[i
], columns
[i
]), 0, -1,
103 "expected %d got %d for column %d\n", i
);
110 test_parse_csv_line_comma_size_2(void)
113 char *columns
[] = { "a", "b", "c", "d" };
114 char *test_columns
[] = { "", "", NULL
, "d" };
115 size_t n_columns
= 3;
120 * For reasons unknown, when I do this the normal way with:
122 * gcc is putting it in .rodata,
123 * *** AND combining it with the "," from delims from parse_csv_line***.
129 parse_csv_line(s0
, max
, &n_columns
, (const char **)columns
);
131 assert_equal_return(s0
[0], '\0', -1, "got %#hhx expected %#hhx\n");
132 assert_equal_return(n_columns
, 2, -1, "got %#hhx expected %#hhx\n");
133 for (i
= 0; i
< 2; i
++) {
134 assert_equal_return(strcmp(test_columns
[i
], columns
[i
]), 0, -1,
135 "expected %d got %d for column %d\n", i
);
137 for (i
= 2; i
< 3; i
++) {
138 assert_equal_return(test_columns
[i
], columns
[i
], -1,
139 "expected %p got %p for column %d\n",
142 for (i
= 3; i
< 4; i
++) {
143 assert_equal_return(strcmp(test_columns
[i
], columns
[i
]), 0, -1,
144 "expected %d got %d for column %d\n", i
);
161 struct test_entry test_entries
[]= {
162 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
163 { 3, { "a", "b", "c", NULL
, NULL
, NULL
, NULL
} },
164 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
165 { 3, { "a", "b", "c", NULL
, NULL
, NULL
, NULL
} },
171 EFI_STATUS efi_status
;
173 INIT_LIST_HEAD(&entry_list
);
174 assert_equal_return(list_size(&entry_list
), 0, -1,
175 "got %d expected %d\n");
177 memcpy(csv
, (char [])UTF8_BOM
, UTF8_BOM_SIZE
);
180 end
= csv
+ sizeof(csv
) - 1;
182 efi_status
= parse_csv_data(current
, end
, 7, &entry_list
);
183 assert_equal_return(efi_status
, EFI_SUCCESS
, -1, "got %x expected %x\n");
186 list_for_each(pos
, &entry_list
) {
187 struct csv_row
*csv_row
;
188 struct test_entry
*test_entry
= &test_entries
[i
++];
191 assert_goto(i
> 0 && i
<= 4, fail
, "got %d expected 0 to 4\n", i
);
193 csv_row
= list_entry(pos
, struct csv_row
, list
);
195 assert_equal_goto(csv_row
->n_columns
, test_entry
->n_columns
,
196 fail
, "got %d expected %d\n");
197 for (j
= 0; j
< csv_row
->n_columns
; j
++) {
198 assert_equal_goto(strcmp(csv_row
->columns
[j
],
199 test_entry
->columns
[j
]), 0,
200 fail
, "got %d expected %d\n");
204 assert_equal_return(list_size(&entry_list
), 4, -1,
205 "got %d expected %d\n");
206 free_csv_list(&entry_list
);
207 assert_equal_return(list_size(&entry_list
), 0, -1,
208 "got %d expected %d\n");
211 free_csv_list(&entry_list
);
225 struct test_entry test_entries
[]= {
226 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
227 { 3, { "a", "b", "c", NULL
, NULL
, NULL
, NULL
} },
228 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
229 { 3, { "a", "b", "c", NULL
, NULL
, NULL
, NULL
} },
235 EFI_STATUS efi_status
;
237 INIT_LIST_HEAD(&entry_list
);
238 assert_equal_return(list_size(&entry_list
), 0, -1,
239 "got %d expected %d\n");
242 end
= csv
+ sizeof(csv
) - 1;
244 efi_status
= parse_csv_data(current
, end
, 7, &entry_list
);
245 assert_equal_return(efi_status
, EFI_SUCCESS
, -1, "got %x expected %x\n");
248 list_for_each(pos
, &entry_list
) {
249 struct csv_row
*csv_row
;
250 struct test_entry
*test_entry
= &test_entries
[i
++];
253 assert_goto(i
> 0 && i
<= 4, fail
, "got %d expected 0 to 4\n", i
);
255 csv_row
= list_entry(pos
, struct csv_row
, list
);
257 assert_equal_goto(csv_row
->n_columns
, test_entry
->n_columns
,
258 fail
, "got %d expected %d\n");
259 for (j
= 0; j
< csv_row
->n_columns
; j
++) {
260 assert_equal_goto(strcmp(csv_row
->columns
[j
],
261 test_entry
->columns
[j
]), 0,
262 fail
, "got %d expected %d\n");
266 assert_equal_return(list_size(&entry_list
), 4, -1,
267 "got %d expected %d\n");
268 free_csv_list(&entry_list
);
269 assert_equal_return(list_size(&entry_list
), 0, -1,
270 "got %d expected %d\n");
273 free_csv_list(&entry_list
);
283 ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,c\n"
288 struct test_entry test_entries
[]= {
289 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
290 { 7, { "", "", "", "", "", "", "" } },
291 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
292 { 3, { "a", "b", "c", NULL
, NULL
, NULL
, NULL
} },
298 EFI_STATUS efi_status
;
300 INIT_LIST_HEAD(&entry_list
);
301 assert_equal_return(list_size(&entry_list
), 0, -1,
302 "got %d expected %d\n");
304 memcpy(csv
, (char [])UTF8_BOM
, UTF8_BOM_SIZE
);
307 end
= csv
+ sizeof(csv
) - 1;
309 efi_status
= parse_csv_data(current
, end
, 7, &entry_list
);
310 assert_equal_return(efi_status
, EFI_SUCCESS
, -1, "got %x expected %x\n");
313 list_for_each(pos
, &entry_list
) {
314 struct csv_row
*csv_row
;
315 struct test_entry
*test_entry
= &test_entries
[i
++];
318 assert_goto(i
> 0 && i
<= 7, fail
, "got %d expected 0 to 7\n", i
);
319 csv_row
= list_entry(pos
, struct csv_row
, list
);
321 assert_equal_goto(csv_row
->n_columns
, test_entry
->n_columns
,
322 fail
, "got %d expected %d\n");
323 for (j
= 0; j
< csv_row
->n_columns
; j
++) {
324 assert_equal_goto(strcmp(csv_row
->columns
[j
],
325 test_entry
->columns
[j
]), 0,
326 fail
, "got %d expected %d\n");
330 free_csv_list(&entry_list
);
331 assert_equal_return(list_size(&entry_list
), 0, -1,
332 "got %d expected %d\n");
336 free_csv_list(&entry_list
);
350 struct test_entry test_entries
[]= {
351 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
352 { 3, { "a", "b", "c", NULL
, NULL
, NULL
, NULL
} },
353 { 7, { "a", "b", "c", "d", "e", "f", "g" } },
354 { 3, { "a", "b", "c", NULL
, NULL
, NULL
, NULL
} },
360 EFI_STATUS efi_status
;
362 INIT_LIST_HEAD(&entry_list
);
363 assert_equal_return(list_size(&entry_list
), 0, -1,
364 "got %d expected %d\n");
367 end
= csv
+ sizeof(csv
) - 1;
369 efi_status
= parse_csv_data(current
, end
, 7, &entry_list
);
370 assert_equal_return(efi_status
, EFI_SUCCESS
, -1, "got %x expected %x\n");
373 list_for_each(pos
, &entry_list
) {
374 struct csv_row
*csv_row
;
375 struct test_entry
*test_entry
= &test_entries
[i
++];
378 assert_goto(i
> 0 && i
<= 4, fail
, "got %d expected 0 to 4\n", i
);
380 csv_row
= list_entry(pos
, struct csv_row
, list
);
382 assert_equal_goto(csv_row
->n_columns
, test_entry
->n_columns
,
383 fail
, "got %d expected %d\n");
384 for (j
= 0; j
< csv_row
->n_columns
; j
++) {
385 assert_equal_goto(strcmp(csv_row
->columns
[j
],
386 test_entry
->columns
[j
]), 0,
387 fail
, "got %d expected %d\n");
391 assert_equal_return(list_size(&entry_list
), 4, -1,
392 "got %d expected %d\n");
393 free_csv_list(&entry_list
);
394 assert_equal_return(list_size(&entry_list
), 0, -1,
395 "got %d expected %d\n");
398 free_csv_list(&entry_list
);
403 test_simple_sbat_csv(void)
406 "test1,1,SBAT test1,acme1,1,testURL1\n"
407 "test2,2,SBAT test2,acme2,2,testURL2\n";
408 struct test_entry test_entries
[]= {
409 { 6, { "test1", "1", "SBAT test1", "acme1", "1", "testURL1" } },
410 { 6, { "test2", "2", "SBAT test2", "acme2", "2", "testURL2" } },
416 EFI_STATUS efi_status
;
418 INIT_LIST_HEAD(&entry_list
);
419 assert_equal_return(list_size(&entry_list
), 0, -1,
420 "got %d expected %d\n");
423 end
= csv
+ sizeof(csv
) - 1;
425 efi_status
= parse_csv_data(current
, end
, 6, &entry_list
);
426 assert_equal_return(efi_status
, EFI_SUCCESS
, -1,
427 "got %d expected %d\n");
430 list_for_each(pos
, &entry_list
) {
431 struct csv_row
*csv_row
;
432 struct test_entry
*test_entry
= &test_entries
[i
++];
435 csv_row
= list_entry(pos
, struct csv_row
, list
);
437 assert_equal_goto(csv_row
->n_columns
, test_entry
->n_columns
,
438 fail
, "got %d expected %d");
440 for (j
= 0; j
< csv_row
->n_columns
; j
++) {
441 assert_equal_goto(strcmp(csv_row
->columns
[j
],
442 test_entry
->columns
[j
]), 0,
443 fail
, "got %d expected %d\n");
447 assert_equal_return(list_size(&entry_list
), 2, -1,
448 "got %d expected %d\n");
449 free_csv_list(&entry_list
);
450 assert_equal_return(list_size(&entry_list
), 0, -1,
451 "got %d expected %d\n");
455 free_csv_list(&entry_list
);
461 test_csv_simple_fuzz(char *random_bin
, size_t random_bin_len
,
468 EFI_STATUS efi_status
;
470 INIT_LIST_HEAD(&entry_list
);
471 assert_equal_return(list_size(&entry_list
), 0, -1,
472 "got %d expected %d\n");
474 current
= &random_bin
[0];
475 current
= current
+ 1 - 1;
476 end
= current
+ random_bin_len
- 1;
479 efi_status
= parse_csv_data(current
, end
, 7, &entry_list
);
480 assert_equal_return(efi_status
, EFI_SUCCESS
, -1, "expected %#x got %#x\n");
481 printf("parsed %zd entries\n", list_size(&entry_list
));
483 assert_goto(list_size(&entry_list
) > 0, fail
,
484 "expected >0 entries\n");
487 list_for_each(pos
, &entry_list
) {
488 struct csv_row
*csv_row
;
490 csv_row
= list_entry(pos
, struct csv_row
, list
);
491 dprint("row[%zd]: %zd columns\n", i
, csv_row
->n_columns
);
495 free_csv_list(&entry_list
);
496 assert_equal_return(list_size(&entry_list
), 0, -1,
497 "got %d expected %d\n");
501 free_csv_list(&entry_list
);
505 #include "test-random.h"
513 setbuf(stdout
, NULL
);
514 test(test_parse_csv_line_size_0
);
515 test(test_parse_csv_line_size_1
);
516 test(test_parse_csv_line_comma_size_1
);
517 test(test_parse_csv_line_comma_size_2
);
522 test(test_simple_sbat_csv
);
523 test(test_csv_simple_fuzz
, random_bin
, random_bin_len
, false);
524 for (i
= 0; i
< random_bin_len
; i
++) {
526 while (random_bin
[i
] == '\0')
529 test(test_csv_simple_fuzz
, random_bin
, random_bin_len
, true);
534 // vim:fenc=utf-8:tw=75:noet