1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-27 23:22:37 +02:00
flashrom/tests/helpers.c
Anastasia Klimchuk 2daeea2ac6 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>
2021-04-01 10:11:21 +00:00

78 lines
1.9 KiB
C

/*
* This file is part of the flashrom project.
*
* Copyright 2020 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.
*/
#include <include/test.h>
#include "flash.h"
#include <stdint.h>
void address_to_bits_test_success(void **state)
{
(void) state; /* unused */
assert_int_equal(16, address_to_bits(0xAA55));
}
void bitcount_test_success(void **state)
{
(void) state; /* unused */
assert_int_equal(4, bitcount(0xAA));
}
void minmax_test_success(void **state)
{
(void) state; /* unused */
assert_int_equal(0x55, min(0xAA, 0x55));
assert_int_equal(0xAA, max(0xAA, 0x55));
}
void strcat_realloc_test_success(void **state)
{
(void) state; /* unused */
const char src0[] = "hello";
const char src1[] = " world";
char *dest = calloc(1, 1);
dest = strcat_realloc(dest, src0);
dest = strcat_realloc(dest, src1);
assert_string_equal("hello world", dest);
free(dest);
}
void tolower_string_test_success(void **state)
{
(void) state; /* unused */
char str[] = "HELLO AGAIN";
assert_string_equal("HELLO AGAIN", str);
tolower_string(str);
assert_string_equal("hello again", str);
}
void reverse_byte_test_success(void **state)
{
(void) state; /* unused */
assert_int_equal(0x5A, reverse_byte(0x5A));
assert_int_equal(0x0F, reverse_byte(0xF0));
}
void reverse_bytes_test_success(void **state)
{
(void) state; /* unused */
uint8_t src[] = { 0xAA, 0x55 };
uint8_t dst[2];
reverse_bytes(dst, src, 2);
assert_int_equal(src[0], dst[1]);
assert_int_equal(src[1], dst[0]);
}