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