1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-01 14:11:15 +02:00

libflashrom: Update the API for Logger Callback

The initial implementation does not account for user_data, requiring
the calling application to use a global scope. This may lead to issues
related to object lifecycle management and other architectural
concerns.

This patch adds user_data to the user’s log callback. Moreover, it
performs message formatting, so the application only needs to pass
the formatted string to the selected output.

The change does not break the existing logging API but extends it.
A new API version is introduced with the v2 suffix.

Testing: Both unit tests and CLI tools serve as libflashrom clients.
    All unit tests run successfully.

Change-Id: Iea738bd371fa3d69b9cf222c89ee67490d30af39
Signed-off-by: Dmitry Zhadinets <dzhadinets@gmail.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/86875
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-by: Peter Marheine <pmarheine@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Dmitry Zhadinets
2025-03-16 21:50:00 -04:00
committed by Anastasia Klimchuk
parent 90cc93d9bb
commit b1794138f0
10 changed files with 153 additions and 1 deletions

66
tests/libflashrom.c Normal file
View File

@ -0,0 +1,66 @@
/*
* This file is part of the flashrom project.
*
* Copyright 2025 Dmitry Zhadinets (dzhadinets@gmail.com)
*
* 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 <stdlib.h>
#include <include/test.h>
#include "tests.h"
#include "libflashrom.h"
#include "flash.h"
static int test_log_callback(enum flashrom_log_level level, const char *format,
va_list vargs)
{
/* check that loglevel has passed corectly */
assert_int_equal(level, FLASHROM_MSG_INFO);
char message[3] = {0};
vsnprintf(message, 3, format, vargs);
assert_string_equal(message, "1\n");
return 0x666 + 1;
}
static void test_log_callback_v2(enum flashrom_log_level level,
const char *message, void *user_data)
{
/* check that loglevel has passed corectly */
assert_int_equal(level, FLASHROM_MSG_ERROR);
/* check that user dta has passed */
assert_ptr_not_equal(user_data, 0);
/* check that user_data is correct */
assert_int_equal(*(int *)(user_data), 100500);
/* check that format is working correctly */
assert_string_equal(message, "2\n");
*(int*)user_data = 0x666 + 2;
}
void flashrom_set_log_callback_test_success(void **state)
{
(void)state; /* unused */
flashrom_set_log_callback(test_log_callback);
/* check that callback is called */
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + 1);
flashrom_set_log_callback(NULL);
}
void flashrom_set_log_callback_v2_test_success(void **state)
{
(void)state; /* unused */
int user_data = 100500;
flashrom_set_log_callback_v2(test_log_callback_v2, &user_data);
print(FLASHROM_MSG_ERROR, "2%s", "\n");
/* check that callback is called */
assert_int_equal(user_data, 0x666 + 2);
flashrom_set_log_callback_v2(NULL, NULL);
}

View File

@ -17,6 +17,7 @@ test_srcs = files(
'libusb_wraps.c',
'helpers.c',
'flashrom.c',
'libflashrom.c',
'spi25.c',
'lifecycle.c',
'layout.c',

View File

@ -478,6 +478,12 @@ int main(int argc, char *argv[])
};
ret |= cmocka_run_group_tests_name("flashrom.c tests", flashrom_tests, NULL, NULL);
const struct CMUnitTest libflashrom_tests[] = {
cmocka_unit_test(flashrom_set_log_callback_test_success),
cmocka_unit_test(flashrom_set_log_callback_v2_test_success),
};
ret |= cmocka_run_group_tests_name("libflashrom.c tests", libflashrom_tests, NULL, NULL);
const struct CMUnitTest spi25_tests[] = {
cmocka_unit_test(spi_write_enable_test_success),
cmocka_unit_test(spi_write_disable_test_success),

View File

@ -31,6 +31,10 @@ void reverse_bytes_test_success(void **state);
/* flashrom.c */
void flashbuses_to_text_test_success(void **state);
/* libflashrom.c */
void flashrom_set_log_callback_test_success(void **state);
void flashrom_set_log_callback_v2_test_success(void **state);
/* spi25.c */
void spi_write_enable_test_success(void **state);
void spi_write_disable_test_success(void **state);