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