mirror of
https://review.coreboot.org/flashrom.git
synced 2025-11-14 03:30:41 +01:00
libflashrom: Add flashrom_create_context to create and init context
flashrom_create_context does create and all initialisations needed for flash context. The real life usage of flashrom_flash_probe_v2 discovered the issue: error: variable 'flashctx' has initializer but incomplete type error: storage size of 'flashctx' isn't known flashrom_create_context fixes this, it would need to be called prior to any other api calls that require flash context. The patch also adds test which runs as external client for libflashrom. The test runs in a separate test executable, so that it does not use flashrom source code but instead uses libflashrom as a library. It is also an example how to use libflashrom api. Change-Id: I483f6cabb2b4ed27e0ee10bf621ae1bddb1fc9f3 Signed-off-by: Anastasia Klimchuk <aklm@flashrom.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/89603 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Peter Marheine <pmarheine@chromium.org>
This commit is contained in:
26
tests/client_tests.c
Normal file
26
tests/client_tests.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* This file is part of the flashrom project.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
* SPDX-FileCopyrightText: 2025 Google LLC
|
||||
*/
|
||||
|
||||
#include "client_tests.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (argc > 1)
|
||||
cmocka_set_test_filter(argv[1]);
|
||||
|
||||
cmocka_set_message_output(CM_OUTPUT_STDOUT);
|
||||
|
||||
const struct CMUnitTest external_client_tests[] = {
|
||||
cmocka_unit_test(flashrom_init_probe_erase_shutdown),
|
||||
};
|
||||
ret |= cmocka_run_group_tests_name("external_client.c tests",
|
||||
external_client_tests, NULL, NULL);
|
||||
|
||||
return ret;
|
||||
}
|
||||
16
tests/client_tests.h
Normal file
16
tests/client_tests.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* This file is part of the flashrom project.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
* SPDX-FileCopyrightText: 2025 Google LLC
|
||||
*/
|
||||
|
||||
#ifndef CLIENT_TESTS_H
|
||||
#define CLIENT_TESTS_H
|
||||
|
||||
#include <include/test.h>
|
||||
|
||||
/* external_client.c */
|
||||
void flashrom_init_probe_erase_shutdown(void **state);
|
||||
|
||||
#endif /* CLIENT_TESTS_H */
|
||||
@@ -257,8 +257,7 @@ static void dummy_test_shutdown(struct flashrom_flashctx *flashctx,
|
||||
|
||||
io_mock_register(NULL);
|
||||
|
||||
flashrom_layout_release(flashctx->default_layout);
|
||||
free(flashctx->chip);
|
||||
flashrom_flash_release(flashctx);
|
||||
}
|
||||
|
||||
void dummy_probe_and_read(void **state)
|
||||
@@ -266,21 +265,22 @@ void dummy_probe_and_read(void **state)
|
||||
(void) state; /* unused */
|
||||
|
||||
struct flashrom_programmer *flashprog = NULL;
|
||||
struct flashrom_flashctx flashctx = { 0 };
|
||||
struct flashrom_flashctx *flashctx = NULL;
|
||||
assert_int_equal(0, flashrom_create_context(&flashctx));
|
||||
|
||||
dummy_test_init_and_probe(&flashctx, flashprog);
|
||||
dummy_test_init_and_probe(flashctx, flashprog);
|
||||
|
||||
const unsigned long image_size = 16384 * KiB;
|
||||
unsigned char *buf = calloc(image_size, sizeof(unsigned char));
|
||||
assert_non_null(buf);
|
||||
|
||||
printf("Testing flashrom_image_read ...\n");
|
||||
assert_int_equal(0, flashrom_image_read(&flashctx, buf, image_size));
|
||||
assert_int_equal(0, flashrom_image_read(flashctx, buf, image_size));
|
||||
printf("... flashrom_image_read is successful.\n");
|
||||
|
||||
free(buf);
|
||||
|
||||
dummy_test_shutdown(&flashctx, flashprog);
|
||||
dummy_test_shutdown(flashctx, flashprog);
|
||||
}
|
||||
|
||||
void dummy_probe_and_write(void **state)
|
||||
@@ -288,21 +288,22 @@ void dummy_probe_and_write(void **state)
|
||||
(void) state; /* unused */
|
||||
|
||||
struct flashrom_programmer *flashprog = NULL;
|
||||
struct flashrom_flashctx flashctx = { 0 };
|
||||
struct flashrom_flashctx *flashctx = NULL;
|
||||
assert_int_equal(0, flashrom_create_context(&flashctx));
|
||||
|
||||
dummy_test_init_and_probe(&flashctx, flashprog);
|
||||
dummy_test_init_and_probe(flashctx, flashprog);
|
||||
|
||||
const unsigned long image_size = 16384 * KiB;
|
||||
uint8_t *const newcontents = malloc(image_size);
|
||||
assert_non_null(newcontents);
|
||||
|
||||
printf("Testing flashrom_image_write ...\n");
|
||||
assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, image_size, NULL));
|
||||
assert_int_equal(0, flashrom_image_write(flashctx, newcontents, image_size, NULL));
|
||||
printf("... flashrom_image_write is successful.\n");
|
||||
|
||||
free(newcontents);
|
||||
|
||||
dummy_test_shutdown(&flashctx, flashprog);
|
||||
dummy_test_shutdown(flashctx, flashprog);
|
||||
}
|
||||
|
||||
void dummy_probe_and_erase(void **state)
|
||||
@@ -310,15 +311,16 @@ void dummy_probe_and_erase(void **state)
|
||||
(void) state; /* unused */
|
||||
|
||||
struct flashrom_programmer *flashprog = NULL;
|
||||
struct flashrom_flashctx flashctx = { 0 };
|
||||
struct flashrom_flashctx *flashctx = NULL;
|
||||
assert_int_equal(0, flashrom_create_context(&flashctx));
|
||||
|
||||
dummy_test_init_and_probe(&flashctx, flashprog);
|
||||
dummy_test_init_and_probe(flashctx, flashprog);
|
||||
|
||||
printf("Testing flashrom_flash_erase ...\n");
|
||||
assert_int_equal(0, flashrom_flash_erase(&flashctx));
|
||||
assert_int_equal(0, flashrom_flash_erase(flashctx));
|
||||
printf("... flashrom_flash_erase is successful.\n");
|
||||
|
||||
dummy_test_shutdown(&flashctx, flashprog);
|
||||
dummy_test_shutdown(flashctx, flashprog);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
84
tests/external_client.c
Normal file
84
tests/external_client.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of the flashrom project.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
* SPDX-FileCopyrightText: 2025 Google LLC
|
||||
*/
|
||||
|
||||
/*
|
||||
* The purpose of this test is to use libflashrom API as an external client.
|
||||
*
|
||||
* Therefore, the test *must not* include any flashrom-internal headers.
|
||||
* In meson files, test depends on a special `libflashrom_test_dep` and runs
|
||||
* in a separate test executable `flashrom_client_tests`.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "client_tests.h"
|
||||
#include "libflashrom.h"
|
||||
|
||||
#define DUMMYFLASHER_NAME "dummy"
|
||||
|
||||
void flashrom_init_probe_erase_shutdown(void **state)
|
||||
{
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(0, flashrom_init(1));
|
||||
printf("flashrom_init with selfcheck: OK\n");
|
||||
|
||||
struct flashrom_programmer *flashprog = NULL;
|
||||
struct flashrom_flashctx *flashctx;
|
||||
const char **all_matched_names = NULL;
|
||||
|
||||
assert_int_equal(0, flashrom_create_context(&flashctx));
|
||||
printf("flashrom_create_context: OK\n");
|
||||
|
||||
const char **progs_names = flashrom_supported_programmers();
|
||||
assert_non_null(progs_names);
|
||||
|
||||
const char **ptr = progs_names;
|
||||
bool dummyflasher_support = false;
|
||||
do {
|
||||
if (!strcmp(*ptr, DUMMYFLASHER_NAME)) {
|
||||
dummyflasher_support = true;
|
||||
break;
|
||||
}
|
||||
} while (*(++ptr));
|
||||
flashrom_data_free(progs_names);
|
||||
|
||||
if (dummyflasher_support) {
|
||||
printf("dummyflasher supported: OK\n");
|
||||
|
||||
assert_int_equal(0, flashrom_programmer_init(
|
||||
&flashprog,
|
||||
DUMMYFLASHER_NAME,
|
||||
"bus=spi,emulate=W25Q128FV"));
|
||||
printf("flashrom_programmer_init for dummy with params 'bus=spi,emulate=W25Q128FV': OK\n");
|
||||
|
||||
assert_int_equal(1, flashrom_flash_probe_v2(
|
||||
flashctx,
|
||||
&all_matched_names,
|
||||
flashprog,
|
||||
NULL));
|
||||
printf("flashrom_flash_probe_v2 found 1 chip: OK\n");
|
||||
assert_int_equal(0, strcmp("W25Q128.V", all_matched_names[0]));
|
||||
printf("chip name matches 'W25Q128.V': OK\n");
|
||||
assert_int_equal(16777216, flashrom_flash_getsize(flashctx));
|
||||
printf("chip size 16M: OK\n");
|
||||
|
||||
assert_int_equal(0, flashrom_flash_erase(flashctx));
|
||||
printf("flashrom_flash_erase: OK\n");
|
||||
|
||||
assert_int_equal(0, flashrom_programmer_shutdown(flashprog));
|
||||
printf("flashrom_programmer_shutdown: OK\n");
|
||||
} else {
|
||||
printf("WARNING: dummyflasher is disabled, cannot test probe and erase\n");
|
||||
}
|
||||
|
||||
flashrom_flash_release(flashctx);
|
||||
printf("flashrom_flash_release: OK\n");
|
||||
|
||||
assert_int_equal(0, flashrom_shutdown());
|
||||
printf("flashrom_shutdown: OK\n");
|
||||
}
|
||||
@@ -133,7 +133,9 @@ void probe_v2_error_code_propagation(void **state)
|
||||
{
|
||||
(void) state; /* unused */
|
||||
|
||||
struct flashrom_flashctx flashctx = { 0 };
|
||||
struct flashrom_flashctx *flashctx = NULL;
|
||||
assert_int_equal(0, flashrom_create_context(&flashctx));
|
||||
|
||||
struct flashrom_programmer *flashprog;
|
||||
const char **all_matched_names = NULL;
|
||||
|
||||
@@ -148,7 +150,7 @@ void probe_v2_error_code_propagation(void **state)
|
||||
registered_masters[0].spi.command = &always_fail_spi_send_command;
|
||||
|
||||
assert_int_equal(0 /* no chips found */,
|
||||
flashrom_flash_probe_v2(&flashctx, &all_matched_names,
|
||||
flashrom_flash_probe_v2(flashctx, &all_matched_names,
|
||||
flashprog,
|
||||
NULL));
|
||||
|
||||
@@ -159,6 +161,7 @@ void probe_v2_error_code_propagation(void **state)
|
||||
assert_int_equal(0, flashrom_programmer_shutdown(flashprog));
|
||||
|
||||
flashrom_data_free(all_matched_names);
|
||||
flashrom_flash_release(flashctx);
|
||||
}
|
||||
#else
|
||||
SKIP_TEST(probe_v2_error_code_propagation)
|
||||
|
||||
@@ -13,11 +13,12 @@ static void probe_chip_v2(const struct programmer_entry *prog,
|
||||
const char **expected_matched_names,
|
||||
unsigned int expected_matched_count)
|
||||
{
|
||||
struct flashrom_flashctx flashctx = { 0 };
|
||||
struct flashrom_flashctx *flashctx = NULL;
|
||||
assert_int_equal(0, flashrom_create_context(&flashctx));
|
||||
const char **all_matched_names = NULL;
|
||||
|
||||
printf("Testing flashrom_flash_probe_v2 for programmer=%s, chip=%s ... \n", prog->name, chip_name);
|
||||
assert_int_equal(expected_matched_count, flashrom_flash_probe_v2(&flashctx, &all_matched_names,
|
||||
assert_int_equal(expected_matched_count, flashrom_flash_probe_v2(flashctx, &all_matched_names,
|
||||
flashprog, chip_name));
|
||||
|
||||
for (unsigned int i = 0; i < expected_matched_count; i++)
|
||||
@@ -26,14 +27,13 @@ static void probe_chip_v2(const struct programmer_entry *prog,
|
||||
assert_null(all_matched_names[expected_matched_count]);
|
||||
|
||||
if (chip_name && expected_matched_count > 0)
|
||||
assert_int_equal(0, strcmp(chip_name, flashctx.chip->name));
|
||||
assert_int_equal(0, strcmp(chip_name, flashctx->chip->name));
|
||||
|
||||
printf("... flashrom_flash_probe_v2 for programmer=%s successful\n", prog->name);
|
||||
|
||||
/* cleanup */
|
||||
flashrom_data_free(all_matched_names);
|
||||
flashrom_layout_release(flashctx.default_layout);
|
||||
free(flashctx.chip);
|
||||
flashrom_flash_release(flashctx);
|
||||
}
|
||||
|
||||
static void run_lifecycle(void **state, const struct io_mock *io, const struct programmer_entry *prog,
|
||||
|
||||
@@ -134,3 +134,18 @@ test('cmocka test flashrom', flashrom_tests)
|
||||
if get_option('llvm_cov').enabled()
|
||||
run_target('llvm-cov-tests', command : ['../scripts/llvm-cov', flashrom_tests])
|
||||
endif
|
||||
|
||||
# Creating a second test executable which runs tests as if it is an external
|
||||
# flashrom client using libflashrom via public API.
|
||||
|
||||
client_test_srcs = files(
|
||||
'external_client.c',
|
||||
'client_tests.c',
|
||||
)
|
||||
|
||||
flashrom_client_tests = executable('flashrom_client_tests',
|
||||
client_test_srcs,
|
||||
export_dynamic : true,
|
||||
dependencies : [cmocka_dep, libflashrom_test_dep],
|
||||
)
|
||||
test('flashrom client tests', flashrom_client_tests)
|
||||
|
||||
Reference in New Issue
Block a user