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 | |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <errno.h> |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 8 | |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 9 | #include <map> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 10 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <vector> |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 12 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 13 | #include <base/file_path.h> |
| 14 | #include <base/file_util.h> |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 15 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 16 | #include <base/stringprintf.h> |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include "update_engine/test_utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 20 | #include "update_engine/utils.h" |
| 21 | |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 22 | using std::map; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 23 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | using std::vector; |
| 25 | |
| 26 | namespace chromeos_update_engine { |
| 27 | |
| 28 | class UtilsTest : public ::testing::Test { }; |
| 29 | |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 30 | TEST(UtilsTest, CanParseECVersion) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 31 | // Should be able to parse and valid key value line. |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 32 | EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345")); |
| 33 | EXPECT_EQ("123456", utils::ParseECVersion( |
| 34 | "b=1231a fw_version=123456 a=fasd2")); |
| 35 | EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345")); |
| 36 | EXPECT_EQ("00VFA616", utils::ParseECVersion( |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 37 | "vendor=\"sam\" fw_version=\"00VFA616\"")); |
| 38 | |
| 39 | // For invalid entries, should return the empty string. |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 40 | EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2")); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 41 | } |
| 42 | |
J. Richard Barnette | 3084293 | 2013-10-28 15:04:23 -0700 | [diff] [blame] | 43 | |
| 44 | TEST(UtilsTest, KernelDeviceOfBootDevice) { |
| 45 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo")); |
| 46 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0")); |
| 47 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1")); |
| 48 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2")); |
| 49 | EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3")); |
| 50 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4")); |
| 51 | EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5")); |
| 52 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6")); |
| 53 | EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7")); |
| 54 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8")); |
| 55 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9")); |
| 56 | |
| 57 | EXPECT_EQ("/dev/mmcblk0p2", |
| 58 | utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3")); |
| 59 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4")); |
| 60 | |
| 61 | EXPECT_EQ("/dev/ubi2", utils::KernelDeviceOfBootDevice("/dev/ubi3")); |
| 62 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4")); |
| 63 | |
| 64 | EXPECT_EQ("/dev/mtdblock2", |
| 65 | utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0")); |
| 66 | EXPECT_EQ("/dev/mtdblock4", |
| 67 | utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0")); |
| 68 | EXPECT_EQ("/dev/mtdblock6", |
| 69 | utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0")); |
| 70 | EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0")); |
| 71 | } |
| 72 | |
| 73 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | TEST(UtilsTest, NormalizePathTest) { |
| 75 | EXPECT_EQ("", utils::NormalizePath("", false)); |
| 76 | EXPECT_EQ("", utils::NormalizePath("", true)); |
| 77 | EXPECT_EQ("/", utils::NormalizePath("/", false)); |
| 78 | EXPECT_EQ("", utils::NormalizePath("/", true)); |
| 79 | EXPECT_EQ("/", utils::NormalizePath("//", false)); |
| 80 | EXPECT_EQ("", utils::NormalizePath("//", true)); |
| 81 | EXPECT_EQ("foo", utils::NormalizePath("foo", false)); |
| 82 | EXPECT_EQ("foo", utils::NormalizePath("foo", true)); |
| 83 | EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false)); |
| 84 | EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true)); |
| 85 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 86 | false)); |
| 87 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 88 | true)); |
| 89 | EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 90 | false)); |
| 91 | EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 92 | true)); |
| 93 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false)); |
| 94 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true)); |
| 95 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", false)); |
| 96 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", true)); |
| 97 | EXPECT_EQ("/spaces s/ ok/s / / /", |
| 98 | utils::NormalizePath("/spaces s/ ok/s / / /", false)); |
| 99 | EXPECT_EQ("/spaces s/ ok/s / / ", |
| 100 | utils::NormalizePath("/spaces s/ ok/s / / /", true)); |
| 101 | } |
| 102 | |
| 103 | TEST(UtilsTest, ReadFileFailure) { |
| 104 | vector<char> empty; |
| 105 | EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty)); |
| 106 | } |
| 107 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 108 | TEST(UtilsTest, ReadFileChunk) { |
| 109 | FilePath file; |
| 110 | EXPECT_TRUE(file_util::CreateTemporaryFile(&file)); |
| 111 | ScopedPathUnlinker unlinker(file.value()); |
| 112 | vector<char> data; |
| 113 | const size_t kSize = 1024 * 1024; |
| 114 | for (size_t i = 0; i < kSize; i++) { |
| 115 | data.push_back(i % 255); |
| 116 | } |
| 117 | EXPECT_TRUE(utils::WriteFile(file.value().c_str(), &data[0], data.size())); |
| 118 | vector<char> in_data; |
| 119 | EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data)); |
| 120 | EXPECT_TRUE(in_data.empty()); |
| 121 | EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data)); |
| 122 | EXPECT_TRUE(data == in_data); |
| 123 | in_data.clear(); |
| 124 | EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data)); |
| 125 | EXPECT_TRUE(vector<char>(data.begin() + 10, data.begin() + 10 + 20) == |
| 126 | in_data); |
| 127 | } |
| 128 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 129 | TEST(UtilsTest, ErrnoNumberAsStringTest) { |
| 130 | EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT)); |
| 131 | } |
| 132 | |
| 133 | TEST(UtilsTest, StringHasSuffixTest) { |
| 134 | EXPECT_TRUE(utils::StringHasSuffix("foo", "foo")); |
| 135 | EXPECT_TRUE(utils::StringHasSuffix("foo", "o")); |
| 136 | EXPECT_TRUE(utils::StringHasSuffix("", "")); |
| 137 | EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc")); |
| 138 | EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere")); |
| 139 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh")); |
| 140 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "")); |
| 141 | EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo")); |
| 142 | EXPECT_FALSE(utils::StringHasSuffix("", "x")); |
| 143 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg")); |
| 144 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab")); |
| 145 | } |
| 146 | |
| 147 | TEST(UtilsTest, StringHasPrefixTest) { |
| 148 | EXPECT_TRUE(utils::StringHasPrefix("foo", "foo")); |
| 149 | EXPECT_TRUE(utils::StringHasPrefix("foo", "f")); |
| 150 | EXPECT_TRUE(utils::StringHasPrefix("", "")); |
| 151 | EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc")); |
| 152 | EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl")); |
| 153 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab")); |
| 154 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "")); |
| 155 | EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa")); |
| 156 | EXPECT_FALSE(utils::StringHasPrefix("", "x")); |
| 157 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc")); |
| 158 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh")); |
| 159 | } |
| 160 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 161 | TEST(UtilsTest, RecursiveUnlinkDirTest) { |
Gilad Arnold | b4346a2 | 2013-07-16 06:44:45 -0700 | [diff] [blame] | 162 | string first_dir_name; |
| 163 | ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-a-XXXXXX", |
| 164 | &first_dir_name)); |
| 165 | ASSERT_EQ(0, Chmod(first_dir_name, 0755)); |
| 166 | string second_dir_name; |
| 167 | ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-b-XXXXXX", |
| 168 | &second_dir_name)); |
| 169 | ASSERT_EQ(0, Chmod(second_dir_name, 0755)); |
| 170 | |
| 171 | EXPECT_EQ(0, Symlink(string("../") + first_dir_name, |
| 172 | second_dir_name + "/link")); |
| 173 | EXPECT_EQ(0, System(string("echo hi > ") + second_dir_name + "/file")); |
| 174 | EXPECT_EQ(0, Mkdir(second_dir_name + "/dir", 0755)); |
| 175 | EXPECT_EQ(0, System(string("echo ok > ") + second_dir_name + "/dir/subfile")); |
| 176 | EXPECT_TRUE(utils::RecursiveUnlinkDir(second_dir_name)); |
| 177 | EXPECT_TRUE(utils::FileExists(first_dir_name.c_str())); |
| 178 | EXPECT_EQ(0, System(string("rm -rf ") + first_dir_name)); |
| 179 | EXPECT_FALSE(utils::FileExists(second_dir_name.c_str())); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 180 | EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist")); |
| 181 | } |
| 182 | |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 183 | TEST(UtilsTest, IsSymlinkTest) { |
| 184 | string temp_dir; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 185 | EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir)); |
Darin Petkov | 002b2fe | 2010-11-22 13:53:22 -0800 | [diff] [blame] | 186 | string temp_file = temp_dir + "temp-file"; |
| 187 | EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0)); |
| 188 | string temp_symlink = temp_dir + "temp-symlink"; |
| 189 | EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str())); |
| 190 | EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str())); |
| 191 | EXPECT_FALSE(utils::IsSymlink(temp_file.c_str())); |
| 192 | EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str())); |
| 193 | EXPECT_FALSE(utils::IsSymlink("/non/existent/path")); |
| 194 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 195 | } |
| 196 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 197 | TEST(UtilsTest, TempFilenameTest) { |
| 198 | const string original = "/foo.XXXXXX"; |
| 199 | const string result = utils::TempFilename(original); |
| 200 | EXPECT_EQ(original.size(), result.size()); |
| 201 | EXPECT_TRUE(utils::StringHasPrefix(result, "/foo.")); |
| 202 | EXPECT_FALSE(utils::StringHasSuffix(result, "XXXXXX")); |
| 203 | } |
| 204 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 205 | TEST(UtilsTest, RootDeviceTest) { |
| 206 | EXPECT_EQ("/dev/sda", utils::RootDevice("/dev/sda3")); |
| 207 | EXPECT_EQ("/dev/mmc0", utils::RootDevice("/dev/mmc0p3")); |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 208 | EXPECT_EQ("", utils::RootDevice("/dev/foo/bar")); |
| 209 | EXPECT_EQ("", utils::RootDevice("/")); |
| 210 | EXPECT_EQ("", utils::RootDevice("")); |
| 211 | } |
| 212 | |
| 213 | TEST(UtilsTest, SysfsBlockDeviceTest) { |
| 214 | EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda")); |
| 215 | EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda")); |
| 216 | EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar")); |
| 217 | EXPECT_EQ("", utils::SysfsBlockDevice("/")); |
| 218 | EXPECT_EQ("", utils::SysfsBlockDevice("./")); |
| 219 | EXPECT_EQ("", utils::SysfsBlockDevice("")); |
| 220 | } |
| 221 | |
| 222 | TEST(UtilsTest, IsRemovableDeviceTest) { |
| 223 | EXPECT_FALSE(utils::IsRemovableDevice("")); |
| 224 | EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device")); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | TEST(UtilsTest, PartitionNumberTest) { |
| 228 | EXPECT_EQ("3", utils::PartitionNumber("/dev/sda3")); |
| 229 | EXPECT_EQ("3", utils::PartitionNumber("/dev/mmc0p3")); |
| 230 | } |
| 231 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 232 | TEST(UtilsTest, CompareCpuSharesTest) { |
| 233 | EXPECT_LT(utils::CompareCpuShares(utils::kCpuSharesLow, |
| 234 | utils::kCpuSharesNormal), 0); |
| 235 | EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesNormal, |
| 236 | utils::kCpuSharesLow), 0); |
| 237 | EXPECT_EQ(utils::CompareCpuShares(utils::kCpuSharesNormal, |
| 238 | utils::kCpuSharesNormal), 0); |
| 239 | EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesHigh, |
| 240 | utils::kCpuSharesNormal), 0); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 243 | TEST(UtilsTest, FuzzIntTest) { |
| 244 | static const unsigned int kRanges[] = { 0, 1, 2, 20 }; |
| 245 | for (size_t r = 0; r < arraysize(kRanges); ++r) { |
| 246 | unsigned int range = kRanges[r]; |
| 247 | const int kValue = 50; |
| 248 | for (int tries = 0; tries < 100; ++tries) { |
| 249 | int value = utils::FuzzInt(kValue, range); |
| 250 | EXPECT_GE(value, kValue - range / 2); |
| 251 | EXPECT_LE(value, kValue + range - range / 2); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Andrew de los Reyes | cc92cd3 | 2010-10-05 16:56:14 -0700 | [diff] [blame] | 256 | TEST(UtilsTest, ApplyMapTest) { |
| 257 | int initial_values[] = {1, 2, 3, 4, 6}; |
| 258 | vector<int> collection(&initial_values[0], |
| 259 | initial_values + arraysize(initial_values)); |
| 260 | EXPECT_EQ(arraysize(initial_values), collection.size()); |
| 261 | int expected_values[] = {1, 2, 5, 4, 8}; |
| 262 | map<int, int> value_map; |
| 263 | value_map[3] = 5; |
| 264 | value_map[6] = 8; |
| 265 | value_map[5] = 10; |
| 266 | |
| 267 | utils::ApplyMap(&collection, value_map); |
| 268 | |
| 269 | size_t index = 0; |
| 270 | for (vector<int>::iterator it = collection.begin(), e = collection.end(); |
| 271 | it != e; ++it) { |
| 272 | EXPECT_EQ(expected_values[index++], *it); |
| 273 | } |
| 274 | } |
| 275 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 276 | TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) { |
| 277 | string img; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 278 | EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, NULL)); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 279 | ScopedPathUnlinker img_unlinker(img); |
| 280 | CreateExtImageAtPath(img, NULL); |
| 281 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
| 282 | EXPECT_EQ(0, System(base::StringPrintf( |
| 283 | "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1", |
| 284 | img.c_str()))); |
| 285 | EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); |
| 286 | int block_count = 0; |
| 287 | int block_size = 0; |
| 288 | EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size)); |
| 289 | EXPECT_EQ(4096, block_size); |
| 290 | EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count); |
| 291 | } |
| 292 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 293 | TEST(UtilsTest, GetInstallDevTest) { |
| 294 | string boot_dev = "/dev/sda5"; |
| 295 | string install_dev; |
| 296 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 297 | EXPECT_EQ(install_dev, "/dev/sda3"); |
| 298 | |
| 299 | boot_dev = "/dev/sda3"; |
| 300 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 301 | EXPECT_EQ(install_dev, "/dev/sda5"); |
| 302 | |
| 303 | boot_dev = "/dev/sda12"; |
| 304 | EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev)); |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame] | 305 | |
| 306 | boot_dev = "/dev/ubiblock3_0"; |
| 307 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 308 | EXPECT_EQ(install_dev, "/dev/ubiblock5_0"); |
| 309 | |
| 310 | boot_dev = "/dev/ubiblock5_0"; |
| 311 | EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev)); |
| 312 | EXPECT_EQ(install_dev, "/dev/ubiblock3_0"); |
| 313 | |
| 314 | boot_dev = "/dev/ubiblock12_0"; |
| 315 | EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev)); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 318 | namespace { |
| 319 | gboolean TerminateScheduleCrashReporterUploadTest(void* arg) { |
| 320 | GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg); |
| 321 | g_main_loop_quit(loop); |
| 322 | return FALSE; // Don't call this callback again |
| 323 | } |
| 324 | } // namespace {} |
| 325 | |
| 326 | TEST(UtilsTest, ScheduleCrashReporterUploadTest) { |
| 327 | // Not much to test. At least this tests for memory leaks, crashes, |
| 328 | // log errors. |
| 329 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 330 | utils::ScheduleCrashReporterUpload(); |
| 331 | g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop); |
| 332 | g_main_loop_run(loop); |
| 333 | g_main_loop_unref(loop); |
| 334 | } |
| 335 | |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 336 | TEST(UtilsTest, FormatTimeDeltaTest) { |
| 337 | // utils::FormatTimeDelta() is not locale-aware (it's only used for logging |
| 338 | // which is not localized) so we only need to test the C locale |
| 339 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)), |
| 340 | "0.1s"); |
| 341 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)), |
| 342 | "0s"); |
| 343 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)), |
| 344 | "1s"); |
| 345 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)), |
| 346 | "59s"); |
| 347 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)), |
| 348 | "1m0s"); |
| 349 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)), |
| 350 | "1m1s"); |
| 351 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)), |
| 352 | "1m30s"); |
| 353 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)), |
| 354 | "20m5s"); |
| 355 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)), |
| 356 | "1h0m0s"); |
| 357 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)), |
| 358 | "1h0m1s"); |
| 359 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)), |
| 360 | "1h1m1s"); |
| 361 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)), |
| 362 | "2h1m1s"); |
| 363 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)), |
| 364 | "1d0h0m0s"); |
| 365 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)), |
| 366 | "1d0h0m1s"); |
| 367 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)), |
| 368 | "2d7h33m20s"); |
| 369 | EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) + |
| 370 | base::TimeDelta::FromMilliseconds(1)), |
| 371 | "2d7h33m20.001s"); |
| 372 | } |
| 373 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 374 | TEST(UtilsTest, TimeFromStructTimespecTest) { |
| 375 | struct timespec ts; |
| 376 | |
| 377 | // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970) |
| 378 | ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0}; |
| 379 | EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts)); |
| 380 | |
| 381 | // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001) |
| 382 | ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000, |
| 383 | .tv_nsec = 42 * 1000 * 1000}; |
| 384 | base::Time::Exploded exploded = (base::Time::Exploded) { |
| 385 | .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9, |
| 386 | .hour = 1, .minute = 46, .second = 40, .millisecond = 42}; |
| 387 | EXPECT_EQ(base::Time::FromUTCExploded(exploded), |
| 388 | utils::TimeFromStructTimespec(&ts)); |
| 389 | } |
| 390 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 391 | TEST(UtilsTest, DecodeAndStoreBase64String) { |
| 392 | base::FilePath path; |
| 393 | |
| 394 | // Ensure we return false on empty strings or invalid base64. |
| 395 | EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path)); |
| 396 | EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path)); |
| 397 | |
| 398 | // Pass known base64 and check that it matches. This string was generated |
| 399 | // the following way: |
| 400 | // |
| 401 | // $ echo "Update Engine" | base64 |
| 402 | // VXBkYXRlIEVuZ2luZQo= |
| 403 | EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=", |
| 404 | &path)); |
| 405 | ScopedPathUnlinker unlinker(path.value()); |
| 406 | string expected_contents = "Update Engine\n"; |
| 407 | string contents; |
| 408 | EXPECT_TRUE(utils::ReadFile(path.value(), &contents)); |
| 409 | EXPECT_EQ(contents, expected_contents); |
| 410 | EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size()); |
| 411 | } |
| 412 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame^] | 413 | TEST(UtilsTest, ConvertToOmahaInstallDate) { |
| 414 | // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a |
| 415 | // Monday. In Unix time, this point in time is easily obtained via |
| 416 | // the date(1) command like this: |
| 417 | // |
| 418 | // $ date +"%s" --date="Jan 1, 2007 0:00 PST" |
| 419 | const time_t omaha_epoch = 1167638400; |
| 420 | int value; |
| 421 | |
| 422 | // Points in time *on and after* the Omaha epoch should not fail. |
| 423 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 424 | base::Time::FromTimeT(omaha_epoch), &value)); |
| 425 | EXPECT_GE(value, 0); |
| 426 | |
| 427 | // Anything before the Omaha epoch should fail. We test it for two points. |
| 428 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
| 429 | base::Time::FromTimeT(omaha_epoch - 1), &value)); |
| 430 | EXPECT_FALSE(utils::ConvertToOmahaInstallDate( |
| 431 | base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value)); |
| 432 | |
| 433 | // Check that we jump from 0 to 7 exactly on the one-week mark, e.g. |
| 434 | // on Jan 8, 2007 0:00 PST. |
| 435 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 436 | base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value)); |
| 437 | EXPECT_EQ(value, 0); |
| 438 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 439 | base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value)); |
| 440 | EXPECT_EQ(value, 7); |
| 441 | |
| 442 | // Check a couple of more values. |
| 443 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 444 | base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value)); |
| 445 | EXPECT_EQ(value, 7); |
| 446 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 447 | base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value)); |
| 448 | EXPECT_EQ(value, 14); |
| 449 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 450 | base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value)); |
| 451 | EXPECT_EQ(value, 21); |
| 452 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 453 | base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value)); |
| 454 | EXPECT_EQ(value, 28); |
| 455 | |
| 456 | // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point |
| 457 | // where the Omaha InstallDate jumps 7 days. Its unix time is |
| 458 | // 1180940400. Notably, this is a point in time where Daylight |
| 459 | // Savings Time (DST) was is in effect (e.g. it's PDT, not PST). |
| 460 | // |
| 461 | // Note that as utils::ConvertToOmahaInstallDate() _deliberately_ |
| 462 | // ignores DST (as it's hard to implement in a thread-safe way using |
| 463 | // glibc, see comments in utils.h) we have to fudge by the DST |
| 464 | // offset which is one hour. Conveniently, if the function were |
| 465 | // someday modified to be DST aware, this test would have to be |
| 466 | // modified as well. |
| 467 | const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT. |
| 468 | const time_t fudge = 3600; |
| 469 | int value1, value2; |
| 470 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 471 | base::Time::FromTimeT(dst_time + fudge - 1), &value1)); |
| 472 | EXPECT_TRUE(utils::ConvertToOmahaInstallDate( |
| 473 | base::Time::FromTimeT(dst_time + fudge), &value2)); |
| 474 | EXPECT_EQ(value1, value2 - 7); |
| 475 | } |
| 476 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 477 | } // namespace chromeos_update_engine |