xref: /aosp_15_r20/external/vboot_reference/tests/chromeos_config_tests.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2020 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 <stdbool.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "2common.h"
12 #include "2return_codes.h"
13 #include "chromeos_config.h"
14 #include "common/tests.h"
15 #include "host_misc.h"
16 
17 static struct {
18 	const char *path;
19 	const char *data;
20 } fakefs[] = {
21 	{"/run/chromeos-config/v1/name", "bleh_model"},
22 	{"/run/chromeos-config/v1/brand-code", "ZZCR"},
23 	{"/run/chromeos-config/v1/identity/sku-id", "7"},
24 	{"/run/chromeos-config/v1/firmware/image-name", "bloop"},
25 	{"/run/chromeos-config/v1/auto-night-light", "true"},
26 	{"/run/chromeos-config/v1/hardware-properties/is-lid-convertible",
27 	 "false"},
28 };
29 
vb2_read_file(const char * filepath,uint8_t ** data_ptr,uint32_t * size_ptr)30 vb2_error_t vb2_read_file(const char *filepath, uint8_t **data_ptr,
31 			  uint32_t *size_ptr)
32 {
33 	*data_ptr = NULL;
34 	*size_ptr = 0;
35 
36 	for (size_t i = 0; i < ARRAY_SIZE(fakefs); i++) {
37 		if (!strcmp(fakefs[i].path, filepath)) {
38 			*size_ptr = strlen(fakefs[i].data);
39 			*data_ptr = malloc(*size_ptr);
40 
41 			if (!*data_ptr)
42 				return VB2_ERROR_READ_FILE_ALLOC;
43 
44 			memcpy(*data_ptr, fakefs[i].data, *size_ptr);
45 			return VB2_SUCCESS;
46 		}
47 	}
48 
49 	return VB2_ERROR_READ_FILE_OPEN;
50 }
51 
test_get_string(void)52 static void test_get_string(void)
53 {
54 	char *val_out;
55 
56 	TEST_EQ(chromeos_config_get_string("/firmware", "image-name", &val_out),
57 		VB2_SUCCESS, "Reading a string is successful");
58 	TEST_STR_EQ(val_out, "bloop", "The string is the correct value");
59 	free(val_out);
60 }
61 
test_get_boolean_true(void)62 static void test_get_boolean_true(void)
63 {
64 	bool val_out;
65 
66 	TEST_EQ(chromeos_config_get_boolean("/", "auto-night-light", &val_out),
67 		VB2_SUCCESS, "Reading a true boolean is successful");
68 	TEST_EQ(val_out, true, "The resulting boolean is true");
69 }
70 
test_get_boolean_false(void)71 static void test_get_boolean_false(void)
72 {
73 	bool val_out;
74 
75 	TEST_EQ(chromeos_config_get_boolean("/hardware-properties",
76 					    "is-lid-convertible", &val_out),
77 		VB2_SUCCESS, "Reading a false boolean is successful");
78 	TEST_EQ(val_out, false, "The resulting boolean is false");
79 }
80 
test_get_integer(void)81 static void test_get_integer(void)
82 {
83 	int val_out;
84 
85 	TEST_EQ(chromeos_config_get_integer("/identity", "sku-id", &val_out),
86 		VB2_SUCCESS, "Reading an integer is successful");
87 	TEST_EQ(val_out, 7, "The resulting integer is correct");
88 }
89 
test_get_no_exist(void)90 static void test_get_no_exist(void)
91 {
92 	char *val_out;
93 
94 	TEST_NEQ(
95 		chromeos_config_get_string("/this/does", "not-exist", &val_out),
96 		VB2_SUCCESS, "Reading non-existent property fails");
97 	free(val_out);
98 }
99 
test_get_bad_path(void)100 static void test_get_bad_path(void)
101 {
102 	char *val_out;
103 
104 	TEST_NEQ(chromeos_config_get_string("name", "name", &val_out),
105 		 VB2_SUCCESS, "Reading bad path fails");
106 	free(val_out);
107 }
108 
test_get_bad_path2(void)109 static void test_get_bad_path2(void)
110 {
111 	char *val_out;
112 
113 	TEST_NEQ(chromeos_config_get_string("//name", "name", &val_out),
114 		 VB2_SUCCESS, "Reading bad path fails");
115 	free(val_out);
116 }
117 
test_get_bad_property(void)118 static void test_get_bad_property(void)
119 {
120 	char *val_out;
121 
122 	TEST_NEQ(chromeos_config_get_string("/firmware", "/image-name",
123 					    &val_out),
124 		 VB2_SUCCESS, "Reading bad property fails");
125 	free(val_out);
126 }
127 
test_get_not_boolean(void)128 static void test_get_not_boolean(void)
129 {
130 	bool val_out;
131 
132 	TEST_NEQ(chromeos_config_get_boolean("/identity", "sku-id", &val_out),
133 		 VB2_SUCCESS, "Reading integer as boolean fails");
134 }
135 
test_get_not_integer(void)136 static void test_get_not_integer(void)
137 {
138 	int val_out;
139 
140 	TEST_NEQ(chromeos_config_get_integer("/", "brand-code", &val_out),
141 		 VB2_SUCCESS, "Reading string as integer fails");
142 }
143 
main(int argc,char * argv[])144 int main(int argc, char *argv[])
145 {
146 	test_get_string();
147 	test_get_boolean_true();
148 	test_get_boolean_false();
149 	test_get_integer();
150 	test_get_no_exist();
151 	test_get_bad_path();
152 	test_get_bad_path2();
153 	test_get_bad_property();
154 	test_get_not_boolean();
155 	test_get_not_integer();
156 
157 	return gTestSuccess ? 0 : 255;
158 }
159