diff options
-rw-r--r-- | meson.build | 4 | ||||
-rw-r--r-- | tests/flashrom.c | 39 | ||||
-rw-r--r-- | tests/helpers.c | 2 | ||||
-rw-r--r-- | unittest_env.h | 48 |
4 files changed, 81 insertions, 12 deletions
diff --git a/meson.build b/meson.build index d5895d8e..5b339e06 100644 --- a/meson.build +++ b/meson.build @@ -472,6 +472,10 @@ flashrom_test_dep = declare_dependency( 'cli_output.c', 'flashrom.c', ], + compile_args : [ + '-includestdlib.h', + '-includeunittest_env.h', + ], dependencies : [ deps, ], diff --git a/tests/flashrom.c b/tests/flashrom.c index 50464ddb..c3508c51 100644 --- a/tests/flashrom.c +++ b/tests/flashrom.c @@ -17,35 +17,54 @@ #include "programmer.h" +#define assert_equal_and_free(text, expected) \ + do { \ + assert_string_equal(text, expected); \ + free(text); \ + } while (0) + +#define assert_not_equal_and_free(text, expected) \ + do { \ + assert_string_not_equal(text, expected); \ + free(text); \ + } while (0) + + void flashbuses_to_text_test_success(void **state) { (void) state; /* unused */ enum chipbustype bustype; + char *text; bustype = BUS_NONSPI; - assert_string_equal(flashbuses_to_text(bustype), "Non-SPI"); + text = flashbuses_to_text(bustype); + assert_equal_and_free(text, "Non-SPI"); bustype |= BUS_PARALLEL; - assert_string_not_equal(flashbuses_to_text(bustype), "Non-SPI, Parallel"); + text = flashbuses_to_text(bustype); + assert_not_equal_and_free(text, "Non-SPI, Parallel"); bustype = BUS_PARALLEL; bustype |= BUS_LPC; - assert_string_equal(flashbuses_to_text(bustype), "Parallel, LPC"); + text = flashbuses_to_text(bustype); + assert_equal_and_free(text, "Parallel, LPC"); bustype |= BUS_FWH; //BUS_NONSPI = BUS_PARALLEL | BUS_LPC | BUS_FWH, - assert_string_equal(flashbuses_to_text(bustype), "Non-SPI"); + text = flashbuses_to_text(bustype); + assert_equal_and_free(text, "Non-SPI"); bustype |= BUS_SPI; - assert_string_equal(flashbuses_to_text(bustype), "Parallel, LPC, FWH, SPI"); + text = flashbuses_to_text(bustype); + assert_equal_and_free(text, "Parallel, LPC, FWH, SPI"); bustype |= BUS_PROG; - assert_string_equal( - flashbuses_to_text(bustype), - "Parallel, LPC, FWH, SPI, Programmer-specific" - ); + text = flashbuses_to_text(bustype); + assert_equal_and_free(text, + "Parallel, LPC, FWH, SPI, Programmer-specific"); bustype = BUS_NONE; - assert_string_equal(flashbuses_to_text(bustype), "None"); + text = flashbuses_to_text(bustype); + assert_equal_and_free(text, "None"); } diff --git a/tests/helpers.c b/tests/helpers.c index a920c156..4376eee9 100644 --- a/tests/helpers.c +++ b/tests/helpers.c @@ -18,8 +18,6 @@ #include "flash.h" #include <stdint.h> -#include <stdlib.h> - void address_to_bits_test_success(void **state) { diff --git a/unittest_env.h b/unittest_env.h new file mode 100644 index 00000000..9bd7509c --- /dev/null +++ b/unittest_env.h @@ -0,0 +1,48 @@ +/* + * This file is part of the flashrom project. + * + * Copyright 2021 Google LLC + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +/* + * This header is included in all files when they are built for unit test + * environment, for all the files to be covered by dynamic memory allocation + * checks (checks for memory leaks, buffer overflows and underflows). + * + * See flashrom_test_dep in meson.build for more details. + * + * https://api.cmocka.org/group__cmocka__alloc.html + */ + +extern void* _test_malloc(const size_t size, const char* file, const int line); +extern void* _test_realloc(void *ptr, const size_t size, const char* file, const int line); +extern void* _test_calloc(const size_t number_of_elements, const size_t size, + const char* file, const int line); +extern void _test_free(void* const ptr, const char* file, const int line); + +#ifdef malloc +#undef malloc +#endif +#ifdef calloc +#undef calloc +#endif +#ifdef realloc +#undef realloc +#endif +#ifdef free +#undef free +#endif + +#define malloc(size) _test_malloc(size, __FILE__, __LINE__) +#define realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__) +#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) +#define free(ptr) _test_free(ptr, __FILE__, __LINE__) |