diff options
author | Anastasia Klimchuk <aklm@chromium.org> | 2021-03-04 09:54:56 +1100 |
---|---|---|
committer | Nico Huber <nico.h@gmx.de> | 2021-04-01 10:11:21 +0000 |
commit | 2daeea2ac6ef54914f64c19b291ddbeacfc0820d (patch) | |
tree | 043436974a2c2a15315c65fd2519cf1b3f346ad0 /tests/flashrom.c | |
parent | e704583ad53b24e1b39aa7a2e5077fa95d5f9244 (diff) | |
download | flashrom-2daeea2ac6ef54914f64c19b291ddbeacfc0820d.tar.gz flashrom-2daeea2ac6ef54914f64c19b291ddbeacfc0820d.tar.bz2 flashrom-2daeea2ac6ef54914f64c19b291ddbeacfc0820d.zip |
Enable dynamic memory allocation checks for cmocka unit tests
This commit enables the feature and makes changes to existing
files and tests. I am writing more new tests with this.
Commit includes tests/flashrom.c because after enabling memory
checks the test started to fail (it used to leak memory indeed).
If you are wondering how to verify it works (because at the moment
all tests [still] pass so it’s not obvious that anything has
changed), then for example:
1) Remove free’s in flashbuses_to_text_test_success test, and it
will fail with message similar to this (line numbers from your local
source)
[ ERROR ] --- Blocks allocated...
../flashrom.c:1239: note: block 0x55f42304b640 allocated here
../flashrom.c:1239: note: block 0x55f42304b5c0 allocated here
../flashrom.c:1239: note: block 0x55f42304b3d0 allocated here
../flashrom.c:1239: note: block 0x55f42304b700 allocated here
../flashrom.c:1239: note: block 0x55f42304b780 allocated here
../flashrom.c:1239: note: block 0x55f42304bb00 allocated here
../flashrom.c:1239: note: block 0x55f42304b810 allocated here
ERROR: flashbuses_to_text_test_success leaked 7 block(s)
2) Add char *temp = malloc just before return from strcat_realloc
[ ERROR ] --- Blocks allocated...
../helpers.c:88: note: block 0x55a51307b6c0 allocated here
../helpers.c:88: note: block 0x55a51307b9e0 allocated here
ERROR: strcat_realloc_test_success leaked 2 block(s)
BUG=b:181803212
TEST=builds and ninja test
nm builddir/tests/flashrom_unit_tests.p/.._flashrom.c.o
nm builddir/tests/flashrom_unit_tests.p/flashrom.c.o
nm builddir/flashrom.p/flashrom.c.o
Change-Id: I0c6b6b8dc17aaee28640e3fca3d1fc9f7feabf5f
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/51243
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Diffstat (limited to 'tests/flashrom.c')
-rw-r--r-- | tests/flashrom.c | 39 |
1 files changed, 29 insertions, 10 deletions
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"); } |