| 1 |
/* |
| 2 |
* This file is part of the flashrom project. |
| 3 |
* |
| 4 |
* Copyright 2025 Dmitry Zhadinets <[email protected]> |
| 5 |
* Copyright 2025 Google LLC |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; version 2 of the License. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
|
| 19 |
#include <include/test.h> |
| 20 |
#include "tests.h" |
| 21 |
#include "libflashrom.h" |
| 22 |
#include "flash.h" |
| 23 |
#include "programmer.h" |
| 24 |
|
| 25 |
static int test_log_callback(enum flashrom_log_level level, const char *format, |
| 26 |
va_list vargs) |
| 27 |
{ |
| 28 |
char message[3] = {0}; |
| 29 |
vsnprintf(message, 3, format, vargs); |
| 30 |
assert_string_equal(message, "1\n"); |
| 31 |
return 0x666 + (int)level; |
| 32 |
} |
| 33 |
|
| 34 |
static void test_log_callback_v2(enum flashrom_log_level level, |
| 35 |
const char *message, void *user_data) |
| 36 |
{ |
| 37 |
/* check that user dta has passed */ |
| 38 |
assert_ptr_not_equal(user_data, 0); |
| 39 |
/* check that user_data is correct */ |
| 40 |
assert_int_equal(*(int *)(user_data), 100500); |
| 41 |
/* check that format is working correctly */ |
| 42 |
assert_string_equal(message, "2\n"); |
| 43 |
*(int*)user_data = 0x666 + (int)level; |
| 44 |
} |
| 45 |
|
| 46 |
void flashrom_set_log_callback_test_success(void **state) |
| 47 |
{ |
| 48 |
(void)state; /* unused */ |
| 49 |
flashrom_set_log_callback(test_log_callback); |
| 50 |
/* check that callback is called */ |
| 51 |
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_INFO); |
| 52 |
flashrom_set_log_callback(NULL); |
| 53 |
} |
| 54 |
|
| 55 |
void flashrom_set_log_callback_v2_test_success(void **state) |
| 56 |
{ |
| 57 |
(void)state; /* unused */ |
| 58 |
int user_data = 100500; |
| 59 |
flashrom_set_log_callback_v2(test_log_callback_v2, &user_data); |
| 60 |
print(FLASHROM_MSG_ERROR, "2%s", "\n"); |
| 61 |
/* check that callback is called */ |
| 62 |
assert_int_equal(user_data, 0x666 + (int)FLASHROM_MSG_ERROR); |
| 63 |
flashrom_set_log_callback_v2(NULL, NULL); |
| 64 |
} |
| 65 |
|
| 66 |
void flashrom_set_log_level_test_success(void **state) |
| 67 |
{ |
| 68 |
(void)state; /* unused */ |
| 69 |
int user_data; |
| 70 |
|
| 71 |
flashrom_set_log_level(FLASHROM_MSG_WARN); |
| 72 |
|
| 73 |
/* v2 API check */ |
| 74 |
user_data = 100500; |
| 75 |
flashrom_set_log_callback_v2(test_log_callback_v2, &user_data); |
| 76 |
print(FLASHROM_MSG_DEBUG, "2%s", "\n"); |
| 77 |
/* check that callback is not called */ |
| 78 |
assert_int_equal(user_data, 100500); |
| 79 |
print(FLASHROM_MSG_ERROR, "2%s", "\n"); |
| 80 |
/* check that callback is called for less */ |
| 81 |
assert_int_equal(user_data, 0x666 + (int)FLASHROM_MSG_ERROR); |
| 82 |
user_data = 100500; |
| 83 |
print(FLASHROM_MSG_WARN, "2%s", "\n"); |
| 84 |
/* check that callback is called for equal */ |
| 85 |
assert_int_equal(user_data, 0x666 + (int)FLASHROM_MSG_WARN); |
| 86 |
|
| 87 |
/* v1 API check */ |
| 88 |
flashrom_set_log_callback(test_log_callback); |
| 89 |
|
| 90 |
/* check that callback is not called */ |
| 91 |
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0); |
| 92 |
/* check that callback is called for equal */ |
| 93 |
assert_int_equal(print(FLASHROM_MSG_WARN, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_WARN); |
| 94 |
/* check that callback is called for less*/ |
| 95 |
assert_int_equal(print(FLASHROM_MSG_ERROR, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_ERROR); |
| 96 |
|
| 97 |
flashrom_set_log_level(FLASHROM_MSG_INFO); |
| 98 |
/* check that callback is called after the change*/ |
| 99 |
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_INFO); |
| 100 |
flashrom_set_log_callback(NULL); |
| 101 |
} |
| 102 |
|
| 103 |
void flashrom_supported_programmers_test_success(void **state) |
| 104 |
{ |
| 105 |
(void) state; /* unused */ |
| 106 |
const char **array = flashrom_supported_programmers(); |
| 107 |
const char **ptr = array; |
| 108 |
|
| 109 |
assert_non_null(array); |
| 110 |
|
| 111 |
do { |
| 112 |
assert_non_null(*ptr); |
| 113 |
}while (*(++ptr)); |
| 114 |
|
| 115 |
flashrom_data_free(array); |
| 116 |
|
| 117 |
assert_int_not_equal(ptr - array, 0); |
| 118 |
} |
| 119 |
|
| 120 |
#if CONFIG_DUMMY == 1 |
| 121 |
typedef void* (map_flash_fn)(const char *descr, uintptr_t phys_addr, size_t len); |
| 122 |
typedef int (spi_send_fn)(const struct flashctx *flash, unsigned int writecnt, |
| 123 |
unsigned int readcnt, |
| 124 |
const unsigned char *writearr, |
| 125 |
unsigned char *readarr); |
| 126 |
|
| 127 |
static void *always_fail_map(const char *descr, uintptr_t phys_addr, size_t len) |
| 128 |
{ |
| 129 |
return ERROR_PTR; |
| 130 |
} |
| 131 |
|
| 132 |
static int always_fail_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 133 |
unsigned int readcnt, |
| 134 |
const unsigned char *writearr, |
| 135 |
unsigned char *readarr) |
| 136 |
{ |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
void probe_v2_error_code_propagation(void **state) |
| 141 |
{ |
| 142 |
(void) state; /* unused */ |
| 143 |
|
| 144 |
struct flashrom_flashctx flashctx = { 0 }; |
| 145 |
struct flashrom_programmer *flashprog; |
| 146 |
const char **all_matched_names = NULL; |
| 147 |
|
| 148 |
assert_int_equal(0, flashrom_programmer_init(&flashprog, |
| 149 |
programmer_dummy.name, |
| 150 |
"bus=spi,emulate=W25Q128FV")); |
| 151 |
|
| 152 |
map_flash_fn *original_map_flash = registered_masters[0].spi.map_flash_region; |
| 153 |
spi_send_fn *original_spi_send = registered_masters[0].spi.command; |
| 154 |
/* This makes sure probe_flash fails. */ |
| 155 |
registered_masters[0].spi.map_flash_region = &always_fail_map; |
| 156 |
registered_masters[0].spi.command = &always_fail_spi_send_command; |
| 157 |
|
| 158 |
assert_int_equal(0 /* no chips found */, |
| 159 |
flashrom_flash_probe_v2(&flashctx, &all_matched_names, |
| 160 |
flashprog, |
| 161 |
NULL)); |
| 162 |
|
| 163 |
/* restore programmer functions */ |
| 164 |
registered_masters[0].spi.map_flash_region = original_map_flash; |
| 165 |
registered_masters[0].spi.command = original_spi_send; |
| 166 |
|
| 167 |
assert_int_equal(0, flashrom_programmer_shutdown(flashprog)); |
| 168 |
|
| 169 |
flashrom_data_free(all_matched_names); |
| 170 |
} |
| 171 |
#else |
| 172 |
SKIP_TEST(probe_v2_error_code_propagation) |
| 173 |
#endif /* CONFIG_DUMMY */ |