blob: bb39770d4ab4af904ce36fd06fd9c98ac3e2575c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/utils.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080018
Alex Deymo5ba93ad2016-08-22 19:51:59 -070019#include <fcntl.h>
Ben Chan9abb7632014-08-07 00:10:53 -070020#include <stdint.h>
Alex Deymo5ba93ad2016-08-22 19:51:59 -070021#include <sys/mount.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000022#include <sys/stat.h>
23#include <sys/types.h>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070024
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070025#include <limits>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080026#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000027#include <vector>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070028
Alex Deymoc1711e22014-08-08 13:16:23 -070029#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080030#include <base/files/file_util.h>
Sen Jiang371615b2016-04-13 15:54:29 -070031#include <base/files/scoped_temp_dir.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070032#include <gtest/gtest.h>
33
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000035
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070036using std::numeric_limits;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080037using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000038using std::vector;
39
40namespace chromeos_update_engine {
41
42class UtilsTest : public ::testing::Test { };
43
Chris Sosac1972482013-04-30 22:31:10 -070044TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070045 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070046 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
47 EXPECT_EQ("123456", utils::ParseECVersion(
48 "b=1231a fw_version=123456 a=fasd2"));
49 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
50 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070051 "vendor=\"sam\" fw_version=\"00VFA616\""));
52
53 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070054 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070055}
56
Alex Deymo335516c2016-11-28 13:37:06 -080057TEST(UtilsTest, WriteFileOpenFailure) {
58 EXPECT_FALSE(utils::WriteFile("/this/doesn't/exist", "hello", 5));
59}
60
61TEST(UtilsTest, WriteFileReadFile) {
62 base::FilePath file;
63 EXPECT_TRUE(base::CreateTemporaryFile(&file));
64 ScopedPathUnlinker unlinker(file.value());
65 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), "hello", 5));
66
67 brillo::Blob readback;
68 EXPECT_TRUE(utils::ReadFile(file.value().c_str(), &readback));
69 EXPECT_EQ("hello", string(readback.begin(), readback.end()));
70}
71
adlr@google.com3defe6a2009-12-04 20:57:17 +000072TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070073 brillo::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +000074 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
75}
76
Darin Petkov8e447e02013-04-16 16:23:50 +020077TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070078 base::FilePath file;
79 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +020080 ScopedPathUnlinker unlinker(file.value());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070081 brillo::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +020082 const size_t kSize = 1024 * 1024;
83 for (size_t i = 0; i < kSize; i++) {
84 data.push_back(i % 255);
85 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080086 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070087 brillo::Blob in_data;
Darin Petkov8e447e02013-04-16 16:23:50 +020088 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
89 EXPECT_TRUE(in_data.empty());
90 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
91 EXPECT_TRUE(data == in_data);
92 in_data.clear();
93 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070094 EXPECT_TRUE(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
Darin Petkov8e447e02013-04-16 16:23:50 +020095 in_data);
96}
97
adlr@google.com3defe6a2009-12-04 20:57:17 +000098TEST(UtilsTest, ErrnoNumberAsStringTest) {
99 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
100}
101
Darin Petkov002b2fe2010-11-22 13:53:22 -0800102TEST(UtilsTest, IsSymlinkTest) {
Sen Jiang371615b2016-04-13 15:54:29 -0700103 base::ScopedTempDir temp_dir;
104 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900105 string temp_file = temp_dir.GetPath().Append("temp-file").value();
Darin Petkov002b2fe2010-11-22 13:53:22 -0800106 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900107 string temp_symlink = temp_dir.GetPath().Append("temp-symlink").value();
Darin Petkov002b2fe2010-11-22 13:53:22 -0800108 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900109 EXPECT_FALSE(utils::IsSymlink(temp_dir.GetPath().value().c_str()));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800110 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
111 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
112 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800113}
114
Alex Deymo763e7db2015-08-27 21:08:08 -0700115TEST(UtilsTest, SplitPartitionNameTest) {
116 string disk;
117 int part_num;
Darin Petkovf74eb652010-08-04 12:08:38 -0700118
Alex Deymo763e7db2015-08-27 21:08:08 -0700119 EXPECT_TRUE(utils::SplitPartitionName("/dev/sda3", &disk, &part_num));
120 EXPECT_EQ("/dev/sda", disk);
121 EXPECT_EQ(3, part_num);
Darin Petkovf74eb652010-08-04 12:08:38 -0700122
Alex Deymo763e7db2015-08-27 21:08:08 -0700123 EXPECT_TRUE(utils::SplitPartitionName("/dev/sdp1234", &disk, &part_num));
124 EXPECT_EQ("/dev/sdp", disk);
125 EXPECT_EQ(1234, part_num);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700126
Alex Deymo763e7db2015-08-27 21:08:08 -0700127 EXPECT_TRUE(utils::SplitPartitionName("/dev/mmcblk0p3", &disk, &part_num));
128 EXPECT_EQ("/dev/mmcblk0", disk);
129 EXPECT_EQ(3, part_num);
130
131 EXPECT_TRUE(utils::SplitPartitionName("/dev/ubiblock3_2", &disk, &part_num));
132 EXPECT_EQ("/dev/ubiblock", disk);
133 EXPECT_EQ(3, part_num);
134
135 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10", &disk, &part_num));
136 EXPECT_EQ("/dev/loop", disk);
137 EXPECT_EQ(10, part_num);
138
139 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11", &disk, &part_num));
140 EXPECT_EQ("/dev/loop28", disk);
141 EXPECT_EQ(11, part_num);
142
143 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10_0", &disk, &part_num));
144 EXPECT_EQ("/dev/loop", disk);
145 EXPECT_EQ(10, part_num);
146
147 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11_0", &disk, &part_num));
148 EXPECT_EQ("/dev/loop28", disk);
149 EXPECT_EQ(11, part_num);
150
151 EXPECT_FALSE(utils::SplitPartitionName("/dev/mmcblk0p", &disk, &part_num));
152 EXPECT_FALSE(utils::SplitPartitionName("/dev/sda", &disk, &part_num));
153 EXPECT_FALSE(utils::SplitPartitionName("/dev/foo/bar", &disk, &part_num));
154 EXPECT_FALSE(utils::SplitPartitionName("/", &disk, &part_num));
155 EXPECT_FALSE(utils::SplitPartitionName("", &disk, &part_num));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700156}
157
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700158TEST(UtilsTest, MakePartitionNameTest) {
159 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
160 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
161 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
162 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
163 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
164 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800165 EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5));
166 EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4));
167 EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3));
168 EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2));
169 EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1));
170}
171
172TEST(UtilsTest, MakePartitionNameForMountTest) {
173 EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4"));
174 EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123"));
175 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2"));
176 EXPECT_EQ("/dev/mmcblk0p2",
177 utils::MakePartitionNameForMount("/dev/mmcblk0p2"));
178 EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0"));
179 EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8"));
180 EXPECT_EQ("/dev/loop12p2",
181 utils::MakePartitionNameForMount("/dev/loop12p2"));
182 EXPECT_EQ("/dev/ubiblock5_0",
183 utils::MakePartitionNameForMount("/dev/ubiblock5_0"));
184 EXPECT_EQ("/dev/mtd4",
185 utils::MakePartitionNameForMount("/dev/ubi4_0"));
186 EXPECT_EQ("/dev/ubiblock3_0",
187 utils::MakePartitionNameForMount("/dev/ubiblock3"));
188 EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2"));
189 EXPECT_EQ("/dev/ubi1_0",
190 utils::MakePartitionNameForMount("/dev/ubiblock1"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700191}
192
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700193TEST(UtilsTest, FuzzIntTest) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800194 static const uint32_t kRanges[] = { 0, 1, 2, 20 };
195 for (uint32_t range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700196 const int kValue = 50;
197 for (int tries = 0; tries < 100; ++tries) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800198 uint32_t value = utils::FuzzInt(kValue, range);
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700199 EXPECT_GE(value, kValue - range / 2);
200 EXPECT_LE(value, kValue + range - range / 2);
201 }
202 }
203}
204
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800205namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700206void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700207 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800208 test_utils::ScopedTempFile file;
Alex Deymobffa0602016-02-12 17:16:29 -0800209 ASSERT_TRUE(utils::WriteFile(file.path().c_str(),
Alex Deymo032e7722014-03-25 17:53:56 -0700210 reinterpret_cast<const char*>(contents.data()),
211 contents.size()));
Alex Deymobffa0602016-02-12 17:16:29 -0800212 EXPECT_EQ(expected, utils::GetFileFormat(file.path()));
Alex Deymo032e7722014-03-25 17:53:56 -0700213}
Alex Deymo10875d92014-11-10 21:52:57 -0800214} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700215
216TEST(UtilsTest, GetFileFormatTest) {
217 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700218 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
219 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700220
221 // Real tests from cros_installer on different boards.
222 // ELF 32-bit LSB executable, Intel 80386
223 GetFileFormatTester(
224 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700225 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
228 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700229
Alex Deymoc1711e22014-08-08 13:16:23 -0700230 // ELF 32-bit LSB executable, MIPS
231 GetFileFormatTester(
232 "ELF 32-bit little-endian mips",
233 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
236 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
237
Alex Deymo032e7722014-03-25 17:53:56 -0700238 // ELF 32-bit LSB executable, ARM
239 GetFileFormatTester(
240 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700241 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
244 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700245
246 // ELF 64-bit LSB executable, x86-64
247 GetFileFormatTester(
248 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700249 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
250 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
252 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700253}
254
David Zeuthen674c3182013-04-18 14:05:20 -0700255TEST(UtilsTest, FormatTimeDeltaTest) {
256 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
257 // which is not localized) so we only need to test the C locale
258 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
259 "0.1s");
260 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
261 "0s");
262 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
263 "1s");
264 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
265 "59s");
266 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
267 "1m0s");
268 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
269 "1m1s");
270 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
271 "1m30s");
272 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
273 "20m5s");
274 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
275 "1h0m0s");
276 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
277 "1h0m1s");
278 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
279 "1h1m1s");
280 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
281 "2h1m1s");
282 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
283 "1d0h0m0s");
284 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
285 "1d0h0m1s");
286 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
287 "2d7h33m20s");
288 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
289 base::TimeDelta::FromMilliseconds(1)),
290 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400291 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
292 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700293}
294
David Zeuthen27a48bc2013-08-06 12:06:29 -0700295TEST(UtilsTest, TimeFromStructTimespecTest) {
296 struct timespec ts;
297
298 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
299 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
300 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
301
302 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
303 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
304 .tv_nsec = 42 * 1000 * 1000};
305 base::Time::Exploded exploded = (base::Time::Exploded) {
306 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
307 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
Hidehiko Abeeab915e2018-09-23 16:54:06 +0900308 base::Time time;
309 EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time));
310 EXPECT_EQ(time, utils::TimeFromStructTimespec(&ts));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700311}
312
David Zeuthene7f89172013-10-31 10:21:04 -0700313TEST(UtilsTest, DecodeAndStoreBase64String) {
314 base::FilePath path;
315
316 // Ensure we return false on empty strings or invalid base64.
317 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
318 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
319
320 // Pass known base64 and check that it matches. This string was generated
321 // the following way:
322 //
323 // $ echo "Update Engine" | base64
324 // VXBkYXRlIEVuZ2luZQo=
325 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
326 &path));
327 ScopedPathUnlinker unlinker(path.value());
328 string expected_contents = "Update Engine\n";
329 string contents;
330 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
331 EXPECT_EQ(contents, expected_contents);
Alex Deymo5fe0c4e2016-02-16 18:46:24 -0800332 EXPECT_EQ(static_cast<off_t>(expected_contents.size()),
333 utils::FileSize(path.value()));
David Zeuthene7f89172013-10-31 10:21:04 -0700334}
335
David Zeuthen639aa362014-02-03 16:23:44 -0800336TEST(UtilsTest, ConvertToOmahaInstallDate) {
337 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
338 // Monday. In Unix time, this point in time is easily obtained via
339 // the date(1) command like this:
340 //
341 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
342 const time_t omaha_epoch = 1167638400;
343 int value;
344
345 // Points in time *on and after* the Omaha epoch should not fail.
346 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
347 base::Time::FromTimeT(omaha_epoch), &value));
348 EXPECT_GE(value, 0);
349
350 // Anything before the Omaha epoch should fail. We test it for two points.
351 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
352 base::Time::FromTimeT(omaha_epoch - 1), &value));
353 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
354 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
355
356 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
357 // on Jan 8, 2007 0:00 PST.
358 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
359 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
360 EXPECT_EQ(value, 0);
361 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
362 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
363 EXPECT_EQ(value, 7);
364
365 // Check a couple of more values.
366 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
367 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
368 EXPECT_EQ(value, 7);
369 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
370 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
371 EXPECT_EQ(value, 14);
372 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
373 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
374 EXPECT_EQ(value, 21);
375 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
376 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
377 EXPECT_EQ(value, 28);
378
379 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
380 // where the Omaha InstallDate jumps 7 days. Its unix time is
381 // 1180940400. Notably, this is a point in time where Daylight
382 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
383 //
384 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
385 // ignores DST (as it's hard to implement in a thread-safe way using
386 // glibc, see comments in utils.h) we have to fudge by the DST
387 // offset which is one hour. Conveniently, if the function were
388 // someday modified to be DST aware, this test would have to be
389 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700390 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800391 const time_t fudge = 3600;
392 int value1, value2;
393 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
394 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
395 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
396 base::Time::FromTimeT(dst_time + fudge), &value2));
397 EXPECT_EQ(value1, value2 - 7);
398}
399
Allie Wood78750a42015-02-11 15:42:11 -0800400TEST(UtilsTest, GetMinorVersion) {
401 // Test GetMinorVersion by verifying that it parses the conf file and returns
402 // the correct value.
Allie Wood78750a42015-02-11 15:42:11 -0800403 uint32_t minor_version;
404
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700405 brillo::KeyValueStore store;
Alex Deymob42b98d2015-07-06 17:42:38 -0700406 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800407
Alex Deymob42b98d2015-07-06 17:42:38 -0700408 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n"));
409 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800410
Alex Deymob42b98d2015-07-06 17:42:38 -0700411 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n"));
412 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800413 EXPECT_EQ(123U, minor_version);
Allie Wood78750a42015-02-11 15:42:11 -0800414}
415
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800416static bool BoolMacroTestHelper() {
417 int i = 1;
418 unsigned int ui = 1;
419 bool b = 1;
420 std::unique_ptr<char> cptr(new char);
421
422 TEST_AND_RETURN_FALSE(i);
423 TEST_AND_RETURN_FALSE(ui);
424 TEST_AND_RETURN_FALSE(b);
425 TEST_AND_RETURN_FALSE(cptr);
426
427 TEST_AND_RETURN_FALSE_ERRNO(i);
428 TEST_AND_RETURN_FALSE_ERRNO(ui);
429 TEST_AND_RETURN_FALSE_ERRNO(b);
430 TEST_AND_RETURN_FALSE_ERRNO(cptr);
431
432 return true;
433}
434
435static void VoidMacroTestHelper(bool* ret) {
436 int i = 1;
437 unsigned int ui = 1;
438 bool b = 1;
439 std::unique_ptr<char> cptr(new char);
440
441 *ret = false;
442
443 TEST_AND_RETURN(i);
444 TEST_AND_RETURN(ui);
445 TEST_AND_RETURN(b);
446 TEST_AND_RETURN(cptr);
447
448 TEST_AND_RETURN_ERRNO(i);
449 TEST_AND_RETURN_ERRNO(ui);
450 TEST_AND_RETURN_ERRNO(b);
451 TEST_AND_RETURN_ERRNO(cptr);
452
453 *ret = true;
454}
455
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700456static void ExpectParseRollbackKeyVersion(const string& version,
457 uint16_t expected_high,
458 uint16_t expected_low) {
459 uint16_t actual_high;
460 uint16_t actual_low;
461 utils::ParseRollbackKeyVersion(version, &actual_high, &actual_low);
462 EXPECT_EQ(expected_high, actual_high);
463 EXPECT_EQ(expected_low, actual_low);
464}
465
466static void ExpectInvalidParseRollbackKeyVersion(const string& version) {
467 ExpectParseRollbackKeyVersion(version,
468 numeric_limits<uint16_t>::max(),
469 numeric_limits<uint16_t>::max());
470}
471
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800472TEST(UtilsTest, TestMacros) {
473 bool void_test = false;
474 VoidMacroTestHelper(&void_test);
475 EXPECT_TRUE(void_test);
476
477 EXPECT_TRUE(BoolMacroTestHelper());
478}
479
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700480TEST(UtilsTest, RunAsRootUnmountFilesystemFailureTest) {
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700481 EXPECT_FALSE(utils::UnmountFilesystem("/path/to/non-existing-dir"));
482}
483
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700484TEST(UtilsTest, RunAsRootUnmountFilesystemBusyFailureTest) {
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700485 string tmp_image;
486 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &tmp_image, nullptr));
487 ScopedPathUnlinker tmp_image_unlinker(tmp_image);
488
489 EXPECT_TRUE(base::CopyFile(
490 test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
491 base::FilePath(tmp_image)));
492
493 base::ScopedTempDir mnt_dir;
494 EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
495
496 string loop_dev;
497 test_utils::ScopedLoopbackDeviceBinder loop_binder(
498 tmp_image, true, &loop_dev);
499
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900500 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700501 // This is the actual test part. While we hold a file descriptor open for the
502 // mounted filesystem, umount should still succeed.
503 EXPECT_TRUE(utils::MountFilesystem(
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900504 loop_dev, mnt_dir.GetPath().value(), MS_RDONLY, "ext4", ""));
Alex Deymof4116502017-03-22 17:00:31 -0700505 // Verify the directory is a mount point now.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900506 EXPECT_TRUE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700507
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900508 string target_file = mnt_dir.GetPath().Append("empty-file").value();
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700509 int fd = HANDLE_EINTR(open(target_file.c_str(), O_RDONLY));
510 EXPECT_GE(fd, 0);
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900511 EXPECT_TRUE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700512 // The filesystem should be already unmounted at this point.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900513 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700514 IGNORE_EINTR(close(fd));
515 // The filesystem was already unmounted so this call should fail.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900516 EXPECT_FALSE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700517}
518
Alex Deymof4116502017-03-22 17:00:31 -0700519TEST(UtilsTest, IsMountpointTest) {
520 EXPECT_TRUE(utils::IsMountpoint("/"));
521 EXPECT_FALSE(utils::IsMountpoint("/path/to/nowhere"));
522
523 base::ScopedTempDir mnt_dir;
524 EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900525 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700526
527 base::FilePath file;
528 EXPECT_TRUE(base::CreateTemporaryFile(&file));
529 ScopedPathUnlinker unlinker(file.value());
530 EXPECT_FALSE(utils::IsMountpoint(file.value()));
531}
532
Marton Hunyadya0302682018-05-16 18:52:13 +0200533TEST(UtilsTest, VersionPrefix) {
534 EXPECT_EQ(10575, utils::VersionPrefix("10575.39."));
535 EXPECT_EQ(10575, utils::VersionPrefix("10575.39"));
536 EXPECT_EQ(10575, utils::VersionPrefix("10575.x"));
537 EXPECT_EQ(10575, utils::VersionPrefix("10575."));
538 EXPECT_EQ(10575, utils::VersionPrefix("10575"));
539 EXPECT_EQ(0, utils::VersionPrefix(""));
540 EXPECT_EQ(-1, utils::VersionPrefix("x"));
541 EXPECT_EQ(-1, utils::VersionPrefix("1x"));
542 EXPECT_EQ(-1, utils::VersionPrefix("x.1"));
543}
544
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700545TEST(UtilsTest, ParseDottedVersion) {
546 // Valid case.
547 ExpectParseRollbackKeyVersion("2.3", 2, 3);
548 ExpectParseRollbackKeyVersion("65535.65535", 65535, 65535);
549
550 // Zero is technically allowed but never actually used.
551 ExpectParseRollbackKeyVersion("0.0", 0, 0);
552
553 // Invalid cases.
554 ExpectInvalidParseRollbackKeyVersion("");
555 ExpectInvalidParseRollbackKeyVersion("2");
556 ExpectInvalidParseRollbackKeyVersion("2.");
557 ExpectInvalidParseRollbackKeyVersion(".2");
558 ExpectInvalidParseRollbackKeyVersion("2.2.");
559 ExpectInvalidParseRollbackKeyVersion("2.2.3");
560 ExpectInvalidParseRollbackKeyVersion(".2.2");
561 ExpectInvalidParseRollbackKeyVersion("a.b");
562 ExpectInvalidParseRollbackKeyVersion("1.b");
563 ExpectInvalidParseRollbackKeyVersion("a.2");
564 ExpectInvalidParseRollbackKeyVersion("65536.65536");
565 ExpectInvalidParseRollbackKeyVersion("99999.99999");
566 ExpectInvalidParseRollbackKeyVersion("99999.1");
567 ExpectInvalidParseRollbackKeyVersion("1.99999");
568}
569
adlr@google.com3defe6a2009-12-04 20:57:17 +0000570} // namespace chromeos_update_engine