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 | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
| 18 | |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 23 | |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 24 | #include <map> |
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> |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 30 | #include <base/files/scoped_temp_dir.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 31 | #include <base/strings/string_util.h> |
| 32 | #include <base/strings/stringprintf.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 33 | #include <chromeos/message_loops/fake_message_loop.h> |
| 34 | #include <chromeos/message_loops/message_loop_utils.h> |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 35 | #include <gtest/gtest.h> |
| 36 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 37 | #include "update_engine/fake_clock.h" |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 38 | #include "update_engine/fake_prefs.h" |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 39 | #include "update_engine/fake_system_state.h" |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 40 | #include "update_engine/prefs.h" |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 41 | #include "update_engine/test_utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 42 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 43 | using chromeos::FakeMessageLoop; |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 44 | using std::map; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 45 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 46 | using std::vector; |
| 47 | |
| 48 | namespace chromeos_update_engine { |
| 49 | |
| 50 | class UtilsTest : public ::testing::Test { }; |
| 51 | |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 52 | TEST(UtilsTest, CanParseECVersion) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 53 | // Should be able to parse and valid key value line. |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 54 | EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345")); |
| 55 | EXPECT_EQ("123456", utils::ParseECVersion( |
| 56 | "b=1231a fw_version=123456 a=fasd2")); |
| 57 | EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345")); |
| 58 | EXPECT_EQ("00VFA616", utils::ParseECVersion( |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 59 | "vendor=\"sam\" fw_version=\"00VFA616\"")); |
| 60 | |
| 61 | // For invalid entries, should return the empty string. |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 62 | EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2")); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 63 | } |
| 64 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | TEST(UtilsTest, ReadFileFailure) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 66 | chromeos::Blob empty; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty)); |
| 68 | } |
| 69 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 70 | TEST(UtilsTest, ReadFileChunk) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 71 | base::FilePath file; |
| 72 | EXPECT_TRUE(base::CreateTemporaryFile(&file)); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 73 | ScopedPathUnlinker unlinker(file.value()); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 74 | chromeos::Blob data; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 75 | const size_t kSize = 1024 * 1024; |
| 76 | for (size_t i = 0; i < kSize; i++) { |
| 77 | data.push_back(i % 255); |
| 78 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 79 | EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size())); |
| 80 | chromeos::Blob in_data; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 81 | EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data)); |
| 82 | EXPECT_TRUE(in_data.empty()); |
| 83 | EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data)); |
| 84 | EXPECT_TRUE(data == in_data); |
| 85 | in_data.clear(); |
| 86 | EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data)); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 87 | EXPECT_TRUE(chromeos::Blob(data.begin() + 10, data.begin() + 10 + 20) == |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 88 | in_data); |
| 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) { |
| 96 | string temp_dir; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 97 | EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir)); |
Alex Deymo | 8d92529 | 2014-05-21 19:15:25 -0700 | [diff] [blame] | 98 | string temp_file = temp_dir + "/temp-file"; |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 99 | EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0)); |
Alex Deymo | 8d92529 | 2014-05-21 19:15:25 -0700 | [diff] [blame] | 100 | string temp_symlink = temp_dir + "/temp-symlink"; |
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())); |
| 102 | EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str())); |
| 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")); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 106 | EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir)); |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 109 | TEST(UtilsTest, SplitPartitionNameTest) { |
| 110 | string disk; |
| 111 | int part_num; |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 112 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 113 | EXPECT_TRUE(utils::SplitPartitionName("/dev/sda3", &disk, &part_num)); |
| 114 | EXPECT_EQ("/dev/sda", disk); |
| 115 | EXPECT_EQ(3, part_num); |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 116 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 117 | EXPECT_TRUE(utils::SplitPartitionName("/dev/sdp1234", &disk, &part_num)); |
| 118 | EXPECT_EQ("/dev/sdp", disk); |
| 119 | EXPECT_EQ(1234, part_num); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 120 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 121 | EXPECT_TRUE(utils::SplitPartitionName("/dev/mmcblk0p3", &disk, &part_num)); |
| 122 | EXPECT_EQ("/dev/mmcblk0", disk); |
| 123 | EXPECT_EQ(3, part_num); |
| 124 | |
| 125 | EXPECT_TRUE(utils::SplitPartitionName("/dev/ubiblock3_2", &disk, &part_num)); |
| 126 | EXPECT_EQ("/dev/ubiblock", disk); |
| 127 | EXPECT_EQ(3, part_num); |
| 128 | |
| 129 | EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10", &disk, &part_num)); |
| 130 | EXPECT_EQ("/dev/loop", disk); |
| 131 | EXPECT_EQ(10, part_num); |
| 132 | |
| 133 | EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11", &disk, &part_num)); |
| 134 | EXPECT_EQ("/dev/loop28", disk); |
| 135 | EXPECT_EQ(11, part_num); |
| 136 | |
| 137 | EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10_0", &disk, &part_num)); |
| 138 | EXPECT_EQ("/dev/loop", disk); |
| 139 | EXPECT_EQ(10, part_num); |
| 140 | |
| 141 | EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11_0", &disk, &part_num)); |
| 142 | EXPECT_EQ("/dev/loop28", disk); |
| 143 | EXPECT_EQ(11, part_num); |
| 144 | |
| 145 | EXPECT_FALSE(utils::SplitPartitionName("/dev/mmcblk0p", &disk, &part_num)); |
| 146 | EXPECT_FALSE(utils::SplitPartitionName("/dev/sda", &disk, &part_num)); |
| 147 | EXPECT_FALSE(utils::SplitPartitionName("/dev/foo/bar", &disk, &part_num)); |
| 148 | EXPECT_FALSE(utils::SplitPartitionName("/", &disk, &part_num)); |
| 149 | EXPECT_FALSE(utils::SplitPartitionName("", &disk, &part_num)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 152 | TEST(UtilsTest, MakePartitionNameTest) { |
| 153 | EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4)); |
| 154 | EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123)); |
| 155 | EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2)); |
| 156 | EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2)); |
| 157 | EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8)); |
| 158 | EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2)); |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 159 | EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5)); |
| 160 | EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4)); |
| 161 | EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3)); |
| 162 | EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2)); |
| 163 | EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1)); |
| 164 | } |
| 165 | |
| 166 | TEST(UtilsTest, MakePartitionNameForMountTest) { |
| 167 | EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4")); |
| 168 | EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123")); |
| 169 | EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2")); |
| 170 | EXPECT_EQ("/dev/mmcblk0p2", |
| 171 | utils::MakePartitionNameForMount("/dev/mmcblk0p2")); |
| 172 | EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0")); |
| 173 | EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8")); |
| 174 | EXPECT_EQ("/dev/loop12p2", |
| 175 | utils::MakePartitionNameForMount("/dev/loop12p2")); |
| 176 | EXPECT_EQ("/dev/ubiblock5_0", |
| 177 | utils::MakePartitionNameForMount("/dev/ubiblock5_0")); |
| 178 | EXPECT_EQ("/dev/mtd4", |
| 179 | utils::MakePartitionNameForMount("/dev/ubi4_0")); |
| 180 | EXPECT_EQ("/dev/ubiblock3_0", |
| 181 | utils::MakePartitionNameForMount("/dev/ubiblock3")); |
| 182 | EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2")); |
| 183 | EXPECT_EQ("/dev/ubi1_0", |
| 184 | utils::MakePartitionNameForMount("/dev/ubiblock1")); |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 187 | namespace { |
| 188 | // Compares cpu shares and returns an integer that is less |
| 189 | // than, equal to or greater than 0 if |shares_lhs| is, |
| 190 | // respectively, lower than, same as or higher than |shares_rhs|. |
| 191 | int CompareCpuShares(utils::CpuShares shares_lhs, |
| 192 | utils::CpuShares shares_rhs) { |
| 193 | return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs); |
| 194 | } |
| 195 | } // namespace |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 196 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 197 | // Tests the CPU shares enum is in the order we expect it. |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 198 | TEST(UtilsTest, CompareCpuSharesTest) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 199 | EXPECT_LT(CompareCpuShares(utils::kCpuSharesLow, |
| 200 | utils::kCpuSharesNormal), 0); |
| 201 | EXPECT_GT(CompareCpuShares(utils::kCpuSharesNormal, |
| 202 | utils::kCpuSharesLow), 0); |
| 203 | EXPECT_EQ(CompareCpuShares(utils::kCpuSharesNormal, |
| 204 | utils::kCpuSharesNormal), 0); |
| 205 | EXPECT_GT(CompareCpuShares(utils::kCpuSharesHigh, |
| 206 | utils::kCpuSharesNormal), 0); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 209 | TEST(UtilsTest, FuzzIntTest) { |
| 210 | static const unsigned int kRanges[] = { 0, 1, 2, 20 }; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 211 | for (unsigned int range : kRanges) { |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 212 | const int kValue = 50; |
| 213 | for (int tries = 0; tries < 100; ++tries) { |
| 214 | int value = utils::FuzzInt(kValue, range); |
| 215 | EXPECT_GE(value, kValue - range / 2); |
| 216 | EXPECT_LE(value, kValue + range - range / 2); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 221 | TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) { |
| 222 | string img; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 223 | EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr)); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 224 | ScopedPathUnlinker img_unlinker(img); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 225 | test_utils::CreateExtImageAtPath(img, nullptr); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 226 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 227 | EXPECT_EQ(0, test_utils::System(base::StringPrintf( |
Alex Deymo | 1f93d03 | 2015-03-10 18:58:32 -0700 | [diff] [blame] | 228 | "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none", |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 229 | img.c_str()))); |
| 230 | EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); |
| 231 | int block_count = 0; |
| 232 | int block_size = 0; |
| 233 | EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size)); |
| 234 | EXPECT_EQ(4096, block_size); |
| 235 | EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count); |
| 236 | } |
| 237 | |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 238 | // Squashfs example filesystem, generated with: |
| 239 | // echo hola>hola |
| 240 | // mksquashfs hola hola.sqfs -noappend -nopad |
| 241 | // hexdump hola.sqfs -e '16/1 "%02x, " "\n"' |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 242 | const uint8_t kSquashfsFile[] = { |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 243 | 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes |
| 244 | 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00, |
| 245 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00, |
| 246 | 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor |
| 247 | 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode |
| 248 | 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used |
| 249 | 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 250 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 251 | 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 252 | 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 253 | 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 254 | 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 255 | 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78, |
| 256 | 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0, |
| 257 | 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0, |
| 258 | 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc, |
| 259 | 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44, |
| 260 | 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5, |
| 261 | 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63, |
| 262 | 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0, |
| 263 | 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04, |
| 264 | 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00, |
| 265 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, |
| 266 | 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00, |
| 267 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78, |
| 268 | 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d, |
| 269 | 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00, |
| 270 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99, |
| 271 | 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd, |
| 272 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 273 | }; |
| 274 | |
| 275 | TEST(UtilsTest, GetSquashfs4Size) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 276 | uint8_t buffer[sizeof(kSquashfsFile)]; |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 277 | memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile)); |
| 278 | |
| 279 | int block_count = -1; |
| 280 | int block_size = -1; |
| 281 | // Not enough bytes passed. |
| 282 | EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr)); |
| 283 | |
| 284 | // The whole file system is passed, which is enough for parsing. |
| 285 | EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile), |
| 286 | &block_count, &block_size)); |
| 287 | EXPECT_EQ(4096, block_size); |
| 288 | EXPECT_EQ(1, block_count); |
| 289 | |
| 290 | // Modify the major version to 5. |
| 291 | uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c); |
| 292 | *s_major = 5; |
| 293 | EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr)); |
| 294 | memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile)); |
| 295 | |
| 296 | // Modify the bytes_used to have 6 blocks. |
| 297 | int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28); |
| 298 | *bytes_used = 4096 * 5 + 1; // 6 "blocks". |
| 299 | EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile), |
| 300 | &block_count, &block_size)); |
| 301 | EXPECT_EQ(4096, block_size); |
| 302 | EXPECT_EQ(6, block_count); |
| 303 | } |
| 304 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 305 | namespace { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 306 | void GetFileFormatTester(const string& expected, |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 307 | const vector<uint8_t>& contents) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 308 | test_utils::ScopedTempFile file; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 309 | ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(), |
| 310 | reinterpret_cast<const char*>(contents.data()), |
| 311 | contents.size())); |
| 312 | EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath())); |
| 313 | } |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 314 | } // namespace |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 315 | |
| 316 | TEST(UtilsTest, GetFileFormatTest) { |
| 317 | EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere")); |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 318 | GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8}); |
| 319 | GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 320 | |
| 321 | // Real tests from cros_installer on different boards. |
| 322 | // ELF 32-bit LSB executable, Intel 80386 |
| 323 | GetFileFormatTester( |
| 324 | "ELF 32-bit little-endian x86", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 325 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 326 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 327 | 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 328 | 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 329 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 330 | // ELF 32-bit LSB executable, MIPS |
| 331 | GetFileFormatTester( |
| 332 | "ELF 32-bit little-endian mips", |
| 333 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 334 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 335 | 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 336 | 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00}); |
| 337 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 338 | // ELF 32-bit LSB executable, ARM |
| 339 | GetFileFormatTester( |
| 340 | "ELF 32-bit little-endian arm", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 341 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 342 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 343 | 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 344 | 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 345 | |
| 346 | // ELF 64-bit LSB executable, x86-64 |
| 347 | GetFileFormatTester( |
| 348 | "ELF 64-bit little-endian x86-64", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 349 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, |
| 350 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 351 | 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 352 | 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 355 | TEST(UtilsTest, ScheduleCrashReporterUploadTest) { |
| 356 | // Not much to test. At least this tests for memory leaks, crashes, |
| 357 | // log errors. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 358 | FakeMessageLoop loop(nullptr); |
| 359 | loop.SetAsCurrent(); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 360 | utils::ScheduleCrashReporterUpload(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 361 | // Test that we scheduled one callback from the crash reporter. |
| 362 | EXPECT_EQ(1, chromeos::MessageLoopRunMaxIterations(&loop, 100)); |
| 363 | EXPECT_FALSE(loop.PendingTasks()); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 364 | } |
| 365 | |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 366 | TEST(UtilsTest, FormatTimeDeltaTest) { |
| 367 | // utils::FormatTimeDelta() is not locale-aware (it's only used for logging |
| 368 | // which is not localized) so we only need to test the C locale |
| 369 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)), |
| 370 | "0.1s"); |
| 371 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)), |
| 372 | "0s"); |
| 373 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)), |
| 374 | "1s"); |
| 375 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)), |
| 376 | "59s"); |
| 377 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)), |
| 378 | "1m0s"); |
| 379 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)), |
| 380 | "1m1s"); |
| 381 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)), |
| 382 | "1m30s"); |
| 383 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)), |
| 384 | "20m5s"); |
| 385 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)), |
| 386 | "1h0m0s"); |
| 387 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)), |
| 388 | "1h0m1s"); |
| 389 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)), |
| 390 | "1h1m1s"); |
| 391 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)), |
| 392 | "2h1m1s"); |
| 393 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)), |
| 394 | "1d0h0m0s"); |
| 395 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)), |
| 396 | "1d0h0m1s"); |
| 397 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)), |
| 398 | "2d7h33m20s"); |
| 399 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) + |
| 400 | base::TimeDelta::FromMilliseconds(1)), |
| 401 | "2d7h33m20.001s"); |
David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 402 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)), |
| 403 | "-1s"); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 404 | } |
| 405 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 406 | TEST(UtilsTest, TimeFromStructTimespecTest) { |
| 407 | struct timespec ts; |
| 408 | |
| 409 | // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970) |
| 410 | ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0}; |
| 411 | EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts)); |
| 412 | |
| 413 | // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001) |
| 414 | ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000, |
| 415 | .tv_nsec = 42 * 1000 * 1000}; |
| 416 | base::Time::Exploded exploded = (base::Time::Exploded) { |
| 417 | .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9, |
| 418 | .hour = 1, .minute = 46, .second = 40, .millisecond = 42}; |
| 419 | EXPECT_EQ(base::Time::FromUTCExploded(exploded), |
| 420 | utils::TimeFromStructTimespec(&ts)); |
| 421 | } |
| 422 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 423 | TEST(UtilsTest, DecodeAndStoreBase64String) { |
| 424 | base::FilePath path; |
| 425 | |
| 426 | // Ensure we return false on empty strings or invalid base64. |
| 427 | EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path)); |
| 428 | EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path)); |
| 429 | |
| 430 | // Pass known base64 and check that it matches. This string was generated |
| 431 | // the following way: |
| 432 | // |
| 433 | // $ echo "Update Engine" | base64 |
| 434 | // VXBkYXRlIEVuZ2luZQo= |
| 435 | EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=", |
| 436 | &path)); |
| 437 | ScopedPathUnlinker unlinker(path.value()); |
| 438 | string expected_contents = "Update Engine\n"; |
| 439 | string contents; |
| 440 | EXPECT_TRUE(utils::ReadFile(path.value(), &contents)); |
| 441 | EXPECT_EQ(contents, expected_contents); |
| 442 | EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size()); |
| 443 | } |
| 444 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 445 | TEST(UtilsTest, ConvertToOmahaInstallDate) { |
| 446 | // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a |
| 447 | // Monday. In Unix time, this point in time is easily obtained via |
| 448 | // the date(1) command like this: |
| 449 | // |
| 450 | // $ date +"%s" --date="Jan 1, 2007 0:00 PST" |
| 451 | const time_t omaha_epoch = 1167638400; |
| 452 | int value; |
| 453 | |
| 454 | // Points in time *on and after* the Omaha epoch should not fail. |
| 455 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 456 | base::Time::FromTimeT(omaha_epoch), &value)); |
| 457 | EXPECT_GE(value, 0); |
| 458 | |
| 459 | // Anything before the Omaha epoch should fail. We test it for two points. |
| 460 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
| 461 | base::Time::FromTimeT(omaha_epoch - 1), &value)); |
| 462 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
| 463 | base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value)); |
| 464 | |
| 465 | // Check that we jump from 0 to 7 exactly on the one-week mark, e.g. |
| 466 | // on Jan 8, 2007 0:00 PST. |
| 467 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 468 | base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value)); |
| 469 | EXPECT_EQ(value, 0); |
| 470 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 471 | base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value)); |
| 472 | EXPECT_EQ(value, 7); |
| 473 | |
| 474 | // Check a couple of more values. |
| 475 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 476 | base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value)); |
| 477 | EXPECT_EQ(value, 7); |
| 478 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 479 | base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value)); |
| 480 | EXPECT_EQ(value, 14); |
| 481 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 482 | base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value)); |
| 483 | EXPECT_EQ(value, 21); |
| 484 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 485 | base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value)); |
| 486 | EXPECT_EQ(value, 28); |
| 487 | |
| 488 | // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point |
| 489 | // where the Omaha InstallDate jumps 7 days. Its unix time is |
| 490 | // 1180940400. Notably, this is a point in time where Daylight |
| 491 | // Savings Time (DST) was is in effect (e.g. it's PDT, not PST). |
| 492 | // |
| 493 | // Note that as utils::ConvertToOmahaInstallDate() _deliberately_ |
| 494 | // ignores DST (as it's hard to implement in a thread-safe way using |
| 495 | // glibc, see comments in utils.h) we have to fudge by the DST |
| 496 | // offset which is one hour. Conveniently, if the function were |
| 497 | // someday modified to be DST aware, this test would have to be |
| 498 | // modified as well. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 499 | const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT. |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 500 | const time_t fudge = 3600; |
| 501 | int value1, value2; |
| 502 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 503 | base::Time::FromTimeT(dst_time + fudge - 1), &value1)); |
| 504 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 505 | base::Time::FromTimeT(dst_time + fudge), &value2)); |
| 506 | EXPECT_EQ(value1, value2 - 7); |
| 507 | } |
| 508 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 509 | TEST(UtilsTest, WallclockDurationHelper) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 510 | FakeSystemState fake_system_state; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 511 | FakeClock fake_clock; |
| 512 | base::TimeDelta duration; |
| 513 | string state_variable_key = "test-prefs"; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 514 | FakePrefs fake_prefs; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 515 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 516 | fake_system_state.set_clock(&fake_clock); |
| 517 | fake_system_state.set_prefs(&fake_prefs); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 518 | |
| 519 | // Initialize wallclock to 1 sec. |
| 520 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000)); |
| 521 | |
| 522 | // First time called so no previous measurement available. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 523 | EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 524 | state_variable_key, |
| 525 | &duration)); |
| 526 | |
| 527 | // Next time, we should get zero since the clock didn't advance. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 528 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 529 | state_variable_key, |
| 530 | &duration)); |
| 531 | EXPECT_EQ(duration.InSeconds(), 0); |
| 532 | |
| 533 | // We can also call it as many times as we want with it being |
| 534 | // considered a failure. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 535 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 536 | state_variable_key, |
| 537 | &duration)); |
| 538 | EXPECT_EQ(duration.InSeconds(), 0); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 539 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 540 | state_variable_key, |
| 541 | &duration)); |
| 542 | EXPECT_EQ(duration.InSeconds(), 0); |
| 543 | |
| 544 | // Advance the clock one second, then we should get 1 sec on the |
| 545 | // next call and 0 sec on the subsequent call. |
| 546 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 547 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 548 | state_variable_key, |
| 549 | &duration)); |
| 550 | EXPECT_EQ(duration.InSeconds(), 1); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 551 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 552 | state_variable_key, |
| 553 | &duration)); |
| 554 | EXPECT_EQ(duration.InSeconds(), 0); |
| 555 | |
| 556 | // Advance clock two seconds and we should get 2 sec and then 0 sec. |
| 557 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 558 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 559 | state_variable_key, |
| 560 | &duration)); |
| 561 | EXPECT_EQ(duration.InSeconds(), 2); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 562 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 563 | state_variable_key, |
| 564 | &duration)); |
| 565 | EXPECT_EQ(duration.InSeconds(), 0); |
| 566 | |
| 567 | // There's a possibility that the wallclock can go backwards (NTP |
| 568 | // adjustments, for example) so check that we properly handle this |
| 569 | // case. |
| 570 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 571 | EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 572 | state_variable_key, |
| 573 | &duration)); |
| 574 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 575 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 576 | state_variable_key, |
| 577 | &duration)); |
| 578 | EXPECT_EQ(duration.InSeconds(), 1); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | TEST(UtilsTest, MonotonicDurationHelper) { |
| 582 | int64_t storage = 0; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 583 | FakeSystemState fake_system_state; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 584 | FakeClock fake_clock; |
| 585 | base::TimeDelta duration; |
| 586 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 587 | fake_system_state.set_clock(&fake_clock); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 588 | |
| 589 | // Initialize monotonic clock to 1 sec. |
| 590 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000)); |
| 591 | |
| 592 | // First time called so no previous measurement available. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 593 | EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 594 | &storage, |
| 595 | &duration)); |
| 596 | |
| 597 | // Next time, we should get zero since the clock didn't advance. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 598 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 599 | &storage, |
| 600 | &duration)); |
| 601 | EXPECT_EQ(duration.InSeconds(), 0); |
| 602 | |
| 603 | // We can also call it as many times as we want with it being |
| 604 | // considered a failure. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 605 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 606 | &storage, |
| 607 | &duration)); |
| 608 | EXPECT_EQ(duration.InSeconds(), 0); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 609 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 610 | &storage, |
| 611 | &duration)); |
| 612 | EXPECT_EQ(duration.InSeconds(), 0); |
| 613 | |
| 614 | // Advance the clock one second, then we should get 1 sec on the |
| 615 | // next call and 0 sec on the subsequent call. |
| 616 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 617 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 618 | &storage, |
| 619 | &duration)); |
| 620 | EXPECT_EQ(duration.InSeconds(), 1); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 621 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 622 | &storage, |
| 623 | &duration)); |
| 624 | EXPECT_EQ(duration.InSeconds(), 0); |
| 625 | |
| 626 | // Advance clock two seconds and we should get 2 sec and then 0 sec. |
| 627 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 628 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 629 | &storage, |
| 630 | &duration)); |
| 631 | EXPECT_EQ(duration.InSeconds(), 2); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 632 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 633 | &storage, |
| 634 | &duration)); |
| 635 | EXPECT_EQ(duration.InSeconds(), 0); |
| 636 | } |
| 637 | |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 638 | TEST(UtilsTest, GetConnectionType) { |
| 639 | // Check that expected combinations map to the right value. |
| 640 | EXPECT_EQ(metrics::ConnectionType::kUnknown, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 641 | utils::GetConnectionType(NetworkConnectionType::kUnknown, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 642 | NetworkTethering::kUnknown)); |
| 643 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 644 | utils::GetConnectionType(NetworkConnectionType::kEthernet, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 645 | NetworkTethering::kUnknown)); |
| 646 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 647 | utils::GetConnectionType(NetworkConnectionType::kWifi, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 648 | NetworkTethering::kUnknown)); |
| 649 | EXPECT_EQ(metrics::ConnectionType::kWimax, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 650 | utils::GetConnectionType(NetworkConnectionType::kWimax, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 651 | NetworkTethering::kUnknown)); |
| 652 | EXPECT_EQ(metrics::ConnectionType::kBluetooth, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 653 | utils::GetConnectionType(NetworkConnectionType::kBluetooth, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 654 | NetworkTethering::kUnknown)); |
| 655 | EXPECT_EQ(metrics::ConnectionType::kCellular, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 656 | utils::GetConnectionType(NetworkConnectionType::kCellular, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 657 | NetworkTethering::kUnknown)); |
| 658 | EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 659 | utils::GetConnectionType(NetworkConnectionType::kEthernet, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 660 | NetworkTethering::kConfirmed)); |
| 661 | EXPECT_EQ(metrics::ConnectionType::kTetheredWifi, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 662 | utils::GetConnectionType(NetworkConnectionType::kWifi, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 663 | NetworkTethering::kConfirmed)); |
| 664 | |
| 665 | // Ensure that we don't report tethered ethernet unless it's confirmed. |
| 666 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 667 | utils::GetConnectionType(NetworkConnectionType::kEthernet, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 668 | NetworkTethering::kNotDetected)); |
| 669 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 670 | utils::GetConnectionType(NetworkConnectionType::kEthernet, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 671 | NetworkTethering::kSuspected)); |
| 672 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 673 | utils::GetConnectionType(NetworkConnectionType::kEthernet, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 674 | NetworkTethering::kUnknown)); |
| 675 | |
| 676 | // Ditto for tethered wifi. |
| 677 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 678 | utils::GetConnectionType(NetworkConnectionType::kWifi, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 679 | NetworkTethering::kNotDetected)); |
| 680 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 681 | utils::GetConnectionType(NetworkConnectionType::kWifi, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 682 | NetworkTethering::kSuspected)); |
| 683 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 684 | utils::GetConnectionType(NetworkConnectionType::kWifi, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 685 | NetworkTethering::kUnknown)); |
| 686 | } |
| 687 | |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 688 | TEST(UtilsTest, GetMinorVersion) { |
| 689 | // Test GetMinorVersion by verifying that it parses the conf file and returns |
| 690 | // the correct value. |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 691 | uint32_t minor_version; |
| 692 | |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 693 | chromeos::KeyValueStore store; |
| 694 | EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version)); |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 695 | |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 696 | EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n")); |
| 697 | EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version)); |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 698 | |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 699 | EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n")); |
| 700 | EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version)); |
| 701 | EXPECT_EQ(minor_version, 123); |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Nam T. Nguyen | 2b67a59 | 2014-12-03 14:56:00 -0800 | [diff] [blame] | 704 | static bool BoolMacroTestHelper() { |
| 705 | int i = 1; |
| 706 | unsigned int ui = 1; |
| 707 | bool b = 1; |
| 708 | std::unique_ptr<char> cptr(new char); |
| 709 | |
| 710 | TEST_AND_RETURN_FALSE(i); |
| 711 | TEST_AND_RETURN_FALSE(ui); |
| 712 | TEST_AND_RETURN_FALSE(b); |
| 713 | TEST_AND_RETURN_FALSE(cptr); |
| 714 | |
| 715 | TEST_AND_RETURN_FALSE_ERRNO(i); |
| 716 | TEST_AND_RETURN_FALSE_ERRNO(ui); |
| 717 | TEST_AND_RETURN_FALSE_ERRNO(b); |
| 718 | TEST_AND_RETURN_FALSE_ERRNO(cptr); |
| 719 | |
| 720 | return true; |
| 721 | } |
| 722 | |
| 723 | static void VoidMacroTestHelper(bool* ret) { |
| 724 | int i = 1; |
| 725 | unsigned int ui = 1; |
| 726 | bool b = 1; |
| 727 | std::unique_ptr<char> cptr(new char); |
| 728 | |
| 729 | *ret = false; |
| 730 | |
| 731 | TEST_AND_RETURN(i); |
| 732 | TEST_AND_RETURN(ui); |
| 733 | TEST_AND_RETURN(b); |
| 734 | TEST_AND_RETURN(cptr); |
| 735 | |
| 736 | TEST_AND_RETURN_ERRNO(i); |
| 737 | TEST_AND_RETURN_ERRNO(ui); |
| 738 | TEST_AND_RETURN_ERRNO(b); |
| 739 | TEST_AND_RETURN_ERRNO(cptr); |
| 740 | |
| 741 | *ret = true; |
| 742 | } |
| 743 | |
| 744 | TEST(UtilsTest, TestMacros) { |
| 745 | bool void_test = false; |
| 746 | VoidMacroTestHelper(&void_test); |
| 747 | EXPECT_TRUE(void_test); |
| 748 | |
| 749 | EXPECT_TRUE(BoolMacroTestHelper()); |
| 750 | } |
| 751 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 752 | } // namespace chromeos_update_engine |