blob: 739f29810426078bdfb82d288550ac716c43a2cd [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
Amin Hassanib2689592019-01-13 17:04:28 -080042class UtilsTest : public ::testing::Test {};
adlr@google.com3defe6a2009-12-04 20:57:17 +000043
Alex Deymo335516c2016-11-28 13:37:06 -080044TEST(UtilsTest, WriteFileOpenFailure) {
45 EXPECT_FALSE(utils::WriteFile("/this/doesn't/exist", "hello", 5));
46}
47
48TEST(UtilsTest, WriteFileReadFile) {
Amin Hassanied03b442020-10-26 17:21:29 -070049 ScopedTempFile file;
Sen Jiang0779a152018-07-02 17:34:56 -070050 EXPECT_TRUE(utils::WriteFile(file.path().c_str(), "hello", 5));
Alex Deymo335516c2016-11-28 13:37:06 -080051
52 brillo::Blob readback;
Sen Jiang0779a152018-07-02 17:34:56 -070053 EXPECT_TRUE(utils::ReadFile(file.path().c_str(), &readback));
Alex Deymo335516c2016-11-28 13:37:06 -080054 EXPECT_EQ("hello", string(readback.begin(), readback.end()));
55}
56
adlr@google.com3defe6a2009-12-04 20:57:17 +000057TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070058 brillo::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +000059 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
60}
61
Darin Petkov8e447e02013-04-16 16:23:50 +020062TEST(UtilsTest, ReadFileChunk) {
Amin Hassanied03b442020-10-26 17:21:29 -070063 ScopedTempFile file;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070064 brillo::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +020065 const size_t kSize = 1024 * 1024;
66 for (size_t i = 0; i < kSize; i++) {
67 data.push_back(i % 255);
68 }
Sen Jiang0779a152018-07-02 17:34:56 -070069 EXPECT_TRUE(test_utils::WriteFileVector(file.path(), data));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070070 brillo::Blob in_data;
Sen Jiang0779a152018-07-02 17:34:56 -070071 EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), kSize, 10, &in_data));
Darin Petkov8e447e02013-04-16 16:23:50 +020072 EXPECT_TRUE(in_data.empty());
Sen Jiang0779a152018-07-02 17:34:56 -070073 EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 0, -1, &in_data));
74 EXPECT_EQ(data, in_data);
Darin Petkov8e447e02013-04-16 16:23:50 +020075 in_data.clear();
Sen Jiang0779a152018-07-02 17:34:56 -070076 EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 10, 20, &in_data));
77 EXPECT_EQ(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20), in_data);
Darin Petkov8e447e02013-04-16 16:23:50 +020078}
79
Darin Petkov002b2fe2010-11-22 13:53:22 -080080TEST(UtilsTest, IsSymlinkTest) {
Sen Jiang371615b2016-04-13 15:54:29 -070081 base::ScopedTempDir temp_dir;
82 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090083 string temp_file = temp_dir.GetPath().Append("temp-file").value();
Darin Petkov002b2fe2010-11-22 13:53:22 -080084 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090085 string temp_symlink = temp_dir.GetPath().Append("temp-symlink").value();
Darin Petkov002b2fe2010-11-22 13:53:22 -080086 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090087 EXPECT_FALSE(utils::IsSymlink(temp_dir.GetPath().value().c_str()));
Darin Petkov002b2fe2010-11-22 13:53:22 -080088 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
89 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
90 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Darin Petkov002b2fe2010-11-22 13:53:22 -080091}
92
Alex Deymo763e7db2015-08-27 21:08:08 -070093TEST(UtilsTest, SplitPartitionNameTest) {
94 string disk;
95 int part_num;
Darin Petkovf74eb652010-08-04 12:08:38 -070096
Alex Deymo763e7db2015-08-27 21:08:08 -070097 EXPECT_TRUE(utils::SplitPartitionName("/dev/sda3", &disk, &part_num));
98 EXPECT_EQ("/dev/sda", disk);
99 EXPECT_EQ(3, part_num);
Darin Petkovf74eb652010-08-04 12:08:38 -0700100
Alex Deymo763e7db2015-08-27 21:08:08 -0700101 EXPECT_TRUE(utils::SplitPartitionName("/dev/sdp1234", &disk, &part_num));
102 EXPECT_EQ("/dev/sdp", disk);
103 EXPECT_EQ(1234, part_num);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700104
Alex Deymo763e7db2015-08-27 21:08:08 -0700105 EXPECT_TRUE(utils::SplitPartitionName("/dev/mmcblk0p3", &disk, &part_num));
106 EXPECT_EQ("/dev/mmcblk0", disk);
107 EXPECT_EQ(3, part_num);
108
Alex Deymo763e7db2015-08-27 21:08:08 -0700109 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10", &disk, &part_num));
110 EXPECT_EQ("/dev/loop", disk);
111 EXPECT_EQ(10, part_num);
112
113 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11", &disk, &part_num));
114 EXPECT_EQ("/dev/loop28", disk);
115 EXPECT_EQ(11, part_num);
116
Alex Deymo763e7db2015-08-27 21:08:08 -0700117 EXPECT_FALSE(utils::SplitPartitionName("/dev/mmcblk0p", &disk, &part_num));
118 EXPECT_FALSE(utils::SplitPartitionName("/dev/sda", &disk, &part_num));
119 EXPECT_FALSE(utils::SplitPartitionName("/dev/foo/bar", &disk, &part_num));
120 EXPECT_FALSE(utils::SplitPartitionName("/", &disk, &part_num));
121 EXPECT_FALSE(utils::SplitPartitionName("", &disk, &part_num));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700122}
123
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700124TEST(UtilsTest, MakePartitionNameTest) {
125 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
126 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
127 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
128 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
129 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
130 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700131}
132
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700133TEST(UtilsTest, FuzzIntTest) {
Amin Hassanib2689592019-01-13 17:04:28 -0800134 static const uint32_t kRanges[] = {0, 1, 2, 20};
Alex Deymo80f70ff2016-02-10 16:08:11 -0800135 for (uint32_t range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700136 const int kValue = 50;
137 for (int tries = 0; tries < 100; ++tries) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800138 uint32_t value = utils::FuzzInt(kValue, range);
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700139 EXPECT_GE(value, kValue - range / 2);
140 EXPECT_LE(value, kValue + range - range / 2);
141 }
142 }
143}
144
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800145namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700146void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700147 const vector<uint8_t>& contents) {
Amin Hassanied03b442020-10-26 17:21:29 -0700148 ScopedTempFile file;
Alex Deymobffa0602016-02-12 17:16:29 -0800149 ASSERT_TRUE(utils::WriteFile(file.path().c_str(),
Alex Deymo032e7722014-03-25 17:53:56 -0700150 reinterpret_cast<const char*>(contents.data()),
151 contents.size()));
Alex Deymobffa0602016-02-12 17:16:29 -0800152 EXPECT_EQ(expected, utils::GetFileFormat(file.path()));
Alex Deymo032e7722014-03-25 17:53:56 -0700153}
Alex Deymo10875d92014-11-10 21:52:57 -0800154} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700155
156TEST(UtilsTest, GetFileFormatTest) {
157 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700158 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
159 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700160
161 // Real tests from cros_installer on different boards.
162 // ELF 32-bit LSB executable, Intel 80386
163 GetFileFormatTester(
164 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700165 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
166 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
168 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700169
Alex Deymoc1711e22014-08-08 13:16:23 -0700170 // ELF 32-bit LSB executable, MIPS
171 GetFileFormatTester(
172 "ELF 32-bit little-endian mips",
173 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
174 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
176 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
177
Alex Deymo032e7722014-03-25 17:53:56 -0700178 // ELF 32-bit LSB executable, ARM
179 GetFileFormatTester(
180 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700181 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
182 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
184 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700185
186 // ELF 64-bit LSB executable, x86-64
187 GetFileFormatTester(
188 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700189 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
192 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700193}
194
David Zeuthen674c3182013-04-18 14:05:20 -0700195TEST(UtilsTest, FormatTimeDeltaTest) {
196 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
197 // which is not localized) so we only need to test the C locale
198 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
199 "0.1s");
Amin Hassanib2689592019-01-13 17:04:28 -0800200 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)), "0s");
201 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)), "1s");
202 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)), "59s");
203 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)), "1m0s");
204 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)), "1m1s");
205 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)), "1m30s");
David Zeuthen674c3182013-04-18 14:05:20 -0700206 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
207 "20m5s");
208 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
209 "1h0m0s");
210 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
211 "1h0m1s");
212 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
213 "1h1m1s");
214 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
215 "2h1m1s");
216 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
217 "1d0h0m0s");
218 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
219 "1d0h0m1s");
220 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
221 "2d7h33m20s");
222 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
223 base::TimeDelta::FromMilliseconds(1)),
224 "2d7h33m20.001s");
Amin Hassanib2689592019-01-13 17:04:28 -0800225 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)), "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700226}
227
David Zeuthen639aa362014-02-03 16:23:44 -0800228TEST(UtilsTest, ConvertToOmahaInstallDate) {
229 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
230 // Monday. In Unix time, this point in time is easily obtained via
231 // the date(1) command like this:
232 //
233 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
234 const time_t omaha_epoch = 1167638400;
235 int value;
236
237 // Points in time *on and after* the Omaha epoch should not fail.
238 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
239 base::Time::FromTimeT(omaha_epoch), &value));
240 EXPECT_GE(value, 0);
241
242 // Anything before the Omaha epoch should fail. We test it for two points.
243 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
244 base::Time::FromTimeT(omaha_epoch - 1), &value));
245 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
Amin Hassanib2689592019-01-13 17:04:28 -0800246 base::Time::FromTimeT(omaha_epoch - 100 * 24 * 3600), &value));
David Zeuthen639aa362014-02-03 16:23:44 -0800247
248 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
249 // on Jan 8, 2007 0:00 PST.
250 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
Amin Hassanib2689592019-01-13 17:04:28 -0800251 base::Time::FromTimeT(omaha_epoch + 7 * 24 * 3600 - 1), &value));
David Zeuthen639aa362014-02-03 16:23:44 -0800252 EXPECT_EQ(value, 0);
253 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
Amin Hassanib2689592019-01-13 17:04:28 -0800254 base::Time::FromTimeT(omaha_epoch + 7 * 24 * 3600), &value));
David Zeuthen639aa362014-02-03 16:23:44 -0800255 EXPECT_EQ(value, 7);
256
257 // Check a couple of more values.
258 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
Amin Hassanib2689592019-01-13 17:04:28 -0800259 base::Time::FromTimeT(omaha_epoch + 10 * 24 * 3600), &value));
David Zeuthen639aa362014-02-03 16:23:44 -0800260 EXPECT_EQ(value, 7);
261 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
Amin Hassanib2689592019-01-13 17:04:28 -0800262 base::Time::FromTimeT(omaha_epoch + 20 * 24 * 3600), &value));
David Zeuthen639aa362014-02-03 16:23:44 -0800263 EXPECT_EQ(value, 14);
264 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
Amin Hassanib2689592019-01-13 17:04:28 -0800265 base::Time::FromTimeT(omaha_epoch + 26 * 24 * 3600), &value));
David Zeuthen639aa362014-02-03 16:23:44 -0800266 EXPECT_EQ(value, 21);
267 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
Amin Hassanib2689592019-01-13 17:04:28 -0800268 base::Time::FromTimeT(omaha_epoch + 29 * 24 * 3600), &value));
David Zeuthen639aa362014-02-03 16:23:44 -0800269 EXPECT_EQ(value, 28);
270
271 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
272 // where the Omaha InstallDate jumps 7 days. Its unix time is
273 // 1180940400. Notably, this is a point in time where Daylight
274 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
275 //
276 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
277 // ignores DST (as it's hard to implement in a thread-safe way using
278 // glibc, see comments in utils.h) we have to fudge by the DST
279 // offset which is one hour. Conveniently, if the function were
280 // someday modified to be DST aware, this test would have to be
281 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700282 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800283 const time_t fudge = 3600;
284 int value1, value2;
285 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
286 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
287 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
288 base::Time::FromTimeT(dst_time + fudge), &value2));
289 EXPECT_EQ(value1, value2 - 7);
290}
291
Allie Wood78750a42015-02-11 15:42:11 -0800292TEST(UtilsTest, GetMinorVersion) {
293 // Test GetMinorVersion by verifying that it parses the conf file and returns
294 // the correct value.
Allie Wood78750a42015-02-11 15:42:11 -0800295 uint32_t minor_version;
296
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700297 brillo::KeyValueStore store;
Alex Deymob42b98d2015-07-06 17:42:38 -0700298 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800299
Alex Deymob42b98d2015-07-06 17:42:38 -0700300 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n"));
301 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800302
Alex Deymob42b98d2015-07-06 17:42:38 -0700303 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n"));
304 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800305 EXPECT_EQ(123U, minor_version);
Allie Wood78750a42015-02-11 15:42:11 -0800306}
307
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800308static bool BoolMacroTestHelper() {
309 int i = 1;
310 unsigned int ui = 1;
311 bool b = 1;
312 std::unique_ptr<char> cptr(new char);
313
314 TEST_AND_RETURN_FALSE(i);
315 TEST_AND_RETURN_FALSE(ui);
316 TEST_AND_RETURN_FALSE(b);
317 TEST_AND_RETURN_FALSE(cptr);
318
319 TEST_AND_RETURN_FALSE_ERRNO(i);
320 TEST_AND_RETURN_FALSE_ERRNO(ui);
321 TEST_AND_RETURN_FALSE_ERRNO(b);
322 TEST_AND_RETURN_FALSE_ERRNO(cptr);
323
324 return true;
325}
326
327static void VoidMacroTestHelper(bool* ret) {
328 int i = 1;
329 unsigned int ui = 1;
330 bool b = 1;
331 std::unique_ptr<char> cptr(new char);
332
333 *ret = false;
334
335 TEST_AND_RETURN(i);
336 TEST_AND_RETURN(ui);
337 TEST_AND_RETURN(b);
338 TEST_AND_RETURN(cptr);
339
340 TEST_AND_RETURN_ERRNO(i);
341 TEST_AND_RETURN_ERRNO(ui);
342 TEST_AND_RETURN_ERRNO(b);
343 TEST_AND_RETURN_ERRNO(cptr);
344
345 *ret = true;
346}
347
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700348static void ExpectParseRollbackKeyVersion(const string& version,
349 uint16_t expected_high,
350 uint16_t expected_low) {
351 uint16_t actual_high;
352 uint16_t actual_low;
353 utils::ParseRollbackKeyVersion(version, &actual_high, &actual_low);
354 EXPECT_EQ(expected_high, actual_high);
355 EXPECT_EQ(expected_low, actual_low);
356}
357
358static void ExpectInvalidParseRollbackKeyVersion(const string& version) {
359 ExpectParseRollbackKeyVersion(version,
360 numeric_limits<uint16_t>::max(),
361 numeric_limits<uint16_t>::max());
362}
363
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800364TEST(UtilsTest, TestMacros) {
365 bool void_test = false;
366 VoidMacroTestHelper(&void_test);
367 EXPECT_TRUE(void_test);
368
369 EXPECT_TRUE(BoolMacroTestHelper());
370}
371
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700372TEST(UtilsTest, RunAsRootUnmountFilesystemFailureTest) {
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700373 EXPECT_FALSE(utils::UnmountFilesystem("/path/to/non-existing-dir"));
374}
375
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700376TEST(UtilsTest, RunAsRootUnmountFilesystemBusyFailureTest) {
Amin Hassanied03b442020-10-26 17:21:29 -0700377 ScopedTempFile tmp_image("img.XXXXXX");
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700378
379 EXPECT_TRUE(base::CopyFile(
380 test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
Sen Jiang0779a152018-07-02 17:34:56 -0700381 base::FilePath(tmp_image.path())));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700382
383 base::ScopedTempDir mnt_dir;
384 EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
385
386 string loop_dev;
387 test_utils::ScopedLoopbackDeviceBinder loop_binder(
Sen Jiang0779a152018-07-02 17:34:56 -0700388 tmp_image.path(), true, &loop_dev);
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700389
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900390 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700391 // This is the actual test part. While we hold a file descriptor open for the
392 // mounted filesystem, umount should still succeed.
393 EXPECT_TRUE(utils::MountFilesystem(
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900394 loop_dev, mnt_dir.GetPath().value(), MS_RDONLY, "ext4", ""));
Alex Deymof4116502017-03-22 17:00:31 -0700395 // Verify the directory is a mount point now.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900396 EXPECT_TRUE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700397
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900398 string target_file = mnt_dir.GetPath().Append("empty-file").value();
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700399 int fd = HANDLE_EINTR(open(target_file.c_str(), O_RDONLY));
400 EXPECT_GE(fd, 0);
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900401 EXPECT_TRUE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700402 // The filesystem should be already unmounted at this point.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900403 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700404 IGNORE_EINTR(close(fd));
405 // The filesystem was already unmounted so this call should fail.
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900406 EXPECT_FALSE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700407}
408
Alex Deymof4116502017-03-22 17:00:31 -0700409TEST(UtilsTest, IsMountpointTest) {
410 EXPECT_TRUE(utils::IsMountpoint("/"));
411 EXPECT_FALSE(utils::IsMountpoint("/path/to/nowhere"));
412
413 base::ScopedTempDir mnt_dir;
414 EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
Hidehiko Abe2b9d2412017-12-13 18:56:18 +0900415 EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
Alex Deymof4116502017-03-22 17:00:31 -0700416
Amin Hassanied03b442020-10-26 17:21:29 -0700417 ScopedTempFile file;
Sen Jiang0779a152018-07-02 17:34:56 -0700418 EXPECT_FALSE(utils::IsMountpoint(file.path()));
Alex Deymof4116502017-03-22 17:00:31 -0700419}
420
Marton Hunyadya0302682018-05-16 18:52:13 +0200421TEST(UtilsTest, VersionPrefix) {
422 EXPECT_EQ(10575, utils::VersionPrefix("10575.39."));
423 EXPECT_EQ(10575, utils::VersionPrefix("10575.39"));
424 EXPECT_EQ(10575, utils::VersionPrefix("10575.x"));
425 EXPECT_EQ(10575, utils::VersionPrefix("10575."));
426 EXPECT_EQ(10575, utils::VersionPrefix("10575"));
427 EXPECT_EQ(0, utils::VersionPrefix(""));
428 EXPECT_EQ(-1, utils::VersionPrefix("x"));
429 EXPECT_EQ(-1, utils::VersionPrefix("1x"));
430 EXPECT_EQ(-1, utils::VersionPrefix("x.1"));
431}
432
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700433TEST(UtilsTest, ParseDottedVersion) {
434 // Valid case.
435 ExpectParseRollbackKeyVersion("2.3", 2, 3);
436 ExpectParseRollbackKeyVersion("65535.65535", 65535, 65535);
437
438 // Zero is technically allowed but never actually used.
439 ExpectParseRollbackKeyVersion("0.0", 0, 0);
440
441 // Invalid cases.
442 ExpectInvalidParseRollbackKeyVersion("");
443 ExpectInvalidParseRollbackKeyVersion("2");
444 ExpectInvalidParseRollbackKeyVersion("2.");
445 ExpectInvalidParseRollbackKeyVersion(".2");
446 ExpectInvalidParseRollbackKeyVersion("2.2.");
447 ExpectInvalidParseRollbackKeyVersion("2.2.3");
448 ExpectInvalidParseRollbackKeyVersion(".2.2");
449 ExpectInvalidParseRollbackKeyVersion("a.b");
450 ExpectInvalidParseRollbackKeyVersion("1.b");
451 ExpectInvalidParseRollbackKeyVersion("a.2");
452 ExpectInvalidParseRollbackKeyVersion("65536.65536");
453 ExpectInvalidParseRollbackKeyVersion("99999.99999");
454 ExpectInvalidParseRollbackKeyVersion("99999.1");
455 ExpectInvalidParseRollbackKeyVersion("1.99999");
456}
457
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900458TEST(UtilsTest, GetFilePathTest) {
Amin Hassanied03b442020-10-26 17:21:29 -0700459 ScopedTempFile file;
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900460 int fd = HANDLE_EINTR(open(file.path().c_str(), O_RDONLY));
461 EXPECT_GE(fd, 0);
462 EXPECT_EQ(file.path(), utils::GetFilePath(fd));
463 EXPECT_EQ("not found", utils::GetFilePath(-1));
464 IGNORE_EINTR(close(fd));
465}
466
Kelvin Zhangd7191032020-08-11 10:48:16 -0400467TEST(UtilsTest, ValidatePerPartitionTimestamp) {
Yifan Hong87029332020-09-01 17:20:08 -0700468 ASSERT_EQ(ErrorCode::kPayloadTimestampError,
469 utils::IsTimestampNewer("10", "5"));
470 ASSERT_EQ(ErrorCode::kSuccess, utils::IsTimestampNewer("10", "11"));
471 ASSERT_EQ(ErrorCode::kDownloadManifestParseError,
472 utils::IsTimestampNewer("10", "lol"));
473 ASSERT_EQ(ErrorCode::kError, utils::IsTimestampNewer("lol", "ZZZ"));
474 ASSERT_EQ(ErrorCode::kSuccess, utils::IsTimestampNewer("10", ""));
Kelvin Zhangd7191032020-08-11 10:48:16 -0400475}
476
adlr@google.com3defe6a2009-12-04 20:57:17 +0000477} // namespace chromeos_update_engine