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