blob: 4787e8406b7f8163b0f83b41fcbaff8780da153d [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
Darin Petkovd3f8c892010-10-12 21:38:45 -0700240TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
241 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700242 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700243 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800244 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700245 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800246 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Alex Deymo1f93d032015-03-10 18:58:32 -0700247 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none",
Darin Petkovd3f8c892010-10-12 21:38:45 -0700248 img.c_str())));
249 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
250 int block_count = 0;
251 int block_size = 0;
252 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
253 EXPECT_EQ(4096, block_size);
254 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
255}
256
Alex Deymo192393b2014-11-10 15:58:38 -0800257// Squashfs example filesystem, generated with:
258// echo hola>hola
259// mksquashfs hola hola.sqfs -noappend -nopad
260// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800261const uint8_t kSquashfsFile[] = {
Alex Deymo192393b2014-11-10 15:58:38 -0800262 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
263 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
264 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
265 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
266 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
267 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
268 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
270 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
271 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
272 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
274 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
275 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
276 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
277 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
278 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
279 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
280 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
281 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
282 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
283 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
284 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
285 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
286 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
287 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
288 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
289 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
290 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
292};
293
294TEST(UtilsTest, GetSquashfs4Size) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800295 uint8_t buffer[sizeof(kSquashfsFile)];
Alex Deymo192393b2014-11-10 15:58:38 -0800296 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
297
298 int block_count = -1;
299 int block_size = -1;
300 // Not enough bytes passed.
301 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
302
303 // The whole file system is passed, which is enough for parsing.
304 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
305 &block_count, &block_size));
306 EXPECT_EQ(4096, block_size);
307 EXPECT_EQ(1, block_count);
308
309 // Modify the major version to 5.
310 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
311 *s_major = 5;
312 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
313 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
314
315 // Modify the bytes_used to have 6 blocks.
316 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
317 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
318 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
319 &block_count, &block_size));
320 EXPECT_EQ(4096, block_size);
321 EXPECT_EQ(6, block_count);
322}
323
Chris Sosad317e402013-06-12 13:47:09 -0700324TEST(UtilsTest, GetInstallDevTest) {
325 string boot_dev = "/dev/sda5";
326 string install_dev;
327 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
328 EXPECT_EQ(install_dev, "/dev/sda3");
329
330 boot_dev = "/dev/sda3";
331 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
332 EXPECT_EQ(install_dev, "/dev/sda5");
333
334 boot_dev = "/dev/sda12";
335 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700336
337 boot_dev = "/dev/ubiblock3_0";
338 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800339 EXPECT_EQ(install_dev, "/dev/ubi5_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700340
341 boot_dev = "/dev/ubiblock5_0";
342 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800343 EXPECT_EQ(install_dev, "/dev/ubi3_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700344
345 boot_dev = "/dev/ubiblock12_0";
346 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700347}
348
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800349namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700350void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700351 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800352 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700353 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
354 reinterpret_cast<const char*>(contents.data()),
355 contents.size()));
356 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
357}
Alex Deymo10875d92014-11-10 21:52:57 -0800358} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700359
360TEST(UtilsTest, GetFileFormatTest) {
361 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700362 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
363 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700364
365 // Real tests from cros_installer on different boards.
366 // ELF 32-bit LSB executable, Intel 80386
367 GetFileFormatTester(
368 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700369 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
370 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
371 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
372 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700373
Alex Deymoc1711e22014-08-08 13:16:23 -0700374 // ELF 32-bit LSB executable, MIPS
375 GetFileFormatTester(
376 "ELF 32-bit little-endian mips",
377 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
378 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
379 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
380 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
381
Alex Deymo032e7722014-03-25 17:53:56 -0700382 // ELF 32-bit LSB executable, ARM
383 GetFileFormatTester(
384 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700385 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
386 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
387 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
388 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700389
390 // ELF 64-bit LSB executable, x86-64
391 GetFileFormatTester(
392 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700393 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
394 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
395 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
396 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700397}
398
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800399TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
400 // Not much to test. At least this tests for memory leaks, crashes,
401 // log errors.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700402 FakeMessageLoop loop(nullptr);
403 loop.SetAsCurrent();
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800404 utils::ScheduleCrashReporterUpload();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700405 // Test that we scheduled one callback from the crash reporter.
406 EXPECT_EQ(1, chromeos::MessageLoopRunMaxIterations(&loop, 100));
407 EXPECT_FALSE(loop.PendingTasks());
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800408}
409
David Zeuthen674c3182013-04-18 14:05:20 -0700410TEST(UtilsTest, FormatTimeDeltaTest) {
411 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
412 // which is not localized) so we only need to test the C locale
413 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
414 "0.1s");
415 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
416 "0s");
417 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
418 "1s");
419 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
420 "59s");
421 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
422 "1m0s");
423 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
424 "1m1s");
425 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
426 "1m30s");
427 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
428 "20m5s");
429 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
430 "1h0m0s");
431 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
432 "1h0m1s");
433 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
434 "1h1m1s");
435 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
436 "2h1m1s");
437 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
438 "1d0h0m0s");
439 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
440 "1d0h0m1s");
441 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
442 "2d7h33m20s");
443 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
444 base::TimeDelta::FromMilliseconds(1)),
445 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400446 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
447 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700448}
449
David Zeuthen27a48bc2013-08-06 12:06:29 -0700450TEST(UtilsTest, TimeFromStructTimespecTest) {
451 struct timespec ts;
452
453 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
454 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
455 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
456
457 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
458 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
459 .tv_nsec = 42 * 1000 * 1000};
460 base::Time::Exploded exploded = (base::Time::Exploded) {
461 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
462 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
463 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
464 utils::TimeFromStructTimespec(&ts));
465}
466
David Zeuthene7f89172013-10-31 10:21:04 -0700467TEST(UtilsTest, DecodeAndStoreBase64String) {
468 base::FilePath path;
469
470 // Ensure we return false on empty strings or invalid base64.
471 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
472 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
473
474 // Pass known base64 and check that it matches. This string was generated
475 // the following way:
476 //
477 // $ echo "Update Engine" | base64
478 // VXBkYXRlIEVuZ2luZQo=
479 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
480 &path));
481 ScopedPathUnlinker unlinker(path.value());
482 string expected_contents = "Update Engine\n";
483 string contents;
484 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
485 EXPECT_EQ(contents, expected_contents);
486 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
487}
488
David Zeuthen639aa362014-02-03 16:23:44 -0800489TEST(UtilsTest, ConvertToOmahaInstallDate) {
490 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
491 // Monday. In Unix time, this point in time is easily obtained via
492 // the date(1) command like this:
493 //
494 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
495 const time_t omaha_epoch = 1167638400;
496 int value;
497
498 // Points in time *on and after* the Omaha epoch should not fail.
499 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
500 base::Time::FromTimeT(omaha_epoch), &value));
501 EXPECT_GE(value, 0);
502
503 // Anything before the Omaha epoch should fail. We test it for two points.
504 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
505 base::Time::FromTimeT(omaha_epoch - 1), &value));
506 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
507 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
508
509 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
510 // on Jan 8, 2007 0:00 PST.
511 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
512 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
513 EXPECT_EQ(value, 0);
514 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
515 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
516 EXPECT_EQ(value, 7);
517
518 // Check a couple of more values.
519 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
520 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
521 EXPECT_EQ(value, 7);
522 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
523 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
524 EXPECT_EQ(value, 14);
525 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
526 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
527 EXPECT_EQ(value, 21);
528 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
529 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
530 EXPECT_EQ(value, 28);
531
532 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
533 // where the Omaha InstallDate jumps 7 days. Its unix time is
534 // 1180940400. Notably, this is a point in time where Daylight
535 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
536 //
537 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
538 // ignores DST (as it's hard to implement in a thread-safe way using
539 // glibc, see comments in utils.h) we have to fudge by the DST
540 // offset which is one hour. Conveniently, if the function were
541 // someday modified to be DST aware, this test would have to be
542 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700543 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800544 const time_t fudge = 3600;
545 int value1, value2;
546 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
547 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
548 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
549 base::Time::FromTimeT(dst_time + fudge), &value2));
550 EXPECT_EQ(value1, value2 - 7);
551}
552
David Zeuthen33bae492014-02-25 16:16:18 -0800553TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700554 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800555 FakeClock fake_clock;
556 base::TimeDelta duration;
557 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800558 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800559
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700560 fake_system_state.set_clock(&fake_clock);
561 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800562
563 // Initialize wallclock to 1 sec.
564 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
565
566 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700567 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800568 state_variable_key,
569 &duration));
570
571 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700572 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800573 state_variable_key,
574 &duration));
575 EXPECT_EQ(duration.InSeconds(), 0);
576
577 // We can also call it as many times as we want with it being
578 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700579 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800580 state_variable_key,
581 &duration));
582 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700583 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800584 state_variable_key,
585 &duration));
586 EXPECT_EQ(duration.InSeconds(), 0);
587
588 // Advance the clock one second, then we should get 1 sec on the
589 // next call and 0 sec on the subsequent call.
590 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700591 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800592 state_variable_key,
593 &duration));
594 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700595 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800596 state_variable_key,
597 &duration));
598 EXPECT_EQ(duration.InSeconds(), 0);
599
600 // Advance clock two seconds and we should get 2 sec and then 0 sec.
601 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700602 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800603 state_variable_key,
604 &duration));
605 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700606 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800607 state_variable_key,
608 &duration));
609 EXPECT_EQ(duration.InSeconds(), 0);
610
611 // There's a possibility that the wallclock can go backwards (NTP
612 // adjustments, for example) so check that we properly handle this
613 // case.
614 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700615 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800616 state_variable_key,
617 &duration));
618 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700619 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800620 state_variable_key,
621 &duration));
622 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800623}
624
625TEST(UtilsTest, MonotonicDurationHelper) {
626 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700627 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800628 FakeClock fake_clock;
629 base::TimeDelta duration;
630
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700631 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800632
633 // Initialize monotonic clock to 1 sec.
634 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
635
636 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700637 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800638 &storage,
639 &duration));
640
641 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700642 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800643 &storage,
644 &duration));
645 EXPECT_EQ(duration.InSeconds(), 0);
646
647 // We can also call it as many times as we want with it being
648 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700649 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800650 &storage,
651 &duration));
652 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700653 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800654 &storage,
655 &duration));
656 EXPECT_EQ(duration.InSeconds(), 0);
657
658 // Advance the clock one second, then we should get 1 sec on the
659 // next call and 0 sec on the subsequent call.
660 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700661 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800662 &storage,
663 &duration));
664 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700665 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800666 &storage,
667 &duration));
668 EXPECT_EQ(duration.InSeconds(), 0);
669
670 // Advance clock two seconds and we should get 2 sec and then 0 sec.
671 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700672 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800673 &storage,
674 &duration));
675 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700676 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800677 &storage,
678 &duration));
679 EXPECT_EQ(duration.InSeconds(), 0);
680}
681
David Zeuthenb281f072014-04-02 10:20:19 -0700682TEST(UtilsTest, GetConnectionType) {
683 // Check that expected combinations map to the right value.
684 EXPECT_EQ(metrics::ConnectionType::kUnknown,
685 utils::GetConnectionType(kNetUnknown,
686 NetworkTethering::kUnknown));
687 EXPECT_EQ(metrics::ConnectionType::kEthernet,
688 utils::GetConnectionType(kNetEthernet,
689 NetworkTethering::kUnknown));
690 EXPECT_EQ(metrics::ConnectionType::kWifi,
691 utils::GetConnectionType(kNetWifi,
692 NetworkTethering::kUnknown));
693 EXPECT_EQ(metrics::ConnectionType::kWimax,
694 utils::GetConnectionType(kNetWimax,
695 NetworkTethering::kUnknown));
696 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
697 utils::GetConnectionType(kNetBluetooth,
698 NetworkTethering::kUnknown));
699 EXPECT_EQ(metrics::ConnectionType::kCellular,
700 utils::GetConnectionType(kNetCellular,
701 NetworkTethering::kUnknown));
702 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
703 utils::GetConnectionType(kNetEthernet,
704 NetworkTethering::kConfirmed));
705 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
706 utils::GetConnectionType(kNetWifi,
707 NetworkTethering::kConfirmed));
708
709 // Ensure that we don't report tethered ethernet unless it's confirmed.
710 EXPECT_EQ(metrics::ConnectionType::kEthernet,
711 utils::GetConnectionType(kNetEthernet,
712 NetworkTethering::kNotDetected));
713 EXPECT_EQ(metrics::ConnectionType::kEthernet,
714 utils::GetConnectionType(kNetEthernet,
715 NetworkTethering::kSuspected));
716 EXPECT_EQ(metrics::ConnectionType::kEthernet,
717 utils::GetConnectionType(kNetEthernet,
718 NetworkTethering::kUnknown));
719
720 // Ditto for tethered wifi.
721 EXPECT_EQ(metrics::ConnectionType::kWifi,
722 utils::GetConnectionType(kNetWifi,
723 NetworkTethering::kNotDetected));
724 EXPECT_EQ(metrics::ConnectionType::kWifi,
725 utils::GetConnectionType(kNetWifi,
726 NetworkTethering::kSuspected));
727 EXPECT_EQ(metrics::ConnectionType::kWifi,
728 utils::GetConnectionType(kNetWifi,
729 NetworkTethering::kUnknown));
730}
731
Allie Wood78750a42015-02-11 15:42:11 -0800732TEST(UtilsTest, GetMinorVersion) {
733 // Test GetMinorVersion by verifying that it parses the conf file and returns
734 // the correct value.
Allie Wood78750a42015-02-11 15:42:11 -0800735 uint32_t minor_version;
736
Alex Deymob42b98d2015-07-06 17:42:38 -0700737 chromeos::KeyValueStore store;
738 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800739
Alex Deymob42b98d2015-07-06 17:42:38 -0700740 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n"));
741 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800742
Alex Deymob42b98d2015-07-06 17:42:38 -0700743 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n"));
744 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
745 EXPECT_EQ(minor_version, 123);
Allie Wood78750a42015-02-11 15:42:11 -0800746}
747
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800748static bool BoolMacroTestHelper() {
749 int i = 1;
750 unsigned int ui = 1;
751 bool b = 1;
752 std::unique_ptr<char> cptr(new char);
753
754 TEST_AND_RETURN_FALSE(i);
755 TEST_AND_RETURN_FALSE(ui);
756 TEST_AND_RETURN_FALSE(b);
757 TEST_AND_RETURN_FALSE(cptr);
758
759 TEST_AND_RETURN_FALSE_ERRNO(i);
760 TEST_AND_RETURN_FALSE_ERRNO(ui);
761 TEST_AND_RETURN_FALSE_ERRNO(b);
762 TEST_AND_RETURN_FALSE_ERRNO(cptr);
763
764 return true;
765}
766
767static void VoidMacroTestHelper(bool* ret) {
768 int i = 1;
769 unsigned int ui = 1;
770 bool b = 1;
771 std::unique_ptr<char> cptr(new char);
772
773 *ret = false;
774
775 TEST_AND_RETURN(i);
776 TEST_AND_RETURN(ui);
777 TEST_AND_RETURN(b);
778 TEST_AND_RETURN(cptr);
779
780 TEST_AND_RETURN_ERRNO(i);
781 TEST_AND_RETURN_ERRNO(ui);
782 TEST_AND_RETURN_ERRNO(b);
783 TEST_AND_RETURN_ERRNO(cptr);
784
785 *ret = true;
786}
787
788TEST(UtilsTest, TestMacros) {
789 bool void_test = false;
790 VoidMacroTestHelper(&void_test);
791 EXPECT_TRUE(void_test);
792
793 EXPECT_TRUE(BoolMacroTestHelper());
794}
795
adlr@google.com3defe6a2009-12-04 20:57:17 +0000796} // namespace chromeos_update_engine