xref: /aosp_15_r20/external/flashrom/tests/dummyflasher.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright 2021 Google LLC
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include "lifecycle.h"
17 
18 #if CONFIG_DUMMY == 1
dummy_basic_lifecycle_test_success(void ** state)19 void dummy_basic_lifecycle_test_success(void **state)
20 {
21 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
22 		.noc = 0,
23 		.paths = { LOCK_FILE },
24 	};
25 	const struct io_mock dummy_io = {
26 		.fallback_open_state = &dummy_fallback_open_state,
27 	};
28 
29 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=parallel+lpc+fwh+spi+prog");
30 }
31 
dummy_probe_lifecycle_test_success(void ** state)32 void dummy_probe_lifecycle_test_success(void **state)
33 {
34 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
35 		.noc = 0,
36 		.paths = { LOCK_FILE },
37 	};
38 	const struct io_mock dummy_io = {
39 		.fallback_open_state = &dummy_fallback_open_state,
40 	};
41 
42 	run_probe_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi,emulate=W25Q128FV", "W25Q128.V");
43 }
44 
dummy_probe_variable_size_test_success(void ** state)45 void dummy_probe_variable_size_test_success(void **state)
46 {
47 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
48 		.noc = 0,
49 		.paths = { LOCK_FILE },
50 	};
51 	const struct io_mock dummy_io = {
52 		.fallback_open_state = &dummy_fallback_open_state,
53 	};
54 
55 	run_probe_lifecycle(state, &dummy_io, &programmer_dummy, "size=8388608,emulate=VARIABLE_SIZE", "Opaque flash chip");
56 }
57 
dummy_init_fails_unhandled_param_test_success(void ** state)58 void dummy_init_fails_unhandled_param_test_success(void **state)
59 {
60 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
61 		.noc = 0,
62 		.paths = { NULL },
63 	};
64 	const struct io_mock dummy_io = {
65 		.fallback_open_state = &dummy_fallback_open_state,
66 	};
67 
68 	/*
69 	 * Programmer init should fail due to `dummy_init` failure caused by
70 	 * invalid value of `emulate` param. There is unhandled param left
71 	 * at the end of param string.
72 	 */
73 	run_init_error_path(state, &dummy_io, &programmer_dummy, "bus=spi,emulate=INVALID,unhandled=value", 1);
74 }
75 
dummy_init_success_invalid_param_test_success(void ** state)76 void dummy_init_success_invalid_param_test_success(void **state)
77 {
78 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
79 		.noc = 0,
80 		.paths = { NULL },
81 	};
82 	const struct io_mock dummy_io = {
83 		.fallback_open_state = &dummy_fallback_open_state,
84 	};
85 
86 	/*
87 	 * Programmer init should fail despite of the fact that `dummy_init`
88 	 * is successful, due to invalid param at the end of param string.
89 	 */
90 	run_init_error_path(state, &dummy_io, &programmer_dummy,
91 				"bus=spi,emulate=W25Q128FV,invalid=value", ERROR_FLASHROM_FATAL);
92 }
93 
dummy_init_success_unhandled_param_test_success(void ** state)94 void dummy_init_success_unhandled_param_test_success(void **state)
95 {
96 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
97 		.noc = 0,
98 		.paths = { NULL },
99 	};
100 	const struct io_mock dummy_io = {
101 		.fallback_open_state = &dummy_fallback_open_state,
102 	};
103 
104 	/*
105 	 * Programmer init should fail despite of the fact that `dummy_init`
106 	 * is successful, due to unhandled param at the end of param string.
107 	 * Unhandled param `voltage` is not used for dummyflasher.
108 	 */
109 	run_init_error_path(state, &dummy_io, &programmer_dummy,
110 				"bus=spi,emulate=W25Q128FV,voltage=3.5V", ERROR_FLASHROM_FATAL);
111 }
112 
dummy_null_prog_param_test_success(void ** state)113 void dummy_null_prog_param_test_success(void **state)
114 {
115 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
116 		.noc = 0,
117 		.paths = { NULL },
118 	};
119 	const struct io_mock dummy_io = {
120 		.fallback_open_state = &dummy_fallback_open_state,
121 	};
122 
123 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, NULL);
124 }
125 
dummy_all_buses_test_success(void ** state)126 void dummy_all_buses_test_success(void **state)
127 {
128 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
129 		.noc = 0,
130 		.paths = { NULL },
131 	};
132 	const struct io_mock dummy_io = {
133 		.fallback_open_state = &dummy_fallback_open_state,
134 	};
135 
136 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=lpc+fwh");
137 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi");
138 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=prog");
139 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=parallel+fwh+prog");
140 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi+prog");
141 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=parallel+lpc+spi");
142 }
143 
dummy_freq_param_init(void ** state)144 void dummy_freq_param_init(void **state)
145 {
146 	struct io_mock_fallback_open_state dummy_fallback_open_state = {
147 		.noc = 0,
148 		.paths = { NULL },
149 	};
150 	const struct io_mock dummy_io = {
151 		.fallback_open_state = &dummy_fallback_open_state,
152 	};
153 
154 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi,freq=12Hz");
155 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi,freq=123KHz");
156 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi,freq=345MHz");
157 	run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi,freq=8000MHz");
158 	/* Valid values for freq param are within the range [1Hz, 8000Mhz] */
159 	run_init_error_path(state, &dummy_io, &programmer_dummy, "bus=spi,freq=0Hz", 0x1);
160 	run_init_error_path(state, &dummy_io, &programmer_dummy, "bus=spi,freq=8001Mhz", 0x1);
161 }
162 
163 #else
164 	SKIP_TEST(dummy_basic_lifecycle_test_success)
165 	SKIP_TEST(dummy_probe_lifecycle_test_success)
166 	SKIP_TEST(dummy_probe_variable_size_test_success)
167 	SKIP_TEST(dummy_init_fails_unhandled_param_test_success)
168 	SKIP_TEST(dummy_init_success_invalid_param_test_success)
169 	SKIP_TEST(dummy_init_success_unhandled_param_test_success)
170 	SKIP_TEST(dummy_null_prog_param_test_success)
171 	SKIP_TEST(dummy_all_buses_test_success)
172 	SKIP_TEST(dummy_freq_param_init)
173 #endif /* CONFIG_DUMMY */
174