Debian Codegraph @

tests/tests.c

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2020 Google LLC
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <include/test.h>
17 #include "io_mock.h"
18 #include "tests.h"
19 #include "wraps.h"
20 #include "io_real.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <pthread.h>
27
28 void *not_null(void)
29 {
30 return (void *)MOCK_FD;
31 }
32
33 /* Workaround for https://github.com/clibs/cmocka/issues/17 */
34 char *__wrap_strdup(const char *s)
35 {
36 size_t len = strlen(s) + 1;
37 void *new = malloc(len);
38 if (new == NULL)
39 return NULL;
40 return (char *)memcpy(new, s, len);
41 }
42
43 void __wrap_physunmap(void *virt_addr, size_t len)
44 {
45 LOG_ME;
46 }
47
48 void *__wrap_physmap(const char *descr, uintptr_t phys_addr, size_t len)
49 {
50 LOG_ME;
51 return NULL;
52 }
53
54 struct pci_dev mock_pci_dev = {
55 .device_id = NON_ZERO,
56 };
57
58 struct pci_dev *__wrap_pcidev_init(const struct programmer_cfg *cfg, void *devs, int bar)
59 {
60 LOG_ME;
61 return &mock_pci_dev;
62 }
63
64 uintptr_t __wrap_pcidev_readbar(void *dev, int bar)
65 {
66 LOG_ME;
67 return NON_ZERO;
68 }
69
70 void __wrap_sio_write(uint16_t port, uint8_t reg, uint8_t data)
71 {
72 LOG_ME;
73 }
74
75 uint8_t __wrap_sio_read(uint16_t port, uint8_t reg)
76 {
77 LOG_ME;
78 return (uint8_t)mock();
79 }
80
81 static int mock_open(const char *pathname, int flags, mode_t mode)
82 {
83 maybe_unmock_io(pathname);
84 if (get_io() && get_io()->iom_open)
85 return get_io()->iom_open(get_io()->state, pathname, flags, mode);
86
87 if (get_io() && get_io()->fallback_open_state) {
88 struct io_mock_fallback_open_state *io_state;
89 unsigned int open_state_flags;
90
91 io_state = get_io()->fallback_open_state;
92 assert_true(io_state->noc < MAX_MOCK_OPEN);
93 assert_non_null(io_state->paths[io_state->noc]);
94 assert_string_equal(pathname, io_state->paths[io_state->noc]);
95 open_state_flags = io_state->flags[io_state->noc];
96 assert_int_equal(flags & open_state_flags, open_state_flags);
97 io_state->noc++; // proceed to the next path upon next call.
98 }
99
100 return MOCK_FD;
101 }
102
103 int __wrap_open(const char *pathname, int flags, ...)
104 {
105 LOG_ME;
106 int mode = 0;
107 if (flags & O_CREAT) {
108 va_list ap;
109 va_start(ap, flags);
110 mode = va_arg(ap, int);
111 va_end(ap);
112 }
113 return mock_open(pathname, flags, (mode_t) mode);
114 }
115
116 int __wrap_open64(const char *pathname, int flags, ...)
117 {
118 LOG_ME;
119 int mode = 0;
120 if (flags & O_CREAT) {
121 va_list ap;
122 va_start(ap, flags);
123 mode = va_arg(ap, int);
124 va_end(ap);
125 }
126 return mock_open(pathname, flags, (mode_t) mode);
127 }
128
129 int __wrap___open64_2(const char *pathname, int flags, ...)
130 {
131 LOG_ME;
132 int mode = 0;
133 if (flags & O_CREAT) {
134 va_list ap;
135 va_start(ap, flags);
136 mode = va_arg(ap, int);
137 va_end(ap);
138 }
139 return mock_open(pathname, flags, (mode_t) mode);
140 }
141
142 int __wrap_fcntl(int fd, int cmd, ...)
143 {
144 LOG_ME;
145 if (get_io() && get_io()->iom_ioctl) {
146 va_list args;
147 int out;
148 va_start(args, cmd);
149 out = get_io()->iom_fcntl(get_io()->state, fd, cmd, args);
150 va_end(args);
151 return out;
152 }
153 return 0;
154 }
155
156 int __wrap_fcntl64(int fd, int cmd, ...)
157 {
158 LOG_ME;
159 if (get_io() && get_io()->iom_ioctl) {
160 va_list args;
161 int out;
162 va_start(args, cmd);
163 out = get_io()->iom_fcntl(get_io()->state, fd, cmd, args);
164 va_end(args);
165 return out;
166 }
167 return 0;
168 }
169
170 int __wrap_ioctl(int fd, unsigned long int request, ...)
171 {
172 LOG_ME;
173 if (get_io() && get_io()->iom_ioctl) {
174 va_list args;
175 int out;
176 va_start(args, request);
177 out = get_io()->iom_ioctl(get_io()->state, fd, request, args);
178 va_end(args);
179 return out;
180 }
181 return 0;
182 }
183
184 int __wrap_write(int fd, const void *buf, size_t sz)
185 {
186 LOG_ME;
187 if (get_io() && get_io()->iom_write)
188 return get_io()->iom_write(get_io()->state, fd, buf, sz);
189 return sz;
190 }
191
192 int __wrap_read(int fd, void *buf, size_t sz)
193 {
194 LOG_ME;
195 if (get_io() && get_io()->iom_read)
196 return get_io()->iom_read(get_io()->state, fd, buf, sz);
197 return sz;
198 }
199
200 FILE *__wrap_fopen(const char *pathname, const char *mode)
201 {
202 LOG_ME;
203 maybe_unmock_io(pathname);
204 if (get_io() && get_io()->iom_fopen)
205 return get_io()->iom_fopen(get_io()->state, pathname, mode);
206 return not_null();
207 }
208
209 FILE *__wrap_fopen64(const char *pathname, const char *mode)
210 {
211 LOG_ME;
212 maybe_unmock_io(pathname);
213 if (get_io() && get_io()->iom_fopen)
214 return get_io()->iom_fopen(get_io()->state, pathname, mode);
215 return not_null();
216 }
217
218 FILE *__wrap_fdopen(int fd, const char *mode)
219 {
220 LOG_ME;
221 if (get_io() && get_io()->iom_fdopen)
222 return get_io()->iom_fdopen(get_io()->state, fd, mode);
223 return not_null();
224 }
225
226 int __wrap_stat(const char *path, void *buf)
227 {
228 LOG_ME;
229 return 0;
230 }
231
232 int __wrap_stat64(const char *path, void *buf)
233 {
234 LOG_ME;
235 return 0;
236 }
237
238 int __wrap___xstat(const char *path, void *buf)
239 {
240 LOG_ME;
241 return 0;
242 }
243
244 int __wrap___xstat64(const char *path, void *buf)
245 {
246 LOG_ME;
247 return 0;
248 }
249
250 int __wrap_fstat(int fd, void *buf)
251 {
252 LOG_ME;
253 return 0;
254 }
255
256 int __wrap_fstat64(int fd, void *buf)
257 {
258 LOG_ME;
259 return 0;
260 }
261
262 int __wrap___fstat50(int fd, void *buf)
263 {
264 LOG_ME;
265 return 0;
266 }
267
268 int __wrap___fxstat(int fd, void *buf)
269 {
270 LOG_ME;
271 return 0;
272 }
273
274 int __wrap___fxstat64(int fd, void *buf)
275 {
276 LOG_ME;
277 return 0;
278 }
279
280 char *__wrap_fgets(char *buf, int len, FILE *fp)
281 {
282 LOG_ME;
283 if (get_io() && get_io()->iom_fgets)
284 return get_io()->iom_fgets(get_io()->state, buf, len, fp);
285 return NULL;
286 }
287
288 size_t __wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *fp)
289 {
290 LOG_ME;
291 if (get_io() && get_io()->iom_fread)
292 return get_io()->iom_fread(get_io()->state, ptr, size, nmemb, fp);
293 return nmemb;
294 }
295
296 size_t __wrap_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *fp)
297 {
298 LOG_ME;
299 if (get_io() && get_io()->iom_fwrite)
300 return get_io()->iom_fwrite(get_io()->state, ptr, size, nmemb, fp);
301 return nmemb;
302 }
303
304 int __wrap_fflush(FILE *fp)
305 {
306 LOG_ME;
307 return 0;
308 }
309
310 int __wrap_fileno(FILE *fp)
311 {
312 LOG_ME;
313 return MOCK_FD;
314 }
315
316 int __wrap_fsync(int fd)
317 {
318 LOG_ME;
319 return 0;
320 }
321
322 int __wrap_setvbuf(FILE *fp, char *buf, int type, size_t size)
323 {
324 LOG_ME;
325 return 0;
326 }
327
328 int __wrap_fprintf(FILE *fp, const char *fmt, ...)
329 {
330 LOG_ME;
331 if (get_io() && get_io()->iom_fprintf) {
332 va_list args;
333 int out;
334 va_start(args, fmt);
335 out = get_io()->iom_fprintf(get_io()->state, fp, fmt, args);
336 va_end(args);
337 return out;
338 }
339 return 0;
340 }
341
342 int __wrap_fclose(FILE *fp)
343 {
344 LOG_ME;
345 if (get_io() && get_io()->iom_fclose)
346 return get_io()->iom_fclose(get_io()->state, fp);
347 return 0;
348 }
349
350 int __wrap_feof(FILE *fp)
351 {
352 /* LOG_ME; */
353 return 0;
354 }
355
356 int __wrap_ferror(FILE *fp)
357 {
358 /* LOG_ME; */
359 return 0;
360 }
361 void __wrap_clearerr(FILE *fp)
362 {
363 /* LOG_ME; */
364 return;
365 }
366
367 int __wrap_rget_io_perms(void)
368 {
369 LOG_ME;
370 return 0;
371 }
372
373 void __wrap_OUTB(unsigned char value, unsigned short port)
374 {
375 /* LOG_ME; */
376 if (get_io() && get_io()->outb)
377 get_io()->outb(get_io()->state, value, port);
378 }
379
380 unsigned char __wrap_INB(unsigned short port)
381 {
382 /* LOG_ME; */
383 if (get_io() && get_io()->inb)
384 return get_io()->inb(get_io()->state, port);
385 return 0;
386 }
387
388 void __wrap_OUTW(unsigned short value, unsigned short port)
389 {
390 /* LOG_ME; */
391 if (get_io() && get_io()->outw)
392 get_io()->outw(get_io()->state, value, port);
393 }
394
395 unsigned short __wrap_INW(unsigned short port)
396 {
397 /* LOG_ME; */
398 if (get_io() && get_io()->inw)
399 return get_io()->inw(get_io()->state, port);
400 return 0;
401 }
402
403 void __wrap_OUTL(unsigned int value, unsigned short port)
404 {
405 /* LOG_ME; */
406 if (get_io() && get_io()->outl)
407 get_io()->outl(get_io()->state, value, port);
408 }
409
410 unsigned int __wrap_INL(unsigned short port)
411 {
412 /* LOG_ME; */
413 if (get_io() && get_io()->inl)
414 return get_io()->inl(get_io()->state, port);
415 return 0;
416 }
417
418 int __wrap_tcgetattr(int fd, struct termios *termios_p)
419 {
420 LOG_ME;
421 return 0;
422 }
423
424 int __wrap_tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
425 {
426 LOG_ME;
427 return 0;
428 }
429
430 static void *doing_nothing(void *vargp) {
431 return NULL;
432 }
433
434 int main(int argc, char *argv[])
435 {
436 int ret = 0;
437
438 if (argc > 1)
439 cmocka_set_test_filter(argv[1]);
440
441 cmocka_set_message_output(CM_OUTPUT_STDOUT);
442
443 /*
444 * Creating new thread which is doing nothing, to trigger __isthreaded being 1.
445 * This is a workaround for BSD family. In multi-threaded environment fileno
446 * macro is expanded into a function which is possible to mock in unit tests.
447 * Without this workaround, on a single-thread environment, fileno macro is
448 * expanded into an inline access of a private field of a file descriptor,
449 * which is impossible to mock.
450 *
451 * In other OSes this is just creating a thread which is doing nothing.
452 */
453 pthread_t thread_id;
454 pthread_create(&thread_id, NULL, doing_nothing, NULL);
455
456 const struct CMUnitTest helpers_tests[] = {
457 cmocka_unit_test(address_to_bits_test_success),
458 cmocka_unit_test(bitcount_test_success),
459 cmocka_unit_test(minmax_test_success),
460 cmocka_unit_test(strcat_realloc_test_success),
461 cmocka_unit_test(tolower_string_test_success),
462 cmocka_unit_test(reverse_byte_test_success),
463 cmocka_unit_test(reverse_bytes_test_success),
464 };
465 ret |= cmocka_run_group_tests_name("helpers.c tests", helpers_tests, NULL, NULL);
466
467 const struct CMUnitTest selfcheck[] = {
468 cmocka_unit_test(selfcheck_programmer_table),
469 cmocka_unit_test(selfcheck_flashchips_table),
470 cmocka_unit_test(selfcheck_eraseblocks),
471 cmocka_unit_test(selfcheck_board_matches_table),
472 };
473 ret |= cmocka_run_group_tests_name("selfcheck.c tests", selfcheck,
474 NULL, NULL);
475
476 const struct CMUnitTest flashrom_tests[] = {
477 cmocka_unit_test(flashbuses_to_text_test_success),
478 };
479 ret |= cmocka_run_group_tests_name("flashrom.c tests", flashrom_tests, NULL, NULL);
480
481 const struct CMUnitTest libflashrom_tests[] = {
482 cmocka_unit_test(flashrom_set_log_callback_test_success),
483 cmocka_unit_test(flashrom_set_log_callback_v2_test_success),
484 cmocka_unit_test(flashrom_set_log_level_test_success),
485 cmocka_unit_test(flashrom_supported_programmers_test_success),
486 cmocka_unit_test(probe_v2_error_code_propagation),
487 };
488 ret |= cmocka_run_group_tests_name("libflashrom.c tests", libflashrom_tests, NULL, NULL);
489
490 const struct CMUnitTest spi25_tests[] = {
491 cmocka_unit_test(spi_write_enable_test_success),
492 cmocka_unit_test(spi_write_disable_test_success),
493 cmocka_unit_test(default_spi_read_test_success),
494 cmocka_unit_test(probe_spi_rdid_test_success),
495 cmocka_unit_test(probe_spi_rdid4_test_success),
496 cmocka_unit_test(probe_spi_rems_test_success),
497 cmocka_unit_test(probe_spi_res1_test_success),
498 cmocka_unit_test(probe_spi_res2_test_success),
499 cmocka_unit_test(probe_spi_res3_test_success),
500 cmocka_unit_test(probe_spi_at25f_test_success),
501 cmocka_unit_test(probe_spi_st95_test_success), /* spi95.c */
502 };
503 ret |= cmocka_run_group_tests_name("spi25.c tests", spi25_tests, NULL, NULL);
504
505 const struct CMUnitTest lifecycle_tests[] = {
506 cmocka_unit_test(dummy_basic_lifecycle_test_success),
507 cmocka_unit_test(dummy_probe_lifecycle_test_success),
508 cmocka_unit_test(dummy_probe_v2_one_match_for_W25Q128FV),
509 cmocka_unit_test(dummy_probe_v2_six_matches_for_MX25L6436),
510 cmocka_unit_test(dummy_probe_v2_no_matches_found),
511 cmocka_unit_test(dummy_probe_variable_size_test_success),
512 cmocka_unit_test(dummy_init_fails_unhandled_param_test_success),
513 cmocka_unit_test(dummy_init_success_invalid_param_test_success),
514 cmocka_unit_test(dummy_init_success_unhandled_param_test_success),
515 cmocka_unit_test(dummy_null_prog_param_test_success),
516 cmocka_unit_test(dummy_all_buses_test_success),
517 cmocka_unit_test(dummy_freq_param_init),
518 cmocka_unit_test(nicrealtek_basic_lifecycle_test_success),
519 cmocka_unit_test(raiden_debug_basic_lifecycle_test_success),
520 cmocka_unit_test(raiden_debug_targetAP_basic_lifecycle_test_success),
521 cmocka_unit_test(raiden_debug_targetEC_basic_lifecycle_test_success),
522 cmocka_unit_test(raiden_debug_target0_basic_lifecycle_test_success),
523 cmocka_unit_test(raiden_debug_target1_basic_lifecycle_test_success),
524 cmocka_unit_test(dediprog_basic_lifecycle_test_success),
525 cmocka_unit_test(linux_mtd_probe_lifecycle_test_success),
526 cmocka_unit_test(linux_spi_probe_lifecycle_test_success),
527 cmocka_unit_test(parade_lspcon_basic_lifecycle_test_success),
528 cmocka_unit_test(parade_lspcon_no_allow_brick_test_success),
529 cmocka_unit_test(mediatek_i2c_spi_basic_lifecycle_test_success),
530 cmocka_unit_test(mediatek_i2c_no_allow_brick_test_success),
531 cmocka_unit_test(realtek_mst_basic_lifecycle_test_success),
532 cmocka_unit_test(realtek_mst_no_allow_brick_test_success),
533 cmocka_unit_test(ch341a_spi_basic_lifecycle_test_success),
534 cmocka_unit_test(ch341a_spi_probe_lifecycle_test_success),
535 cmocka_unit_test(spidriver_probe_lifecycle_test_success),
536 };
537 ret |= cmocka_run_group_tests_name("lifecycle.c tests", lifecycle_tests, NULL, NULL);
538
539 const struct CMUnitTest layout_tests[] = {
540 cmocka_unit_test(included_regions_dont_overlap_test_success),
541 cmocka_unit_test(included_regions_overlap_test_success),
542 cmocka_unit_test(region_not_included_overlap_test_success),
543 cmocka_unit_test(layout_pass_sanity_checks_test_success),
544 cmocka_unit_test(layout_region_invalid_address_test_success),
545 cmocka_unit_test(layout_region_invalid_range_test_success),
546 };
547 ret |= cmocka_run_group_tests_name("layout.c tests", layout_tests, NULL, NULL);
548
549 const struct CMUnitTest chip_tests[] = {
550 cmocka_unit_test(erase_chip_test_success),
551 cmocka_unit_test(erase_chip_with_progress),
552 cmocka_unit_test(erase_chip_with_dummyflasher_test_success),
553 cmocka_unit_test(read_chip_test_success),
554 cmocka_unit_test(read_chip_with_progress),
555 cmocka_unit_test(read_chip_with_dummyflasher_test_success),
556 cmocka_unit_test(write_chip_test_success),
557 cmocka_unit_test(write_chip_with_progress),
558 cmocka_unit_test(write_chip_with_dummyflasher_test_success),
559 cmocka_unit_test(write_chip_feature_no_erase),
560 cmocka_unit_test(write_chip_feature_no_erase_with_progress),
561 cmocka_unit_test(write_nonaligned_region_with_dummyflasher_test_success),
562 cmocka_unit_test(verify_chip_test_success),
563 cmocka_unit_test(verify_chip_with_dummyflasher_test_success),
564 };
565 ret |= cmocka_run_group_tests_name("chip.c tests", chip_tests, NULL, NULL);
566
567 const struct CMUnitTest delay_tests[] = {
568 cmocka_unit_test(udelay_test_short),
569 };
570 ret |= cmocka_run_group_tests_name("udelay.c tests", delay_tests, NULL, NULL);
571
572 size_t n_erase_tests;
573 struct CMUnitTest *erase_func_algo_tests = get_erase_func_algo_tests(&n_erase_tests);
574 ret |= _cmocka_run_group_tests("erase_func_algo.c tests", erase_func_algo_tests, n_erase_tests, NULL, NULL);
575 free(erase_func_algo_tests);
576
577 size_t n_erase_protected_region_tests;
578 struct CMUnitTest *erase_protected_region_algo_tests
579 = get_erase_protected_region_algo_tests(&n_erase_protected_region_tests);
580 ret |= _cmocka_run_group_tests("erase_func_algo.c protected region tests",
581 erase_protected_region_algo_tests,
582 n_erase_protected_region_tests,
583 NULL,
584 NULL);
585 free(erase_protected_region_algo_tests);
586
587 // Write-protect group should run last.
588 const struct CMUnitTest chip_wp_tests[] = {
589 cmocka_unit_test(invalid_wp_range_dummyflasher_test_success),
590 cmocka_unit_test(set_wp_range_dummyflasher_test_success),
591 cmocka_unit_test(switch_wp_mode_dummyflasher_test_success),
592 cmocka_unit_test(wp_init_from_status_dummyflasher_test_success),
593 cmocka_unit_test(full_chip_erase_with_wp_dummyflasher_test_success),
594 cmocka_unit_test(partial_chip_erase_with_wp_dummyflasher_test_success),
595 cmocka_unit_test(wp_get_register_values_and_masks),
596 };
597 ret |= cmocka_run_group_tests_name("chip_wp.c tests", chip_wp_tests, NULL, NULL);
598
599 return ret;
600 }