blob: 41ea036ca6d2907189e5e51facaeef2878fb84d0 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/utils.h"
18
Alex Vakulenko44cab302014-07-23 13:12:15 -070019#include <errno.h>
Ben Chan9abb7632014-08-07 00:10:53 -070020#include <stdint.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <sys/stat.h>
22#include <sys/types.h>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070023
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070024#include <map>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080025#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000026#include <vector>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070027
Alex Deymoc1711e22014-08-08 13:16:23 -070028#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080029#include <base/files/file_util.h>
Allie Wood78750a42015-02-11 15:42:11 -080030#include <base/files/scoped_temp_dir.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070031#include <base/strings/string_util.h>
32#include <base/strings/stringprintf.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070033#include <chromeos/message_loops/fake_message_loop.h>
34#include <chromeos/message_loops/message_loop_utils.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070035#include <gtest/gtest.h>
36
David Zeuthen33bae492014-02-25 16:16:18 -080037#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080038#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070039#include "update_engine/fake_system_state.h"
David Zeuthen33bae492014-02-25 16:16:18 -080040#include "update_engine/prefs.h"
Darin Petkovd3f8c892010-10-12 21:38:45 -070041#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000042
Alex Deymo60ca1a72015-06-18 18:19:15 -070043using chromeos::FakeMessageLoop;
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070044using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080045using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000046using std::vector;
47
48namespace chromeos_update_engine {
49
50class UtilsTest : public ::testing::Test { };
51
Chris Sosac1972482013-04-30 22:31:10 -070052TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070053 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070054 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
55 EXPECT_EQ("123456", utils::ParseECVersion(
56 "b=1231a fw_version=123456 a=fasd2"));
57 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
58 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070059 "vendor=\"sam\" fw_version=\"00VFA616\""));
60
61 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070062 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070063}
64
J. Richard Barnette30842932013-10-28 15:04:23 -070065
66TEST(UtilsTest, KernelDeviceOfBootDevice) {
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -070067 EXPECT_EQ("", utils::KernelDeviceOfBootDevice(""));
J. Richard Barnette30842932013-10-28 15:04:23 -070068 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo"));
69 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0"));
70 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1"));
71 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2"));
72 EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3"));
73 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4"));
74 EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5"));
75 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6"));
76 EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7"));
77 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8"));
78 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9"));
79
80 EXPECT_EQ("/dev/mmcblk0p2",
81 utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3"));
82 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4"));
83
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080084 EXPECT_EQ("/dev/mtd2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
J. Richard Barnette30842932013-10-28 15:04:23 -070085 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
86
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080087 EXPECT_EQ("/dev/mtd2",
J. Richard Barnette30842932013-10-28 15:04:23 -070088 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080089 EXPECT_EQ("/dev/mtd4",
J. Richard Barnette30842932013-10-28 15:04:23 -070090 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080091 EXPECT_EQ("/dev/mtd6",
J. Richard Barnette30842932013-10-28 15:04:23 -070092 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
93 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
94}
95
adlr@google.com3defe6a2009-12-04 20:57:17 +000096TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080097 chromeos::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +000098 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
99}
100
Darin Petkov8e447e02013-04-16 16:23:50 +0200101TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700102 base::FilePath file;
103 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +0200104 ScopedPathUnlinker unlinker(file.value());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800105 chromeos::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200106 const size_t kSize = 1024 * 1024;
107 for (size_t i = 0; i < kSize; i++) {
108 data.push_back(i % 255);
109 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800110 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
111 chromeos::Blob in_data;
Darin Petkov8e447e02013-04-16 16:23:50 +0200112 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
113 EXPECT_TRUE(in_data.empty());
114 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
115 EXPECT_TRUE(data == in_data);
116 in_data.clear();
117 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800118 EXPECT_TRUE(chromeos::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
Darin Petkov8e447e02013-04-16 16:23:50 +0200119 in_data);
120}
121
adlr@google.com3defe6a2009-12-04 20:57:17 +0000122TEST(UtilsTest, ErrnoNumberAsStringTest) {
123 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
124}
125
Darin Petkov002b2fe2010-11-22 13:53:22 -0800126TEST(UtilsTest, IsSymlinkTest) {
127 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800128 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700129 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800130 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700131 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800132 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
133 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
134 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
135 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
136 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Alex Deymo10875d92014-11-10 21:52:57 -0800137 EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800138}
139
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800140TEST(UtilsTest, GetDiskNameTest) {
141 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700142 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800143 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
144 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
145 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700146 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800147 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
148 EXPECT_EQ("", utils::GetDiskName("/"));
149 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700150}
151
152TEST(UtilsTest, SysfsBlockDeviceTest) {
153 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
154 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
155 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
156 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
157 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
158 EXPECT_EQ("", utils::SysfsBlockDevice(""));
159}
160
161TEST(UtilsTest, IsRemovableDeviceTest) {
162 EXPECT_FALSE(utils::IsRemovableDevice(""));
163 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700164}
165
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800166TEST(UtilsTest, GetPartitionNumberTest) {
167 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700168 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800169 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
170 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
171 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700172 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800173 EXPECT_EQ(0, utils::GetPartitionNumber(""));
174 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
175 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
176 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700177 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
178 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
179 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
180 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700181}
182
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700183TEST(UtilsTest, MakePartitionNameTest) {
184 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
185 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
186 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
187 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
188 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
189 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800190 EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5));
191 EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4));
192 EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3));
193 EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2));
194 EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1));
195}
196
197TEST(UtilsTest, MakePartitionNameForMountTest) {
198 EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4"));
199 EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123"));
200 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2"));
201 EXPECT_EQ("/dev/mmcblk0p2",
202 utils::MakePartitionNameForMount("/dev/mmcblk0p2"));
203 EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0"));
204 EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8"));
205 EXPECT_EQ("/dev/loop12p2",
206 utils::MakePartitionNameForMount("/dev/loop12p2"));
207 EXPECT_EQ("/dev/ubiblock5_0",
208 utils::MakePartitionNameForMount("/dev/ubiblock5_0"));
209 EXPECT_EQ("/dev/mtd4",
210 utils::MakePartitionNameForMount("/dev/ubi4_0"));
211 EXPECT_EQ("/dev/ubiblock3_0",
212 utils::MakePartitionNameForMount("/dev/ubiblock3"));
213 EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2"));
214 EXPECT_EQ("/dev/ubi1_0",
215 utils::MakePartitionNameForMount("/dev/ubiblock1"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700216}
217
Alex Deymo10875d92014-11-10 21:52:57 -0800218namespace {
219// Compares cpu shares and returns an integer that is less
220// than, equal to or greater than 0 if |shares_lhs| is,
221// respectively, lower than, same as or higher than |shares_rhs|.
222int CompareCpuShares(utils::CpuShares shares_lhs,
223 utils::CpuShares shares_rhs) {
224 return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs);
225}
226} // namespace
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700227
Alex Deymo10875d92014-11-10 21:52:57 -0800228// Tests the CPU shares enum is in the order we expect it.
Chris Sosa4f8ee272012-11-30 13:01:54 -0800229TEST(UtilsTest, CompareCpuSharesTest) {
Alex Deymo10875d92014-11-10 21:52:57 -0800230 EXPECT_LT(CompareCpuShares(utils::kCpuSharesLow,
231 utils::kCpuSharesNormal), 0);
232 EXPECT_GT(CompareCpuShares(utils::kCpuSharesNormal,
233 utils::kCpuSharesLow), 0);
234 EXPECT_EQ(CompareCpuShares(utils::kCpuSharesNormal,
235 utils::kCpuSharesNormal), 0);
236 EXPECT_GT(CompareCpuShares(utils::kCpuSharesHigh,
237 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700238}
239
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700240TEST(UtilsTest, FuzzIntTest) {
241 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800242 for (unsigned int range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700243 const int kValue = 50;
244 for (int tries = 0; tries < 100; ++tries) {
245 int value = utils::FuzzInt(kValue, range);
246 EXPECT_GE(value, kValue - range / 2);
247 EXPECT_LE(value, kValue + range - range / 2);
248 }
249 }
250}
251
Darin Petkovd3f8c892010-10-12 21:38:45 -0700252TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
253 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700254 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700255 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800256 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700257 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800258 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Alex Deymo1f93d032015-03-10 18:58:32 -0700259 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none",
Darin Petkovd3f8c892010-10-12 21:38:45 -0700260 img.c_str())));
261 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
262 int block_count = 0;
263 int block_size = 0;
264 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
265 EXPECT_EQ(4096, block_size);
266 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
267}
268
Alex Deymo192393b2014-11-10 15:58:38 -0800269// Squashfs example filesystem, generated with:
270// echo hola>hola
271// mksquashfs hola hola.sqfs -noappend -nopad
272// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800273const uint8_t kSquashfsFile[] = {
Alex Deymo192393b2014-11-10 15:58:38 -0800274 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
275 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
276 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
277 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
278 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
279 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
280 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
281 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
282 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
283 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
285 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
287 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
288 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
289 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
290 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
291 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
292 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
293 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
294 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
295 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
296 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
297 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
298 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
299 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
300 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
302 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
304};
305
306TEST(UtilsTest, GetSquashfs4Size) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800307 uint8_t buffer[sizeof(kSquashfsFile)];
Alex Deymo192393b2014-11-10 15:58:38 -0800308 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
309
310 int block_count = -1;
311 int block_size = -1;
312 // Not enough bytes passed.
313 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
314
315 // The whole file system is passed, which is enough for parsing.
316 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
317 &block_count, &block_size));
318 EXPECT_EQ(4096, block_size);
319 EXPECT_EQ(1, block_count);
320
321 // Modify the major version to 5.
322 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
323 *s_major = 5;
324 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
325 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
326
327 // Modify the bytes_used to have 6 blocks.
328 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
329 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
330 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
331 &block_count, &block_size));
332 EXPECT_EQ(4096, block_size);
333 EXPECT_EQ(6, block_count);
334}
335
Chris Sosad317e402013-06-12 13:47:09 -0700336TEST(UtilsTest, GetInstallDevTest) {
337 string boot_dev = "/dev/sda5";
338 string install_dev;
339 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
340 EXPECT_EQ(install_dev, "/dev/sda3");
341
342 boot_dev = "/dev/sda3";
343 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
344 EXPECT_EQ(install_dev, "/dev/sda5");
345
346 boot_dev = "/dev/sda12";
347 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700348
349 boot_dev = "/dev/ubiblock3_0";
350 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800351 EXPECT_EQ(install_dev, "/dev/ubi5_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700352
353 boot_dev = "/dev/ubiblock5_0";
354 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800355 EXPECT_EQ(install_dev, "/dev/ubi3_0");
Liam McLoughlin049d1652013-07-31 18:47:46 -0700356
357 boot_dev = "/dev/ubiblock12_0";
358 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700359}
360
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800361namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700362void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700363 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800364 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700365 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
366 reinterpret_cast<const char*>(contents.data()),
367 contents.size()));
368 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
369}
Alex Deymo10875d92014-11-10 21:52:57 -0800370} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700371
372TEST(UtilsTest, GetFileFormatTest) {
373 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700374 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
375 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700376
377 // Real tests from cros_installer on different boards.
378 // ELF 32-bit LSB executable, Intel 80386
379 GetFileFormatTester(
380 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700381 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
382 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
383 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
384 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700385
Alex Deymoc1711e22014-08-08 13:16:23 -0700386 // ELF 32-bit LSB executable, MIPS
387 GetFileFormatTester(
388 "ELF 32-bit little-endian mips",
389 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
390 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
391 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
392 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
393
Alex Deymo032e7722014-03-25 17:53:56 -0700394 // ELF 32-bit LSB executable, ARM
395 GetFileFormatTester(
396 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700397 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
398 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
399 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
400 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700401
402 // ELF 64-bit LSB executable, x86-64
403 GetFileFormatTester(
404 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700405 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
406 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
408 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700409}
410
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800411TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
412 // Not much to test. At least this tests for memory leaks, crashes,
413 // log errors.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700414 FakeMessageLoop loop(nullptr);
415 loop.SetAsCurrent();
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800416 utils::ScheduleCrashReporterUpload();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700417 // Test that we scheduled one callback from the crash reporter.
418 EXPECT_EQ(1, chromeos::MessageLoopRunMaxIterations(&loop, 100));
419 EXPECT_FALSE(loop.PendingTasks());
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800420}
421
David Zeuthen674c3182013-04-18 14:05:20 -0700422TEST(UtilsTest, FormatTimeDeltaTest) {
423 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
424 // which is not localized) so we only need to test the C locale
425 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
426 "0.1s");
427 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
428 "0s");
429 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
430 "1s");
431 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
432 "59s");
433 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
434 "1m0s");
435 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
436 "1m1s");
437 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
438 "1m30s");
439 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
440 "20m5s");
441 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
442 "1h0m0s");
443 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
444 "1h0m1s");
445 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
446 "1h1m1s");
447 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
448 "2h1m1s");
449 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
450 "1d0h0m0s");
451 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
452 "1d0h0m1s");
453 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
454 "2d7h33m20s");
455 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
456 base::TimeDelta::FromMilliseconds(1)),
457 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400458 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
459 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700460}
461
David Zeuthen27a48bc2013-08-06 12:06:29 -0700462TEST(UtilsTest, TimeFromStructTimespecTest) {
463 struct timespec ts;
464
465 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
466 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
467 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
468
469 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
470 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
471 .tv_nsec = 42 * 1000 * 1000};
472 base::Time::Exploded exploded = (base::Time::Exploded) {
473 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
474 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
475 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
476 utils::TimeFromStructTimespec(&ts));
477}
478
David Zeuthene7f89172013-10-31 10:21:04 -0700479TEST(UtilsTest, DecodeAndStoreBase64String) {
480 base::FilePath path;
481
482 // Ensure we return false on empty strings or invalid base64.
483 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
484 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
485
486 // Pass known base64 and check that it matches. This string was generated
487 // the following way:
488 //
489 // $ echo "Update Engine" | base64
490 // VXBkYXRlIEVuZ2luZQo=
491 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
492 &path));
493 ScopedPathUnlinker unlinker(path.value());
494 string expected_contents = "Update Engine\n";
495 string contents;
496 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
497 EXPECT_EQ(contents, expected_contents);
498 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
499}
500
David Zeuthen639aa362014-02-03 16:23:44 -0800501TEST(UtilsTest, ConvertToOmahaInstallDate) {
502 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
503 // Monday. In Unix time, this point in time is easily obtained via
504 // the date(1) command like this:
505 //
506 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
507 const time_t omaha_epoch = 1167638400;
508 int value;
509
510 // Points in time *on and after* the Omaha epoch should not fail.
511 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
512 base::Time::FromTimeT(omaha_epoch), &value));
513 EXPECT_GE(value, 0);
514
515 // Anything before the Omaha epoch should fail. We test it for two points.
516 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
517 base::Time::FromTimeT(omaha_epoch - 1), &value));
518 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
519 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
520
521 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
522 // on Jan 8, 2007 0:00 PST.
523 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
524 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
525 EXPECT_EQ(value, 0);
526 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
527 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
528 EXPECT_EQ(value, 7);
529
530 // Check a couple of more values.
531 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
532 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
533 EXPECT_EQ(value, 7);
534 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
535 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
536 EXPECT_EQ(value, 14);
537 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
538 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
539 EXPECT_EQ(value, 21);
540 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
541 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
542 EXPECT_EQ(value, 28);
543
544 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
545 // where the Omaha InstallDate jumps 7 days. Its unix time is
546 // 1180940400. Notably, this is a point in time where Daylight
547 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
548 //
549 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
550 // ignores DST (as it's hard to implement in a thread-safe way using
551 // glibc, see comments in utils.h) we have to fudge by the DST
552 // offset which is one hour. Conveniently, if the function were
553 // someday modified to be DST aware, this test would have to be
554 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700555 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800556 const time_t fudge = 3600;
557 int value1, value2;
558 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
559 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
560 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
561 base::Time::FromTimeT(dst_time + fudge), &value2));
562 EXPECT_EQ(value1, value2 - 7);
563}
564
David Zeuthen33bae492014-02-25 16:16:18 -0800565TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700566 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800567 FakeClock fake_clock;
568 base::TimeDelta duration;
569 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800570 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800571
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700572 fake_system_state.set_clock(&fake_clock);
573 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800574
575 // Initialize wallclock to 1 sec.
576 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
577
578 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700579 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800580 state_variable_key,
581 &duration));
582
583 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700584 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800585 state_variable_key,
586 &duration));
587 EXPECT_EQ(duration.InSeconds(), 0);
588
589 // We can also call it as many times as we want with it being
590 // considered a failure.
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(), 0);
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 the clock one second, then we should get 1 sec on the
601 // next call and 0 sec on the subsequent call.
602 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700603 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800604 state_variable_key,
605 &duration));
606 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700607 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800608 state_variable_key,
609 &duration));
610 EXPECT_EQ(duration.InSeconds(), 0);
611
612 // Advance clock two seconds and we should get 2 sec and then 0 sec.
613 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700614 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800615 state_variable_key,
616 &duration));
617 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700618 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800619 state_variable_key,
620 &duration));
621 EXPECT_EQ(duration.InSeconds(), 0);
622
623 // There's a possibility that the wallclock can go backwards (NTP
624 // adjustments, for example) so check that we properly handle this
625 // case.
626 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700627 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800628 state_variable_key,
629 &duration));
630 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700631 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800632 state_variable_key,
633 &duration));
634 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800635}
636
637TEST(UtilsTest, MonotonicDurationHelper) {
638 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700639 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800640 FakeClock fake_clock;
641 base::TimeDelta duration;
642
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700643 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800644
645 // Initialize monotonic clock to 1 sec.
646 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
647
648 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700649 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800650 &storage,
651 &duration));
652
653 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700654 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800655 &storage,
656 &duration));
657 EXPECT_EQ(duration.InSeconds(), 0);
658
659 // We can also call it as many times as we want with it being
660 // considered a failure.
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(), 0);
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 the clock one second, then we should get 1 sec on the
671 // next call and 0 sec on the subsequent call.
672 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700673 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800674 &storage,
675 &duration));
676 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700677 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800678 &storage,
679 &duration));
680 EXPECT_EQ(duration.InSeconds(), 0);
681
682 // Advance clock two seconds and we should get 2 sec and then 0 sec.
683 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700684 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800685 &storage,
686 &duration));
687 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700688 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800689 &storage,
690 &duration));
691 EXPECT_EQ(duration.InSeconds(), 0);
692}
693
David Zeuthenb281f072014-04-02 10:20:19 -0700694TEST(UtilsTest, GetConnectionType) {
695 // Check that expected combinations map to the right value.
696 EXPECT_EQ(metrics::ConnectionType::kUnknown,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700697 utils::GetConnectionType(NetworkConnectionType::kUnknown,
David Zeuthenb281f072014-04-02 10:20:19 -0700698 NetworkTethering::kUnknown));
699 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700700 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700701 NetworkTethering::kUnknown));
702 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700703 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700704 NetworkTethering::kUnknown));
705 EXPECT_EQ(metrics::ConnectionType::kWimax,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700706 utils::GetConnectionType(NetworkConnectionType::kWimax,
David Zeuthenb281f072014-04-02 10:20:19 -0700707 NetworkTethering::kUnknown));
708 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700709 utils::GetConnectionType(NetworkConnectionType::kBluetooth,
David Zeuthenb281f072014-04-02 10:20:19 -0700710 NetworkTethering::kUnknown));
711 EXPECT_EQ(metrics::ConnectionType::kCellular,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700712 utils::GetConnectionType(NetworkConnectionType::kCellular,
David Zeuthenb281f072014-04-02 10:20:19 -0700713 NetworkTethering::kUnknown));
714 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700715 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700716 NetworkTethering::kConfirmed));
717 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700718 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700719 NetworkTethering::kConfirmed));
720
721 // Ensure that we don't report tethered ethernet unless it's confirmed.
722 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700723 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700724 NetworkTethering::kNotDetected));
725 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700726 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700727 NetworkTethering::kSuspected));
728 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700729 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700730 NetworkTethering::kUnknown));
731
732 // Ditto for tethered wifi.
733 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700734 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700735 NetworkTethering::kNotDetected));
736 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700737 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700738 NetworkTethering::kSuspected));
739 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700740 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700741 NetworkTethering::kUnknown));
742}
743
Allie Wood78750a42015-02-11 15:42:11 -0800744TEST(UtilsTest, GetMinorVersion) {
745 // Test GetMinorVersion by verifying that it parses the conf file and returns
746 // the correct value.
Allie Wood78750a42015-02-11 15:42:11 -0800747 uint32_t minor_version;
748
Alex Deymob42b98d2015-07-06 17:42:38 -0700749 chromeos::KeyValueStore store;
750 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800751
Alex Deymob42b98d2015-07-06 17:42:38 -0700752 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n"));
753 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800754
Alex Deymob42b98d2015-07-06 17:42:38 -0700755 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n"));
756 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
757 EXPECT_EQ(minor_version, 123);
Allie Wood78750a42015-02-11 15:42:11 -0800758}
759
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800760static bool BoolMacroTestHelper() {
761 int i = 1;
762 unsigned int ui = 1;
763 bool b = 1;
764 std::unique_ptr<char> cptr(new char);
765
766 TEST_AND_RETURN_FALSE(i);
767 TEST_AND_RETURN_FALSE(ui);
768 TEST_AND_RETURN_FALSE(b);
769 TEST_AND_RETURN_FALSE(cptr);
770
771 TEST_AND_RETURN_FALSE_ERRNO(i);
772 TEST_AND_RETURN_FALSE_ERRNO(ui);
773 TEST_AND_RETURN_FALSE_ERRNO(b);
774 TEST_AND_RETURN_FALSE_ERRNO(cptr);
775
776 return true;
777}
778
779static void VoidMacroTestHelper(bool* ret) {
780 int i = 1;
781 unsigned int ui = 1;
782 bool b = 1;
783 std::unique_ptr<char> cptr(new char);
784
785 *ret = false;
786
787 TEST_AND_RETURN(i);
788 TEST_AND_RETURN(ui);
789 TEST_AND_RETURN(b);
790 TEST_AND_RETURN(cptr);
791
792 TEST_AND_RETURN_ERRNO(i);
793 TEST_AND_RETURN_ERRNO(ui);
794 TEST_AND_RETURN_ERRNO(b);
795 TEST_AND_RETURN_ERRNO(cptr);
796
797 *ret = true;
798}
799
800TEST(UtilsTest, TestMacros) {
801 bool void_test = false;
802 VoidMacroTestHelper(&void_test);
803 EXPECT_TRUE(void_test);
804
805 EXPECT_TRUE(BoolMacroTestHelper());
806}
807
adlr@google.com3defe6a2009-12-04 20:57:17 +0000808} // namespace chromeos_update_engine