blob: b30b2d2ad60763ee450a5170d7c0281a5795910f [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) {
Sen Jiang0779a152018-07-02 17:34:56 -070062 test_utils::ScopedTempFile file;
63 EXPECT_TRUE(utils::WriteFile(file.path().c_str(), "hello", 5));
Alex Deymo335516c2016-11-28 13:37:06 -080064
65 brillo::Blob readback;
Sen Jiang0779a152018-07-02 17:34:56 -070066 EXPECT_TRUE(utils::ReadFile(file.path().c_str(), &readback));
Alex Deymo335516c2016-11-28 13:37:06 -080067 EXPECT_EQ("hello", string(readback.begin(), readback.end()));
68}
69
adlr@google.com3defe6a2009-12-04 20:57:17 +000070TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070071 brillo::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +000072 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
73}
74
Darin Petkov8e447e02013-04-16 16:23:50 +020075TEST(UtilsTest, ReadFileChunk) {
Sen Jiang0779a152018-07-02 17:34:56 -070076 test_utils::ScopedTempFile file;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070077 brillo::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +020078 const size_t kSize = 1024 * 1024;
79 for (size_t i = 0; i < kSize; i++) {
80 data.push_back(i % 255);
81 }
Sen Jiang0779a152018-07-02 17:34:56 -070082 EXPECT_TRUE(test_utils::WriteFileVector(file.path(), data));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070083 brillo::Blob in_data;
Sen Jiang0779a152018-07-02 17:34:56 -070084 EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), kSize, 10, &in_data));
Darin Petkov8e447e02013-04-16 16:23:50 +020085 EXPECT_TRUE(in_data.empty());
Sen Jiang0779a152018-07-02 17:34:56 -070086 EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 0, -1, &in_data));
87 EXPECT_EQ(data, in_data);
Darin Petkov8e447e02013-04-16 16:23:50 +020088 in_data.clear();
Sen Jiang0779a152018-07-02 17:34:56 -070089 EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 10, 20, &in_data));
90 EXPECT_EQ(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20), in_data);
Darin Petkov8e447e02013-04-16 16:23:50 +020091}
92
adlr@google.com3defe6a2009-12-04 20:57:17 +000093TEST(UtilsTest, ErrnoNumberAsStringTest) {
94 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
95}
96
Darin Petkov002b2fe2010-11-22 13:53:22 -080097TEST(UtilsTest, IsSymlinkTest) {
Sen Jiang371615b2016-04-13 15:54:29 -070098 base::ScopedTempDir temp_dir;
99 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900100 string temp_file = temp_dir.GetPath().Append("temp-file").value();
Darin Petkov002b2fe2010-11-22 13:53:22 -0800101 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900102 string temp_symlink = temp_dir.GetPath().Append("temp-symlink").value();
Darin Petkov002b2fe2010-11-22 13:53:22 -0800103 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900104 EXPECT_FALSE(utils::IsSymlink(temp_dir.GetPath().value().c_str()));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800105 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
106 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
107 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800108}
109
Alex Deymo763e7db2015-08-27 21:08:08 -0700110TEST(UtilsTest, SplitPartitionNameTest) {
111 string disk;
112 int part_num;
Darin Petkovf74eb652010-08-04 12:08:38 -0700113
Alex Deymo763e7db2015-08-27 21:08:08 -0700114 EXPECT_TRUE(utils::SplitPartitionName("/dev/sda3", &disk, &part_num));
115 EXPECT_EQ("/dev/sda", disk);
116 EXPECT_EQ(3, part_num);
Darin Petkovf74eb652010-08-04 12:08:38 -0700117
Alex Deymo763e7db2015-08-27 21:08:08 -0700118 EXPECT_TRUE(utils::SplitPartitionName("/dev/sdp1234", &disk, &part_num));
119 EXPECT_EQ("/dev/sdp", disk);
120 EXPECT_EQ(1234, part_num);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700121
Alex Deymo763e7db2015-08-27 21:08:08 -0700122 EXPECT_TRUE(utils::SplitPartitionName("/dev/mmcblk0p3", &disk, &part_num));
123 EXPECT_EQ("/dev/mmcblk0", disk);
124 EXPECT_EQ(3, part_num);
125
126 EXPECT_TRUE(utils::SplitPartitionName("/dev/ubiblock3_2", &disk, &part_num));
127 EXPECT_EQ("/dev/ubiblock", disk);
128 EXPECT_EQ(3, part_num);
129
130 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10", &disk, &part_num));
131 EXPECT_EQ("/dev/loop", disk);
132 EXPECT_EQ(10, part_num);
133
134 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11", &disk, &part_num));
135 EXPECT_EQ("/dev/loop28", disk);
136 EXPECT_EQ(11, part_num);
137
138 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10_0", &disk, &part_num));
139 EXPECT_EQ("/dev/loop", disk);
140 EXPECT_EQ(10, part_num);
141
142 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11_0", &disk, &part_num));
143 EXPECT_EQ("/dev/loop28", disk);
144 EXPECT_EQ(11, part_num);
145
146 EXPECT_FALSE(utils::SplitPartitionName("/dev/mmcblk0p", &disk, &part_num));
147 EXPECT_FALSE(utils::SplitPartitionName("/dev/sda", &disk, &part_num));
148 EXPECT_FALSE(utils::SplitPartitionName("/dev/foo/bar", &disk, &part_num));
149 EXPECT_FALSE(utils::SplitPartitionName("/", &disk, &part_num));
150 EXPECT_FALSE(utils::SplitPartitionName("", &disk, &part_num));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700151}
152
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700153TEST(UtilsTest, MakePartitionNameTest) {
154 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
155 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
156 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
157 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
158 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
159 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800160 EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5));
161 EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4));
162 EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3));
163 EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2));
164 EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1));
165}
166
167TEST(UtilsTest, MakePartitionNameForMountTest) {
168 EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4"));
169 EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123"));
170 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2"));
171 EXPECT_EQ("/dev/mmcblk0p2",
172 utils::MakePartitionNameForMount("/dev/mmcblk0p2"));
173 EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0"));
174 EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8"));
175 EXPECT_EQ("/dev/loop12p2",
176 utils::MakePartitionNameForMount("/dev/loop12p2"));
177 EXPECT_EQ("/dev/ubiblock5_0",
178 utils::MakePartitionNameForMount("/dev/ubiblock5_0"));
179 EXPECT_EQ("/dev/mtd4",
180 utils::MakePartitionNameForMount("/dev/ubi4_0"));
181 EXPECT_EQ("/dev/ubiblock3_0",
182 utils::MakePartitionNameForMount("/dev/ubiblock3"));
183 EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2"));
184 EXPECT_EQ("/dev/ubi1_0",
185 utils::MakePartitionNameForMount("/dev/ubiblock1"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700186}
187
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700188TEST(UtilsTest, FuzzIntTest) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800189 static const uint32_t kRanges[] = { 0, 1, 2, 20 };
190 for (uint32_t range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700191 const int kValue = 50;
192 for (int tries = 0; tries < 100; ++tries) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800193 uint32_t value = utils::FuzzInt(kValue, range);
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700194 EXPECT_GE(value, kValue - range / 2);
195 EXPECT_LE(value, kValue + range - range / 2);
196 }
197 }
198}
199
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800200namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700201void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700202 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800203 test_utils::ScopedTempFile file;
Alex Deymobffa0602016-02-12 17:16:29 -0800204 ASSERT_TRUE(utils::WriteFile(file.path().c_str(),
Alex Deymo032e7722014-03-25 17:53:56 -0700205 reinterpret_cast<const char*>(contents.data()),
206 contents.size()));
Alex Deymobffa0602016-02-12 17:16:29 -0800207 EXPECT_EQ(expected, utils::GetFileFormat(file.path()));
Alex Deymo032e7722014-03-25 17:53:56 -0700208}
Alex Deymo10875d92014-11-10 21:52:57 -0800209} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700210
211TEST(UtilsTest, GetFileFormatTest) {
212 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700213 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
214 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700215
216 // Real tests from cros_installer on different boards.
217 // ELF 32-bit LSB executable, Intel 80386
218 GetFileFormatTester(
219 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700220 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
221 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
223 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700224
Alex Deymoc1711e22014-08-08 13:16:23 -0700225 // ELF 32-bit LSB executable, MIPS
226 GetFileFormatTester(
227 "ELF 32-bit little-endian mips",
228 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
231 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
232
Alex Deymo032e7722014-03-25 17:53:56 -0700233 // ELF 32-bit LSB executable, ARM
234 GetFileFormatTester(
235 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700236 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
239 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700240
241 // ELF 64-bit LSB executable, x86-64
242 GetFileFormatTester(
243 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700244 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
247 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700248}
249
David Zeuthen674c3182013-04-18 14:05:20 -0700250TEST(UtilsTest, FormatTimeDeltaTest) {
251 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
252 // which is not localized) so we only need to test the C locale
253 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
254 "0.1s");
255 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
256 "0s");
257 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
258 "1s");
259 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
260 "59s");
261 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
262 "1m0s");
263 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
264 "1m1s");
265 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
266 "1m30s");
267 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
268 "20m5s");
269 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
270 "1h0m0s");
271 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
272 "1h0m1s");
273 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
274 "1h1m1s");
275 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
276 "2h1m1s");
277 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
278 "1d0h0m0s");
279 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
280 "1d0h0m1s");
281 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
282 "2d7h33m20s");
283 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
284 base::TimeDelta::FromMilliseconds(1)),
285 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400286 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
287 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700288}
289
David Zeuthen639aa362014-02-03 16:23:44 -0800290TEST(UtilsTest, ConvertToOmahaInstallDate) {
291 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
292 // Monday. In Unix time, this point in time is easily obtained via
293 // the date(1) command like this:
294 //
295 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
296 const time_t omaha_epoch = 1167638400;
297 int value;
298
299 // Points in time *on and after* the Omaha epoch should not fail.
300 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
301 base::Time::FromTimeT(omaha_epoch), &value));
302 EXPECT_GE(value, 0);
303
304 // Anything before the Omaha epoch should fail. We test it for two points.
305 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
306 base::Time::FromTimeT(omaha_epoch - 1), &value));
307 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
308 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
309
310 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
311 // on Jan 8, 2007 0:00 PST.
312 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
313 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
314 EXPECT_EQ(value, 0);
315 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
316 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
317 EXPECT_EQ(value, 7);
318
319 // Check a couple of more values.
320 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
321 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
322 EXPECT_EQ(value, 7);
323 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
324 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
325 EXPECT_EQ(value, 14);
326 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
327 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
328 EXPECT_EQ(value, 21);
329 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
330 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
331 EXPECT_EQ(value, 28);
332
333 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
334 // where the Omaha InstallDate jumps 7 days. Its unix time is
335 // 1180940400. Notably, this is a point in time where Daylight
336 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
337 //
338 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
339 // ignores DST (as it's hard to implement in a thread-safe way using
340 // glibc, see comments in utils.h) we have to fudge by the DST
341 // offset which is one hour. Conveniently, if the function were
342 // someday modified to be DST aware, this test would have to be
343 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700344 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800345 const time_t fudge = 3600;
346 int value1, value2;
347 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
348 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
349 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
350 base::Time::FromTimeT(dst_time + fudge), &value2));
351 EXPECT_EQ(value1, value2 - 7);
352}
353
Allie Wood78750a42015-02-11 15:42:11 -0800354TEST(UtilsTest, GetMinorVersion) {
355 // Test GetMinorVersion by verifying that it parses the conf file and returns
356 // the correct value.
Allie Wood78750a42015-02-11 15:42:11 -0800357 uint32_t minor_version;
358
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700359 brillo::KeyValueStore store;
Alex Deymob42b98d2015-07-06 17:42:38 -0700360 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800361
Alex Deymob42b98d2015-07-06 17:42:38 -0700362 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n"));
363 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800364
Alex Deymob42b98d2015-07-06 17:42:38 -0700365 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n"));
366 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800367 EXPECT_EQ(123U, minor_version);
Allie Wood78750a42015-02-11 15:42:11 -0800368}
369
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800370static bool BoolMacroTestHelper() {
371 int i = 1;
372 unsigned int ui = 1;
373 bool b = 1;
374 std::unique_ptr<char> cptr(new char);
375
376 TEST_AND_RETURN_FALSE(i);
377 TEST_AND_RETURN_FALSE(ui);
378 TEST_AND_RETURN_FALSE(b);
379 TEST_AND_RETURN_FALSE(cptr);
380
381 TEST_AND_RETURN_FALSE_ERRNO(i);
382 TEST_AND_RETURN_FALSE_ERRNO(ui);
383 TEST_AND_RETURN_FALSE_ERRNO(b);
384 TEST_AND_RETURN_FALSE_ERRNO(cptr);
385
386 return true;
387}
388
389static void VoidMacroTestHelper(bool* ret) {
390 int i = 1;
391 unsigned int ui = 1;
392 bool b = 1;
393 std::unique_ptr<char> cptr(new char);
394
395 *ret = false;
396
397 TEST_AND_RETURN(i);
398 TEST_AND_RETURN(ui);
399 TEST_AND_RETURN(b);
400 TEST_AND_RETURN(cptr);
401
402 TEST_AND_RETURN_ERRNO(i);
403 TEST_AND_RETURN_ERRNO(ui);
404 TEST_AND_RETURN_ERRNO(b);
405 TEST_AND_RETURN_ERRNO(cptr);
406
407 *ret = true;
408}
409
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700410static void ExpectParseRollbackKeyVersion(const string& version,
411 uint16_t expected_high,
412 uint16_t expected_low) {
413 uint16_t actual_high;
414 uint16_t actual_low;
415 utils::ParseRollbackKeyVersion(version, &actual_high, &actual_low);
416 EXPECT_EQ(expected_high, actual_high);
417 EXPECT_EQ(expected_low, actual_low);
418}
419
420static void ExpectInvalidParseRollbackKeyVersion(const string& version) {
421 ExpectParseRollbackKeyVersion(version,
422 numeric_limits<uint16_t>::max(),
423 numeric_limits<uint16_t>::max());
424}
425
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800426TEST(UtilsTest, TestMacros) {
427 bool void_test = false;
428 VoidMacroTestHelper(&void_test);
429 EXPECT_TRUE(void_test);
430
431 EXPECT_TRUE(BoolMacroTestHelper());
432}
433
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700434TEST(UtilsTest, RunAsRootUnmountFilesystemFailureTest) {
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700435 EXPECT_FALSE(utils::UnmountFilesystem("/path/to/non-existing-dir"));
436}
437
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700438TEST(UtilsTest, RunAsRootUnmountFilesystemBusyFailureTest) {
Sen Jiang0779a152018-07-02 17:34:56 -0700439 test_utils::ScopedTempFile tmp_image("img.XXXXXX");
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700440
441 EXPECT_TRUE(base::CopyFile(
442 test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
Sen Jiang0779a152018-07-02 17:34:56 -0700443 base::FilePath(tmp_image.path())));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700444
445 base::ScopedTempDir mnt_dir;
446 EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
447
448 string loop_dev;
449 test_utils::ScopedLoopbackDeviceBinder loop_binder(
Sen Jiang0779a152018-07-02 17:34:56 -0700450 tmp_image.path(), true, &loop_dev);
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700451
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900452 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700453 // This is the actual test part. While we hold a file descriptor open for the
454 // mounted filesystem, umount should still succeed.
455 EXPECT_TRUE(utils::MountFilesystem(
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900456 loop_dev, mnt_dir.GetPath().value(), MS_RDONLY, "ext4", ""));
Alex Deymof4116502017-03-22 17:00:31 -0700457 // Verify the directory is a mount point now.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900458 EXPECT_TRUE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700459
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900460 string target_file = mnt_dir.GetPath().Append("empty-file").value();
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700461 int fd = HANDLE_EINTR(open(target_file.c_str(), O_RDONLY));
462 EXPECT_GE(fd, 0);
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900463 EXPECT_TRUE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700464 // The filesystem should be already unmounted at this point.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900465 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700466 IGNORE_EINTR(close(fd));
467 // The filesystem was already unmounted so this call should fail.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900468 EXPECT_FALSE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700469}
470
Alex Deymof4116502017-03-22 17:00:31 -0700471TEST(UtilsTest, IsMountpointTest) {
472 EXPECT_TRUE(utils::IsMountpoint("/"));
473 EXPECT_FALSE(utils::IsMountpoint("/path/to/nowhere"));
474
475 base::ScopedTempDir mnt_dir;
476 EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900477 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700478
Sen Jiang0779a152018-07-02 17:34:56 -0700479 test_utils::ScopedTempFile file;
480 EXPECT_FALSE(utils::IsMountpoint(file.path()));
Alex Deymof4116502017-03-22 17:00:31 -0700481}
482
Marton Hunyadya0302682018-05-16 18:52:13 +0200483TEST(UtilsTest, VersionPrefix) {
484 EXPECT_EQ(10575, utils::VersionPrefix("10575.39."));
485 EXPECT_EQ(10575, utils::VersionPrefix("10575.39"));
486 EXPECT_EQ(10575, utils::VersionPrefix("10575.x"));
487 EXPECT_EQ(10575, utils::VersionPrefix("10575."));
488 EXPECT_EQ(10575, utils::VersionPrefix("10575"));
489 EXPECT_EQ(0, utils::VersionPrefix(""));
490 EXPECT_EQ(-1, utils::VersionPrefix("x"));
491 EXPECT_EQ(-1, utils::VersionPrefix("1x"));
492 EXPECT_EQ(-1, utils::VersionPrefix("x.1"));
493}
494
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700495TEST(UtilsTest, ParseDottedVersion) {
496 // Valid case.
497 ExpectParseRollbackKeyVersion("2.3", 2, 3);
498 ExpectParseRollbackKeyVersion("65535.65535", 65535, 65535);
499
500 // Zero is technically allowed but never actually used.
501 ExpectParseRollbackKeyVersion("0.0", 0, 0);
502
503 // Invalid cases.
504 ExpectInvalidParseRollbackKeyVersion("");
505 ExpectInvalidParseRollbackKeyVersion("2");
506 ExpectInvalidParseRollbackKeyVersion("2.");
507 ExpectInvalidParseRollbackKeyVersion(".2");
508 ExpectInvalidParseRollbackKeyVersion("2.2.");
509 ExpectInvalidParseRollbackKeyVersion("2.2.3");
510 ExpectInvalidParseRollbackKeyVersion(".2.2");
511 ExpectInvalidParseRollbackKeyVersion("a.b");
512 ExpectInvalidParseRollbackKeyVersion("1.b");
513 ExpectInvalidParseRollbackKeyVersion("a.2");
514 ExpectInvalidParseRollbackKeyVersion("65536.65536");
515 ExpectInvalidParseRollbackKeyVersion("99999.99999");
516 ExpectInvalidParseRollbackKeyVersion("99999.1");
517 ExpectInvalidParseRollbackKeyVersion("1.99999");
518}
519
adlr@google.com3defe6a2009-12-04 20:57:17 +0000520} // namespace chromeos_update_engine