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