Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/utils.h" |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 21 | #include <sys/mount.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 24 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 25 | #include <limits> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 26 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | #include <vector> |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 28 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 29 | #include <base/files/file_path.h> |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 30 | #include <base/files/file_util.h> |
Sen Jiang | 371615b | 2016-04-13 15:54:29 -0700 | [diff] [blame] | 31 | #include <base/files/scoped_temp_dir.h> |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 32 | #include <gtest/gtest.h> |
| 33 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #include "update_engine/common/test_utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 36 | using std::numeric_limits; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 37 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 38 | using std::vector; |
| 39 | |
| 40 | namespace chromeos_update_engine { |
| 41 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 42 | class UtilsTest : public ::testing::Test {}; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 43 | |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 44 | TEST(UtilsTest, CanParseECVersion) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 45 | // Should be able to parse and valid key value line. |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 46 | EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345")); |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 47 | EXPECT_EQ("123456", |
| 48 | utils::ParseECVersion("b=1231a fw_version=123456 a=fasd2")); |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 49 | EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345")); |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 50 | EXPECT_EQ("00VFA616", |
| 51 | utils::ParseECVersion("vendor=\"sam\" fw_version=\"00VFA616\"")); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 52 | |
| 53 | // For invalid entries, should return the empty string. |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 54 | EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2")); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alex Deymo | 335516c | 2016-11-28 13:37:06 -0800 | [diff] [blame] | 57 | TEST(UtilsTest, WriteFileOpenFailure) { |
| 58 | EXPECT_FALSE(utils::WriteFile("/this/doesn't/exist", "hello", 5)); |
| 59 | } |
| 60 | |
| 61 | TEST(UtilsTest, WriteFileReadFile) { |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 62 | test_utils::ScopedTempFile file; |
| 63 | EXPECT_TRUE(utils::WriteFile(file.path().c_str(), "hello", 5)); |
Alex Deymo | 335516c | 2016-11-28 13:37:06 -0800 | [diff] [blame] | 64 | |
| 65 | brillo::Blob readback; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 66 | EXPECT_TRUE(utils::ReadFile(file.path().c_str(), &readback)); |
Alex Deymo | 335516c | 2016-11-28 13:37:06 -0800 | [diff] [blame] | 67 | EXPECT_EQ("hello", string(readback.begin(), readback.end())); |
| 68 | } |
| 69 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 70 | TEST(UtilsTest, ReadFileFailure) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 71 | brillo::Blob empty; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 72 | EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty)); |
| 73 | } |
| 74 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 75 | TEST(UtilsTest, ReadFileChunk) { |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 76 | test_utils::ScopedTempFile file; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 77 | brillo::Blob data; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 78 | const size_t kSize = 1024 * 1024; |
| 79 | for (size_t i = 0; i < kSize; i++) { |
| 80 | data.push_back(i % 255); |
| 81 | } |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 82 | EXPECT_TRUE(test_utils::WriteFileVector(file.path(), data)); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 83 | brillo::Blob in_data; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), kSize, 10, &in_data)); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 85 | EXPECT_TRUE(in_data.empty()); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 0, -1, &in_data)); |
| 87 | EXPECT_EQ(data, in_data); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 88 | in_data.clear(); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 89 | 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 Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 91 | } |
| 92 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | TEST(UtilsTest, ErrnoNumberAsStringTest) { |
| 94 | EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT)); |
| 95 | } |
| 96 | |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 97 | TEST(UtilsTest, IsSymlinkTest) { |
Sen Jiang | 371615b | 2016-04-13 15:54:29 -0700 | [diff] [blame] | 98 | base::ScopedTempDir temp_dir; |
| 99 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 100 | string temp_file = temp_dir.GetPath().Append("temp-file").value(); |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 101 | EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0)); |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 102 | string temp_symlink = temp_dir.GetPath().Append("temp-symlink").value(); |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 103 | EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str())); |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 104 | EXPECT_FALSE(utils::IsSymlink(temp_dir.GetPath().value().c_str())); |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 105 | 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 Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 110 | TEST(UtilsTest, SplitPartitionNameTest) { |
| 111 | string disk; |
| 112 | int part_num; |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 113 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 114 | EXPECT_TRUE(utils::SplitPartitionName("/dev/sda3", &disk, &part_num)); |
| 115 | EXPECT_EQ("/dev/sda", disk); |
| 116 | EXPECT_EQ(3, part_num); |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 117 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 118 | EXPECT_TRUE(utils::SplitPartitionName("/dev/sdp1234", &disk, &part_num)); |
| 119 | EXPECT_EQ("/dev/sdp", disk); |
| 120 | EXPECT_EQ(1234, part_num); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 121 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(utils::SplitPartitionName("/dev/mmcblk0p3", &disk, &part_num)); |
| 123 | EXPECT_EQ("/dev/mmcblk0", disk); |
| 124 | EXPECT_EQ(3, part_num); |
| 125 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 126 | EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10", &disk, &part_num)); |
| 127 | EXPECT_EQ("/dev/loop", disk); |
| 128 | EXPECT_EQ(10, part_num); |
| 129 | |
| 130 | EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11", &disk, &part_num)); |
| 131 | EXPECT_EQ("/dev/loop28", disk); |
| 132 | EXPECT_EQ(11, part_num); |
| 133 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 134 | EXPECT_FALSE(utils::SplitPartitionName("/dev/mmcblk0p", &disk, &part_num)); |
| 135 | EXPECT_FALSE(utils::SplitPartitionName("/dev/sda", &disk, &part_num)); |
| 136 | EXPECT_FALSE(utils::SplitPartitionName("/dev/foo/bar", &disk, &part_num)); |
| 137 | EXPECT_FALSE(utils::SplitPartitionName("/", &disk, &part_num)); |
| 138 | EXPECT_FALSE(utils::SplitPartitionName("", &disk, &part_num)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 141 | TEST(UtilsTest, MakePartitionNameTest) { |
| 142 | EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4)); |
| 143 | EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123)); |
| 144 | EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2)); |
| 145 | EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2)); |
| 146 | EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8)); |
| 147 | EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2)); |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 150 | TEST(UtilsTest, FuzzIntTest) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 151 | static const uint32_t kRanges[] = {0, 1, 2, 20}; |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 152 | for (uint32_t range : kRanges) { |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 153 | const int kValue = 50; |
| 154 | for (int tries = 0; tries < 100; ++tries) { |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 155 | uint32_t value = utils::FuzzInt(kValue, range); |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 156 | EXPECT_GE(value, kValue - range / 2); |
| 157 | EXPECT_LE(value, kValue + range - range / 2); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 162 | namespace { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 163 | void GetFileFormatTester(const string& expected, |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 164 | const vector<uint8_t>& contents) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 165 | test_utils::ScopedTempFile file; |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 166 | ASSERT_TRUE(utils::WriteFile(file.path().c_str(), |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 167 | reinterpret_cast<const char*>(contents.data()), |
| 168 | contents.size())); |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 169 | EXPECT_EQ(expected, utils::GetFileFormat(file.path())); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 170 | } |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 171 | } // namespace |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 172 | |
| 173 | TEST(UtilsTest, GetFileFormatTest) { |
| 174 | EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere")); |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 175 | GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8}); |
| 176 | GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 177 | |
| 178 | // Real tests from cros_installer on different boards. |
| 179 | // ELF 32-bit LSB executable, Intel 80386 |
| 180 | GetFileFormatTester( |
| 181 | "ELF 32-bit little-endian x86", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 182 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 183 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 184 | 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 185 | 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 186 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 187 | // ELF 32-bit LSB executable, MIPS |
| 188 | GetFileFormatTester( |
| 189 | "ELF 32-bit little-endian mips", |
| 190 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 191 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 192 | 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 193 | 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00}); |
| 194 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 195 | // ELF 32-bit LSB executable, ARM |
| 196 | GetFileFormatTester( |
| 197 | "ELF 32-bit little-endian arm", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 198 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 199 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 200 | 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 201 | 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 202 | |
| 203 | // ELF 64-bit LSB executable, x86-64 |
| 204 | GetFileFormatTester( |
| 205 | "ELF 64-bit little-endian x86-64", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 206 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, |
| 207 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 208 | 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 209 | 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 210 | } |
| 211 | |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 212 | TEST(UtilsTest, FormatTimeDeltaTest) { |
| 213 | // utils::FormatTimeDelta() is not locale-aware (it's only used for logging |
| 214 | // which is not localized) so we only need to test the C locale |
| 215 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)), |
| 216 | "0.1s"); |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 217 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)), "0s"); |
| 218 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)), "1s"); |
| 219 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)), "59s"); |
| 220 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)), "1m0s"); |
| 221 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)), "1m1s"); |
| 222 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)), "1m30s"); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 223 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)), |
| 224 | "20m5s"); |
| 225 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)), |
| 226 | "1h0m0s"); |
| 227 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)), |
| 228 | "1h0m1s"); |
| 229 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)), |
| 230 | "1h1m1s"); |
| 231 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)), |
| 232 | "2h1m1s"); |
| 233 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)), |
| 234 | "1d0h0m0s"); |
| 235 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)), |
| 236 | "1d0h0m1s"); |
| 237 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)), |
| 238 | "2d7h33m20s"); |
| 239 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) + |
| 240 | base::TimeDelta::FromMilliseconds(1)), |
| 241 | "2d7h33m20.001s"); |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 242 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)), "-1s"); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 243 | } |
| 244 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 245 | TEST(UtilsTest, ConvertToOmahaInstallDate) { |
| 246 | // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a |
| 247 | // Monday. In Unix time, this point in time is easily obtained via |
| 248 | // the date(1) command like this: |
| 249 | // |
| 250 | // $ date +"%s" --date="Jan 1, 2007 0:00 PST" |
| 251 | const time_t omaha_epoch = 1167638400; |
| 252 | int value; |
| 253 | |
| 254 | // Points in time *on and after* the Omaha epoch should not fail. |
| 255 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 256 | base::Time::FromTimeT(omaha_epoch), &value)); |
| 257 | EXPECT_GE(value, 0); |
| 258 | |
| 259 | // Anything before the Omaha epoch should fail. We test it for two points. |
| 260 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
| 261 | base::Time::FromTimeT(omaha_epoch - 1), &value)); |
| 262 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 263 | base::Time::FromTimeT(omaha_epoch - 100 * 24 * 3600), &value)); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 264 | |
| 265 | // Check that we jump from 0 to 7 exactly on the one-week mark, e.g. |
| 266 | // on Jan 8, 2007 0:00 PST. |
| 267 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 268 | base::Time::FromTimeT(omaha_epoch + 7 * 24 * 3600 - 1), &value)); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 269 | EXPECT_EQ(value, 0); |
| 270 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 271 | base::Time::FromTimeT(omaha_epoch + 7 * 24 * 3600), &value)); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 272 | EXPECT_EQ(value, 7); |
| 273 | |
| 274 | // Check a couple of more values. |
| 275 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 276 | base::Time::FromTimeT(omaha_epoch + 10 * 24 * 3600), &value)); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 277 | EXPECT_EQ(value, 7); |
| 278 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 279 | base::Time::FromTimeT(omaha_epoch + 20 * 24 * 3600), &value)); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 280 | EXPECT_EQ(value, 14); |
| 281 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 282 | base::Time::FromTimeT(omaha_epoch + 26 * 24 * 3600), &value)); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 283 | EXPECT_EQ(value, 21); |
| 284 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 285 | base::Time::FromTimeT(omaha_epoch + 29 * 24 * 3600), &value)); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 286 | EXPECT_EQ(value, 28); |
| 287 | |
| 288 | // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point |
| 289 | // where the Omaha InstallDate jumps 7 days. Its unix time is |
| 290 | // 1180940400. Notably, this is a point in time where Daylight |
| 291 | // Savings Time (DST) was is in effect (e.g. it's PDT, not PST). |
| 292 | // |
| 293 | // Note that as utils::ConvertToOmahaInstallDate() _deliberately_ |
| 294 | // ignores DST (as it's hard to implement in a thread-safe way using |
| 295 | // glibc, see comments in utils.h) we have to fudge by the DST |
| 296 | // offset which is one hour. Conveniently, if the function were |
| 297 | // someday modified to be DST aware, this test would have to be |
| 298 | // modified as well. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 299 | const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT. |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 300 | const time_t fudge = 3600; |
| 301 | int value1, value2; |
| 302 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 303 | base::Time::FromTimeT(dst_time + fudge - 1), &value1)); |
| 304 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 305 | base::Time::FromTimeT(dst_time + fudge), &value2)); |
| 306 | EXPECT_EQ(value1, value2 - 7); |
| 307 | } |
| 308 | |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 309 | TEST(UtilsTest, GetMinorVersion) { |
| 310 | // Test GetMinorVersion by verifying that it parses the conf file and returns |
| 311 | // the correct value. |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 312 | uint32_t minor_version; |
| 313 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 314 | brillo::KeyValueStore store; |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 315 | EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version)); |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 316 | |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 317 | EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n")); |
| 318 | EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version)); |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 319 | |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 320 | EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n")); |
| 321 | EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version)); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 322 | EXPECT_EQ(123U, minor_version); |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Nam T. Nguyen | 2b67a59 | 2014-12-03 14:56:00 -0800 | [diff] [blame] | 325 | static bool BoolMacroTestHelper() { |
| 326 | int i = 1; |
| 327 | unsigned int ui = 1; |
| 328 | bool b = 1; |
| 329 | std::unique_ptr<char> cptr(new char); |
| 330 | |
| 331 | TEST_AND_RETURN_FALSE(i); |
| 332 | TEST_AND_RETURN_FALSE(ui); |
| 333 | TEST_AND_RETURN_FALSE(b); |
| 334 | TEST_AND_RETURN_FALSE(cptr); |
| 335 | |
| 336 | TEST_AND_RETURN_FALSE_ERRNO(i); |
| 337 | TEST_AND_RETURN_FALSE_ERRNO(ui); |
| 338 | TEST_AND_RETURN_FALSE_ERRNO(b); |
| 339 | TEST_AND_RETURN_FALSE_ERRNO(cptr); |
| 340 | |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | static void VoidMacroTestHelper(bool* ret) { |
| 345 | int i = 1; |
| 346 | unsigned int ui = 1; |
| 347 | bool b = 1; |
| 348 | std::unique_ptr<char> cptr(new char); |
| 349 | |
| 350 | *ret = false; |
| 351 | |
| 352 | TEST_AND_RETURN(i); |
| 353 | TEST_AND_RETURN(ui); |
| 354 | TEST_AND_RETURN(b); |
| 355 | TEST_AND_RETURN(cptr); |
| 356 | |
| 357 | TEST_AND_RETURN_ERRNO(i); |
| 358 | TEST_AND_RETURN_ERRNO(ui); |
| 359 | TEST_AND_RETURN_ERRNO(b); |
| 360 | TEST_AND_RETURN_ERRNO(cptr); |
| 361 | |
| 362 | *ret = true; |
| 363 | } |
| 364 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 365 | static void ExpectParseRollbackKeyVersion(const string& version, |
| 366 | uint16_t expected_high, |
| 367 | uint16_t expected_low) { |
| 368 | uint16_t actual_high; |
| 369 | uint16_t actual_low; |
| 370 | utils::ParseRollbackKeyVersion(version, &actual_high, &actual_low); |
| 371 | EXPECT_EQ(expected_high, actual_high); |
| 372 | EXPECT_EQ(expected_low, actual_low); |
| 373 | } |
| 374 | |
| 375 | static void ExpectInvalidParseRollbackKeyVersion(const string& version) { |
| 376 | ExpectParseRollbackKeyVersion(version, |
| 377 | numeric_limits<uint16_t>::max(), |
| 378 | numeric_limits<uint16_t>::max()); |
| 379 | } |
| 380 | |
Nam T. Nguyen | 2b67a59 | 2014-12-03 14:56:00 -0800 | [diff] [blame] | 381 | TEST(UtilsTest, TestMacros) { |
| 382 | bool void_test = false; |
| 383 | VoidMacroTestHelper(&void_test); |
| 384 | EXPECT_TRUE(void_test); |
| 385 | |
| 386 | EXPECT_TRUE(BoolMacroTestHelper()); |
| 387 | } |
| 388 | |
Alex Deymo | a2ea1c2 | 2016-08-24 17:26:19 -0700 | [diff] [blame] | 389 | TEST(UtilsTest, RunAsRootUnmountFilesystemFailureTest) { |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 390 | EXPECT_FALSE(utils::UnmountFilesystem("/path/to/non-existing-dir")); |
| 391 | } |
| 392 | |
Alex Deymo | a2ea1c2 | 2016-08-24 17:26:19 -0700 | [diff] [blame] | 393 | TEST(UtilsTest, RunAsRootUnmountFilesystemBusyFailureTest) { |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 394 | test_utils::ScopedTempFile tmp_image("img.XXXXXX"); |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 395 | |
| 396 | EXPECT_TRUE(base::CopyFile( |
| 397 | test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"), |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 398 | base::FilePath(tmp_image.path()))); |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 399 | |
| 400 | base::ScopedTempDir mnt_dir; |
| 401 | EXPECT_TRUE(mnt_dir.CreateUniqueTempDir()); |
| 402 | |
| 403 | string loop_dev; |
| 404 | test_utils::ScopedLoopbackDeviceBinder loop_binder( |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 405 | tmp_image.path(), true, &loop_dev); |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 406 | |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 407 | EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value())); |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 408 | // This is the actual test part. While we hold a file descriptor open for the |
| 409 | // mounted filesystem, umount should still succeed. |
| 410 | EXPECT_TRUE(utils::MountFilesystem( |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 411 | loop_dev, mnt_dir.GetPath().value(), MS_RDONLY, "ext4", "")); |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 412 | // Verify the directory is a mount point now. |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 413 | EXPECT_TRUE(utils::IsMountpoint(mnt_dir.GetPath().value())); |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 414 | |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 415 | string target_file = mnt_dir.GetPath().Append("empty-file").value(); |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 416 | int fd = HANDLE_EINTR(open(target_file.c_str(), O_RDONLY)); |
| 417 | EXPECT_GE(fd, 0); |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 418 | EXPECT_TRUE(utils::UnmountFilesystem(mnt_dir.GetPath().value())); |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 419 | // The filesystem should be already unmounted at this point. |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 420 | EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value())); |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 421 | IGNORE_EINTR(close(fd)); |
| 422 | // The filesystem was already unmounted so this call should fail. |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 423 | EXPECT_FALSE(utils::UnmountFilesystem(mnt_dir.GetPath().value())); |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 426 | TEST(UtilsTest, IsMountpointTest) { |
| 427 | EXPECT_TRUE(utils::IsMountpoint("/")); |
| 428 | EXPECT_FALSE(utils::IsMountpoint("/path/to/nowhere")); |
| 429 | |
| 430 | base::ScopedTempDir mnt_dir; |
| 431 | EXPECT_TRUE(mnt_dir.CreateUniqueTempDir()); |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 432 | EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value())); |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 433 | |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 434 | test_utils::ScopedTempFile file; |
| 435 | EXPECT_FALSE(utils::IsMountpoint(file.path())); |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 438 | TEST(UtilsTest, VersionPrefix) { |
| 439 | EXPECT_EQ(10575, utils::VersionPrefix("10575.39.")); |
| 440 | EXPECT_EQ(10575, utils::VersionPrefix("10575.39")); |
| 441 | EXPECT_EQ(10575, utils::VersionPrefix("10575.x")); |
| 442 | EXPECT_EQ(10575, utils::VersionPrefix("10575.")); |
| 443 | EXPECT_EQ(10575, utils::VersionPrefix("10575")); |
| 444 | EXPECT_EQ(0, utils::VersionPrefix("")); |
| 445 | EXPECT_EQ(-1, utils::VersionPrefix("x")); |
| 446 | EXPECT_EQ(-1, utils::VersionPrefix("1x")); |
| 447 | EXPECT_EQ(-1, utils::VersionPrefix("x.1")); |
| 448 | } |
| 449 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 450 | TEST(UtilsTest, ParseDottedVersion) { |
| 451 | // Valid case. |
| 452 | ExpectParseRollbackKeyVersion("2.3", 2, 3); |
| 453 | ExpectParseRollbackKeyVersion("65535.65535", 65535, 65535); |
| 454 | |
| 455 | // Zero is technically allowed but never actually used. |
| 456 | ExpectParseRollbackKeyVersion("0.0", 0, 0); |
| 457 | |
| 458 | // Invalid cases. |
| 459 | ExpectInvalidParseRollbackKeyVersion(""); |
| 460 | ExpectInvalidParseRollbackKeyVersion("2"); |
| 461 | ExpectInvalidParseRollbackKeyVersion("2."); |
| 462 | ExpectInvalidParseRollbackKeyVersion(".2"); |
| 463 | ExpectInvalidParseRollbackKeyVersion("2.2."); |
| 464 | ExpectInvalidParseRollbackKeyVersion("2.2.3"); |
| 465 | ExpectInvalidParseRollbackKeyVersion(".2.2"); |
| 466 | ExpectInvalidParseRollbackKeyVersion("a.b"); |
| 467 | ExpectInvalidParseRollbackKeyVersion("1.b"); |
| 468 | ExpectInvalidParseRollbackKeyVersion("a.2"); |
| 469 | ExpectInvalidParseRollbackKeyVersion("65536.65536"); |
| 470 | ExpectInvalidParseRollbackKeyVersion("99999.99999"); |
| 471 | ExpectInvalidParseRollbackKeyVersion("99999.1"); |
| 472 | ExpectInvalidParseRollbackKeyVersion("1.99999"); |
| 473 | } |
| 474 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 475 | TEST(UtilsTest, GetFilePathTest) { |
| 476 | test_utils::ScopedTempFile file; |
| 477 | int fd = HANDLE_EINTR(open(file.path().c_str(), O_RDONLY)); |
| 478 | EXPECT_GE(fd, 0); |
| 479 | EXPECT_EQ(file.path(), utils::GetFilePath(fd)); |
| 480 | EXPECT_EQ("not found", utils::GetFilePath(-1)); |
| 481 | IGNORE_EINTR(close(fd)); |
| 482 | } |
| 483 | |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame^] | 484 | TEST(UtilsTest, ValidatePerPartitionTimestamp) { |
| 485 | ASSERT_FALSE(utils::IsTimestampNewer("10", "5")); |
| 486 | ASSERT_TRUE(utils::IsTimestampNewer("10", "11")); |
| 487 | ASSERT_FALSE(utils::IsTimestampNewer("10", "lol")); |
| 488 | ASSERT_FALSE(utils::IsTimestampNewer("lol", "ZZZ")); |
| 489 | ASSERT_TRUE(utils::IsTimestampNewer("10", "")); |
| 490 | } |
| 491 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 492 | } // namespace chromeos_update_engine |