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 | |
| 142 | TEST(UtilsTest, StringHasSuffixTest) { |
| 143 | EXPECT_TRUE(utils::StringHasSuffix("foo", "foo")); |
| 144 | EXPECT_TRUE(utils::StringHasSuffix("foo", "o")); |
| 145 | EXPECT_TRUE(utils::StringHasSuffix("", "")); |
| 146 | EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc")); |
| 147 | EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere")); |
| 148 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh")); |
| 149 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "")); |
| 150 | EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo")); |
| 151 | EXPECT_FALSE(utils::StringHasSuffix("", "x")); |
| 152 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg")); |
| 153 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab")); |
| 154 | } |
| 155 | |
| 156 | TEST(UtilsTest, StringHasPrefixTest) { |
| 157 | EXPECT_TRUE(utils::StringHasPrefix("foo", "foo")); |
| 158 | EXPECT_TRUE(utils::StringHasPrefix("foo", "f")); |
| 159 | EXPECT_TRUE(utils::StringHasPrefix("", "")); |
| 160 | EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc")); |
| 161 | EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl")); |
| 162 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab")); |
| 163 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "")); |
| 164 | EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa")); |
| 165 | EXPECT_FALSE(utils::StringHasPrefix("", "x")); |
| 166 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc")); |
| 167 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh")); |
| 168 | } |
| 169 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 170 | TEST(UtilsTest, RecursiveUnlinkDirTest) { |
Gilad Arnold | b4346a2 | 2013-07-16 06:44:45 -0700 | [diff] [blame] | 171 | string first_dir_name; |
| 172 | ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-a-XXXXXX", |
| 173 | &first_dir_name)); |
| 174 | ASSERT_EQ(0, Chmod(first_dir_name, 0755)); |
| 175 | string second_dir_name; |
| 176 | ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-b-XXXXXX", |
| 177 | &second_dir_name)); |
| 178 | ASSERT_EQ(0, Chmod(second_dir_name, 0755)); |
| 179 | |
| 180 | EXPECT_EQ(0, Symlink(string("../") + first_dir_name, |
| 181 | second_dir_name + "/link")); |
| 182 | EXPECT_EQ(0, System(string("echo hi > ") + second_dir_name + "/file")); |
| 183 | EXPECT_EQ(0, Mkdir(second_dir_name + "/dir", 0755)); |
| 184 | EXPECT_EQ(0, System(string("echo ok > ") + second_dir_name + "/dir/subfile")); |
| 185 | EXPECT_TRUE(utils::RecursiveUnlinkDir(second_dir_name)); |
| 186 | EXPECT_TRUE(utils::FileExists(first_dir_name.c_str())); |
| 187 | EXPECT_EQ(0, System(string("rm -rf ") + first_dir_name)); |
| 188 | EXPECT_FALSE(utils::FileExists(second_dir_name.c_str())); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 189 | EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist")); |
| 190 | } |
| 191 | |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 192 | TEST(UtilsTest, IsSymlinkTest) { |
| 193 | string temp_dir; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 194 | EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir)); |
Alex Deymo | 8d92529 | 2014-05-21 19:15:25 -0700 | [diff] [blame] | 195 | string temp_file = temp_dir + "/temp-file"; |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 196 | EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0)); |
Alex Deymo | 8d92529 | 2014-05-21 19:15:25 -0700 | [diff] [blame] | 197 | string temp_symlink = temp_dir + "/temp-symlink"; |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 198 | EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str())); |
| 199 | EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str())); |
| 200 | EXPECT_FALSE(utils::IsSymlink(temp_file.c_str())); |
| 201 | EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str())); |
| 202 | EXPECT_FALSE(utils::IsSymlink("/non/existent/path")); |
| 203 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 204 | } |
| 205 | |
Alex Deymo | 7dc4c50 | 2014-05-20 20:09:58 -0700 | [diff] [blame] | 206 | TEST(UtilsTest, IsDirTest) { |
| 207 | string temp_dir; |
| 208 | EXPECT_TRUE(utils::MakeTempDirectory("isdir-test.XXXXXX", &temp_dir)); |
Alex Deymo | 8d92529 | 2014-05-21 19:15:25 -0700 | [diff] [blame] | 209 | string temp_file = temp_dir + "/temp-file"; |
Alex Deymo | 7dc4c50 | 2014-05-20 20:09:58 -0700 | [diff] [blame] | 210 | EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0)); |
Alex Deymo | 8d92529 | 2014-05-21 19:15:25 -0700 | [diff] [blame] | 211 | string temp_symlink = temp_dir + "/temp-symlink"; |
Alex Deymo | 7dc4c50 | 2014-05-20 20:09:58 -0700 | [diff] [blame] | 212 | EXPECT_EQ(0, symlink(temp_dir.c_str(), temp_symlink.c_str())); |
| 213 | EXPECT_TRUE(utils::IsDir(temp_dir.c_str())); |
| 214 | EXPECT_FALSE(utils::IsDir(temp_file.c_str())); |
| 215 | EXPECT_FALSE(utils::IsDir(temp_symlink.c_str())); |
| 216 | EXPECT_FALSE(utils::IsDir("/non/existent/path")); |
| 217 | ASSERT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 218 | } |
| 219 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 220 | TEST(UtilsTest, GetDiskNameTest) { |
| 221 | EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3")); |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 222 | EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234")); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 223 | EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3")); |
| 224 | EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p")); |
| 225 | EXPECT_EQ("", utils::GetDiskName("/dev/sda")); |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 226 | EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2")); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 227 | EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar")); |
| 228 | EXPECT_EQ("", utils::GetDiskName("/")); |
| 229 | EXPECT_EQ("", utils::GetDiskName("")); |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | TEST(UtilsTest, SysfsBlockDeviceTest) { |
| 233 | EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda")); |
| 234 | EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda")); |
| 235 | EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar")); |
| 236 | EXPECT_EQ("", utils::SysfsBlockDevice("/")); |
| 237 | EXPECT_EQ("", utils::SysfsBlockDevice("./")); |
| 238 | EXPECT_EQ("", utils::SysfsBlockDevice("")); |
| 239 | } |
| 240 | |
| 241 | TEST(UtilsTest, IsRemovableDeviceTest) { |
| 242 | EXPECT_FALSE(utils::IsRemovableDevice("")); |
| 243 | EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device")); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 246 | TEST(UtilsTest, GetPartitionNumberTest) { |
| 247 | EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3")); |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 248 | EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3")); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 249 | EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123")); |
| 250 | EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2")); |
| 251 | EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p")); |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 252 | EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2")); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 253 | EXPECT_EQ(0, utils::GetPartitionNumber("")); |
| 254 | EXPECT_EQ(0, utils::GetPartitionNumber("/")); |
| 255 | EXPECT_EQ(0, utils::GetPartitionNumber("/dev/")); |
| 256 | EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda")); |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 257 | EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10")); |
| 258 | EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11")); |
| 259 | EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0")); |
| 260 | EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0")); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 263 | TEST(UtilsTest, MakePartitionNameTest) { |
| 264 | EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4)); |
| 265 | EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123)); |
| 266 | EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2)); |
| 267 | EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2)); |
| 268 | EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8)); |
| 269 | EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2)); |
| 270 | EXPECT_EQ("/dev/ubiblock3_0", utils::MakePartitionName("/dev/ubiblock", 3)); |
| 271 | } |
| 272 | |
| 273 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 274 | TEST(UtilsTest, CompareCpuSharesTest) { |
| 275 | EXPECT_LT(utils::CompareCpuShares(utils::kCpuSharesLow, |
| 276 | utils::kCpuSharesNormal), 0); |
| 277 | EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesNormal, |
| 278 | utils::kCpuSharesLow), 0); |
| 279 | EXPECT_EQ(utils::CompareCpuShares(utils::kCpuSharesNormal, |
| 280 | utils::kCpuSharesNormal), 0); |
| 281 | EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesHigh, |
| 282 | utils::kCpuSharesNormal), 0); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 285 | TEST(UtilsTest, FuzzIntTest) { |
| 286 | static const unsigned int kRanges[] = { 0, 1, 2, 20 }; |
| 287 | for (size_t r = 0; r < arraysize(kRanges); ++r) { |
| 288 | unsigned int range = kRanges[r]; |
| 289 | const int kValue = 50; |
| 290 | for (int tries = 0; tries < 100; ++tries) { |
| 291 | int value = utils::FuzzInt(kValue, range); |
| 292 | EXPECT_GE(value, kValue - range / 2); |
| 293 | EXPECT_LE(value, kValue + range - range / 2); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 298 | TEST(UtilsTest, ApplyMapTest) { |
| 299 | int initial_values[] = {1, 2, 3, 4, 6}; |
| 300 | vector<int> collection(&initial_values[0], |
| 301 | initial_values + arraysize(initial_values)); |
| 302 | EXPECT_EQ(arraysize(initial_values), collection.size()); |
| 303 | int expected_values[] = {1, 2, 5, 4, 8}; |
| 304 | map<int, int> value_map; |
| 305 | value_map[3] = 5; |
| 306 | value_map[6] = 8; |
| 307 | value_map[5] = 10; |
| 308 | |
| 309 | utils::ApplyMap(&collection, value_map); |
| 310 | |
| 311 | size_t index = 0; |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame^] | 312 | for (const int value : collection) { |
| 313 | EXPECT_EQ(expected_values[index++], value); |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 317 | TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) { |
| 318 | string img; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 319 | EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr)); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 320 | ScopedPathUnlinker img_unlinker(img); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 321 | CreateExtImageAtPath(img, nullptr); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 322 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
| 323 | EXPECT_EQ(0, System(base::StringPrintf( |
| 324 | "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1", |
| 325 | img.c_str()))); |
| 326 | EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); |
| 327 | int block_count = 0; |
| 328 | int block_size = 0; |
| 329 | EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size)); |
| 330 | EXPECT_EQ(4096, block_size); |
| 331 | EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count); |
| 332 | } |
| 333 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 334 | TEST(UtilsTest, GetInstallDevTest) { |
| 335 | string boot_dev = "/dev/sda5"; |
| 336 | string install_dev; |
| 337 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 338 | EXPECT_EQ(install_dev, "/dev/sda3"); |
| 339 | |
| 340 | boot_dev = "/dev/sda3"; |
| 341 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 342 | EXPECT_EQ(install_dev, "/dev/sda5"); |
| 343 | |
| 344 | boot_dev = "/dev/sda12"; |
| 345 | EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev)); |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame] | 346 | |
| 347 | boot_dev = "/dev/ubiblock3_0"; |
| 348 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 349 | EXPECT_EQ(install_dev, "/dev/ubiblock5_0"); |
| 350 | |
| 351 | boot_dev = "/dev/ubiblock5_0"; |
| 352 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 353 | EXPECT_EQ(install_dev, "/dev/ubiblock3_0"); |
| 354 | |
| 355 | boot_dev = "/dev/ubiblock12_0"; |
| 356 | EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev)); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 359 | namespace { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 360 | void GetFileFormatTester(const string& expected, |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 361 | const vector<uint8_t>& contents) { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 362 | ScopedTempFile file; |
| 363 | ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(), |
| 364 | reinterpret_cast<const char*>(contents.data()), |
| 365 | contents.size())); |
| 366 | EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath())); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | TEST(UtilsTest, GetFileFormatTest) { |
| 371 | EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere")); |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 372 | GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8}); |
| 373 | GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 374 | |
| 375 | // Real tests from cros_installer on different boards. |
| 376 | // ELF 32-bit LSB executable, Intel 80386 |
| 377 | GetFileFormatTester( |
| 378 | "ELF 32-bit little-endian x86", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 379 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 380 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 381 | 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 382 | 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 383 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 384 | // ELF 32-bit LSB executable, MIPS |
| 385 | GetFileFormatTester( |
| 386 | "ELF 32-bit little-endian mips", |
| 387 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 388 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 389 | 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 390 | 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00}); |
| 391 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 392 | // ELF 32-bit LSB executable, ARM |
| 393 | GetFileFormatTester( |
| 394 | "ELF 32-bit little-endian arm", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 395 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, |
| 396 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 397 | 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 398 | 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 399 | |
| 400 | // ELF 64-bit LSB executable, x86-64 |
| 401 | GetFileFormatTester( |
| 402 | "ELF 64-bit little-endian x86-64", |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 403 | vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, |
| 404 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 405 | 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 406 | 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | namespace { |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 410 | gboolean TerminateScheduleCrashReporterUploadTest(void* arg) { |
| 411 | GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg); |
| 412 | g_main_loop_quit(loop); |
| 413 | return FALSE; // Don't call this callback again |
| 414 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 415 | } // namespace |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 416 | |
| 417 | TEST(UtilsTest, ScheduleCrashReporterUploadTest) { |
| 418 | // Not much to test. At least this tests for memory leaks, crashes, |
| 419 | // log errors. |
| 420 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 421 | utils::ScheduleCrashReporterUpload(); |
| 422 | g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop); |
| 423 | g_main_loop_run(loop); |
| 424 | g_main_loop_unref(loop); |
| 425 | } |
| 426 | |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 427 | TEST(UtilsTest, FormatTimeDeltaTest) { |
| 428 | // utils::FormatTimeDelta() is not locale-aware (it's only used for logging |
| 429 | // which is not localized) so we only need to test the C locale |
| 430 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)), |
| 431 | "0.1s"); |
| 432 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)), |
| 433 | "0s"); |
| 434 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)), |
| 435 | "1s"); |
| 436 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)), |
| 437 | "59s"); |
| 438 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)), |
| 439 | "1m0s"); |
| 440 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)), |
| 441 | "1m1s"); |
| 442 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)), |
| 443 | "1m30s"); |
| 444 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)), |
| 445 | "20m5s"); |
| 446 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)), |
| 447 | "1h0m0s"); |
| 448 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)), |
| 449 | "1h0m1s"); |
| 450 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)), |
| 451 | "1h1m1s"); |
| 452 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)), |
| 453 | "2h1m1s"); |
| 454 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)), |
| 455 | "1d0h0m0s"); |
| 456 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)), |
| 457 | "1d0h0m1s"); |
| 458 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)), |
| 459 | "2d7h33m20s"); |
| 460 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) + |
| 461 | base::TimeDelta::FromMilliseconds(1)), |
| 462 | "2d7h33m20.001s"); |
David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 463 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)), |
| 464 | "-1s"); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 465 | } |
| 466 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 467 | TEST(UtilsTest, TimeFromStructTimespecTest) { |
| 468 | struct timespec ts; |
| 469 | |
| 470 | // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970) |
| 471 | ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0}; |
| 472 | EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts)); |
| 473 | |
| 474 | // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001) |
| 475 | ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000, |
| 476 | .tv_nsec = 42 * 1000 * 1000}; |
| 477 | base::Time::Exploded exploded = (base::Time::Exploded) { |
| 478 | .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9, |
| 479 | .hour = 1, .minute = 46, .second = 40, .millisecond = 42}; |
| 480 | EXPECT_EQ(base::Time::FromUTCExploded(exploded), |
| 481 | utils::TimeFromStructTimespec(&ts)); |
| 482 | } |
| 483 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 484 | TEST(UtilsTest, DecodeAndStoreBase64String) { |
| 485 | base::FilePath path; |
| 486 | |
| 487 | // Ensure we return false on empty strings or invalid base64. |
| 488 | EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path)); |
| 489 | EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path)); |
| 490 | |
| 491 | // Pass known base64 and check that it matches. This string was generated |
| 492 | // the following way: |
| 493 | // |
| 494 | // $ echo "Update Engine" | base64 |
| 495 | // VXBkYXRlIEVuZ2luZQo= |
| 496 | EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=", |
| 497 | &path)); |
| 498 | ScopedPathUnlinker unlinker(path.value()); |
| 499 | string expected_contents = "Update Engine\n"; |
| 500 | string contents; |
| 501 | EXPECT_TRUE(utils::ReadFile(path.value(), &contents)); |
| 502 | EXPECT_EQ(contents, expected_contents); |
| 503 | EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size()); |
| 504 | } |
| 505 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 506 | TEST(UtilsTest, ConvertToOmahaInstallDate) { |
| 507 | // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a |
| 508 | // Monday. In Unix time, this point in time is easily obtained via |
| 509 | // the date(1) command like this: |
| 510 | // |
| 511 | // $ date +"%s" --date="Jan 1, 2007 0:00 PST" |
| 512 | const time_t omaha_epoch = 1167638400; |
| 513 | int value; |
| 514 | |
| 515 | // Points in time *on and after* the Omaha epoch should not fail. |
| 516 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 517 | base::Time::FromTimeT(omaha_epoch), &value)); |
| 518 | EXPECT_GE(value, 0); |
| 519 | |
| 520 | // Anything before the Omaha epoch should fail. We test it for two points. |
| 521 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
| 522 | base::Time::FromTimeT(omaha_epoch - 1), &value)); |
| 523 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
| 524 | base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value)); |
| 525 | |
| 526 | // Check that we jump from 0 to 7 exactly on the one-week mark, e.g. |
| 527 | // on Jan 8, 2007 0:00 PST. |
| 528 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 529 | base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value)); |
| 530 | EXPECT_EQ(value, 0); |
| 531 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 532 | base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value)); |
| 533 | EXPECT_EQ(value, 7); |
| 534 | |
| 535 | // Check a couple of more values. |
| 536 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 537 | base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value)); |
| 538 | EXPECT_EQ(value, 7); |
| 539 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 540 | base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value)); |
| 541 | EXPECT_EQ(value, 14); |
| 542 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 543 | base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value)); |
| 544 | EXPECT_EQ(value, 21); |
| 545 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 546 | base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value)); |
| 547 | EXPECT_EQ(value, 28); |
| 548 | |
| 549 | // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point |
| 550 | // where the Omaha InstallDate jumps 7 days. Its unix time is |
| 551 | // 1180940400. Notably, this is a point in time where Daylight |
| 552 | // Savings Time (DST) was is in effect (e.g. it's PDT, not PST). |
| 553 | // |
| 554 | // Note that as utils::ConvertToOmahaInstallDate() _deliberately_ |
| 555 | // ignores DST (as it's hard to implement in a thread-safe way using |
| 556 | // glibc, see comments in utils.h) we have to fudge by the DST |
| 557 | // offset which is one hour. Conveniently, if the function were |
| 558 | // someday modified to be DST aware, this test would have to be |
| 559 | // modified as well. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 560 | const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT. |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 561 | const time_t fudge = 3600; |
| 562 | int value1, value2; |
| 563 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 564 | base::Time::FromTimeT(dst_time + fudge - 1), &value1)); |
| 565 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 566 | base::Time::FromTimeT(dst_time + fudge), &value2)); |
| 567 | EXPECT_EQ(value1, value2 - 7); |
| 568 | } |
| 569 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 570 | TEST(UtilsTest, WallclockDurationHelper) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 571 | FakeSystemState fake_system_state; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 572 | FakeClock fake_clock; |
| 573 | base::TimeDelta duration; |
| 574 | string state_variable_key = "test-prefs"; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 575 | FakePrefs fake_prefs; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 576 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 577 | fake_system_state.set_clock(&fake_clock); |
| 578 | fake_system_state.set_prefs(&fake_prefs); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 579 | |
| 580 | // Initialize wallclock to 1 sec. |
| 581 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000)); |
| 582 | |
| 583 | // First time called so no previous measurement available. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 584 | EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 585 | state_variable_key, |
| 586 | &duration)); |
| 587 | |
| 588 | // Next time, we should get zero since the clock didn't advance. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 589 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 590 | state_variable_key, |
| 591 | &duration)); |
| 592 | EXPECT_EQ(duration.InSeconds(), 0); |
| 593 | |
| 594 | // We can also call it as many times as we want with it being |
| 595 | // considered a failure. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 596 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 597 | state_variable_key, |
| 598 | &duration)); |
| 599 | EXPECT_EQ(duration.InSeconds(), 0); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 600 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 601 | state_variable_key, |
| 602 | &duration)); |
| 603 | EXPECT_EQ(duration.InSeconds(), 0); |
| 604 | |
| 605 | // Advance the clock one second, then we should get 1 sec on the |
| 606 | // next call and 0 sec on the subsequent call. |
| 607 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 608 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 609 | state_variable_key, |
| 610 | &duration)); |
| 611 | EXPECT_EQ(duration.InSeconds(), 1); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 612 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 613 | state_variable_key, |
| 614 | &duration)); |
| 615 | EXPECT_EQ(duration.InSeconds(), 0); |
| 616 | |
| 617 | // Advance clock two seconds and we should get 2 sec and then 0 sec. |
| 618 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 619 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 620 | state_variable_key, |
| 621 | &duration)); |
| 622 | EXPECT_EQ(duration.InSeconds(), 2); |
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); |
| 627 | |
| 628 | // There's a possibility that the wallclock can go backwards (NTP |
| 629 | // adjustments, for example) so check that we properly handle this |
| 630 | // case. |
| 631 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 632 | EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 633 | state_variable_key, |
| 634 | &duration)); |
| 635 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 636 | EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 637 | state_variable_key, |
| 638 | &duration)); |
| 639 | EXPECT_EQ(duration.InSeconds(), 1); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | TEST(UtilsTest, MonotonicDurationHelper) { |
| 643 | int64_t storage = 0; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 644 | FakeSystemState fake_system_state; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 645 | FakeClock fake_clock; |
| 646 | base::TimeDelta duration; |
| 647 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 648 | fake_system_state.set_clock(&fake_clock); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 649 | |
| 650 | // Initialize monotonic clock to 1 sec. |
| 651 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000)); |
| 652 | |
| 653 | // First time called so no previous measurement available. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 654 | EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 655 | &storage, |
| 656 | &duration)); |
| 657 | |
| 658 | // Next time, we should get zero since the clock didn't advance. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 659 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 660 | &storage, |
| 661 | &duration)); |
| 662 | EXPECT_EQ(duration.InSeconds(), 0); |
| 663 | |
| 664 | // We can also call it as many times as we want with it being |
| 665 | // considered a failure. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 666 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 667 | &storage, |
| 668 | &duration)); |
| 669 | EXPECT_EQ(duration.InSeconds(), 0); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 670 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 671 | &storage, |
| 672 | &duration)); |
| 673 | EXPECT_EQ(duration.InSeconds(), 0); |
| 674 | |
| 675 | // Advance the clock one second, then we should get 1 sec on the |
| 676 | // next call and 0 sec on the subsequent call. |
| 677 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 678 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 679 | &storage, |
| 680 | &duration)); |
| 681 | EXPECT_EQ(duration.InSeconds(), 1); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 682 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 683 | &storage, |
| 684 | &duration)); |
| 685 | EXPECT_EQ(duration.InSeconds(), 0); |
| 686 | |
| 687 | // Advance clock two seconds and we should get 2 sec and then 0 sec. |
| 688 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000)); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 689 | EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 690 | &storage, |
| 691 | &duration)); |
| 692 | EXPECT_EQ(duration.InSeconds(), 2); |
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); |
| 697 | } |
| 698 | |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 699 | TEST(UtilsTest, GetConnectionType) { |
| 700 | // Check that expected combinations map to the right value. |
| 701 | EXPECT_EQ(metrics::ConnectionType::kUnknown, |
| 702 | utils::GetConnectionType(kNetUnknown, |
| 703 | NetworkTethering::kUnknown)); |
| 704 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
| 705 | utils::GetConnectionType(kNetEthernet, |
| 706 | NetworkTethering::kUnknown)); |
| 707 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
| 708 | utils::GetConnectionType(kNetWifi, |
| 709 | NetworkTethering::kUnknown)); |
| 710 | EXPECT_EQ(metrics::ConnectionType::kWimax, |
| 711 | utils::GetConnectionType(kNetWimax, |
| 712 | NetworkTethering::kUnknown)); |
| 713 | EXPECT_EQ(metrics::ConnectionType::kBluetooth, |
| 714 | utils::GetConnectionType(kNetBluetooth, |
| 715 | NetworkTethering::kUnknown)); |
| 716 | EXPECT_EQ(metrics::ConnectionType::kCellular, |
| 717 | utils::GetConnectionType(kNetCellular, |
| 718 | NetworkTethering::kUnknown)); |
| 719 | EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet, |
| 720 | utils::GetConnectionType(kNetEthernet, |
| 721 | NetworkTethering::kConfirmed)); |
| 722 | EXPECT_EQ(metrics::ConnectionType::kTetheredWifi, |
| 723 | utils::GetConnectionType(kNetWifi, |
| 724 | NetworkTethering::kConfirmed)); |
| 725 | |
| 726 | // Ensure that we don't report tethered ethernet unless it's confirmed. |
| 727 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
| 728 | utils::GetConnectionType(kNetEthernet, |
| 729 | NetworkTethering::kNotDetected)); |
| 730 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
| 731 | utils::GetConnectionType(kNetEthernet, |
| 732 | NetworkTethering::kSuspected)); |
| 733 | EXPECT_EQ(metrics::ConnectionType::kEthernet, |
| 734 | utils::GetConnectionType(kNetEthernet, |
| 735 | NetworkTethering::kUnknown)); |
| 736 | |
| 737 | // Ditto for tethered wifi. |
| 738 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
| 739 | utils::GetConnectionType(kNetWifi, |
| 740 | NetworkTethering::kNotDetected)); |
| 741 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
| 742 | utils::GetConnectionType(kNetWifi, |
| 743 | NetworkTethering::kSuspected)); |
| 744 | EXPECT_EQ(metrics::ConnectionType::kWifi, |
| 745 | utils::GetConnectionType(kNetWifi, |
| 746 | NetworkTethering::kUnknown)); |
| 747 | } |
| 748 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 749 | } // namespace chromeos_update_engine |