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