1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fastboot.h"
18
19 #include <gtest/gtest.h>
20
TEST(FastBoot,ParseOsPatchLevel)21 TEST(FastBoot, ParseOsPatchLevel) {
22 FastBootTool fb;
23 boot_img_hdr_v1 hdr;
24
25 hdr = {};
26 fb.ParseOsPatchLevel(&hdr, "2018-01-05");
27 ASSERT_EQ(2018U, 2000U + ((hdr.os_version >> 4) & 0x7f));
28 ASSERT_EQ(1U, ((hdr.os_version >> 0) & 0xf));
29
30 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018"), "should be YYYY-MM-DD");
31 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-01"), "should be YYYY-MM-DD");
32 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2128-01-05"), "year out of range");
33 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-13-05"), "month out of range");
34 }
35
TEST(FastBoot,ParseOsVersion)36 TEST(FastBoot, ParseOsVersion) {
37 FastBootTool fb;
38 boot_img_hdr_v1 hdr;
39
40 hdr = {};
41 fb.ParseOsVersion(&hdr, "1.2.3");
42 ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
43 ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f));
44 ASSERT_EQ(3U, ((hdr.os_version >> 11) & 0x7f));
45
46 fb.ParseOsVersion(&hdr, "1.2");
47 ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
48 ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f));
49 ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f));
50
51 fb.ParseOsVersion(&hdr, "1");
52 ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
53 ASSERT_EQ(0U, ((hdr.os_version >> 18) & 0x7f));
54 ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f));
55
56 EXPECT_DEATH(fb.ParseOsVersion(&hdr, ""), "bad OS version");
57 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.3.4"), "bad OS version");
58 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "128.2.3"), "bad OS version");
59 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.128.3"), "bad OS version");
60 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.128"), "bad OS version");
61 }
62
63 extern bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
64 bool* invert, std::vector<std::string>* options);
65
ParseRequirementLineTest(const std::string & line,const std::string & expected_name,const std::string & expected_product,bool expected_invert,const std::vector<std::string> & expected_options)66 static void ParseRequirementLineTest(const std::string& line, const std::string& expected_name,
67 const std::string& expected_product, bool expected_invert,
68 const std::vector<std::string>& expected_options) {
69 std::string name;
70 std::string product;
71 bool invert;
72 std::vector<std::string> options;
73
74 EXPECT_TRUE(ParseRequirementLine(line, &name, &product, &invert, &options)) << line;
75
76 EXPECT_EQ(expected_name, name) << line;
77 EXPECT_EQ(expected_product, product) << line;
78 EXPECT_EQ(expected_invert, invert) << line;
79 EXPECT_EQ(expected_options, options) << line;
80 }
81
TEST(FastBoot,ParseRequirementLineSuccesses)82 TEST(FastBoot, ParseRequirementLineSuccesses) {
83 // Examples provided in the code + slight variations.
84 ParseRequirementLineTest("require product=alpha", "product", "", false, {"alpha"});
85 ParseRequirementLineTest("require product=alpha|beta|gamma", "product", "", false,
86 {"alpha", "beta", "gamma"});
87 ParseRequirementLineTest("require version-bootloader=1234", "version-bootloader", "", false,
88 {"1234"});
89 ParseRequirementLineTest("require-for-product:gamma version-bootloader=istanbul",
90 "version-bootloader", "gamma", false, {"istanbul"});
91 ParseRequirementLineTest("require-for-product:gamma version-bootloader=istanbul|constantinople",
92 "version-bootloader", "gamma", false, {"istanbul", "constantinople"});
93 ParseRequirementLineTest("require partition-exists=vendor", "partition-exists", "", false,
94 {"vendor"});
95 ParseRequirementLineTest("reject product=alpha", "product", "", true, {"alpha"});
96 ParseRequirementLineTest("reject product=alpha|beta|gamma", "product", "", true,
97 {"alpha", "beta", "gamma"});
98
99 // Without any prefix, assume 'require'
100 ParseRequirementLineTest("product=alpha|beta|gamma", "product", "", false,
101 {"alpha", "beta", "gamma"});
102 // Including if the variable name is otherwise a prefix keyword
103 ParseRequirementLineTest("require = alpha", "require", "", false, {"alpha"});
104 ParseRequirementLineTest("reject = alpha", "reject", "", false, {"alpha"});
105 ParseRequirementLineTest("require-for-product:gamma = alpha", "require-for-product:gamma", "",
106 false, {"alpha"});
107
108 // Extra spaces are allowed.
109 ParseRequirementLineTest("require product=alpha|beta|gamma", "product", "", false,
110 {"alpha", "beta", "gamma"});
111 ParseRequirementLineTest("require product =alpha|beta|gamma", "product", "", false,
112 {"alpha", "beta", "gamma"});
113 ParseRequirementLineTest("require product= alpha|beta|gamma", "product", "", false,
114 {"alpha", "beta", "gamma"});
115 ParseRequirementLineTest("require product = alpha|beta|gamma", "product", "", false,
116 {"alpha", "beta", "gamma"});
117 ParseRequirementLineTest("require product=alpha |beta|gamma", "product", "", false,
118 {"alpha", "beta", "gamma"});
119 ParseRequirementLineTest("require product=alpha| beta|gamma", "product", "", false,
120 {"alpha", "beta", "gamma"});
121 ParseRequirementLineTest("require product=alpha | beta|gamma", "product", "", false,
122 {"alpha", "beta", "gamma"});
123 ParseRequirementLineTest("require product=alpha|beta|gamma ", "product", "", false,
124 {"alpha", "beta", "gamma"});
125 ParseRequirementLineTest("product = alpha | beta | gamma ", "product", "", false,
126 {"alpha", "beta", "gamma"});
127 ParseRequirementLineTest("require-for-product: gamma version-bootloader=istanbul",
128 "version-bootloader", "gamma", false, {"istanbul"});
129
130 // Extraneous ending | is okay, implies accepting an empty string.
131 ParseRequirementLineTest("require product=alpha|", "product", "", false, {"alpha", ""});
132 ParseRequirementLineTest("require product=alpha|beta|gamma|", "product", "", false,
133 {"alpha", "beta", "gamma", ""});
134
135 // Accept empty options, double ||, etc, implies accepting an empty string.
136 ParseRequirementLineTest("require product=alpha||beta| |gamma", "product", "", false,
137 {"alpha", "", "beta", "", "gamma"});
138 ParseRequirementLineTest("require product=alpha||beta|gamma", "product", "", false,
139 {"alpha", "", "beta", "gamma"});
140 ParseRequirementLineTest("require product=alpha|beta| |gamma", "product", "", false,
141 {"alpha", "beta", "", "gamma"});
142 ParseRequirementLineTest("require product=alpha||", "product", "", false, {"alpha", "", ""});
143 ParseRequirementLineTest("require product=alpha|| ", "product", "", false, {"alpha", "", ""});
144 ParseRequirementLineTest("require product=alpha| ", "product", "", false, {"alpha", ""});
145 ParseRequirementLineTest("require product=alpha|beta| ", "product", "", false,
146 {"alpha", "beta", ""});
147
148 // No option string is also treating as accepting an empty string.
149 ParseRequirementLineTest("require =", "require", "", false, {""});
150 ParseRequirementLineTest("require = |", "require", "", false, {"", ""});
151 ParseRequirementLineTest("reject =", "reject", "", false, {""});
152 ParseRequirementLineTest("reject = |", "reject", "", false, {"", ""});
153 ParseRequirementLineTest("require-for-product: =", "require-for-product:", "", false, {""});
154 ParseRequirementLineTest("require-for-product: = | ", "require-for-product:", "", false,
155 {"", ""});
156 ParseRequirementLineTest("require product=", "product", "", false, {""});
157 ParseRequirementLineTest("require product = ", "product", "", false, {""});
158 ParseRequirementLineTest("require product = | ", "product", "", false, {"", ""});
159 ParseRequirementLineTest("reject product=", "product", "", true, {""});
160 ParseRequirementLineTest("reject product = ", "product", "", true, {""});
161 ParseRequirementLineTest("reject product = | ", "product", "", true, {"", ""});
162 ParseRequirementLineTest("require-for-product:gamma product=", "product", "gamma", false, {""});
163 ParseRequirementLineTest("require-for-product:gamma product = ", "product", "gamma", false,
164 {""});
165 ParseRequirementLineTest("require-for-product:gamma product = |", "product", "gamma", false,
166 {"", ""});
167
168 // Check for board -> product substitution.
169 ParseRequirementLineTest("require board=alpha", "product", "", false, {"alpha"});
170 ParseRequirementLineTest("board=alpha", "product", "", false, {"alpha"});
171 }
172
ParseRequirementLineTestMalformed(const std::string & line)173 static void ParseRequirementLineTestMalformed(const std::string& line) {
174 std::string name;
175 std::string product;
176 bool invert;
177 std::vector<std::string> options;
178
179 EXPECT_FALSE(ParseRequirementLine(line, &name, &product, &invert, &options)) << line;
180 }
181
TEST(FastBoot,ParseRequirementLineMalformed)182 TEST(FastBoot, ParseRequirementLineMalformed) {
183 ParseRequirementLineTestMalformed("nothing");
184 ParseRequirementLineTestMalformed("");
185 ParseRequirementLineTestMalformed("=");
186 ParseRequirementLineTestMalformed("|");
187
188 ParseRequirementLineTestMalformed("require");
189 ParseRequirementLineTestMalformed("require ");
190 ParseRequirementLineTestMalformed("reject");
191 ParseRequirementLineTestMalformed("reject ");
192 ParseRequirementLineTestMalformed("require-for-product:");
193 ParseRequirementLineTestMalformed("require-for-product: ");
194
195 ParseRequirementLineTestMalformed("require product");
196 ParseRequirementLineTestMalformed("reject product");
197
198 ParseRequirementLineTestMalformed("require-for-product:gamma");
199 ParseRequirementLineTestMalformed("require-for-product:gamma product");
200
201 // No spaces allowed before between require-for-product and :.
202 ParseRequirementLineTestMalformed("require-for-product :");
203 }
204