blob: 2c30d30b842806e97a7f3159410246e1b810a64a [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
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080069 EXPECT_EQ("/dev/mtd2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
J. Richard Barnette30842932013-10-28 15:04:23 -070070 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
71
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080072 EXPECT_EQ("/dev/mtd2",
J. Richard Barnette30842932013-10-28 15:04:23 -070073 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080074 EXPECT_EQ("/dev/mtd4",
J. Richard Barnette30842932013-10-28 15:04:23 -070075 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080076 EXPECT_EQ("/dev/mtd6",
J. Richard Barnette30842932013-10-28 15:04:23 -070077 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
78 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
79}
80
adlr@google.com3defe6a2009-12-04 20:57:17 +000081TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080082 chromeos::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +000083 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
84}
85
Darin Petkov8e447e02013-04-16 16:23:50 +020086TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070087 base::FilePath file;
88 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +020089 ScopedPathUnlinker unlinker(file.value());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080090 chromeos::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +020091 const size_t kSize = 1024 * 1024;
92 for (size_t i = 0; i < kSize; i++) {
93 data.push_back(i % 255);
94 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080095 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
96 chromeos::Blob in_data;
Darin Petkov8e447e02013-04-16 16:23:50 +020097 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
98 EXPECT_TRUE(in_data.empty());
99 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
100 EXPECT_TRUE(data == in_data);
101 in_data.clear();
102 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800103 EXPECT_TRUE(chromeos::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
Darin Petkov8e447e02013-04-16 16:23:50 +0200104 in_data);
105}
106
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107TEST(UtilsTest, ErrnoNumberAsStringTest) {
108 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
109}
110
Darin Petkov002b2fe2010-11-22 13:53:22 -0800111TEST(UtilsTest, IsSymlinkTest) {
112 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800113 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700114 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800115 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700116 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800117 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
118 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
119 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
120 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
121 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Alex Deymo10875d92014-11-10 21:52:57 -0800122 EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800123}
124
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800125TEST(UtilsTest, GetDiskNameTest) {
126 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700127 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800128 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
129 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
130 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700131 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800132 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
133 EXPECT_EQ("", utils::GetDiskName("/"));
134 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700135}
136
137TEST(UtilsTest, SysfsBlockDeviceTest) {
138 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
139 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
140 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
141 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
142 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
143 EXPECT_EQ("", utils::SysfsBlockDevice(""));
144}
145
146TEST(UtilsTest, IsRemovableDeviceTest) {
147 EXPECT_FALSE(utils::IsRemovableDevice(""));
148 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700149}
150
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800151TEST(UtilsTest, GetPartitionNumberTest) {
152 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700153 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800154 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
155 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
156 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700157 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800158 EXPECT_EQ(0, utils::GetPartitionNumber(""));
159 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
160 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
161 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700162 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
163 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
164 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
165 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700166}
167
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700168TEST(UtilsTest, MakePartitionNameTest) {
169 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
170 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
171 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
172 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
173 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
174 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800175 EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5));
176 EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4));
177 EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3));
178 EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2));
179 EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1));
180}
181
182TEST(UtilsTest, MakePartitionNameForMountTest) {
183 EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4"));
184 EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123"));
185 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2"));
186 EXPECT_EQ("/dev/mmcblk0p2",
187 utils::MakePartitionNameForMount("/dev/mmcblk0p2"));
188 EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0"));
189 EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8"));
190 EXPECT_EQ("/dev/loop12p2",
191 utils::MakePartitionNameForMount("/dev/loop12p2"));
192 EXPECT_EQ("/dev/ubiblock5_0",
193 utils::MakePartitionNameForMount("/dev/ubiblock5_0"));
194 EXPECT_EQ("/dev/mtd4",
195 utils::MakePartitionNameForMount("/dev/ubi4_0"));
196 EXPECT_EQ("/dev/ubiblock3_0",
197 utils::MakePartitionNameForMount("/dev/ubiblock3"));
198 EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2"));
199 EXPECT_EQ("/dev/ubi1_0",
200 utils::MakePartitionNameForMount("/dev/ubiblock1"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700201}
202
Alex Deymo10875d92014-11-10 21:52:57 -0800203namespace {
204// Compares cpu shares and returns an integer that is less
205// than, equal to or greater than 0 if |shares_lhs| is,
206// respectively, lower than, same as or higher than |shares_rhs|.
207int CompareCpuShares(utils::CpuShares shares_lhs,
208 utils::CpuShares shares_rhs) {
209 return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs);
210}
211} // namespace
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700212
Alex Deymo10875d92014-11-10 21:52:57 -0800213// Tests the CPU shares enum is in the order we expect it.
Chris Sosa4f8ee272012-11-30 13:01:54 -0800214TEST(UtilsTest, CompareCpuSharesTest) {
Alex Deymo10875d92014-11-10 21:52:57 -0800215 EXPECT_LT(CompareCpuShares(utils::kCpuSharesLow,
216 utils::kCpuSharesNormal), 0);
217 EXPECT_GT(CompareCpuShares(utils::kCpuSharesNormal,
218 utils::kCpuSharesLow), 0);
219 EXPECT_EQ(CompareCpuShares(utils::kCpuSharesNormal,
220 utils::kCpuSharesNormal), 0);
221 EXPECT_GT(CompareCpuShares(utils::kCpuSharesHigh,
222 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700223}
224
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700225TEST(UtilsTest, FuzzIntTest) {
226 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800227 for (unsigned int range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700228 const int kValue = 50;
229 for (int tries = 0; tries < 100; ++tries) {
230 int value = utils::FuzzInt(kValue, range);
231 EXPECT_GE(value, kValue - range / 2);
232 EXPECT_LE(value, kValue + range - range / 2);
233 }
234 }
235}
236
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700237TEST(UtilsTest, ApplyMapTest) {
238 int initial_values[] = {1, 2, 3, 4, 6};
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800239 vector<int> collection(std::begin(initial_values), std::end(initial_values));
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700240 EXPECT_EQ(arraysize(initial_values), collection.size());
241 int expected_values[] = {1, 2, 5, 4, 8};
242 map<int, int> value_map;
243 value_map[3] = 5;
244 value_map[6] = 8;
245 value_map[5] = 10;
246
247 utils::ApplyMap(&collection, value_map);
248
249 size_t index = 0;
Alex Deymo020600d2014-11-05 21:05:55 -0800250 for (const int value : collection) {
251 EXPECT_EQ(expected_values[index++], value);
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700252 }
253}
254
Darin Petkovd3f8c892010-10-12 21:38:45 -0700255TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
256 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700257 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700258 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800259 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700260 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800261 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Alex Deymo1f93d032015-03-10 18:58:32 -0700262 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none",
Darin Petkovd3f8c892010-10-12 21:38:45 -0700263 img.c_str())));
264 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
265 int block_count = 0;
266 int block_size = 0;
267 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
268 EXPECT_EQ(4096, block_size);
269 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
270}
271
Alex Deymo192393b2014-11-10 15:58:38 -0800272// Squashfs example filesystem, generated with:
273// echo hola>hola
274// mksquashfs hola hola.sqfs -noappend -nopad
275// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800276const uint8_t kSquashfsFile[] = {
Alex Deymo192393b2014-11-10 15:58:38 -0800277 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
278 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
279 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
280 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
281 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
282 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
283 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
285 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
288 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
290 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
291 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
292 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
293 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
294 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
295 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
296 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
297 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
298 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
299 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
300 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
302 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
303 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
304 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
305 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
307};
308
309TEST(UtilsTest, GetSquashfs4Size) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800310 uint8_t buffer[sizeof(kSquashfsFile)];
Alex Deymo192393b2014-11-10 15:58:38 -0800311 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
312
313 int block_count = -1;
314 int block_size = -1;
315 // Not enough bytes passed.
316 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
317
318 // The whole file system is passed, which is enough for parsing.
319 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
320 &block_count, &block_size));
321 EXPECT_EQ(4096, block_size);
322 EXPECT_EQ(1, block_count);
323
324 // Modify the major version to 5.
325 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
326 *s_major = 5;
327 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
328 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
329
330 // Modify the bytes_used to have 6 blocks.
331 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
332 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
333 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
334 &block_count, &block_size));
335 EXPECT_EQ(4096, block_size);
336 EXPECT_EQ(6, block_count);
337}
338
Chris Sosad317e402013-06-12 13:47:09 -0700339TEST(UtilsTest, GetInstallDevTest) {
340 string boot_dev = "/dev/sda5";
341 string install_dev;
342 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
343 EXPECT_EQ(install_dev, "/dev/sda3");
344
345 boot_dev = "/dev/sda3";
346 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
347 EXPECT_EQ(install_dev, "/dev/sda5");
348
349 boot_dev = "/dev/sda12";
350 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700351
352 boot_dev = "/dev/ubiblock3_0";
353 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800354 EXPECT_EQ(install_dev, "/dev/ubi5_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700355
356 boot_dev = "/dev/ubiblock5_0";
357 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800358 EXPECT_EQ(install_dev, "/dev/ubi3_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700359
360 boot_dev = "/dev/ubiblock12_0";
361 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700362}
363
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800364namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700365void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700366 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800367 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700368 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
369 reinterpret_cast<const char*>(contents.data()),
370 contents.size()));
371 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
372}
Alex Deymo10875d92014-11-10 21:52:57 -0800373} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700374
375TEST(UtilsTest, GetFileFormatTest) {
376 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700377 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
378 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700379
380 // Real tests from cros_installer on different boards.
381 // ELF 32-bit LSB executable, Intel 80386
382 GetFileFormatTester(
383 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700384 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
385 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
387 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700388
Alex Deymoc1711e22014-08-08 13:16:23 -0700389 // ELF 32-bit LSB executable, MIPS
390 GetFileFormatTester(
391 "ELF 32-bit little-endian mips",
392 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
393 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
394 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
395 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
396
Alex Deymo032e7722014-03-25 17:53:56 -0700397 // ELF 32-bit LSB executable, ARM
398 GetFileFormatTester(
399 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700400 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
401 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
402 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
403 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700404
405 // ELF 64-bit LSB executable, x86-64
406 GetFileFormatTester(
407 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700408 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
409 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
410 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
411 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700412}
413
414namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800415gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
416 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
417 g_main_loop_quit(loop);
418 return FALSE; // Don't call this callback again
419}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700420} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800421
422TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
423 // Not much to test. At least this tests for memory leaks, crashes,
424 // log errors.
425 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
426 utils::ScheduleCrashReporterUpload();
427 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
428 g_main_loop_run(loop);
429 g_main_loop_unref(loop);
430}
431
David Zeuthen674c3182013-04-18 14:05:20 -0700432TEST(UtilsTest, FormatTimeDeltaTest) {
433 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
434 // which is not localized) so we only need to test the C locale
435 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
436 "0.1s");
437 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
438 "0s");
439 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
440 "1s");
441 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
442 "59s");
443 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
444 "1m0s");
445 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
446 "1m1s");
447 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
448 "1m30s");
449 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
450 "20m5s");
451 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
452 "1h0m0s");
453 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
454 "1h0m1s");
455 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
456 "1h1m1s");
457 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
458 "2h1m1s");
459 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
460 "1d0h0m0s");
461 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
462 "1d0h0m1s");
463 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
464 "2d7h33m20s");
465 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
466 base::TimeDelta::FromMilliseconds(1)),
467 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400468 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
469 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700470}
471
David Zeuthen27a48bc2013-08-06 12:06:29 -0700472TEST(UtilsTest, TimeFromStructTimespecTest) {
473 struct timespec ts;
474
475 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
476 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
477 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
478
479 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
480 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
481 .tv_nsec = 42 * 1000 * 1000};
482 base::Time::Exploded exploded = (base::Time::Exploded) {
483 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
484 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
485 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
486 utils::TimeFromStructTimespec(&ts));
487}
488
David Zeuthene7f89172013-10-31 10:21:04 -0700489TEST(UtilsTest, DecodeAndStoreBase64String) {
490 base::FilePath path;
491
492 // Ensure we return false on empty strings or invalid base64.
493 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
494 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
495
496 // Pass known base64 and check that it matches. This string was generated
497 // the following way:
498 //
499 // $ echo "Update Engine" | base64
500 // VXBkYXRlIEVuZ2luZQo=
501 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
502 &path));
503 ScopedPathUnlinker unlinker(path.value());
504 string expected_contents = "Update Engine\n";
505 string contents;
506 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
507 EXPECT_EQ(contents, expected_contents);
508 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
509}
510
David Zeuthen639aa362014-02-03 16:23:44 -0800511TEST(UtilsTest, ConvertToOmahaInstallDate) {
512 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
513 // Monday. In Unix time, this point in time is easily obtained via
514 // the date(1) command like this:
515 //
516 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
517 const time_t omaha_epoch = 1167638400;
518 int value;
519
520 // Points in time *on and after* the Omaha epoch should not fail.
521 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
522 base::Time::FromTimeT(omaha_epoch), &value));
523 EXPECT_GE(value, 0);
524
525 // Anything before the Omaha epoch should fail. We test it for two points.
526 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
527 base::Time::FromTimeT(omaha_epoch - 1), &value));
528 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
529 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
530
531 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
532 // on Jan 8, 2007 0:00 PST.
533 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
534 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
535 EXPECT_EQ(value, 0);
536 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
537 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
538 EXPECT_EQ(value, 7);
539
540 // Check a couple of more values.
541 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
542 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
543 EXPECT_EQ(value, 7);
544 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
545 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
546 EXPECT_EQ(value, 14);
547 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
548 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
549 EXPECT_EQ(value, 21);
550 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
551 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
552 EXPECT_EQ(value, 28);
553
554 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
555 // where the Omaha InstallDate jumps 7 days. Its unix time is
556 // 1180940400. Notably, this is a point in time where Daylight
557 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
558 //
559 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
560 // ignores DST (as it's hard to implement in a thread-safe way using
561 // glibc, see comments in utils.h) we have to fudge by the DST
562 // offset which is one hour. Conveniently, if the function were
563 // someday modified to be DST aware, this test would have to be
564 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700565 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800566 const time_t fudge = 3600;
567 int value1, value2;
568 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
569 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
570 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
571 base::Time::FromTimeT(dst_time + fudge), &value2));
572 EXPECT_EQ(value1, value2 - 7);
573}
574
David Zeuthen33bae492014-02-25 16:16:18 -0800575TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700576 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800577 FakeClock fake_clock;
578 base::TimeDelta duration;
579 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800580 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800581
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700582 fake_system_state.set_clock(&fake_clock);
583 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800584
585 // Initialize wallclock to 1 sec.
586 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
587
588 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700589 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800590 state_variable_key,
591 &duration));
592
593 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700594 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800595 state_variable_key,
596 &duration));
597 EXPECT_EQ(duration.InSeconds(), 0);
598
599 // We can also call it as many times as we want with it being
600 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700601 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800602 state_variable_key,
603 &duration));
604 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700605 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800606 state_variable_key,
607 &duration));
608 EXPECT_EQ(duration.InSeconds(), 0);
609
610 // Advance the clock one second, then we should get 1 sec on the
611 // next call and 0 sec on the subsequent call.
612 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700613 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800614 state_variable_key,
615 &duration));
616 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700617 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800618 state_variable_key,
619 &duration));
620 EXPECT_EQ(duration.InSeconds(), 0);
621
622 // Advance clock two seconds and we should get 2 sec and then 0 sec.
623 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700624 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800625 state_variable_key,
626 &duration));
627 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700628 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800629 state_variable_key,
630 &duration));
631 EXPECT_EQ(duration.InSeconds(), 0);
632
633 // There's a possibility that the wallclock can go backwards (NTP
634 // adjustments, for example) so check that we properly handle this
635 // case.
636 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700637 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800638 state_variable_key,
639 &duration));
640 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700641 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800642 state_variable_key,
643 &duration));
644 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800645}
646
647TEST(UtilsTest, MonotonicDurationHelper) {
648 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700649 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800650 FakeClock fake_clock;
651 base::TimeDelta duration;
652
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700653 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800654
655 // Initialize monotonic clock to 1 sec.
656 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
657
658 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700659 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800660 &storage,
661 &duration));
662
663 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700664 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800665 &storage,
666 &duration));
667 EXPECT_EQ(duration.InSeconds(), 0);
668
669 // We can also call it as many times as we want with it being
670 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700671 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800672 &storage,
673 &duration));
674 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700675 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800676 &storage,
677 &duration));
678 EXPECT_EQ(duration.InSeconds(), 0);
679
680 // Advance the clock one second, then we should get 1 sec on the
681 // next call and 0 sec on the subsequent call.
682 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700683 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800684 &storage,
685 &duration));
686 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700687 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800688 &storage,
689 &duration));
690 EXPECT_EQ(duration.InSeconds(), 0);
691
692 // Advance clock two seconds and we should get 2 sec and then 0 sec.
693 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700694 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800695 &storage,
696 &duration));
697 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700698 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800699 &storage,
700 &duration));
701 EXPECT_EQ(duration.InSeconds(), 0);
702}
703
David Zeuthenb281f072014-04-02 10:20:19 -0700704TEST(UtilsTest, GetConnectionType) {
705 // Check that expected combinations map to the right value.
706 EXPECT_EQ(metrics::ConnectionType::kUnknown,
707 utils::GetConnectionType(kNetUnknown,
708 NetworkTethering::kUnknown));
709 EXPECT_EQ(metrics::ConnectionType::kEthernet,
710 utils::GetConnectionType(kNetEthernet,
711 NetworkTethering::kUnknown));
712 EXPECT_EQ(metrics::ConnectionType::kWifi,
713 utils::GetConnectionType(kNetWifi,
714 NetworkTethering::kUnknown));
715 EXPECT_EQ(metrics::ConnectionType::kWimax,
716 utils::GetConnectionType(kNetWimax,
717 NetworkTethering::kUnknown));
718 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
719 utils::GetConnectionType(kNetBluetooth,
720 NetworkTethering::kUnknown));
721 EXPECT_EQ(metrics::ConnectionType::kCellular,
722 utils::GetConnectionType(kNetCellular,
723 NetworkTethering::kUnknown));
724 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
725 utils::GetConnectionType(kNetEthernet,
726 NetworkTethering::kConfirmed));
727 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
728 utils::GetConnectionType(kNetWifi,
729 NetworkTethering::kConfirmed));
730
731 // Ensure that we don't report tethered ethernet unless it's confirmed.
732 EXPECT_EQ(metrics::ConnectionType::kEthernet,
733 utils::GetConnectionType(kNetEthernet,
734 NetworkTethering::kNotDetected));
735 EXPECT_EQ(metrics::ConnectionType::kEthernet,
736 utils::GetConnectionType(kNetEthernet,
737 NetworkTethering::kSuspected));
738 EXPECT_EQ(metrics::ConnectionType::kEthernet,
739 utils::GetConnectionType(kNetEthernet,
740 NetworkTethering::kUnknown));
741
742 // Ditto for tethered wifi.
743 EXPECT_EQ(metrics::ConnectionType::kWifi,
744 utils::GetConnectionType(kNetWifi,
745 NetworkTethering::kNotDetected));
746 EXPECT_EQ(metrics::ConnectionType::kWifi,
747 utils::GetConnectionType(kNetWifi,
748 NetworkTethering::kSuspected));
749 EXPECT_EQ(metrics::ConnectionType::kWifi,
750 utils::GetConnectionType(kNetWifi,
751 NetworkTethering::kUnknown));
752}
753
Allie Wood78750a42015-02-11 15:42:11 -0800754TEST(UtilsTest, GetMinorVersion) {
755 // Test GetMinorVersion by verifying that it parses the conf file and returns
756 // the correct value.
757 string contents = "PAYLOAD_MINOR_VERSION=1\n";
758 uint32_t minor_version;
759
760 base::ScopedTempDir temp_dir;
761 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
762
763 base::FilePath temp_file("update_engine.conf");
764 base::FilePath filepath = temp_dir.path().Append(temp_file);
765
766 ASSERT_TRUE(test_utils::WriteFileString(filepath.value(), contents.c_str()));
767 ASSERT_TRUE(utils::GetMinorVersion(filepath, &minor_version));
768 ASSERT_EQ(minor_version, 1);
769}
770
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800771static bool BoolMacroTestHelper() {
772 int i = 1;
773 unsigned int ui = 1;
774 bool b = 1;
775 std::unique_ptr<char> cptr(new char);
776
777 TEST_AND_RETURN_FALSE(i);
778 TEST_AND_RETURN_FALSE(ui);
779 TEST_AND_RETURN_FALSE(b);
780 TEST_AND_RETURN_FALSE(cptr);
781
782 TEST_AND_RETURN_FALSE_ERRNO(i);
783 TEST_AND_RETURN_FALSE_ERRNO(ui);
784 TEST_AND_RETURN_FALSE_ERRNO(b);
785 TEST_AND_RETURN_FALSE_ERRNO(cptr);
786
787 return true;
788}
789
790static void VoidMacroTestHelper(bool* ret) {
791 int i = 1;
792 unsigned int ui = 1;
793 bool b = 1;
794 std::unique_ptr<char> cptr(new char);
795
796 *ret = false;
797
798 TEST_AND_RETURN(i);
799 TEST_AND_RETURN(ui);
800 TEST_AND_RETURN(b);
801 TEST_AND_RETURN(cptr);
802
803 TEST_AND_RETURN_ERRNO(i);
804 TEST_AND_RETURN_ERRNO(ui);
805 TEST_AND_RETURN_ERRNO(b);
806 TEST_AND_RETURN_ERRNO(cptr);
807
808 *ret = true;
809}
810
811TEST(UtilsTest, TestMacros) {
812 bool void_test = false;
813 VoidMacroTestHelper(&void_test);
814 EXPECT_TRUE(void_test);
815
816 EXPECT_TRUE(BoolMacroTestHelper());
817}
818
adlr@google.com3defe6a2009-12-04 20:57:17 +0000819} // namespace chromeos_update_engine