blob: ce65c5001a46a8d84186272b87afae614fc748b7 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo2c0db7b2014-11-04 12:23:39 -08005#include "update_engine/utils.h"
6
Alex Vakulenko44cab302014-07-23 13:12:15 -07007#include <errno.h>
Ben Chan9abb7632014-08-07 00:10:53 -07008#include <stdint.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <sys/stat.h>
10#include <sys/types.h>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070011
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070012#include <map>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080013#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include <vector>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070015
Alex Deymoc1711e22014-08-08 13:16:23 -070016#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070018#include <base/strings/string_util.h>
19#include <base/strings/stringprintf.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070020#include <gtest/gtest.h>
21
David Zeuthen33bae492014-02-25 16:16:18 -080022#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080023#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070024#include "update_engine/fake_system_state.h"
David Zeuthen33bae492014-02-25 16:16:18 -080025#include "update_engine/prefs.h"
Darin Petkovd3f8c892010-10-12 21:38:45 -070026#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000027
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070028using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080029using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000030using std::vector;
31
32namespace chromeos_update_engine {
33
34class UtilsTest : public ::testing::Test { };
35
Chris Sosac1972482013-04-30 22:31:10 -070036TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070037 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070038 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
39 EXPECT_EQ("123456", utils::ParseECVersion(
40 "b=1231a fw_version=123456 a=fasd2"));
41 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
42 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070043 "vendor=\"sam\" fw_version=\"00VFA616\""));
44
45 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070046 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070047}
48
J. Richard Barnette30842932013-10-28 15:04:23 -070049
50TEST(UtilsTest, KernelDeviceOfBootDevice) {
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -070051 EXPECT_EQ("", utils::KernelDeviceOfBootDevice(""));
J. Richard Barnette30842932013-10-28 15:04:23 -070052 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo"));
53 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0"));
54 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1"));
55 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2"));
56 EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3"));
57 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4"));
58 EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5"));
59 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6"));
60 EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7"));
61 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8"));
62 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9"));
63
64 EXPECT_EQ("/dev/mmcblk0p2",
65 utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3"));
66 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4"));
67
68 EXPECT_EQ("/dev/ubi2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
69 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
70
71 EXPECT_EQ("/dev/mtdblock2",
72 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
73 EXPECT_EQ("/dev/mtdblock4",
74 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
75 EXPECT_EQ("/dev/mtdblock6",
76 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
77 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
78}
79
80
adlr@google.com3defe6a2009-12-04 20:57:17 +000081TEST(UtilsTest, NormalizePathTest) {
82 EXPECT_EQ("", utils::NormalizePath("", false));
83 EXPECT_EQ("", utils::NormalizePath("", true));
84 EXPECT_EQ("/", utils::NormalizePath("/", false));
85 EXPECT_EQ("", utils::NormalizePath("/", true));
86 EXPECT_EQ("/", utils::NormalizePath("//", false));
87 EXPECT_EQ("", utils::NormalizePath("//", true));
88 EXPECT_EQ("foo", utils::NormalizePath("foo", false));
89 EXPECT_EQ("foo", utils::NormalizePath("foo", true));
90 EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false));
91 EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true));
92 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
93 false));
94 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
95 true));
96 EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/",
97 false));
98 EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/",
99 true));
100 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false));
101 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700102 EXPECT_EQ("\\:/;$PATH\n\\",
103 utils::NormalizePath("\\://;$PATH\n\\", false));
104 EXPECT_EQ("\\:/;$PATH\n\\",
105 utils::NormalizePath("\\://;$PATH\n\\", true));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000106 EXPECT_EQ("/spaces s/ ok/s / / /",
107 utils::NormalizePath("/spaces s/ ok/s / / /", false));
108 EXPECT_EQ("/spaces s/ ok/s / / ",
109 utils::NormalizePath("/spaces s/ ok/s / / /", true));
110}
111
112TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800113 chromeos::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000114 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
115}
116
Darin Petkov8e447e02013-04-16 16:23:50 +0200117TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700118 base::FilePath file;
119 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +0200120 ScopedPathUnlinker unlinker(file.value());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800121 chromeos::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200122 const size_t kSize = 1024 * 1024;
123 for (size_t i = 0; i < kSize; i++) {
124 data.push_back(i % 255);
125 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800126 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
127 chromeos::Blob in_data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200128 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
129 EXPECT_TRUE(in_data.empty());
130 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
131 EXPECT_TRUE(data == in_data);
132 in_data.clear();
133 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800134 EXPECT_TRUE(chromeos::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
Darin Petkov8e447e02013-04-16 16:23:50 +0200135 in_data);
136}
137
adlr@google.com3defe6a2009-12-04 20:57:17 +0000138TEST(UtilsTest, ErrnoNumberAsStringTest) {
139 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
140}
141
Darin Petkov002b2fe2010-11-22 13:53:22 -0800142TEST(UtilsTest, IsSymlinkTest) {
143 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800144 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700145 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800146 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700147 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800148 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
149 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
150 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
151 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
152 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Alex Deymo10875d92014-11-10 21:52:57 -0800153 EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800154}
155
Alex Deymo7dc4c502014-05-20 20:09:58 -0700156TEST(UtilsTest, IsDirTest) {
157 string temp_dir;
158 EXPECT_TRUE(utils::MakeTempDirectory("isdir-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700159 string temp_file = temp_dir + "/temp-file";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700160 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700161 string temp_symlink = temp_dir + "/temp-symlink";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700162 EXPECT_EQ(0, symlink(temp_dir.c_str(), temp_symlink.c_str()));
163 EXPECT_TRUE(utils::IsDir(temp_dir.c_str()));
164 EXPECT_FALSE(utils::IsDir(temp_file.c_str()));
165 EXPECT_FALSE(utils::IsDir(temp_symlink.c_str()));
166 EXPECT_FALSE(utils::IsDir("/non/existent/path"));
Alex Deymo10875d92014-11-10 21:52:57 -0800167 ASSERT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
Alex Deymo7dc4c502014-05-20 20:09:58 -0700168}
169
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800170TEST(UtilsTest, GetDiskNameTest) {
171 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700172 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800173 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
174 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
175 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700176 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800177 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
178 EXPECT_EQ("", utils::GetDiskName("/"));
179 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700180}
181
182TEST(UtilsTest, SysfsBlockDeviceTest) {
183 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
184 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
185 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
186 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
187 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
188 EXPECT_EQ("", utils::SysfsBlockDevice(""));
189}
190
191TEST(UtilsTest, IsRemovableDeviceTest) {
192 EXPECT_FALSE(utils::IsRemovableDevice(""));
193 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700194}
195
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800196TEST(UtilsTest, GetPartitionNumberTest) {
197 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700198 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800199 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
200 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
201 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700202 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800203 EXPECT_EQ(0, utils::GetPartitionNumber(""));
204 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
205 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
206 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700207 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
208 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
209 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
210 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700211}
212
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700213TEST(UtilsTest, MakePartitionNameTest) {
214 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
215 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
216 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
217 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
218 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
219 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
220 EXPECT_EQ("/dev/ubiblock3_0", utils::MakePartitionName("/dev/ubiblock", 3));
221}
222
Alex Deymo10875d92014-11-10 21:52:57 -0800223namespace {
224// Compares cpu shares and returns an integer that is less
225// than, equal to or greater than 0 if |shares_lhs| is,
226// respectively, lower than, same as or higher than |shares_rhs|.
227int CompareCpuShares(utils::CpuShares shares_lhs,
228 utils::CpuShares shares_rhs) {
229 return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs);
230}
231} // namespace
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700232
Alex Deymo10875d92014-11-10 21:52:57 -0800233// Tests the CPU shares enum is in the order we expect it.
Chris Sosa4f8ee272012-11-30 13:01:54 -0800234TEST(UtilsTest, CompareCpuSharesTest) {
Alex Deymo10875d92014-11-10 21:52:57 -0800235 EXPECT_LT(CompareCpuShares(utils::kCpuSharesLow,
236 utils::kCpuSharesNormal), 0);
237 EXPECT_GT(CompareCpuShares(utils::kCpuSharesNormal,
238 utils::kCpuSharesLow), 0);
239 EXPECT_EQ(CompareCpuShares(utils::kCpuSharesNormal,
240 utils::kCpuSharesNormal), 0);
241 EXPECT_GT(CompareCpuShares(utils::kCpuSharesHigh,
242 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700243}
244
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700245TEST(UtilsTest, FuzzIntTest) {
246 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800247 for (unsigned int range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700248 const int kValue = 50;
249 for (int tries = 0; tries < 100; ++tries) {
250 int value = utils::FuzzInt(kValue, range);
251 EXPECT_GE(value, kValue - range / 2);
252 EXPECT_LE(value, kValue + range - range / 2);
253 }
254 }
255}
256
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700257TEST(UtilsTest, ApplyMapTest) {
258 int initial_values[] = {1, 2, 3, 4, 6};
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800259 vector<int> collection(std::begin(initial_values), std::end(initial_values));
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700260 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;
Alex Deymo020600d2014-11-05 21:05:55 -0800270 for (const int value : collection) {
271 EXPECT_EQ(expected_values[index++], value);
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700272 }
273}
274
Darin Petkovd3f8c892010-10-12 21:38:45 -0700275TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
276 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700277 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700278 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800279 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700280 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800281 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Darin Petkovd3f8c892010-10-12 21:38:45 -0700282 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
283 img.c_str())));
284 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
285 int block_count = 0;
286 int block_size = 0;
287 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
288 EXPECT_EQ(4096, block_size);
289 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
290}
291
Alex Deymo192393b2014-11-10 15:58:38 -0800292// Squashfs example filesystem, generated with:
293// echo hola>hola
294// mksquashfs hola hola.sqfs -noappend -nopad
295// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800296const uint8_t kSquashfsFile[] = {
Alex Deymo192393b2014-11-10 15:58:38 -0800297 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
298 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
299 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
300 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
301 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
302 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
303 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
305 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
310 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
311 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
312 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
313 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
314 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
315 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
316 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
317 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
318 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
320 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
321 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
322 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
323 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
324 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
325 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
327};
328
329TEST(UtilsTest, GetSquashfs4Size) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800330 uint8_t buffer[sizeof(kSquashfsFile)];
Alex Deymo192393b2014-11-10 15:58:38 -0800331 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
332
333 int block_count = -1;
334 int block_size = -1;
335 // Not enough bytes passed.
336 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
337
338 // The whole file system is passed, which is enough for parsing.
339 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
340 &block_count, &block_size));
341 EXPECT_EQ(4096, block_size);
342 EXPECT_EQ(1, block_count);
343
344 // Modify the major version to 5.
345 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
346 *s_major = 5;
347 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
348 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
349
350 // Modify the bytes_used to have 6 blocks.
351 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
352 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
353 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
354 &block_count, &block_size));
355 EXPECT_EQ(4096, block_size);
356 EXPECT_EQ(6, block_count);
357}
358
Chris Sosad317e402013-06-12 13:47:09 -0700359TEST(UtilsTest, GetInstallDevTest) {
360 string boot_dev = "/dev/sda5";
361 string install_dev;
362 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
363 EXPECT_EQ(install_dev, "/dev/sda3");
364
365 boot_dev = "/dev/sda3";
366 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
367 EXPECT_EQ(install_dev, "/dev/sda5");
368
369 boot_dev = "/dev/sda12";
370 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700371
372 boot_dev = "/dev/ubiblock3_0";
373 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
374 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
375
376 boot_dev = "/dev/ubiblock5_0";
377 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
378 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
379
380 boot_dev = "/dev/ubiblock12_0";
381 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700382}
383
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800384namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700385void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700386 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800387 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700388 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
389 reinterpret_cast<const char*>(contents.data()),
390 contents.size()));
391 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
392}
Alex Deymo10875d92014-11-10 21:52:57 -0800393} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700394
395TEST(UtilsTest, GetFileFormatTest) {
396 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700397 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
398 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700399
400 // Real tests from cros_installer on different boards.
401 // ELF 32-bit LSB executable, Intel 80386
402 GetFileFormatTester(
403 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700404 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
407 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700408
Alex Deymoc1711e22014-08-08 13:16:23 -0700409 // ELF 32-bit LSB executable, MIPS
410 GetFileFormatTester(
411 "ELF 32-bit little-endian mips",
412 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
413 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
414 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
415 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
416
Alex Deymo032e7722014-03-25 17:53:56 -0700417 // ELF 32-bit LSB executable, ARM
418 GetFileFormatTester(
419 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700420 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
421 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
422 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
423 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700424
425 // ELF 64-bit LSB executable, x86-64
426 GetFileFormatTester(
427 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700428 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
429 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
430 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
431 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700432}
433
434namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800435gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
436 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
437 g_main_loop_quit(loop);
438 return FALSE; // Don't call this callback again
439}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700440} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800441
442TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
443 // Not much to test. At least this tests for memory leaks, crashes,
444 // log errors.
445 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
446 utils::ScheduleCrashReporterUpload();
447 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
448 g_main_loop_run(loop);
449 g_main_loop_unref(loop);
450}
451
David Zeuthen674c3182013-04-18 14:05:20 -0700452TEST(UtilsTest, FormatTimeDeltaTest) {
453 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
454 // which is not localized) so we only need to test the C locale
455 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
456 "0.1s");
457 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
458 "0s");
459 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
460 "1s");
461 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
462 "59s");
463 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
464 "1m0s");
465 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
466 "1m1s");
467 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
468 "1m30s");
469 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
470 "20m5s");
471 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
472 "1h0m0s");
473 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
474 "1h0m1s");
475 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
476 "1h1m1s");
477 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
478 "2h1m1s");
479 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
480 "1d0h0m0s");
481 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
482 "1d0h0m1s");
483 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
484 "2d7h33m20s");
485 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
486 base::TimeDelta::FromMilliseconds(1)),
487 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400488 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
489 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700490}
491
David Zeuthen27a48bc2013-08-06 12:06:29 -0700492TEST(UtilsTest, TimeFromStructTimespecTest) {
493 struct timespec ts;
494
495 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
496 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
497 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
498
499 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
500 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
501 .tv_nsec = 42 * 1000 * 1000};
502 base::Time::Exploded exploded = (base::Time::Exploded) {
503 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
504 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
505 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
506 utils::TimeFromStructTimespec(&ts));
507}
508
David Zeuthene7f89172013-10-31 10:21:04 -0700509TEST(UtilsTest, DecodeAndStoreBase64String) {
510 base::FilePath path;
511
512 // Ensure we return false on empty strings or invalid base64.
513 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
514 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
515
516 // Pass known base64 and check that it matches. This string was generated
517 // the following way:
518 //
519 // $ echo "Update Engine" | base64
520 // VXBkYXRlIEVuZ2luZQo=
521 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
522 &path));
523 ScopedPathUnlinker unlinker(path.value());
524 string expected_contents = "Update Engine\n";
525 string contents;
526 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
527 EXPECT_EQ(contents, expected_contents);
528 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
529}
530
David Zeuthen639aa362014-02-03 16:23:44 -0800531TEST(UtilsTest, ConvertToOmahaInstallDate) {
532 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
533 // Monday. In Unix time, this point in time is easily obtained via
534 // the date(1) command like this:
535 //
536 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
537 const time_t omaha_epoch = 1167638400;
538 int value;
539
540 // Points in time *on and after* the Omaha epoch should not fail.
541 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
542 base::Time::FromTimeT(omaha_epoch), &value));
543 EXPECT_GE(value, 0);
544
545 // Anything before the Omaha epoch should fail. We test it for two points.
546 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
547 base::Time::FromTimeT(omaha_epoch - 1), &value));
548 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
549 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
550
551 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
552 // on Jan 8, 2007 0:00 PST.
553 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
554 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
555 EXPECT_EQ(value, 0);
556 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
557 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
558 EXPECT_EQ(value, 7);
559
560 // Check a couple of more values.
561 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
562 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
563 EXPECT_EQ(value, 7);
564 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
565 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
566 EXPECT_EQ(value, 14);
567 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
568 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
569 EXPECT_EQ(value, 21);
570 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
571 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
572 EXPECT_EQ(value, 28);
573
574 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
575 // where the Omaha InstallDate jumps 7 days. Its unix time is
576 // 1180940400. Notably, this is a point in time where Daylight
577 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
578 //
579 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
580 // ignores DST (as it's hard to implement in a thread-safe way using
581 // glibc, see comments in utils.h) we have to fudge by the DST
582 // offset which is one hour. Conveniently, if the function were
583 // someday modified to be DST aware, this test would have to be
584 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700585 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800586 const time_t fudge = 3600;
587 int value1, value2;
588 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
589 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
590 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
591 base::Time::FromTimeT(dst_time + fudge), &value2));
592 EXPECT_EQ(value1, value2 - 7);
593}
594
David Zeuthen33bae492014-02-25 16:16:18 -0800595TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700596 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800597 FakeClock fake_clock;
598 base::TimeDelta duration;
599 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800600 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800601
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700602 fake_system_state.set_clock(&fake_clock);
603 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800604
605 // Initialize wallclock to 1 sec.
606 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
607
608 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700609 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800610 state_variable_key,
611 &duration));
612
613 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700614 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800615 state_variable_key,
616 &duration));
617 EXPECT_EQ(duration.InSeconds(), 0);
618
619 // We can also call it as many times as we want with it being
620 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700621 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800622 state_variable_key,
623 &duration));
624 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700625 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800626 state_variable_key,
627 &duration));
628 EXPECT_EQ(duration.InSeconds(), 0);
629
630 // Advance the clock one second, then we should get 1 sec on the
631 // next call and 0 sec on the subsequent call.
632 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700633 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800634 state_variable_key,
635 &duration));
636 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700637 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800638 state_variable_key,
639 &duration));
640 EXPECT_EQ(duration.InSeconds(), 0);
641
642 // Advance clock two seconds and we should get 2 sec and then 0 sec.
643 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700644 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800645 state_variable_key,
646 &duration));
647 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700648 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800649 state_variable_key,
650 &duration));
651 EXPECT_EQ(duration.InSeconds(), 0);
652
653 // There's a possibility that the wallclock can go backwards (NTP
654 // adjustments, for example) so check that we properly handle this
655 // case.
656 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700657 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800658 state_variable_key,
659 &duration));
660 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700661 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800662 state_variable_key,
663 &duration));
664 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800665}
666
667TEST(UtilsTest, MonotonicDurationHelper) {
668 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700669 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800670 FakeClock fake_clock;
671 base::TimeDelta duration;
672
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700673 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800674
675 // Initialize monotonic clock to 1 sec.
676 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
677
678 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700679 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800680 &storage,
681 &duration));
682
683 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700684 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800685 &storage,
686 &duration));
687 EXPECT_EQ(duration.InSeconds(), 0);
688
689 // We can also call it as many times as we want with it being
690 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700691 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800692 &storage,
693 &duration));
694 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700695 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800696 &storage,
697 &duration));
698 EXPECT_EQ(duration.InSeconds(), 0);
699
700 // Advance the clock one second, then we should get 1 sec on the
701 // next call and 0 sec on the subsequent call.
702 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700703 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800704 &storage,
705 &duration));
706 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700707 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800708 &storage,
709 &duration));
710 EXPECT_EQ(duration.InSeconds(), 0);
711
712 // Advance clock two seconds and we should get 2 sec and then 0 sec.
713 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700714 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800715 &storage,
716 &duration));
717 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700718 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800719 &storage,
720 &duration));
721 EXPECT_EQ(duration.InSeconds(), 0);
722}
723
David Zeuthenb281f072014-04-02 10:20:19 -0700724TEST(UtilsTest, GetConnectionType) {
725 // Check that expected combinations map to the right value.
726 EXPECT_EQ(metrics::ConnectionType::kUnknown,
727 utils::GetConnectionType(kNetUnknown,
728 NetworkTethering::kUnknown));
729 EXPECT_EQ(metrics::ConnectionType::kEthernet,
730 utils::GetConnectionType(kNetEthernet,
731 NetworkTethering::kUnknown));
732 EXPECT_EQ(metrics::ConnectionType::kWifi,
733 utils::GetConnectionType(kNetWifi,
734 NetworkTethering::kUnknown));
735 EXPECT_EQ(metrics::ConnectionType::kWimax,
736 utils::GetConnectionType(kNetWimax,
737 NetworkTethering::kUnknown));
738 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
739 utils::GetConnectionType(kNetBluetooth,
740 NetworkTethering::kUnknown));
741 EXPECT_EQ(metrics::ConnectionType::kCellular,
742 utils::GetConnectionType(kNetCellular,
743 NetworkTethering::kUnknown));
744 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
745 utils::GetConnectionType(kNetEthernet,
746 NetworkTethering::kConfirmed));
747 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
748 utils::GetConnectionType(kNetWifi,
749 NetworkTethering::kConfirmed));
750
751 // Ensure that we don't report tethered ethernet unless it's confirmed.
752 EXPECT_EQ(metrics::ConnectionType::kEthernet,
753 utils::GetConnectionType(kNetEthernet,
754 NetworkTethering::kNotDetected));
755 EXPECT_EQ(metrics::ConnectionType::kEthernet,
756 utils::GetConnectionType(kNetEthernet,
757 NetworkTethering::kSuspected));
758 EXPECT_EQ(metrics::ConnectionType::kEthernet,
759 utils::GetConnectionType(kNetEthernet,
760 NetworkTethering::kUnknown));
761
762 // Ditto for tethered wifi.
763 EXPECT_EQ(metrics::ConnectionType::kWifi,
764 utils::GetConnectionType(kNetWifi,
765 NetworkTethering::kNotDetected));
766 EXPECT_EQ(metrics::ConnectionType::kWifi,
767 utils::GetConnectionType(kNetWifi,
768 NetworkTethering::kSuspected));
769 EXPECT_EQ(metrics::ConnectionType::kWifi,
770 utils::GetConnectionType(kNetWifi,
771 NetworkTethering::kUnknown));
772}
773
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800774static bool BoolMacroTestHelper() {
775 int i = 1;
776 unsigned int ui = 1;
777 bool b = 1;
778 std::unique_ptr<char> cptr(new char);
779
780 TEST_AND_RETURN_FALSE(i);
781 TEST_AND_RETURN_FALSE(ui);
782 TEST_AND_RETURN_FALSE(b);
783 TEST_AND_RETURN_FALSE(cptr);
784
785 TEST_AND_RETURN_FALSE_ERRNO(i);
786 TEST_AND_RETURN_FALSE_ERRNO(ui);
787 TEST_AND_RETURN_FALSE_ERRNO(b);
788 TEST_AND_RETURN_FALSE_ERRNO(cptr);
789
790 return true;
791}
792
793static void VoidMacroTestHelper(bool* ret) {
794 int i = 1;
795 unsigned int ui = 1;
796 bool b = 1;
797 std::unique_ptr<char> cptr(new char);
798
799 *ret = false;
800
801 TEST_AND_RETURN(i);
802 TEST_AND_RETURN(ui);
803 TEST_AND_RETURN(b);
804 TEST_AND_RETURN(cptr);
805
806 TEST_AND_RETURN_ERRNO(i);
807 TEST_AND_RETURN_ERRNO(ui);
808 TEST_AND_RETURN_ERRNO(b);
809 TEST_AND_RETURN_ERRNO(cptr);
810
811 *ret = true;
812}
813
814TEST(UtilsTest, TestMacros) {
815 bool void_test = false;
816 VoidMacroTestHelper(&void_test);
817 EXPECT_TRUE(void_test);
818
819 EXPECT_TRUE(BoolMacroTestHelper());
820}
821
adlr@google.com3defe6a2009-12-04 20:57:17 +0000822} // namespace chromeos_update_engine