Debian Codegraph @

tests/lifecycle.c

raw
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 static void probe_chip_v2(const struct programmer_entry *prog,
19 struct flashrom_programmer *flashprog,
20 const char *const chip_name,
21 const char **expected_matched_names,
22 unsigned int expected_matched_count)
23 {
24 struct flashrom_flashctx flashctx = { 0 };
25 const char **all_matched_names = NULL;
26
27 printf("Testing flashrom_flash_probe_v2 for programmer=%s, chip=%s ... \n", prog->name, chip_name);
28 assert_int_equal(expected_matched_count, flashrom_flash_probe_v2(&flashctx, &all_matched_names,
29 flashprog, chip_name));
30
31 for (unsigned int i = 0; i < expected_matched_count; i++)
32 assert_int_equal(0, strcmp(expected_matched_names[i], all_matched_names[i]));
33
34 assert_null(all_matched_names[expected_matched_count]);
35
36 if (chip_name && expected_matched_count > 0)
37 assert_int_equal(0, strcmp(chip_name, flashctx.chip->name));
38
39 printf("... flashrom_flash_probe_v2 for programmer=%s successful\n", prog->name);
40
41 /* cleanup */
42 flashrom_data_free(all_matched_names);
43 flashrom_layout_release(flashctx.default_layout);
44 free(flashctx.chip);
45 }
46
47 static void run_lifecycle(void **state, const struct io_mock *io, const struct programmer_entry *prog,
48 const char *param, const char *const chip_name,
49 const char **expected_matched_names,
50 unsigned int expected_matched_count,
51 void (*action)(const struct programmer_entry *prog,
52 struct flashrom_programmer *flashprog,
53 const char *const chip_name,
54 const char **expected_matched_names,
55 unsigned int expected_matched_count))
56 {
57 (void) state; /* unused */
58
59 io_mock_register(io);
60
61 struct flashrom_programmer *flashprog;
62
63 printf("Testing flashrom_programmer_init for programmer=%s ...\n", prog->name);
64 assert_int_equal(0, flashrom_programmer_init(&flashprog, prog->name, param));
65 printf("... flashrom_programmer_init for programmer=%s successful\n", prog->name);
66
67 if (action)
68 action(prog, flashprog, chip_name, expected_matched_names, expected_matched_count);
69
70 printf("Testing flashrom_programmer_shutdown for programmer=%s ...\n", prog->name);
71 assert_int_equal(0, flashrom_programmer_shutdown(flashprog));
72 printf("... flashrom_programmer_shutdown for programmer=%s successful\n", prog->name);
73
74 io_mock_register(NULL);
75 }
76
77 void run_basic_lifecycle(void **state, const struct io_mock *io,
78 const struct programmer_entry *prog, const char *param)
79 {
80 /* Basic lifecycle only does init and shutdown,
81 * so neither chip name nor action is needed. */
82 run_lifecycle(state, io, prog, param, NULL /* chip_name */,
83 NULL /* expected_matched_names, */, 0 /* expected_matched_count, */,
84 NULL /* action */);
85 }
86
87 void run_probe_v2_lifecycle(void **state, const struct io_mock *io,
88 const struct programmer_entry *prog, const char *param,
89 const char *const chip_name,
90 const char **expected_matched_names, unsigned int expected_matched_count)
91 {
92 /* Each probe lifecycle should run independently, without cache. */
93 clear_spi_id_cache();
94 run_lifecycle(state, io, prog, param, chip_name,
95 expected_matched_names, expected_matched_count, &probe_chip_v2);
96 }
97
98 void run_init_error_path(void **state, const struct io_mock *io, const struct programmer_entry *prog,
99 const char *param, const int error_code)
100 {
101 (void) state; /* unused */
102
103 io_mock_register(io);
104
105 struct flashrom_programmer *flashprog;
106
107 printf("Testing init error path for programmer=%s with params: %s ...\n", prog->name, param);
108 assert_int_equal(error_code, flashrom_programmer_init(&flashprog, prog->name, param));
109 printf("... init failed with error code %i as expected\n", error_code);
110
111 /*
112 * `flashrom_programmer_shutdown` runs only registered shutdown functions, which means
113 * if nothing has been registered then nothing runs.
114 * Since this is testing error path on initialisation and error can happen at different
115 * phases of init, we don't know whether shutdown function has already been registered
116 * or not yet. Running `flashrom_programmer_shutdown` covers both situations.
117 */
118 printf("Running programmer shutdown in case anything got registered...\n");
119 assert_int_equal(0, flashrom_programmer_shutdown(flashprog));
120 printf("... completed\n");
121
122 io_mock_register(NULL);
123 }