blob: 308345a74afcc8dd55cab822b4199e4688078dd6 [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>
Alex Deymo60ca1a72015-06-18 18:19:15 -070021#include <chromeos/message_loops/fake_message_loop.h>
22#include <chromeos/message_loops/message_loop_utils.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070023#include <gtest/gtest.h>
24
David Zeuthen33bae492014-02-25 16:16:18 -080025#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080026#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070027#include "update_engine/fake_system_state.h"
David Zeuthen33bae492014-02-25 16:16:18 -080028#include "update_engine/prefs.h"
Darin Petkovd3f8c892010-10-12 21:38:45 -070029#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000030
Alex Deymo60ca1a72015-06-18 18:19:15 -070031using chromeos::FakeMessageLoop;
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070032using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080033using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000034using std::vector;
35
36namespace chromeos_update_engine {
37
38class UtilsTest : public ::testing::Test { };
39
Chris Sosac1972482013-04-30 22:31:10 -070040TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070041 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070042 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
43 EXPECT_EQ("123456", utils::ParseECVersion(
44 "b=1231a fw_version=123456 a=fasd2"));
45 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
46 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070047 "vendor=\"sam\" fw_version=\"00VFA616\""));
48
49 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070050 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070051}
52
J. Richard Barnette30842932013-10-28 15:04:23 -070053
54TEST(UtilsTest, KernelDeviceOfBootDevice) {
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -070055 EXPECT_EQ("", utils::KernelDeviceOfBootDevice(""));
J. Richard Barnette30842932013-10-28 15:04:23 -070056 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo"));
57 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0"));
58 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1"));
59 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2"));
60 EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3"));
61 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4"));
62 EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5"));
63 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6"));
64 EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7"));
65 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8"));
66 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9"));
67
68 EXPECT_EQ("/dev/mmcblk0p2",
69 utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3"));
70 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4"));
71
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080072 EXPECT_EQ("/dev/mtd2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
J. Richard Barnette30842932013-10-28 15:04:23 -070073 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
74
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080075 EXPECT_EQ("/dev/mtd2",
J. Richard Barnette30842932013-10-28 15:04:23 -070076 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080077 EXPECT_EQ("/dev/mtd4",
J. Richard Barnette30842932013-10-28 15:04:23 -070078 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080079 EXPECT_EQ("/dev/mtd6",
J. Richard Barnette30842932013-10-28 15:04:23 -070080 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
81 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
82}
83
adlr@google.com3defe6a2009-12-04 20:57:17 +000084TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080085 chromeos::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +000086 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
87}
88
Darin Petkov8e447e02013-04-16 16:23:50 +020089TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070090 base::FilePath file;
91 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +020092 ScopedPathUnlinker unlinker(file.value());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080093 chromeos::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +020094 const size_t kSize = 1024 * 1024;
95 for (size_t i = 0; i < kSize; i++) {
96 data.push_back(i % 255);
97 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080098 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
99 chromeos::Blob in_data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200100 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
101 EXPECT_TRUE(in_data.empty());
102 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
103 EXPECT_TRUE(data == in_data);
104 in_data.clear();
105 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800106 EXPECT_TRUE(chromeos::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
Darin Petkov8e447e02013-04-16 16:23:50 +0200107 in_data);
108}
109
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110TEST(UtilsTest, ErrnoNumberAsStringTest) {
111 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
112}
113
Darin Petkov002b2fe2010-11-22 13:53:22 -0800114TEST(UtilsTest, IsSymlinkTest) {
115 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800116 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700117 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800118 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700119 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800120 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
121 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
122 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
123 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
124 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Alex Deymo10875d92014-11-10 21:52:57 -0800125 EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800126}
127
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800128TEST(UtilsTest, GetDiskNameTest) {
129 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700130 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800131 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
132 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
133 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700134 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800135 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
136 EXPECT_EQ("", utils::GetDiskName("/"));
137 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700138}
139
140TEST(UtilsTest, SysfsBlockDeviceTest) {
141 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
142 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
143 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
144 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
145 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
146 EXPECT_EQ("", utils::SysfsBlockDevice(""));
147}
148
149TEST(UtilsTest, IsRemovableDeviceTest) {
150 EXPECT_FALSE(utils::IsRemovableDevice(""));
151 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700152}
153
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800154TEST(UtilsTest, GetPartitionNumberTest) {
155 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700156 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800157 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
158 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
159 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700160 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800161 EXPECT_EQ(0, utils::GetPartitionNumber(""));
162 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
163 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
164 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700165 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
166 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
167 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
168 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700169}
170
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700171TEST(UtilsTest, MakePartitionNameTest) {
172 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
173 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
174 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
175 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
176 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
177 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800178 EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5));
179 EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4));
180 EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3));
181 EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2));
182 EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1));
183}
184
185TEST(UtilsTest, MakePartitionNameForMountTest) {
186 EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4"));
187 EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123"));
188 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2"));
189 EXPECT_EQ("/dev/mmcblk0p2",
190 utils::MakePartitionNameForMount("/dev/mmcblk0p2"));
191 EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0"));
192 EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8"));
193 EXPECT_EQ("/dev/loop12p2",
194 utils::MakePartitionNameForMount("/dev/loop12p2"));
195 EXPECT_EQ("/dev/ubiblock5_0",
196 utils::MakePartitionNameForMount("/dev/ubiblock5_0"));
197 EXPECT_EQ("/dev/mtd4",
198 utils::MakePartitionNameForMount("/dev/ubi4_0"));
199 EXPECT_EQ("/dev/ubiblock3_0",
200 utils::MakePartitionNameForMount("/dev/ubiblock3"));
201 EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2"));
202 EXPECT_EQ("/dev/ubi1_0",
203 utils::MakePartitionNameForMount("/dev/ubiblock1"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700204}
205
Alex Deymo10875d92014-11-10 21:52:57 -0800206namespace {
207// Compares cpu shares and returns an integer that is less
208// than, equal to or greater than 0 if |shares_lhs| is,
209// respectively, lower than, same as or higher than |shares_rhs|.
210int CompareCpuShares(utils::CpuShares shares_lhs,
211 utils::CpuShares shares_rhs) {
212 return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs);
213}
214} // namespace
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700215
Alex Deymo10875d92014-11-10 21:52:57 -0800216// Tests the CPU shares enum is in the order we expect it.
Chris Sosa4f8ee272012-11-30 13:01:54 -0800217TEST(UtilsTest, CompareCpuSharesTest) {
Alex Deymo10875d92014-11-10 21:52:57 -0800218 EXPECT_LT(CompareCpuShares(utils::kCpuSharesLow,
219 utils::kCpuSharesNormal), 0);
220 EXPECT_GT(CompareCpuShares(utils::kCpuSharesNormal,
221 utils::kCpuSharesLow), 0);
222 EXPECT_EQ(CompareCpuShares(utils::kCpuSharesNormal,
223 utils::kCpuSharesNormal), 0);
224 EXPECT_GT(CompareCpuShares(utils::kCpuSharesHigh,
225 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700226}
227
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700228TEST(UtilsTest, FuzzIntTest) {
229 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800230 for (unsigned int range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700231 const int kValue = 50;
232 for (int tries = 0; tries < 100; ++tries) {
233 int value = utils::FuzzInt(kValue, range);
234 EXPECT_GE(value, kValue - range / 2);
235 EXPECT_LE(value, kValue + range - range / 2);
236 }
237 }
238}
239
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700240TEST(UtilsTest, ApplyMapTest) {
241 int initial_values[] = {1, 2, 3, 4, 6};
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800242 vector<int> collection(std::begin(initial_values), std::end(initial_values));
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700243 EXPECT_EQ(arraysize(initial_values), collection.size());
244 int expected_values[] = {1, 2, 5, 4, 8};
245 map<int, int> value_map;
246 value_map[3] = 5;
247 value_map[6] = 8;
248 value_map[5] = 10;
249
250 utils::ApplyMap(&collection, value_map);
251
252 size_t index = 0;
Alex Deymo020600d2014-11-05 21:05:55 -0800253 for (const int value : collection) {
254 EXPECT_EQ(expected_values[index++], value);
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700255 }
256}
257
Darin Petkovd3f8c892010-10-12 21:38:45 -0700258TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
259 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700260 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700261 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800262 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700263 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800264 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Alex Deymo1f93d032015-03-10 18:58:32 -0700265 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none",
Darin Petkovd3f8c892010-10-12 21:38:45 -0700266 img.c_str())));
267 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
268 int block_count = 0;
269 int block_size = 0;
270 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
271 EXPECT_EQ(4096, block_size);
272 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
273}
274
Alex Deymo192393b2014-11-10 15:58:38 -0800275// Squashfs example filesystem, generated with:
276// echo hola>hola
277// mksquashfs hola hola.sqfs -noappend -nopad
278// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800279const uint8_t kSquashfsFile[] = {
Alex Deymo192393b2014-11-10 15:58:38 -0800280 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
281 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
282 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
283 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
284 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
285 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
286 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
288 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
293 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
294 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
295 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
296 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
297 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
298 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
299 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
300 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
301 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
302 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
303 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
304 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
305 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
306 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
308 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
310};
311
312TEST(UtilsTest, GetSquashfs4Size) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800313 uint8_t buffer[sizeof(kSquashfsFile)];
Alex Deymo192393b2014-11-10 15:58:38 -0800314 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
315
316 int block_count = -1;
317 int block_size = -1;
318 // Not enough bytes passed.
319 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
320
321 // The whole file system is passed, which is enough for parsing.
322 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
323 &block_count, &block_size));
324 EXPECT_EQ(4096, block_size);
325 EXPECT_EQ(1, block_count);
326
327 // Modify the major version to 5.
328 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
329 *s_major = 5;
330 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
331 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
332
333 // Modify the bytes_used to have 6 blocks.
334 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
335 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
336 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
337 &block_count, &block_size));
338 EXPECT_EQ(4096, block_size);
339 EXPECT_EQ(6, block_count);
340}
341
Chris Sosad317e402013-06-12 13:47:09 -0700342TEST(UtilsTest, GetInstallDevTest) {
343 string boot_dev = "/dev/sda5";
344 string install_dev;
345 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
346 EXPECT_EQ(install_dev, "/dev/sda3");
347
348 boot_dev = "/dev/sda3";
349 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
350 EXPECT_EQ(install_dev, "/dev/sda5");
351
352 boot_dev = "/dev/sda12";
353 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700354
355 boot_dev = "/dev/ubiblock3_0";
356 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800357 EXPECT_EQ(install_dev, "/dev/ubi5_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700358
359 boot_dev = "/dev/ubiblock5_0";
360 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800361 EXPECT_EQ(install_dev, "/dev/ubi3_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700362
363 boot_dev = "/dev/ubiblock12_0";
364 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700365}
366
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800367namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700368void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700369 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800370 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700371 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
372 reinterpret_cast<const char*>(contents.data()),
373 contents.size()));
374 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
375}
Alex Deymo10875d92014-11-10 21:52:57 -0800376} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700377
378TEST(UtilsTest, GetFileFormatTest) {
379 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700380 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
381 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700382
383 // Real tests from cros_installer on different boards.
384 // ELF 32-bit LSB executable, Intel 80386
385 GetFileFormatTester(
386 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700387 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
388 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
390 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700391
Alex Deymoc1711e22014-08-08 13:16:23 -0700392 // ELF 32-bit LSB executable, MIPS
393 GetFileFormatTester(
394 "ELF 32-bit little-endian mips",
395 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
398 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
399
Alex Deymo032e7722014-03-25 17:53:56 -0700400 // ELF 32-bit LSB executable, ARM
401 GetFileFormatTester(
402 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700403 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
404 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
405 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
406 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700407
408 // ELF 64-bit LSB executable, x86-64
409 GetFileFormatTester(
410 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700411 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
412 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
413 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
414 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700415}
416
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800417TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
418 // Not much to test. At least this tests for memory leaks, crashes,
419 // log errors.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700420 FakeMessageLoop loop(nullptr);
421 loop.SetAsCurrent();
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800422 utils::ScheduleCrashReporterUpload();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700423 // Test that we scheduled one callback from the crash reporter.
424 EXPECT_EQ(1, chromeos::MessageLoopRunMaxIterations(&loop, 100));
425 EXPECT_FALSE(loop.PendingTasks());
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800426}
427
David Zeuthen674c3182013-04-18 14:05:20 -0700428TEST(UtilsTest, FormatTimeDeltaTest) {
429 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
430 // which is not localized) so we only need to test the C locale
431 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
432 "0.1s");
433 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
434 "0s");
435 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
436 "1s");
437 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
438 "59s");
439 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
440 "1m0s");
441 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
442 "1m1s");
443 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
444 "1m30s");
445 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
446 "20m5s");
447 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
448 "1h0m0s");
449 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
450 "1h0m1s");
451 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
452 "1h1m1s");
453 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
454 "2h1m1s");
455 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
456 "1d0h0m0s");
457 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
458 "1d0h0m1s");
459 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
460 "2d7h33m20s");
461 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
462 base::TimeDelta::FromMilliseconds(1)),
463 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400464 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
465 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700466}
467
David Zeuthen27a48bc2013-08-06 12:06:29 -0700468TEST(UtilsTest, TimeFromStructTimespecTest) {
469 struct timespec ts;
470
471 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
472 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
473 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
474
475 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
476 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
477 .tv_nsec = 42 * 1000 * 1000};
478 base::Time::Exploded exploded = (base::Time::Exploded) {
479 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
480 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
481 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
482 utils::TimeFromStructTimespec(&ts));
483}
484
David Zeuthene7f89172013-10-31 10:21:04 -0700485TEST(UtilsTest, DecodeAndStoreBase64String) {
486 base::FilePath path;
487
488 // Ensure we return false on empty strings or invalid base64.
489 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
490 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
491
492 // Pass known base64 and check that it matches. This string was generated
493 // the following way:
494 //
495 // $ echo "Update Engine" | base64
496 // VXBkYXRlIEVuZ2luZQo=
497 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
498 &path));
499 ScopedPathUnlinker unlinker(path.value());
500 string expected_contents = "Update Engine\n";
501 string contents;
502 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
503 EXPECT_EQ(contents, expected_contents);
504 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
505}
506
David Zeuthen639aa362014-02-03 16:23:44 -0800507TEST(UtilsTest, ConvertToOmahaInstallDate) {
508 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
509 // Monday. In Unix time, this point in time is easily obtained via
510 // the date(1) command like this:
511 //
512 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
513 const time_t omaha_epoch = 1167638400;
514 int value;
515
516 // Points in time *on and after* the Omaha epoch should not fail.
517 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
518 base::Time::FromTimeT(omaha_epoch), &value));
519 EXPECT_GE(value, 0);
520
521 // Anything before the Omaha epoch should fail. We test it for two points.
522 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
523 base::Time::FromTimeT(omaha_epoch - 1), &value));
524 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
525 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
526
527 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
528 // on Jan 8, 2007 0:00 PST.
529 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
530 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
531 EXPECT_EQ(value, 0);
532 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
533 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
534 EXPECT_EQ(value, 7);
535
536 // Check a couple of more values.
537 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
538 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
539 EXPECT_EQ(value, 7);
540 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
541 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
542 EXPECT_EQ(value, 14);
543 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
544 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
545 EXPECT_EQ(value, 21);
546 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
547 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
548 EXPECT_EQ(value, 28);
549
550 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
551 // where the Omaha InstallDate jumps 7 days. Its unix time is
552 // 1180940400. Notably, this is a point in time where Daylight
553 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
554 //
555 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
556 // ignores DST (as it's hard to implement in a thread-safe way using
557 // glibc, see comments in utils.h) we have to fudge by the DST
558 // offset which is one hour. Conveniently, if the function were
559 // someday modified to be DST aware, this test would have to be
560 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700561 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800562 const time_t fudge = 3600;
563 int value1, value2;
564 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
565 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
566 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
567 base::Time::FromTimeT(dst_time + fudge), &value2));
568 EXPECT_EQ(value1, value2 - 7);
569}
570
David Zeuthen33bae492014-02-25 16:16:18 -0800571TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700572 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800573 FakeClock fake_clock;
574 base::TimeDelta duration;
575 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800576 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800577
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700578 fake_system_state.set_clock(&fake_clock);
579 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800580
581 // Initialize wallclock to 1 sec.
582 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
583
584 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700585 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800586 state_variable_key,
587 &duration));
588
589 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700590 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800591 state_variable_key,
592 &duration));
593 EXPECT_EQ(duration.InSeconds(), 0);
594
595 // We can also call it as many times as we want with it being
596 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700597 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800598 state_variable_key,
599 &duration));
600 EXPECT_EQ(duration.InSeconds(), 0);
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);
605
606 // Advance the clock one second, then we should get 1 sec on the
607 // next call and 0 sec on the subsequent call.
608 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700609 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800610 state_variable_key,
611 &duration));
612 EXPECT_EQ(duration.InSeconds(), 1);
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(), 0);
617
618 // Advance clock two seconds and we should get 2 sec and then 0 sec.
619 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700620 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800621 state_variable_key,
622 &duration));
623 EXPECT_EQ(duration.InSeconds(), 2);
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(), 0);
628
629 // There's a possibility that the wallclock can go backwards (NTP
630 // adjustments, for example) so check that we properly handle this
631 // case.
632 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700633 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800634 state_variable_key,
635 &duration));
636 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700637 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800638 state_variable_key,
639 &duration));
640 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800641}
642
643TEST(UtilsTest, MonotonicDurationHelper) {
644 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700645 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800646 FakeClock fake_clock;
647 base::TimeDelta duration;
648
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700649 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800650
651 // Initialize monotonic clock to 1 sec.
652 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
653
654 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700655 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800656 &storage,
657 &duration));
658
659 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700660 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800661 &storage,
662 &duration));
663 EXPECT_EQ(duration.InSeconds(), 0);
664
665 // We can also call it as many times as we want with it being
666 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700667 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800668 &storage,
669 &duration));
670 EXPECT_EQ(duration.InSeconds(), 0);
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);
675
676 // Advance the clock one second, then we should get 1 sec on the
677 // next call and 0 sec on the subsequent call.
678 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700679 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800680 &storage,
681 &duration));
682 EXPECT_EQ(duration.InSeconds(), 1);
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(), 0);
687
688 // Advance clock two seconds and we should get 2 sec and then 0 sec.
689 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700690 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800691 &storage,
692 &duration));
693 EXPECT_EQ(duration.InSeconds(), 2);
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(), 0);
698}
699
David Zeuthenb281f072014-04-02 10:20:19 -0700700TEST(UtilsTest, GetConnectionType) {
701 // Check that expected combinations map to the right value.
702 EXPECT_EQ(metrics::ConnectionType::kUnknown,
703 utils::GetConnectionType(kNetUnknown,
704 NetworkTethering::kUnknown));
705 EXPECT_EQ(metrics::ConnectionType::kEthernet,
706 utils::GetConnectionType(kNetEthernet,
707 NetworkTethering::kUnknown));
708 EXPECT_EQ(metrics::ConnectionType::kWifi,
709 utils::GetConnectionType(kNetWifi,
710 NetworkTethering::kUnknown));
711 EXPECT_EQ(metrics::ConnectionType::kWimax,
712 utils::GetConnectionType(kNetWimax,
713 NetworkTethering::kUnknown));
714 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
715 utils::GetConnectionType(kNetBluetooth,
716 NetworkTethering::kUnknown));
717 EXPECT_EQ(metrics::ConnectionType::kCellular,
718 utils::GetConnectionType(kNetCellular,
719 NetworkTethering::kUnknown));
720 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
721 utils::GetConnectionType(kNetEthernet,
722 NetworkTethering::kConfirmed));
723 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
724 utils::GetConnectionType(kNetWifi,
725 NetworkTethering::kConfirmed));
726
727 // Ensure that we don't report tethered ethernet unless it's confirmed.
728 EXPECT_EQ(metrics::ConnectionType::kEthernet,
729 utils::GetConnectionType(kNetEthernet,
730 NetworkTethering::kNotDetected));
731 EXPECT_EQ(metrics::ConnectionType::kEthernet,
732 utils::GetConnectionType(kNetEthernet,
733 NetworkTethering::kSuspected));
734 EXPECT_EQ(metrics::ConnectionType::kEthernet,
735 utils::GetConnectionType(kNetEthernet,
736 NetworkTethering::kUnknown));
737
738 // Ditto for tethered wifi.
739 EXPECT_EQ(metrics::ConnectionType::kWifi,
740 utils::GetConnectionType(kNetWifi,
741 NetworkTethering::kNotDetected));
742 EXPECT_EQ(metrics::ConnectionType::kWifi,
743 utils::GetConnectionType(kNetWifi,
744 NetworkTethering::kSuspected));
745 EXPECT_EQ(metrics::ConnectionType::kWifi,
746 utils::GetConnectionType(kNetWifi,
747 NetworkTethering::kUnknown));
748}
749
Allie Wood78750a42015-02-11 15:42:11 -0800750TEST(UtilsTest, GetMinorVersion) {
751 // Test GetMinorVersion by verifying that it parses the conf file and returns
752 // the correct value.
753 string contents = "PAYLOAD_MINOR_VERSION=1\n";
754 uint32_t minor_version;
755
756 base::ScopedTempDir temp_dir;
757 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
758
759 base::FilePath temp_file("update_engine.conf");
760 base::FilePath filepath = temp_dir.path().Append(temp_file);
761
762 ASSERT_TRUE(test_utils::WriteFileString(filepath.value(), contents.c_str()));
763 ASSERT_TRUE(utils::GetMinorVersion(filepath, &minor_version));
764 ASSERT_EQ(minor_version, 1);
765}
766
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800767static bool BoolMacroTestHelper() {
768 int i = 1;
769 unsigned int ui = 1;
770 bool b = 1;
771 std::unique_ptr<char> cptr(new char);
772
773 TEST_AND_RETURN_FALSE(i);
774 TEST_AND_RETURN_FALSE(ui);
775 TEST_AND_RETURN_FALSE(b);
776 TEST_AND_RETURN_FALSE(cptr);
777
778 TEST_AND_RETURN_FALSE_ERRNO(i);
779 TEST_AND_RETURN_FALSE_ERRNO(ui);
780 TEST_AND_RETURN_FALSE_ERRNO(b);
781 TEST_AND_RETURN_FALSE_ERRNO(cptr);
782
783 return true;
784}
785
786static void VoidMacroTestHelper(bool* ret) {
787 int i = 1;
788 unsigned int ui = 1;
789 bool b = 1;
790 std::unique_ptr<char> cptr(new char);
791
792 *ret = false;
793
794 TEST_AND_RETURN(i);
795 TEST_AND_RETURN(ui);
796 TEST_AND_RETURN(b);
797 TEST_AND_RETURN(cptr);
798
799 TEST_AND_RETURN_ERRNO(i);
800 TEST_AND_RETURN_ERRNO(ui);
801 TEST_AND_RETURN_ERRNO(b);
802 TEST_AND_RETURN_ERRNO(cptr);
803
804 *ret = true;
805}
806
807TEST(UtilsTest, TestMacros) {
808 bool void_test = false;
809 VoidMacroTestHelper(&void_test);
810 EXPECT_TRUE(void_test);
811
812 EXPECT_TRUE(BoolMacroTestHelper());
813}
814
adlr@google.com3defe6a2009-12-04 20:57:17 +0000815} // namespace chromeos_update_engine