blob: 3010738b0574dc7aba99294654dff9d4fcecaecd [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>
Allie Wood78750a42015-02-11 15:42:11 -080018#include <base/files/scoped_temp_dir.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/strings/string_util.h>
20#include <base/strings/stringprintf.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070021#include <gtest/gtest.h>
22
David Zeuthen33bae492014-02-25 16:16:18 -080023#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080024#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070025#include "update_engine/fake_system_state.h"
David Zeuthen33bae492014-02-25 16:16:18 -080026#include "update_engine/prefs.h"
Darin Petkovd3f8c892010-10-12 21:38:45 -070027#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000028
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070029using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080030using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000031using std::vector;
32
33namespace chromeos_update_engine {
34
35class UtilsTest : public ::testing::Test { };
36
Chris Sosac1972482013-04-30 22:31:10 -070037TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070038 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070039 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
40 EXPECT_EQ("123456", utils::ParseECVersion(
41 "b=1231a fw_version=123456 a=fasd2"));
42 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
43 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070044 "vendor=\"sam\" fw_version=\"00VFA616\""));
45
46 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070047 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070048}
49
J. Richard Barnette30842932013-10-28 15:04:23 -070050
51TEST(UtilsTest, KernelDeviceOfBootDevice) {
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -070052 EXPECT_EQ("", utils::KernelDeviceOfBootDevice(""));
J. Richard Barnette30842932013-10-28 15:04:23 -070053 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo"));
54 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0"));
55 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1"));
56 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2"));
57 EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3"));
58 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4"));
59 EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5"));
60 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6"));
61 EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7"));
62 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8"));
63 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9"));
64
65 EXPECT_EQ("/dev/mmcblk0p2",
66 utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3"));
67 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4"));
68
69 EXPECT_EQ("/dev/ubi2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
70 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
71
72 EXPECT_EQ("/dev/mtdblock2",
73 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
74 EXPECT_EQ("/dev/mtdblock4",
75 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
76 EXPECT_EQ("/dev/mtdblock6",
77 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
78 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
79}
80
81
adlr@google.com3defe6a2009-12-04 20:57:17 +000082TEST(UtilsTest, NormalizePathTest) {
83 EXPECT_EQ("", utils::NormalizePath("", false));
84 EXPECT_EQ("", utils::NormalizePath("", true));
85 EXPECT_EQ("/", utils::NormalizePath("/", false));
86 EXPECT_EQ("", utils::NormalizePath("/", true));
87 EXPECT_EQ("/", utils::NormalizePath("//", false));
88 EXPECT_EQ("", utils::NormalizePath("//", true));
89 EXPECT_EQ("foo", utils::NormalizePath("foo", false));
90 EXPECT_EQ("foo", utils::NormalizePath("foo", true));
91 EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false));
92 EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true));
93 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
94 false));
95 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
96 true));
97 EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/",
98 false));
99 EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/",
100 true));
101 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false));
102 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700103 EXPECT_EQ("\\:/;$PATH\n\\",
104 utils::NormalizePath("\\://;$PATH\n\\", false));
105 EXPECT_EQ("\\:/;$PATH\n\\",
106 utils::NormalizePath("\\://;$PATH\n\\", true));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107 EXPECT_EQ("/spaces s/ ok/s / / /",
108 utils::NormalizePath("/spaces s/ ok/s / / /", false));
109 EXPECT_EQ("/spaces s/ ok/s / / ",
110 utils::NormalizePath("/spaces s/ ok/s / / /", true));
111}
112
113TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800114 chromeos::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000115 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
116}
117
Darin Petkov8e447e02013-04-16 16:23:50 +0200118TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700119 base::FilePath file;
120 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +0200121 ScopedPathUnlinker unlinker(file.value());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800122 chromeos::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200123 const size_t kSize = 1024 * 1024;
124 for (size_t i = 0; i < kSize; i++) {
125 data.push_back(i % 255);
126 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800127 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
128 chromeos::Blob in_data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200129 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
130 EXPECT_TRUE(in_data.empty());
131 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
132 EXPECT_TRUE(data == in_data);
133 in_data.clear();
134 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800135 EXPECT_TRUE(chromeos::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
Darin Petkov8e447e02013-04-16 16:23:50 +0200136 in_data);
137}
138
adlr@google.com3defe6a2009-12-04 20:57:17 +0000139TEST(UtilsTest, ErrnoNumberAsStringTest) {
140 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
141}
142
Darin Petkov002b2fe2010-11-22 13:53:22 -0800143TEST(UtilsTest, IsSymlinkTest) {
144 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800145 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700146 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800147 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700148 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800149 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
150 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
151 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
152 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
153 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Alex Deymo10875d92014-11-10 21:52:57 -0800154 EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800155}
156
Alex Deymo7dc4c502014-05-20 20:09:58 -0700157TEST(UtilsTest, IsDirTest) {
158 string temp_dir;
159 EXPECT_TRUE(utils::MakeTempDirectory("isdir-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700160 string temp_file = temp_dir + "/temp-file";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700161 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700162 string temp_symlink = temp_dir + "/temp-symlink";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700163 EXPECT_EQ(0, symlink(temp_dir.c_str(), temp_symlink.c_str()));
164 EXPECT_TRUE(utils::IsDir(temp_dir.c_str()));
165 EXPECT_FALSE(utils::IsDir(temp_file.c_str()));
166 EXPECT_FALSE(utils::IsDir(temp_symlink.c_str()));
167 EXPECT_FALSE(utils::IsDir("/non/existent/path"));
Alex Deymo10875d92014-11-10 21:52:57 -0800168 ASSERT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
Alex Deymo7dc4c502014-05-20 20:09:58 -0700169}
170
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800171TEST(UtilsTest, GetDiskNameTest) {
172 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700173 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800174 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
175 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
176 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700177 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800178 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
179 EXPECT_EQ("", utils::GetDiskName("/"));
180 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700181}
182
183TEST(UtilsTest, SysfsBlockDeviceTest) {
184 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
185 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
186 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
187 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
188 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
189 EXPECT_EQ("", utils::SysfsBlockDevice(""));
190}
191
192TEST(UtilsTest, IsRemovableDeviceTest) {
193 EXPECT_FALSE(utils::IsRemovableDevice(""));
194 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700195}
196
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800197TEST(UtilsTest, GetPartitionNumberTest) {
198 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700199 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800200 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
201 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
202 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700203 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800204 EXPECT_EQ(0, utils::GetPartitionNumber(""));
205 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
206 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
207 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700208 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
209 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
210 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
211 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700212}
213
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700214TEST(UtilsTest, MakePartitionNameTest) {
215 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
216 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
217 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
218 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
219 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
220 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
221 EXPECT_EQ("/dev/ubiblock3_0", utils::MakePartitionName("/dev/ubiblock", 3));
222}
223
Alex Deymo10875d92014-11-10 21:52:57 -0800224namespace {
225// Compares cpu shares and returns an integer that is less
226// than, equal to or greater than 0 if |shares_lhs| is,
227// respectively, lower than, same as or higher than |shares_rhs|.
228int CompareCpuShares(utils::CpuShares shares_lhs,
229 utils::CpuShares shares_rhs) {
230 return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs);
231}
232} // namespace
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700233
Alex Deymo10875d92014-11-10 21:52:57 -0800234// Tests the CPU shares enum is in the order we expect it.
Chris Sosa4f8ee272012-11-30 13:01:54 -0800235TEST(UtilsTest, CompareCpuSharesTest) {
Alex Deymo10875d92014-11-10 21:52:57 -0800236 EXPECT_LT(CompareCpuShares(utils::kCpuSharesLow,
237 utils::kCpuSharesNormal), 0);
238 EXPECT_GT(CompareCpuShares(utils::kCpuSharesNormal,
239 utils::kCpuSharesLow), 0);
240 EXPECT_EQ(CompareCpuShares(utils::kCpuSharesNormal,
241 utils::kCpuSharesNormal), 0);
242 EXPECT_GT(CompareCpuShares(utils::kCpuSharesHigh,
243 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700244}
245
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700246TEST(UtilsTest, FuzzIntTest) {
247 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800248 for (unsigned int range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700249 const int kValue = 50;
250 for (int tries = 0; tries < 100; ++tries) {
251 int value = utils::FuzzInt(kValue, range);
252 EXPECT_GE(value, kValue - range / 2);
253 EXPECT_LE(value, kValue + range - range / 2);
254 }
255 }
256}
257
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700258TEST(UtilsTest, ApplyMapTest) {
259 int initial_values[] = {1, 2, 3, 4, 6};
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800260 vector<int> collection(std::begin(initial_values), std::end(initial_values));
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700261 EXPECT_EQ(arraysize(initial_values), collection.size());
262 int expected_values[] = {1, 2, 5, 4, 8};
263 map<int, int> value_map;
264 value_map[3] = 5;
265 value_map[6] = 8;
266 value_map[5] = 10;
267
268 utils::ApplyMap(&collection, value_map);
269
270 size_t index = 0;
Alex Deymo020600d2014-11-05 21:05:55 -0800271 for (const int value : collection) {
272 EXPECT_EQ(expected_values[index++], value);
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700273 }
274}
275
Darin Petkovd3f8c892010-10-12 21:38:45 -0700276TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
277 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700278 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700279 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800280 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700281 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800282 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Alex Deymo1f93d032015-03-10 18:58:32 -0700283 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none",
Darin Petkovd3f8c892010-10-12 21:38:45 -0700284 img.c_str())));
285 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
286 int block_count = 0;
287 int block_size = 0;
288 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
289 EXPECT_EQ(4096, block_size);
290 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
291}
292
Alex Deymo192393b2014-11-10 15:58:38 -0800293// Squashfs example filesystem, generated with:
294// echo hola>hola
295// mksquashfs hola hola.sqfs -noappend -nopad
296// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800297const uint8_t kSquashfsFile[] = {
Alex Deymo192393b2014-11-10 15:58:38 -0800298 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
299 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
300 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
301 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
302 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
303 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
304 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
305 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
306 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
311 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
312 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
313 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
314 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
315 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
316 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
317 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
318 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
319 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
321 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
322 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
323 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
324 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
325 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
326 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
328};
329
330TEST(UtilsTest, GetSquashfs4Size) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800331 uint8_t buffer[sizeof(kSquashfsFile)];
Alex Deymo192393b2014-11-10 15:58:38 -0800332 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
333
334 int block_count = -1;
335 int block_size = -1;
336 // Not enough bytes passed.
337 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
338
339 // The whole file system is passed, which is enough for parsing.
340 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
341 &block_count, &block_size));
342 EXPECT_EQ(4096, block_size);
343 EXPECT_EQ(1, block_count);
344
345 // Modify the major version to 5.
346 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
347 *s_major = 5;
348 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
349 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
350
351 // Modify the bytes_used to have 6 blocks.
352 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
353 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
354 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
355 &block_count, &block_size));
356 EXPECT_EQ(4096, block_size);
357 EXPECT_EQ(6, block_count);
358}
359
Chris Sosad317e402013-06-12 13:47:09 -0700360TEST(UtilsTest, GetInstallDevTest) {
361 string boot_dev = "/dev/sda5";
362 string install_dev;
363 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
364 EXPECT_EQ(install_dev, "/dev/sda3");
365
366 boot_dev = "/dev/sda3";
367 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
368 EXPECT_EQ(install_dev, "/dev/sda5");
369
370 boot_dev = "/dev/sda12";
371 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700372
373 boot_dev = "/dev/ubiblock3_0";
374 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
375 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
376
377 boot_dev = "/dev/ubiblock5_0";
378 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
379 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
380
381 boot_dev = "/dev/ubiblock12_0";
382 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700383}
384
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800385namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700386void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700387 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800388 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700389 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
390 reinterpret_cast<const char*>(contents.data()),
391 contents.size()));
392 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
393}
Alex Deymo10875d92014-11-10 21:52:57 -0800394} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700395
396TEST(UtilsTest, GetFileFormatTest) {
397 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700398 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
399 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700400
401 // Real tests from cros_installer on different boards.
402 // ELF 32-bit LSB executable, Intel 80386
403 GetFileFormatTester(
404 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700405 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
406 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
408 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700409
Alex Deymoc1711e22014-08-08 13:16:23 -0700410 // ELF 32-bit LSB executable, MIPS
411 GetFileFormatTester(
412 "ELF 32-bit little-endian mips",
413 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
414 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
415 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
416 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
417
Alex Deymo032e7722014-03-25 17:53:56 -0700418 // ELF 32-bit LSB executable, ARM
419 GetFileFormatTester(
420 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700421 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
422 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
423 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
424 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700425
426 // ELF 64-bit LSB executable, x86-64
427 GetFileFormatTester(
428 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700429 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
430 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
431 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
432 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700433}
434
435namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800436gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
437 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
438 g_main_loop_quit(loop);
439 return FALSE; // Don't call this callback again
440}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700441} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800442
443TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
444 // Not much to test. At least this tests for memory leaks, crashes,
445 // log errors.
446 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
447 utils::ScheduleCrashReporterUpload();
448 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
449 g_main_loop_run(loop);
450 g_main_loop_unref(loop);
451}
452
David Zeuthen674c3182013-04-18 14:05:20 -0700453TEST(UtilsTest, FormatTimeDeltaTest) {
454 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
455 // which is not localized) so we only need to test the C locale
456 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
457 "0.1s");
458 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
459 "0s");
460 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
461 "1s");
462 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
463 "59s");
464 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
465 "1m0s");
466 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
467 "1m1s");
468 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
469 "1m30s");
470 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
471 "20m5s");
472 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
473 "1h0m0s");
474 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
475 "1h0m1s");
476 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
477 "1h1m1s");
478 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
479 "2h1m1s");
480 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
481 "1d0h0m0s");
482 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
483 "1d0h0m1s");
484 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
485 "2d7h33m20s");
486 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
487 base::TimeDelta::FromMilliseconds(1)),
488 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400489 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
490 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700491}
492
David Zeuthen27a48bc2013-08-06 12:06:29 -0700493TEST(UtilsTest, TimeFromStructTimespecTest) {
494 struct timespec ts;
495
496 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
497 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
498 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
499
500 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
501 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
502 .tv_nsec = 42 * 1000 * 1000};
503 base::Time::Exploded exploded = (base::Time::Exploded) {
504 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
505 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
506 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
507 utils::TimeFromStructTimespec(&ts));
508}
509
David Zeuthene7f89172013-10-31 10:21:04 -0700510TEST(UtilsTest, DecodeAndStoreBase64String) {
511 base::FilePath path;
512
513 // Ensure we return false on empty strings or invalid base64.
514 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
515 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
516
517 // Pass known base64 and check that it matches. This string was generated
518 // the following way:
519 //
520 // $ echo "Update Engine" | base64
521 // VXBkYXRlIEVuZ2luZQo=
522 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
523 &path));
524 ScopedPathUnlinker unlinker(path.value());
525 string expected_contents = "Update Engine\n";
526 string contents;
527 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
528 EXPECT_EQ(contents, expected_contents);
529 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
530}
531
David Zeuthen639aa362014-02-03 16:23:44 -0800532TEST(UtilsTest, ConvertToOmahaInstallDate) {
533 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
534 // Monday. In Unix time, this point in time is easily obtained via
535 // the date(1) command like this:
536 //
537 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
538 const time_t omaha_epoch = 1167638400;
539 int value;
540
541 // Points in time *on and after* the Omaha epoch should not fail.
542 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
543 base::Time::FromTimeT(omaha_epoch), &value));
544 EXPECT_GE(value, 0);
545
546 // Anything before the Omaha epoch should fail. We test it for two points.
547 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
548 base::Time::FromTimeT(omaha_epoch - 1), &value));
549 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
550 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
551
552 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
553 // on Jan 8, 2007 0:00 PST.
554 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
555 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
556 EXPECT_EQ(value, 0);
557 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
558 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
559 EXPECT_EQ(value, 7);
560
561 // Check a couple of more values.
562 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
563 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
564 EXPECT_EQ(value, 7);
565 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
566 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
567 EXPECT_EQ(value, 14);
568 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
569 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
570 EXPECT_EQ(value, 21);
571 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
572 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
573 EXPECT_EQ(value, 28);
574
575 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
576 // where the Omaha InstallDate jumps 7 days. Its unix time is
577 // 1180940400. Notably, this is a point in time where Daylight
578 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
579 //
580 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
581 // ignores DST (as it's hard to implement in a thread-safe way using
582 // glibc, see comments in utils.h) we have to fudge by the DST
583 // offset which is one hour. Conveniently, if the function were
584 // someday modified to be DST aware, this test would have to be
585 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700586 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800587 const time_t fudge = 3600;
588 int value1, value2;
589 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
590 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
591 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
592 base::Time::FromTimeT(dst_time + fudge), &value2));
593 EXPECT_EQ(value1, value2 - 7);
594}
595
David Zeuthen33bae492014-02-25 16:16:18 -0800596TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700597 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800598 FakeClock fake_clock;
599 base::TimeDelta duration;
600 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800601 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800602
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700603 fake_system_state.set_clock(&fake_clock);
604 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800605
606 // Initialize wallclock to 1 sec.
607 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
608
609 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700610 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800611 state_variable_key,
612 &duration));
613
614 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700615 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800616 state_variable_key,
617 &duration));
618 EXPECT_EQ(duration.InSeconds(), 0);
619
620 // We can also call it as many times as we want with it being
621 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700622 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800623 state_variable_key,
624 &duration));
625 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700626 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800627 state_variable_key,
628 &duration));
629 EXPECT_EQ(duration.InSeconds(), 0);
630
631 // Advance the clock one second, then we should get 1 sec on the
632 // next call and 0 sec on the subsequent call.
633 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700634 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800635 state_variable_key,
636 &duration));
637 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700638 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800639 state_variable_key,
640 &duration));
641 EXPECT_EQ(duration.InSeconds(), 0);
642
643 // Advance clock two seconds and we should get 2 sec and then 0 sec.
644 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700645 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800646 state_variable_key,
647 &duration));
648 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700649 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800650 state_variable_key,
651 &duration));
652 EXPECT_EQ(duration.InSeconds(), 0);
653
654 // There's a possibility that the wallclock can go backwards (NTP
655 // adjustments, for example) so check that we properly handle this
656 // case.
657 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700658 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800659 state_variable_key,
660 &duration));
661 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700662 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800663 state_variable_key,
664 &duration));
665 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800666}
667
668TEST(UtilsTest, MonotonicDurationHelper) {
669 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700670 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800671 FakeClock fake_clock;
672 base::TimeDelta duration;
673
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700674 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800675
676 // Initialize monotonic clock to 1 sec.
677 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
678
679 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700680 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800681 &storage,
682 &duration));
683
684 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700685 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800686 &storage,
687 &duration));
688 EXPECT_EQ(duration.InSeconds(), 0);
689
690 // We can also call it as many times as we want with it being
691 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700692 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800693 &storage,
694 &duration));
695 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700696 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800697 &storage,
698 &duration));
699 EXPECT_EQ(duration.InSeconds(), 0);
700
701 // Advance the clock one second, then we should get 1 sec on the
702 // next call and 0 sec on the subsequent call.
703 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700704 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800705 &storage,
706 &duration));
707 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700708 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800709 &storage,
710 &duration));
711 EXPECT_EQ(duration.InSeconds(), 0);
712
713 // Advance clock two seconds and we should get 2 sec and then 0 sec.
714 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700715 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800716 &storage,
717 &duration));
718 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700719 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800720 &storage,
721 &duration));
722 EXPECT_EQ(duration.InSeconds(), 0);
723}
724
David Zeuthenb281f072014-04-02 10:20:19 -0700725TEST(UtilsTest, GetConnectionType) {
726 // Check that expected combinations map to the right value.
727 EXPECT_EQ(metrics::ConnectionType::kUnknown,
728 utils::GetConnectionType(kNetUnknown,
729 NetworkTethering::kUnknown));
730 EXPECT_EQ(metrics::ConnectionType::kEthernet,
731 utils::GetConnectionType(kNetEthernet,
732 NetworkTethering::kUnknown));
733 EXPECT_EQ(metrics::ConnectionType::kWifi,
734 utils::GetConnectionType(kNetWifi,
735 NetworkTethering::kUnknown));
736 EXPECT_EQ(metrics::ConnectionType::kWimax,
737 utils::GetConnectionType(kNetWimax,
738 NetworkTethering::kUnknown));
739 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
740 utils::GetConnectionType(kNetBluetooth,
741 NetworkTethering::kUnknown));
742 EXPECT_EQ(metrics::ConnectionType::kCellular,
743 utils::GetConnectionType(kNetCellular,
744 NetworkTethering::kUnknown));
745 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
746 utils::GetConnectionType(kNetEthernet,
747 NetworkTethering::kConfirmed));
748 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
749 utils::GetConnectionType(kNetWifi,
750 NetworkTethering::kConfirmed));
751
752 // Ensure that we don't report tethered ethernet unless it's confirmed.
753 EXPECT_EQ(metrics::ConnectionType::kEthernet,
754 utils::GetConnectionType(kNetEthernet,
755 NetworkTethering::kNotDetected));
756 EXPECT_EQ(metrics::ConnectionType::kEthernet,
757 utils::GetConnectionType(kNetEthernet,
758 NetworkTethering::kSuspected));
759 EXPECT_EQ(metrics::ConnectionType::kEthernet,
760 utils::GetConnectionType(kNetEthernet,
761 NetworkTethering::kUnknown));
762
763 // Ditto for tethered wifi.
764 EXPECT_EQ(metrics::ConnectionType::kWifi,
765 utils::GetConnectionType(kNetWifi,
766 NetworkTethering::kNotDetected));
767 EXPECT_EQ(metrics::ConnectionType::kWifi,
768 utils::GetConnectionType(kNetWifi,
769 NetworkTethering::kSuspected));
770 EXPECT_EQ(metrics::ConnectionType::kWifi,
771 utils::GetConnectionType(kNetWifi,
772 NetworkTethering::kUnknown));
773}
774
Allie Wood78750a42015-02-11 15:42:11 -0800775TEST(UtilsTest, GetMinorVersion) {
776 // Test GetMinorVersion by verifying that it parses the conf file and returns
777 // the correct value.
778 string contents = "PAYLOAD_MINOR_VERSION=1\n";
779 uint32_t minor_version;
780
781 base::ScopedTempDir temp_dir;
782 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
783
784 base::FilePath temp_file("update_engine.conf");
785 base::FilePath filepath = temp_dir.path().Append(temp_file);
786
787 ASSERT_TRUE(test_utils::WriteFileString(filepath.value(), contents.c_str()));
788 ASSERT_TRUE(utils::GetMinorVersion(filepath, &minor_version));
789 ASSERT_EQ(minor_version, 1);
790}
791
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800792static bool BoolMacroTestHelper() {
793 int i = 1;
794 unsigned int ui = 1;
795 bool b = 1;
796 std::unique_ptr<char> cptr(new char);
797
798 TEST_AND_RETURN_FALSE(i);
799 TEST_AND_RETURN_FALSE(ui);
800 TEST_AND_RETURN_FALSE(b);
801 TEST_AND_RETURN_FALSE(cptr);
802
803 TEST_AND_RETURN_FALSE_ERRNO(i);
804 TEST_AND_RETURN_FALSE_ERRNO(ui);
805 TEST_AND_RETURN_FALSE_ERRNO(b);
806 TEST_AND_RETURN_FALSE_ERRNO(cptr);
807
808 return true;
809}
810
811static void VoidMacroTestHelper(bool* ret) {
812 int i = 1;
813 unsigned int ui = 1;
814 bool b = 1;
815 std::unique_ptr<char> cptr(new char);
816
817 *ret = false;
818
819 TEST_AND_RETURN(i);
820 TEST_AND_RETURN(ui);
821 TEST_AND_RETURN(b);
822 TEST_AND_RETURN(cptr);
823
824 TEST_AND_RETURN_ERRNO(i);
825 TEST_AND_RETURN_ERRNO(ui);
826 TEST_AND_RETURN_ERRNO(b);
827 TEST_AND_RETURN_ERRNO(cptr);
828
829 *ret = true;
830}
831
832TEST(UtilsTest, TestMacros) {
833 bool void_test = false;
834 VoidMacroTestHelper(&void_test);
835 EXPECT_TRUE(void_test);
836
837 EXPECT_TRUE(BoolMacroTestHelper());
838}
839
adlr@google.com3defe6a2009-12-04 20:57:17 +0000840} // namespace chromeos_update_engine