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