blob: d5dd6a95d919619dd34ad0e69cbff91d853929d7 [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) {
113 vector<char> empty;
114 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());
121 vector<char> data;
122 const size_t kSize = 1024 * 1024;
123 for (size_t i = 0; i < kSize; i++) {
124 data.push_back(i % 255);
125 }
126 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), &data[0], data.size()));
127 vector<char> in_data;
128 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));
134 EXPECT_TRUE(vector<char>(data.begin() + 10, data.begin() + 10 + 20) ==
135 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 };
247 for (size_t r = 0; r < arraysize(kRanges); ++r) {
248 unsigned int range = kRanges[r];
249 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};
260 vector<int> collection(&initial_values[0],
261 initial_values + arraysize(initial_values));
262 EXPECT_EQ(arraysize(initial_values), collection.size());
263 int expected_values[] = {1, 2, 5, 4, 8};
264 map<int, int> value_map;
265 value_map[3] = 5;
266 value_map[6] = 8;
267 value_map[5] = 10;
268
269 utils::ApplyMap(&collection, value_map);
270
271 size_t index = 0;
Alex Deymo020600d2014-11-05 21:05:55 -0800272 for (const int value : collection) {
273 EXPECT_EQ(expected_values[index++], value);
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700274 }
275}
276
Darin Petkovd3f8c892010-10-12 21:38:45 -0700277TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
278 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700279 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700280 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800281 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700282 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800283 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Darin Petkovd3f8c892010-10-12 21:38:45 -0700284 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
285 img.c_str())));
286 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
287 int block_count = 0;
288 int block_size = 0;
289 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
290 EXPECT_EQ(4096, block_size);
291 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
292}
293
Alex Deymo192393b2014-11-10 15:58:38 -0800294// Squashfs example filesystem, generated with:
295// echo hola>hola
296// mksquashfs hola hola.sqfs -noappend -nopad
297// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
298const unsigned char kSquashfsFile[] = {
299 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
300 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
301 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
302 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
303 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
304 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
305 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
307 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
312 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
313 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
314 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
315 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
316 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
317 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
318 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
319 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
320 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
321 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
322 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
323 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
324 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
325 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
327 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
329};
330
331TEST(UtilsTest, GetSquashfs4Size) {
332 unsigned char buffer[sizeof(kSquashfsFile)];
333 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
334
335 int block_count = -1;
336 int block_size = -1;
337 // Not enough bytes passed.
338 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
339
340 // The whole file system is passed, which is enough for parsing.
341 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
342 &block_count, &block_size));
343 EXPECT_EQ(4096, block_size);
344 EXPECT_EQ(1, block_count);
345
346 // Modify the major version to 5.
347 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
348 *s_major = 5;
349 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
350 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
351
352 // Modify the bytes_used to have 6 blocks.
353 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
354 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
355 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
356 &block_count, &block_size));
357 EXPECT_EQ(4096, block_size);
358 EXPECT_EQ(6, block_count);
359}
360
Chris Sosad317e402013-06-12 13:47:09 -0700361TEST(UtilsTest, GetInstallDevTest) {
362 string boot_dev = "/dev/sda5";
363 string install_dev;
364 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
365 EXPECT_EQ(install_dev, "/dev/sda3");
366
367 boot_dev = "/dev/sda3";
368 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
369 EXPECT_EQ(install_dev, "/dev/sda5");
370
371 boot_dev = "/dev/sda12";
372 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700373
374 boot_dev = "/dev/ubiblock3_0";
375 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
376 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
377
378 boot_dev = "/dev/ubiblock5_0";
379 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
380 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
381
382 boot_dev = "/dev/ubiblock12_0";
383 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700384}
385
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800386namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700387void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700388 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800389 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700390 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
391 reinterpret_cast<const char*>(contents.data()),
392 contents.size()));
393 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
394}
Alex Deymo10875d92014-11-10 21:52:57 -0800395} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700396
397TEST(UtilsTest, GetFileFormatTest) {
398 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700399 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
400 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700401
402 // Real tests from cros_installer on different boards.
403 // ELF 32-bit LSB executable, Intel 80386
404 GetFileFormatTester(
405 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700406 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
407 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
408 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
409 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700410
Alex Deymoc1711e22014-08-08 13:16:23 -0700411 // ELF 32-bit LSB executable, MIPS
412 GetFileFormatTester(
413 "ELF 32-bit little-endian mips",
414 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
415 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
416 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
417 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
418
Alex Deymo032e7722014-03-25 17:53:56 -0700419 // ELF 32-bit LSB executable, ARM
420 GetFileFormatTester(
421 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700422 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
423 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
424 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
425 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700426
427 // ELF 64-bit LSB executable, x86-64
428 GetFileFormatTester(
429 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700430 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
431 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
432 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
433 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700434}
435
436namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800437gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
438 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
439 g_main_loop_quit(loop);
440 return FALSE; // Don't call this callback again
441}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700442} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800443
444TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
445 // Not much to test. At least this tests for memory leaks, crashes,
446 // log errors.
447 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
448 utils::ScheduleCrashReporterUpload();
449 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
450 g_main_loop_run(loop);
451 g_main_loop_unref(loop);
452}
453
David Zeuthen674c3182013-04-18 14:05:20 -0700454TEST(UtilsTest, FormatTimeDeltaTest) {
455 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
456 // which is not localized) so we only need to test the C locale
457 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
458 "0.1s");
459 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
460 "0s");
461 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
462 "1s");
463 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
464 "59s");
465 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
466 "1m0s");
467 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
468 "1m1s");
469 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
470 "1m30s");
471 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
472 "20m5s");
473 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
474 "1h0m0s");
475 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
476 "1h0m1s");
477 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
478 "1h1m1s");
479 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
480 "2h1m1s");
481 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
482 "1d0h0m0s");
483 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
484 "1d0h0m1s");
485 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
486 "2d7h33m20s");
487 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
488 base::TimeDelta::FromMilliseconds(1)),
489 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400490 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
491 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700492}
493
David Zeuthen27a48bc2013-08-06 12:06:29 -0700494TEST(UtilsTest, TimeFromStructTimespecTest) {
495 struct timespec ts;
496
497 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
498 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
499 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
500
501 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
502 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
503 .tv_nsec = 42 * 1000 * 1000};
504 base::Time::Exploded exploded = (base::Time::Exploded) {
505 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
506 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
507 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
508 utils::TimeFromStructTimespec(&ts));
509}
510
David Zeuthene7f89172013-10-31 10:21:04 -0700511TEST(UtilsTest, DecodeAndStoreBase64String) {
512 base::FilePath path;
513
514 // Ensure we return false on empty strings or invalid base64.
515 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
516 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
517
518 // Pass known base64 and check that it matches. This string was generated
519 // the following way:
520 //
521 // $ echo "Update Engine" | base64
522 // VXBkYXRlIEVuZ2luZQo=
523 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
524 &path));
525 ScopedPathUnlinker unlinker(path.value());
526 string expected_contents = "Update Engine\n";
527 string contents;
528 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
529 EXPECT_EQ(contents, expected_contents);
530 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
531}
532
David Zeuthen639aa362014-02-03 16:23:44 -0800533TEST(UtilsTest, ConvertToOmahaInstallDate) {
534 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
535 // Monday. In Unix time, this point in time is easily obtained via
536 // the date(1) command like this:
537 //
538 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
539 const time_t omaha_epoch = 1167638400;
540 int value;
541
542 // Points in time *on and after* the Omaha epoch should not fail.
543 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
544 base::Time::FromTimeT(omaha_epoch), &value));
545 EXPECT_GE(value, 0);
546
547 // Anything before the Omaha epoch should fail. We test it for two points.
548 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
549 base::Time::FromTimeT(omaha_epoch - 1), &value));
550 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
551 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
552
553 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
554 // on Jan 8, 2007 0:00 PST.
555 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
556 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
557 EXPECT_EQ(value, 0);
558 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
559 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
560 EXPECT_EQ(value, 7);
561
562 // Check a couple of more values.
563 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
564 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
565 EXPECT_EQ(value, 7);
566 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
567 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
568 EXPECT_EQ(value, 14);
569 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
570 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
571 EXPECT_EQ(value, 21);
572 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
573 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
574 EXPECT_EQ(value, 28);
575
576 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
577 // where the Omaha InstallDate jumps 7 days. Its unix time is
578 // 1180940400. Notably, this is a point in time where Daylight
579 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
580 //
581 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
582 // ignores DST (as it's hard to implement in a thread-safe way using
583 // glibc, see comments in utils.h) we have to fudge by the DST
584 // offset which is one hour. Conveniently, if the function were
585 // someday modified to be DST aware, this test would have to be
586 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700587 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800588 const time_t fudge = 3600;
589 int value1, value2;
590 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
591 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
592 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
593 base::Time::FromTimeT(dst_time + fudge), &value2));
594 EXPECT_EQ(value1, value2 - 7);
595}
596
David Zeuthen33bae492014-02-25 16:16:18 -0800597TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700598 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800599 FakeClock fake_clock;
600 base::TimeDelta duration;
601 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800602 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800603
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700604 fake_system_state.set_clock(&fake_clock);
605 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800606
607 // Initialize wallclock to 1 sec.
608 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
609
610 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700611 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800612 state_variable_key,
613 &duration));
614
615 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700616 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800617 state_variable_key,
618 &duration));
619 EXPECT_EQ(duration.InSeconds(), 0);
620
621 // We can also call it as many times as we want with it being
622 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700623 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800624 state_variable_key,
625 &duration));
626 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700627 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800628 state_variable_key,
629 &duration));
630 EXPECT_EQ(duration.InSeconds(), 0);
631
632 // Advance the clock one second, then we should get 1 sec on the
633 // next call and 0 sec on the subsequent call.
634 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700635 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800636 state_variable_key,
637 &duration));
638 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700639 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800640 state_variable_key,
641 &duration));
642 EXPECT_EQ(duration.InSeconds(), 0);
643
644 // Advance clock two seconds and we should get 2 sec and then 0 sec.
645 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700646 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800647 state_variable_key,
648 &duration));
649 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700650 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800651 state_variable_key,
652 &duration));
653 EXPECT_EQ(duration.InSeconds(), 0);
654
655 // There's a possibility that the wallclock can go backwards (NTP
656 // adjustments, for example) so check that we properly handle this
657 // case.
658 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700659 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800660 state_variable_key,
661 &duration));
662 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700663 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800664 state_variable_key,
665 &duration));
666 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800667}
668
669TEST(UtilsTest, MonotonicDurationHelper) {
670 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700671 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800672 FakeClock fake_clock;
673 base::TimeDelta duration;
674
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700675 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800676
677 // Initialize monotonic clock to 1 sec.
678 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
679
680 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700681 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800682 &storage,
683 &duration));
684
685 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700686 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800687 &storage,
688 &duration));
689 EXPECT_EQ(duration.InSeconds(), 0);
690
691 // We can also call it as many times as we want with it being
692 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700693 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800694 &storage,
695 &duration));
696 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700697 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800698 &storage,
699 &duration));
700 EXPECT_EQ(duration.InSeconds(), 0);
701
702 // Advance the clock one second, then we should get 1 sec on the
703 // next call and 0 sec on the subsequent call.
704 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700705 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800706 &storage,
707 &duration));
708 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700709 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800710 &storage,
711 &duration));
712 EXPECT_EQ(duration.InSeconds(), 0);
713
714 // Advance clock two seconds and we should get 2 sec and then 0 sec.
715 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700716 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800717 &storage,
718 &duration));
719 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700720 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800721 &storage,
722 &duration));
723 EXPECT_EQ(duration.InSeconds(), 0);
724}
725
David Zeuthenb281f072014-04-02 10:20:19 -0700726TEST(UtilsTest, GetConnectionType) {
727 // Check that expected combinations map to the right value.
728 EXPECT_EQ(metrics::ConnectionType::kUnknown,
729 utils::GetConnectionType(kNetUnknown,
730 NetworkTethering::kUnknown));
731 EXPECT_EQ(metrics::ConnectionType::kEthernet,
732 utils::GetConnectionType(kNetEthernet,
733 NetworkTethering::kUnknown));
734 EXPECT_EQ(metrics::ConnectionType::kWifi,
735 utils::GetConnectionType(kNetWifi,
736 NetworkTethering::kUnknown));
737 EXPECT_EQ(metrics::ConnectionType::kWimax,
738 utils::GetConnectionType(kNetWimax,
739 NetworkTethering::kUnknown));
740 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
741 utils::GetConnectionType(kNetBluetooth,
742 NetworkTethering::kUnknown));
743 EXPECT_EQ(metrics::ConnectionType::kCellular,
744 utils::GetConnectionType(kNetCellular,
745 NetworkTethering::kUnknown));
746 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
747 utils::GetConnectionType(kNetEthernet,
748 NetworkTethering::kConfirmed));
749 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
750 utils::GetConnectionType(kNetWifi,
751 NetworkTethering::kConfirmed));
752
753 // Ensure that we don't report tethered ethernet unless it's confirmed.
754 EXPECT_EQ(metrics::ConnectionType::kEthernet,
755 utils::GetConnectionType(kNetEthernet,
756 NetworkTethering::kNotDetected));
757 EXPECT_EQ(metrics::ConnectionType::kEthernet,
758 utils::GetConnectionType(kNetEthernet,
759 NetworkTethering::kSuspected));
760 EXPECT_EQ(metrics::ConnectionType::kEthernet,
761 utils::GetConnectionType(kNetEthernet,
762 NetworkTethering::kUnknown));
763
764 // Ditto for tethered wifi.
765 EXPECT_EQ(metrics::ConnectionType::kWifi,
766 utils::GetConnectionType(kNetWifi,
767 NetworkTethering::kNotDetected));
768 EXPECT_EQ(metrics::ConnectionType::kWifi,
769 utils::GetConnectionType(kNetWifi,
770 NetworkTethering::kSuspected));
771 EXPECT_EQ(metrics::ConnectionType::kWifi,
772 utils::GetConnectionType(kNetWifi,
773 NetworkTethering::kUnknown));
774}
775
adlr@google.com3defe6a2009-12-04 20:57:17 +0000776} // namespace chromeos_update_engine