Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 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 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 17 | #include "fastboot.h" |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 18 | |
David Anderson | ada8718 | 2023-02-08 22:06:56 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | TEST(FastBoot, ParseOsPatchLevel) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 23 | FastBootTool fb; |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 24 | boot_img_hdr_v1 hdr; |
| 25 | |
| 26 | hdr = {}; |
| 27 | fb.ParseOsPatchLevel(&hdr, "2018-01-05"); |
| 28 | ASSERT_EQ(2018U, 2000U + ((hdr.os_version >> 4) & 0x7f)); |
| 29 | ASSERT_EQ(1U, ((hdr.os_version >> 0) & 0xf)); |
| 30 | |
| 31 | EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018"), "should be YYYY-MM-DD"); |
| 32 | EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-01"), "should be YYYY-MM-DD"); |
| 33 | EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2128-01-05"), "year out of range"); |
| 34 | EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-13-05"), "month out of range"); |
| 35 | } |
| 36 | |
| 37 | TEST(FastBoot, ParseOsVersion) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 38 | FastBootTool fb; |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 39 | boot_img_hdr_v1 hdr; |
| 40 | |
| 41 | hdr = {}; |
| 42 | fb.ParseOsVersion(&hdr, "1.2.3"); |
| 43 | ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f)); |
| 44 | ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f)); |
| 45 | ASSERT_EQ(3U, ((hdr.os_version >> 11) & 0x7f)); |
| 46 | |
| 47 | fb.ParseOsVersion(&hdr, "1.2"); |
| 48 | ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f)); |
| 49 | ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f)); |
| 50 | ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f)); |
| 51 | |
| 52 | fb.ParseOsVersion(&hdr, "1"); |
| 53 | ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f)); |
| 54 | ASSERT_EQ(0U, ((hdr.os_version >> 18) & 0x7f)); |
| 55 | ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f)); |
| 56 | |
| 57 | EXPECT_DEATH(fb.ParseOsVersion(&hdr, ""), "bad OS version"); |
| 58 | EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.3.4"), "bad OS version"); |
| 59 | EXPECT_DEATH(fb.ParseOsVersion(&hdr, "128.2.3"), "bad OS version"); |
| 60 | EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.128.3"), "bad OS version"); |
| 61 | EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.128"), "bad OS version"); |
| 62 | } |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 63 | |
| 64 | extern bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product, |
| 65 | bool* invert, std::vector<std::string>* options); |
| 66 | |
| 67 | static void ParseRequirementLineTest(const std::string& line, const std::string& expected_name, |
| 68 | const std::string& expected_product, bool expected_invert, |
| 69 | const std::vector<std::string>& expected_options) { |
| 70 | std::string name; |
| 71 | std::string product; |
| 72 | bool invert; |
| 73 | std::vector<std::string> options; |
| 74 | |
| 75 | EXPECT_TRUE(ParseRequirementLine(line, &name, &product, &invert, &options)) << line; |
| 76 | |
| 77 | EXPECT_EQ(expected_name, name) << line; |
| 78 | EXPECT_EQ(expected_product, product) << line; |
| 79 | EXPECT_EQ(expected_invert, invert) << line; |
| 80 | EXPECT_EQ(expected_options, options) << line; |
| 81 | } |
| 82 | |
| 83 | TEST(FastBoot, ParseRequirementLineSuccesses) { |
| 84 | // Examples provided in the code + slight variations. |
| 85 | ParseRequirementLineTest("require product=alpha", "product", "", false, {"alpha"}); |
| 86 | ParseRequirementLineTest("require product=alpha|beta|gamma", "product", "", false, |
| 87 | {"alpha", "beta", "gamma"}); |
| 88 | ParseRequirementLineTest("require version-bootloader=1234", "version-bootloader", "", false, |
| 89 | {"1234"}); |
| 90 | ParseRequirementLineTest("require-for-product:gamma version-bootloader=istanbul", |
| 91 | "version-bootloader", "gamma", false, {"istanbul"}); |
| 92 | ParseRequirementLineTest("require-for-product:gamma version-bootloader=istanbul|constantinople", |
| 93 | "version-bootloader", "gamma", false, {"istanbul", "constantinople"}); |
| 94 | ParseRequirementLineTest("require partition-exists=vendor", "partition-exists", "", false, |
| 95 | {"vendor"}); |
| 96 | ParseRequirementLineTest("reject product=alpha", "product", "", true, {"alpha"}); |
| 97 | ParseRequirementLineTest("reject product=alpha|beta|gamma", "product", "", true, |
| 98 | {"alpha", "beta", "gamma"}); |
| 99 | |
| 100 | // Without any prefix, assume 'require' |
| 101 | ParseRequirementLineTest("product=alpha|beta|gamma", "product", "", false, |
| 102 | {"alpha", "beta", "gamma"}); |
| 103 | // Including if the variable name is otherwise a prefix keyword |
| 104 | ParseRequirementLineTest("require = alpha", "require", "", false, {"alpha"}); |
| 105 | ParseRequirementLineTest("reject = alpha", "reject", "", false, {"alpha"}); |
| 106 | ParseRequirementLineTest("require-for-product:gamma = alpha", "require-for-product:gamma", "", |
| 107 | false, {"alpha"}); |
| 108 | |
| 109 | // Extra spaces are allowed. |
| 110 | ParseRequirementLineTest("require product=alpha|beta|gamma", "product", "", false, |
| 111 | {"alpha", "beta", "gamma"}); |
| 112 | ParseRequirementLineTest("require product =alpha|beta|gamma", "product", "", false, |
| 113 | {"alpha", "beta", "gamma"}); |
| 114 | ParseRequirementLineTest("require product= alpha|beta|gamma", "product", "", false, |
| 115 | {"alpha", "beta", "gamma"}); |
| 116 | ParseRequirementLineTest("require product = alpha|beta|gamma", "product", "", false, |
| 117 | {"alpha", "beta", "gamma"}); |
| 118 | ParseRequirementLineTest("require product=alpha |beta|gamma", "product", "", false, |
| 119 | {"alpha", "beta", "gamma"}); |
| 120 | ParseRequirementLineTest("require product=alpha| beta|gamma", "product", "", false, |
| 121 | {"alpha", "beta", "gamma"}); |
| 122 | ParseRequirementLineTest("require product=alpha | beta|gamma", "product", "", false, |
| 123 | {"alpha", "beta", "gamma"}); |
| 124 | ParseRequirementLineTest("require product=alpha|beta|gamma ", "product", "", false, |
| 125 | {"alpha", "beta", "gamma"}); |
| 126 | ParseRequirementLineTest("product = alpha | beta | gamma ", "product", "", false, |
| 127 | {"alpha", "beta", "gamma"}); |
| 128 | ParseRequirementLineTest("require-for-product: gamma version-bootloader=istanbul", |
| 129 | "version-bootloader", "gamma", false, {"istanbul"}); |
| 130 | |
| 131 | // Extraneous ending | is okay, implies accepting an empty string. |
| 132 | ParseRequirementLineTest("require product=alpha|", "product", "", false, {"alpha", ""}); |
| 133 | ParseRequirementLineTest("require product=alpha|beta|gamma|", "product", "", false, |
| 134 | {"alpha", "beta", "gamma", ""}); |
| 135 | |
| 136 | // Accept empty options, double ||, etc, implies accepting an empty string. |
| 137 | ParseRequirementLineTest("require product=alpha||beta| |gamma", "product", "", false, |
| 138 | {"alpha", "", "beta", "", "gamma"}); |
| 139 | ParseRequirementLineTest("require product=alpha||beta|gamma", "product", "", false, |
| 140 | {"alpha", "", "beta", "gamma"}); |
| 141 | ParseRequirementLineTest("require product=alpha|beta| |gamma", "product", "", false, |
| 142 | {"alpha", "beta", "", "gamma"}); |
| 143 | ParseRequirementLineTest("require product=alpha||", "product", "", false, {"alpha", "", ""}); |
| 144 | ParseRequirementLineTest("require product=alpha|| ", "product", "", false, {"alpha", "", ""}); |
| 145 | ParseRequirementLineTest("require product=alpha| ", "product", "", false, {"alpha", ""}); |
| 146 | ParseRequirementLineTest("require product=alpha|beta| ", "product", "", false, |
| 147 | {"alpha", "beta", ""}); |
| 148 | |
| 149 | // No option string is also treating as accepting an empty string. |
| 150 | ParseRequirementLineTest("require =", "require", "", false, {""}); |
| 151 | ParseRequirementLineTest("require = |", "require", "", false, {"", ""}); |
| 152 | ParseRequirementLineTest("reject =", "reject", "", false, {""}); |
| 153 | ParseRequirementLineTest("reject = |", "reject", "", false, {"", ""}); |
| 154 | ParseRequirementLineTest("require-for-product: =", "require-for-product:", "", false, {""}); |
| 155 | ParseRequirementLineTest("require-for-product: = | ", "require-for-product:", "", false, |
| 156 | {"", ""}); |
| 157 | ParseRequirementLineTest("require product=", "product", "", false, {""}); |
| 158 | ParseRequirementLineTest("require product = ", "product", "", false, {""}); |
| 159 | ParseRequirementLineTest("require product = | ", "product", "", false, {"", ""}); |
| 160 | ParseRequirementLineTest("reject product=", "product", "", true, {""}); |
| 161 | ParseRequirementLineTest("reject product = ", "product", "", true, {""}); |
| 162 | ParseRequirementLineTest("reject product = | ", "product", "", true, {"", ""}); |
| 163 | ParseRequirementLineTest("require-for-product:gamma product=", "product", "gamma", false, {""}); |
| 164 | ParseRequirementLineTest("require-for-product:gamma product = ", "product", "gamma", false, |
| 165 | {""}); |
| 166 | ParseRequirementLineTest("require-for-product:gamma product = |", "product", "gamma", false, |
| 167 | {"", ""}); |
| 168 | |
| 169 | // Check for board -> product substitution. |
| 170 | ParseRequirementLineTest("require board=alpha", "product", "", false, {"alpha"}); |
| 171 | ParseRequirementLineTest("board=alpha", "product", "", false, {"alpha"}); |
| 172 | } |
| 173 | |
| 174 | static void ParseRequirementLineTestMalformed(const std::string& line) { |
| 175 | std::string name; |
| 176 | std::string product; |
| 177 | bool invert; |
| 178 | std::vector<std::string> options; |
| 179 | |
| 180 | EXPECT_FALSE(ParseRequirementLine(line, &name, &product, &invert, &options)) << line; |
| 181 | } |
| 182 | |
| 183 | TEST(FastBoot, ParseRequirementLineMalformed) { |
| 184 | ParseRequirementLineTestMalformed("nothing"); |
| 185 | ParseRequirementLineTestMalformed(""); |
| 186 | ParseRequirementLineTestMalformed("="); |
| 187 | ParseRequirementLineTestMalformed("|"); |
| 188 | |
| 189 | ParseRequirementLineTestMalformed("require"); |
| 190 | ParseRequirementLineTestMalformed("require "); |
| 191 | ParseRequirementLineTestMalformed("reject"); |
| 192 | ParseRequirementLineTestMalformed("reject "); |
| 193 | ParseRequirementLineTestMalformed("require-for-product:"); |
| 194 | ParseRequirementLineTestMalformed("require-for-product: "); |
| 195 | |
| 196 | ParseRequirementLineTestMalformed("require product"); |
| 197 | ParseRequirementLineTestMalformed("reject product"); |
| 198 | |
| 199 | ParseRequirementLineTestMalformed("require-for-product:gamma"); |
| 200 | ParseRequirementLineTestMalformed("require-for-product:gamma product"); |
| 201 | |
| 202 | // No spaces allowed before between require-for-product and :. |
| 203 | ParseRequirementLineTestMalformed("require-for-product :"); |
| 204 | } |
David Anderson | ada8718 | 2023-02-08 22:06:56 -0800 | [diff] [blame] | 205 | |
Dmitrii Merkurev | cdbfa7a | 2023-03-01 23:50:05 +0000 | [diff] [blame^] | 206 | static void ParseNetworkSerialTest(const std::string& description, const std::string& serial, |
| 207 | const std::string& expected_address, |
| 208 | const Socket::Protocol expected_protocol, |
| 209 | const int expected_port) { |
| 210 | const Result<NetworkSerial, FastbootError> parsed = ParseNetworkSerial(serial); |
| 211 | |
| 212 | ASSERT_RESULT_OK(parsed) << description; |
| 213 | |
| 214 | const NetworkSerial network_serial = parsed.value(); |
| 215 | EXPECT_EQ(network_serial.address, expected_address) << description; |
| 216 | EXPECT_EQ(network_serial.protocol, expected_protocol) << description; |
| 217 | EXPECT_EQ(network_serial.port, expected_port) << description; |
| 218 | } |
| 219 | |
| 220 | static void ParseNetworkSerialNegativeTest(const std::string& description, |
| 221 | const std::string& serial, |
| 222 | const FastbootError::Type expected_error) { |
| 223 | const Result<NetworkSerial, FastbootError> parsed = ParseNetworkSerial(serial); |
| 224 | |
| 225 | EXPECT_FALSE(parsed.ok()) << description; |
| 226 | EXPECT_EQ(parsed.error().code(), expected_error) << description; |
| 227 | } |
| 228 | |
| 229 | TEST(FastBoot, ParseNetworkSerial) { |
| 230 | ParseNetworkSerialTest("tcp IPv4 parsed", "tcp:192.168.1.0", "192.168.1.0", |
| 231 | Socket::Protocol::kTcp, 5554); |
| 232 | |
| 233 | ParseNetworkSerialTest("udp IPv4 parsed", "udp:192.168.1.0", "192.168.1.0", |
| 234 | Socket::Protocol::kUdp, 5554); |
| 235 | |
| 236 | ParseNetworkSerialTest("port parsed", "udp:192.168.1.0:9999", "192.168.1.0", |
| 237 | Socket::Protocol::kUdp, 9999); |
| 238 | |
| 239 | ParseNetworkSerialTest("IPv6 parsed", "tcp:2001:db8:3333:4444:5555:6666:7777:8888", |
| 240 | "2001:db8:3333:4444:5555:6666:7777:8888", Socket::Protocol::kTcp, 5554); |
| 241 | |
| 242 | ParseNetworkSerialTest("empty IPv6 parsed", "tcp:::", "::", Socket::Protocol::kTcp, 5554); |
| 243 | |
| 244 | ParseNetworkSerialNegativeTest("wrong prefix", "tcpa:192.168.1.0", |
| 245 | FastbootError::Type::NETWORK_SERIAL_WRONG_PREFIX); |
| 246 | |
| 247 | ParseNetworkSerialNegativeTest("no prefix", "192.168.1.0", |
| 248 | FastbootError::Type::NETWORK_SERIAL_WRONG_PREFIX); |
| 249 | |
| 250 | ParseNetworkSerialNegativeTest("wrong port", "tcp:192.168.1.0:-1", |
| 251 | FastbootError::Type::NETWORK_SERIAL_WRONG_ADDRESS); |
| 252 | } |
| 253 | |
David Anderson | ada8718 | 2023-02-08 22:06:56 -0800 | [diff] [blame] | 254 | int main(int argc, char* argv[]) { |
| 255 | ::testing::InitGoogleTest(&argc, argv); |
| 256 | android::base::InitLogging(argv); |
| 257 | android::base::SetMinimumLogSeverity(android::base::VERBOSE); |
| 258 | |
| 259 | return RUN_ALL_TESTS(); |
| 260 | } |