xref: /aosp_15_r20/external/vboot_reference/tests/cbfstool_tests.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2024 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 
10 #include "2return_codes.h"
11 #include "cbfstool.h"
12 #include "common/tests.h"
13 
14 #define ME "cbfstool_tests"
15 
16 /* Utility functions. */
17 
is_file(const char * path)18 static bool is_file(const char *path)
19 {
20 	struct stat st = {0};
21 	return stat(path, &st) == 0 && S_ISREG(st.st_mode);
22 }
23 
24 /* Setup and teardown functions. */
25 
26 const char *tmp_dir;
27 
setup(void)28 static void setup(void)
29 {
30 	tmp_dir = create_test_tmp_dir(ME);
31 }
32 
teardown(void)33 static void teardown(void)
34 {
35 	free((char *)tmp_dir);
36 }
37 
38 /* Test functions. */
39 
40 #define IMAGE "tests/futility/data/bios_coachz_cbfs.bin"
41 
cbfstool_file_exists_tests(void)42 static void cbfstool_file_exists_tests(void)
43 {
44 	/* Region not exists. */
45 	TEST_FALSE(cbfstool_file_exists(IMAGE, "NO_SUCH_REGION", "config"),
46 		   "region NO_SUCH_REGION not exists");
47 
48 	/* Default FMAP region. */
49 	TEST_TRUE(cbfstool_file_exists(IMAGE, NULL, "font.bin"),
50 		  "font.bin found in COREBOOT");
51 
52 	/* File not found. */
53 	TEST_FALSE(cbfstool_file_exists(IMAGE, "FW_MAIN_A", "font.bin"),
54 		   "font.bin not found in FW_MAIN_A");
55 
56 	/* File found in specified region. */
57 	TEST_TRUE(cbfstool_file_exists(IMAGE, "FW_MAIN_A", "ecrw"),
58 		  "ecrw found in FW_MAIN_A");
59 }
60 
cbfstool_extract_tests(void)61 static void cbfstool_extract_tests(void)
62 {
63 	char *tmp_file = NULL;
64 	xasprintf(&tmp_file, "%s/tmp_file", tmp_dir);
65 
66 	/* Default FMAP region. */
67 	unlink(tmp_file);
68 	TEST_EQ(cbfstool_extract(IMAGE, NULL, "font.bin", tmp_file), 0,
69 		"extract font.bin from COREBOOT");
70 	TEST_TRUE(is_file(tmp_file), "  extracted");
71 
72 	/* File not found. */
73 	unlink(tmp_file);
74 	TEST_NEQ(cbfstool_extract(IMAGE, "FW_MAIN_A", "font.bin", tmp_file), 0,
75 		 "extract font.bin from FW_MAIN_A");
76 
77 	/* File from specified region. */
78 	unlink(tmp_file);
79 	TEST_EQ(cbfstool_extract(IMAGE, "FW_MAIN_A", "ecrw", tmp_file), 0,
80 		"extract ecrw from FW_MAIN_A");
81 	TEST_TRUE(is_file(tmp_file), "  extracted");
82 
83 	free(tmp_file);
84 }
85 
cbfstool_get_config_bool_tests(void)86 static void cbfstool_get_config_bool_tests(void)
87 {
88 	bool value;
89 	vb2_error_t rv;
90 
91 	/* File not found */
92 	value = true;
93 	rv = cbfstool_get_config_bool("no_such_file", NULL,
94 				      "CONFIG_CHROMEOS", &value);
95 	TEST_FAIL(rv, "file not found");
96 	TEST_FALSE(value, "  value is false");
97 
98 	/* Config not found */
99 	value = true;
100 	rv = cbfstool_get_config_bool(IMAGE, NULL,
101 				      "CONFIG_NOT_FOUND", &value);
102 	TEST_SUCC(rv, "config not found");
103 	TEST_FALSE(value, "  value is false");
104 
105 	/* Config CHROMEOS */
106 	value = false;
107 	rv = cbfstool_get_config_bool(IMAGE, NULL,
108 				      "CONFIG_CHROMEOS", &value);
109 	TEST_SUCC(rv, "get CHROMEOS value");
110 	TEST_TRUE(value, "  value is true");
111 
112 	/* Config CHROMEOS from FW_MAIN_A */
113 	value = false;
114 	rv = cbfstool_get_config_bool(IMAGE, "FW_MAIN_A",
115 				      "CONFIG_CHROMEOS", &value);
116 	TEST_SUCC(rv, "get CHROMEOS value from FW_MAIN_A");
117 	TEST_TRUE(value, "  value is true");
118 }
119 
cbfstool_get_config_string_tests(void)120 static void cbfstool_get_config_string_tests(void)
121 {
122 	char *value;
123 	char init_value[] = "INIT_VALUE";
124 	vb2_error_t rv;
125 
126 	/* Config not found */
127 	value = init_value;
128 	rv = cbfstool_get_config_string(IMAGE, NULL,
129 					"CONFIG_NOT_FOUND", &value);
130 	TEST_FAIL(rv, "config not found");
131 	TEST_PTR_EQ(value, NULL, "  value is null");
132 
133 	/* Config MAINBOARD_PART_NUMBER */
134 	value = NULL;
135 	rv = cbfstool_get_config_string(IMAGE, NULL,
136 					"CONFIG_MAINBOARD_PART_NUMBER", &value);
137 	TEST_SUCC(rv, "get MAINBOARD_PART_NUMBER value");
138 	TEST_PTR_NEQ(value, NULL, "  value not null");
139 	TEST_EQ(strcmp(value, "Coachz"), 0, "  value is Coachz");
140 }
141 
main(int argc,char * argv[])142 int main(int argc, char *argv[])
143 {
144 	setup();
145 
146 	cbfstool_file_exists_tests();
147 	cbfstool_extract_tests();
148 	cbfstool_get_config_bool_tests();
149 	cbfstool_get_config_string_tests();
150 
151 	teardown();
152 
153 	return gTestSuccess ? 0 : 255;
154 }
155