blob: fa97f5576f973e8feb50a5c5efcb64f575bd51ab [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
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <errno.h>
Darin Petkov5c0a8af2010-08-24 13:39:13 -07008
Andrew de los Reyescc92cd32010-10-05 16:56:14 -07009#include <map>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080010#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000011#include <vector>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070012
Alex Vakulenko75039d72014-03-25 12:36:28 -070013#include <base/files/file_path.h>
Darin Petkov8e447e02013-04-16 16:23:50 +020014#include <base/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070015#include <base/strings/string_util.h>
16#include <base/strings/stringprintf.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070017#include <gtest/gtest.h>
18
David Zeuthen33bae492014-02-25 16:16:18 -080019#include "update_engine/fake_clock.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070020#include "update_engine/fake_system_state.h"
David Zeuthen33bae492014-02-25 16:16:18 -080021#include "update_engine/prefs.h"
Darin Petkovd3f8c892010-10-12 21:38:45 -070022#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000023#include "update_engine/utils.h"
24
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070025using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080026using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000027using std::vector;
28
29namespace chromeos_update_engine {
30
31class UtilsTest : public ::testing::Test { };
32
Chris Sosac1972482013-04-30 22:31:10 -070033TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070034 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070035 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
36 EXPECT_EQ("123456", utils::ParseECVersion(
37 "b=1231a fw_version=123456 a=fasd2"));
38 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
39 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070040 "vendor=\"sam\" fw_version=\"00VFA616\""));
41
42 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070043 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070044}
45
J. Richard Barnette30842932013-10-28 15:04:23 -070046
47TEST(UtilsTest, KernelDeviceOfBootDevice) {
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -070048 EXPECT_EQ("", utils::KernelDeviceOfBootDevice(""));
J. Richard Barnette30842932013-10-28 15:04:23 -070049 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo"));
50 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0"));
51 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1"));
52 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2"));
53 EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3"));
54 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4"));
55 EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5"));
56 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6"));
57 EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7"));
58 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8"));
59 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9"));
60
61 EXPECT_EQ("/dev/mmcblk0p2",
62 utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3"));
63 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4"));
64
65 EXPECT_EQ("/dev/ubi2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
66 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
67
68 EXPECT_EQ("/dev/mtdblock2",
69 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
70 EXPECT_EQ("/dev/mtdblock4",
71 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
72 EXPECT_EQ("/dev/mtdblock6",
73 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
74 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
75}
76
77
adlr@google.com3defe6a2009-12-04 20:57:17 +000078TEST(UtilsTest, NormalizePathTest) {
79 EXPECT_EQ("", utils::NormalizePath("", false));
80 EXPECT_EQ("", utils::NormalizePath("", true));
81 EXPECT_EQ("/", utils::NormalizePath("/", false));
82 EXPECT_EQ("", utils::NormalizePath("/", true));
83 EXPECT_EQ("/", utils::NormalizePath("//", false));
84 EXPECT_EQ("", utils::NormalizePath("//", true));
85 EXPECT_EQ("foo", utils::NormalizePath("foo", false));
86 EXPECT_EQ("foo", utils::NormalizePath("foo", true));
87 EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false));
88 EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true));
89 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
90 false));
91 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
92 true));
93 EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/",
94 false));
95 EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/",
96 true));
97 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false));
98 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -070099 EXPECT_EQ("\\:/;$PATH\n\\",
100 utils::NormalizePath("\\://;$PATH\n\\", false));
101 EXPECT_EQ("\\:/;$PATH\n\\",
102 utils::NormalizePath("\\://;$PATH\n\\", true));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000103 EXPECT_EQ("/spaces s/ ok/s / / /",
104 utils::NormalizePath("/spaces s/ ok/s / / /", false));
105 EXPECT_EQ("/spaces s/ ok/s / / ",
106 utils::NormalizePath("/spaces s/ ok/s / / /", true));
107}
108
109TEST(UtilsTest, ReadFileFailure) {
110 vector<char> empty;
111 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
112}
113
Darin Petkov8e447e02013-04-16 16:23:50 +0200114TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700115 base::FilePath file;
116 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +0200117 ScopedPathUnlinker unlinker(file.value());
118 vector<char> data;
119 const size_t kSize = 1024 * 1024;
120 for (size_t i = 0; i < kSize; i++) {
121 data.push_back(i % 255);
122 }
123 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), &data[0], data.size()));
124 vector<char> in_data;
125 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
126 EXPECT_TRUE(in_data.empty());
127 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
128 EXPECT_TRUE(data == in_data);
129 in_data.clear();
130 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
131 EXPECT_TRUE(vector<char>(data.begin() + 10, data.begin() + 10 + 20) ==
132 in_data);
133}
134
adlr@google.com3defe6a2009-12-04 20:57:17 +0000135TEST(UtilsTest, ErrnoNumberAsStringTest) {
136 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
137}
138
139TEST(UtilsTest, StringHasSuffixTest) {
140 EXPECT_TRUE(utils::StringHasSuffix("foo", "foo"));
141 EXPECT_TRUE(utils::StringHasSuffix("foo", "o"));
142 EXPECT_TRUE(utils::StringHasSuffix("", ""));
143 EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc"));
144 EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere"));
145 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh"));
146 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", ""));
147 EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo"));
148 EXPECT_FALSE(utils::StringHasSuffix("", "x"));
149 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg"));
150 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab"));
151}
152
153TEST(UtilsTest, StringHasPrefixTest) {
154 EXPECT_TRUE(utils::StringHasPrefix("foo", "foo"));
155 EXPECT_TRUE(utils::StringHasPrefix("foo", "f"));
156 EXPECT_TRUE(utils::StringHasPrefix("", ""));
157 EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc"));
158 EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl"));
159 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab"));
160 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", ""));
161 EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa"));
162 EXPECT_FALSE(utils::StringHasPrefix("", "x"));
163 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc"));
164 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh"));
165}
166
adlr@google.com3defe6a2009-12-04 20:57:17 +0000167TEST(UtilsTest, RecursiveUnlinkDirTest) {
Gilad Arnoldb4346a22013-07-16 06:44:45 -0700168 string first_dir_name;
169 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-a-XXXXXX",
170 &first_dir_name));
171 ASSERT_EQ(0, Chmod(first_dir_name, 0755));
172 string second_dir_name;
173 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-b-XXXXXX",
174 &second_dir_name));
175 ASSERT_EQ(0, Chmod(second_dir_name, 0755));
176
177 EXPECT_EQ(0, Symlink(string("../") + first_dir_name,
178 second_dir_name + "/link"));
179 EXPECT_EQ(0, System(string("echo hi > ") + second_dir_name + "/file"));
180 EXPECT_EQ(0, Mkdir(second_dir_name + "/dir", 0755));
181 EXPECT_EQ(0, System(string("echo ok > ") + second_dir_name + "/dir/subfile"));
182 EXPECT_TRUE(utils::RecursiveUnlinkDir(second_dir_name));
183 EXPECT_TRUE(utils::FileExists(first_dir_name.c_str()));
184 EXPECT_EQ(0, System(string("rm -rf ") + first_dir_name));
185 EXPECT_FALSE(utils::FileExists(second_dir_name.c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000186 EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist"));
187}
188
Darin Petkov002b2fe2010-11-22 13:53:22 -0800189TEST(UtilsTest, IsSymlinkTest) {
190 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800191 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700192 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800193 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700194 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800195 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
196 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
197 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
198 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
199 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
200 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
201}
202
Alex Deymo7dc4c502014-05-20 20:09:58 -0700203TEST(UtilsTest, IsDirTest) {
204 string temp_dir;
205 EXPECT_TRUE(utils::MakeTempDirectory("isdir-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700206 string temp_file = temp_dir + "/temp-file";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700207 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700208 string temp_symlink = temp_dir + "/temp-symlink";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700209 EXPECT_EQ(0, symlink(temp_dir.c_str(), temp_symlink.c_str()));
210 EXPECT_TRUE(utils::IsDir(temp_dir.c_str()));
211 EXPECT_FALSE(utils::IsDir(temp_file.c_str()));
212 EXPECT_FALSE(utils::IsDir(temp_symlink.c_str()));
213 EXPECT_FALSE(utils::IsDir("/non/existent/path"));
214 ASSERT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
215}
216
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800217TEST(UtilsTest, GetDiskNameTest) {
218 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700219 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800220 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
221 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
222 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700223 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800224 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
225 EXPECT_EQ("", utils::GetDiskName("/"));
226 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700227}
228
229TEST(UtilsTest, SysfsBlockDeviceTest) {
230 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
231 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
232 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
233 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
234 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
235 EXPECT_EQ("", utils::SysfsBlockDevice(""));
236}
237
238TEST(UtilsTest, IsRemovableDeviceTest) {
239 EXPECT_FALSE(utils::IsRemovableDevice(""));
240 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700241}
242
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800243TEST(UtilsTest, GetPartitionNumberTest) {
244 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700245 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800246 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
247 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
248 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700249 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800250 EXPECT_EQ(0, utils::GetPartitionNumber(""));
251 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
252 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
253 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700254 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
255 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
256 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
257 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700258}
259
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700260TEST(UtilsTest, MakePartitionNameTest) {
261 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
262 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
263 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
264 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
265 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
266 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
267 EXPECT_EQ("/dev/ubiblock3_0", utils::MakePartitionName("/dev/ubiblock", 3));
268}
269
270
Chris Sosa4f8ee272012-11-30 13:01:54 -0800271TEST(UtilsTest, CompareCpuSharesTest) {
272 EXPECT_LT(utils::CompareCpuShares(utils::kCpuSharesLow,
273 utils::kCpuSharesNormal), 0);
274 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesNormal,
275 utils::kCpuSharesLow), 0);
276 EXPECT_EQ(utils::CompareCpuShares(utils::kCpuSharesNormal,
277 utils::kCpuSharesNormal), 0);
278 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesHigh,
279 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700280}
281
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700282TEST(UtilsTest, FuzzIntTest) {
283 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
284 for (size_t r = 0; r < arraysize(kRanges); ++r) {
285 unsigned int range = kRanges[r];
286 const int kValue = 50;
287 for (int tries = 0; tries < 100; ++tries) {
288 int value = utils::FuzzInt(kValue, range);
289 EXPECT_GE(value, kValue - range / 2);
290 EXPECT_LE(value, kValue + range - range / 2);
291 }
292 }
293}
294
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700295TEST(UtilsTest, ApplyMapTest) {
296 int initial_values[] = {1, 2, 3, 4, 6};
297 vector<int> collection(&initial_values[0],
298 initial_values + arraysize(initial_values));
299 EXPECT_EQ(arraysize(initial_values), collection.size());
300 int expected_values[] = {1, 2, 5, 4, 8};
301 map<int, int> value_map;
302 value_map[3] = 5;
303 value_map[6] = 8;
304 value_map[5] = 10;
305
306 utils::ApplyMap(&collection, value_map);
307
308 size_t index = 0;
309 for (vector<int>::iterator it = collection.begin(), e = collection.end();
310 it != e; ++it) {
311 EXPECT_EQ(expected_values[index++], *it);
312 }
313}
314
Darin Petkovd3f8c892010-10-12 21:38:45 -0700315TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
316 string img;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800317 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, NULL));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700318 ScopedPathUnlinker img_unlinker(img);
319 CreateExtImageAtPath(img, NULL);
320 // Extend the "partition" holding the file system from 10MiB to 20MiB.
321 EXPECT_EQ(0, System(base::StringPrintf(
322 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
323 img.c_str())));
324 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
325 int block_count = 0;
326 int block_size = 0;
327 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
328 EXPECT_EQ(4096, block_size);
329 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
330}
331
Chris Sosad317e402013-06-12 13:47:09 -0700332TEST(UtilsTest, GetInstallDevTest) {
333 string boot_dev = "/dev/sda5";
334 string install_dev;
335 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
336 EXPECT_EQ(install_dev, "/dev/sda3");
337
338 boot_dev = "/dev/sda3";
339 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
340 EXPECT_EQ(install_dev, "/dev/sda5");
341
342 boot_dev = "/dev/sda12";
343 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700344
345 boot_dev = "/dev/ubiblock3_0";
346 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
347 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
348
349 boot_dev = "/dev/ubiblock5_0";
350 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
351 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
352
353 boot_dev = "/dev/ubiblock12_0";
354 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700355}
356
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800357namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700358void GetFileFormatTester(const string& expected,
359 const vector<uint8>& contents) {
360 ScopedTempFile file;
361 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
362 reinterpret_cast<const char*>(contents.data()),
363 contents.size()));
364 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
365}
366}
367
368TEST(UtilsTest, GetFileFormatTest) {
369 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
370 GetFileFormatTester("data", vector<uint8>{1, 2, 3, 4, 5, 6, 7, 8});
371 GetFileFormatTester("ELF", vector<uint8>{0x7f, 0x45, 0x4c, 0x46});
372
373 // Real tests from cros_installer on different boards.
374 // ELF 32-bit LSB executable, Intel 80386
375 GetFileFormatTester(
376 "ELF 32-bit little-endian x86",
377 vector<uint8>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
378 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
379 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
380 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
381
382 // ELF 32-bit LSB executable, ARM
383 GetFileFormatTester(
384 "ELF 32-bit little-endian arm",
385 vector<uint8>{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});
389
390 // ELF 64-bit LSB executable, x86-64
391 GetFileFormatTester(
392 "ELF 64-bit little-endian x86-64",
393 vector<uint8>{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});
397}
398
399namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800400gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
401 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
402 g_main_loop_quit(loop);
403 return FALSE; // Don't call this callback again
404}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700405} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800406
407TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
408 // Not much to test. At least this tests for memory leaks, crashes,
409 // log errors.
410 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
411 utils::ScheduleCrashReporterUpload();
412 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
413 g_main_loop_run(loop);
414 g_main_loop_unref(loop);
415}
416
David Zeuthen674c3182013-04-18 14:05:20 -0700417TEST(UtilsTest, FormatTimeDeltaTest) {
418 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
419 // which is not localized) so we only need to test the C locale
420 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
421 "0.1s");
422 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
423 "0s");
424 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
425 "1s");
426 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
427 "59s");
428 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
429 "1m0s");
430 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
431 "1m1s");
432 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
433 "1m30s");
434 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
435 "20m5s");
436 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
437 "1h0m0s");
438 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
439 "1h0m1s");
440 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
441 "1h1m1s");
442 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
443 "2h1m1s");
444 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
445 "1d0h0m0s");
446 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
447 "1d0h0m1s");
448 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
449 "2d7h33m20s");
450 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
451 base::TimeDelta::FromMilliseconds(1)),
452 "2d7h33m20.001s");
453}
454
David Zeuthen27a48bc2013-08-06 12:06:29 -0700455TEST(UtilsTest, TimeFromStructTimespecTest) {
456 struct timespec ts;
457
458 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
459 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
460 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
461
462 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
463 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
464 .tv_nsec = 42 * 1000 * 1000};
465 base::Time::Exploded exploded = (base::Time::Exploded) {
466 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
467 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
468 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
469 utils::TimeFromStructTimespec(&ts));
470}
471
David Zeuthene7f89172013-10-31 10:21:04 -0700472TEST(UtilsTest, DecodeAndStoreBase64String) {
473 base::FilePath path;
474
475 // Ensure we return false on empty strings or invalid base64.
476 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
477 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
478
479 // Pass known base64 and check that it matches. This string was generated
480 // the following way:
481 //
482 // $ echo "Update Engine" | base64
483 // VXBkYXRlIEVuZ2luZQo=
484 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
485 &path));
486 ScopedPathUnlinker unlinker(path.value());
487 string expected_contents = "Update Engine\n";
488 string contents;
489 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
490 EXPECT_EQ(contents, expected_contents);
491 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
492}
493
David Zeuthen639aa362014-02-03 16:23:44 -0800494TEST(UtilsTest, ConvertToOmahaInstallDate) {
495 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
496 // Monday. In Unix time, this point in time is easily obtained via
497 // the date(1) command like this:
498 //
499 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
500 const time_t omaha_epoch = 1167638400;
501 int value;
502
503 // Points in time *on and after* the Omaha epoch should not fail.
504 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
505 base::Time::FromTimeT(omaha_epoch), &value));
506 EXPECT_GE(value, 0);
507
508 // Anything before the Omaha epoch should fail. We test it for two points.
509 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
510 base::Time::FromTimeT(omaha_epoch - 1), &value));
511 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
512 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
513
514 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
515 // on Jan 8, 2007 0:00 PST.
516 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
517 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
518 EXPECT_EQ(value, 0);
519 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
520 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
521 EXPECT_EQ(value, 7);
522
523 // Check a couple of more values.
524 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
525 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
526 EXPECT_EQ(value, 7);
527 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
528 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
529 EXPECT_EQ(value, 14);
530 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
531 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
532 EXPECT_EQ(value, 21);
533 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
534 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
535 EXPECT_EQ(value, 28);
536
537 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
538 // where the Omaha InstallDate jumps 7 days. Its unix time is
539 // 1180940400. Notably, this is a point in time where Daylight
540 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
541 //
542 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
543 // ignores DST (as it's hard to implement in a thread-safe way using
544 // glibc, see comments in utils.h) we have to fudge by the DST
545 // offset which is one hour. Conveniently, if the function were
546 // someday modified to be DST aware, this test would have to be
547 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700548 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800549 const time_t fudge = 3600;
550 int value1, value2;
551 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
552 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
553 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
554 base::Time::FromTimeT(dst_time + fudge), &value2));
555 EXPECT_EQ(value1, value2 - 7);
556}
557
David Zeuthen33bae492014-02-25 16:16:18 -0800558TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700559 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800560 FakeClock fake_clock;
561 base::TimeDelta duration;
562 string state_variable_key = "test-prefs";
563 string temp_dir;
564 Prefs fake_prefs;
565
566 EXPECT_TRUE(utils::MakeTempDirectory("DurationPrefs.XXXXXX", &temp_dir));
567 fake_prefs.Init(base::FilePath(temp_dir));
568
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700569 fake_system_state.set_clock(&fake_clock);
570 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800571
572 // Initialize wallclock to 1 sec.
573 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
574
575 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700576 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800577 state_variable_key,
578 &duration));
579
580 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700581 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800582 state_variable_key,
583 &duration));
584 EXPECT_EQ(duration.InSeconds(), 0);
585
586 // We can also call it as many times as we want with it being
587 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700588 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800589 state_variable_key,
590 &duration));
591 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700592 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800593 state_variable_key,
594 &duration));
595 EXPECT_EQ(duration.InSeconds(), 0);
596
597 // Advance the clock one second, then we should get 1 sec on the
598 // next call and 0 sec on the subsequent call.
599 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700600 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800601 state_variable_key,
602 &duration));
603 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700604 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800605 state_variable_key,
606 &duration));
607 EXPECT_EQ(duration.InSeconds(), 0);
608
609 // Advance clock two seconds and we should get 2 sec and then 0 sec.
610 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700611 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800612 state_variable_key,
613 &duration));
614 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700615 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800616 state_variable_key,
617 &duration));
618 EXPECT_EQ(duration.InSeconds(), 0);
619
620 // There's a possibility that the wallclock can go backwards (NTP
621 // adjustments, for example) so check that we properly handle this
622 // case.
623 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700624 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800625 state_variable_key,
626 &duration));
627 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700628 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800629 state_variable_key,
630 &duration));
631 EXPECT_EQ(duration.InSeconds(), 1);
632
633 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
634}
635
636TEST(UtilsTest, MonotonicDurationHelper) {
637 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700638 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800639 FakeClock fake_clock;
640 base::TimeDelta duration;
641
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700642 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800643
644 // Initialize monotonic clock to 1 sec.
645 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
646
647 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700648 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800649 &storage,
650 &duration));
651
652 // Next time, we should get zero since the clock didn't advance.
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 // We can also call it as many times as we want with it being
659 // considered a failure.
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);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700664 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800665 &storage,
666 &duration));
667 EXPECT_EQ(duration.InSeconds(), 0);
668
669 // Advance the clock one second, then we should get 1 sec on the
670 // next call and 0 sec on the subsequent call.
671 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
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(), 1);
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 // Advance clock two seconds and we should get 2 sec and then 0 sec.
682 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
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(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700687 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800688 &storage,
689 &duration));
690 EXPECT_EQ(duration.InSeconds(), 0);
691}
692
David Zeuthenb281f072014-04-02 10:20:19 -0700693TEST(UtilsTest, GetConnectionType) {
694 // Check that expected combinations map to the right value.
695 EXPECT_EQ(metrics::ConnectionType::kUnknown,
696 utils::GetConnectionType(kNetUnknown,
697 NetworkTethering::kUnknown));
698 EXPECT_EQ(metrics::ConnectionType::kEthernet,
699 utils::GetConnectionType(kNetEthernet,
700 NetworkTethering::kUnknown));
701 EXPECT_EQ(metrics::ConnectionType::kWifi,
702 utils::GetConnectionType(kNetWifi,
703 NetworkTethering::kUnknown));
704 EXPECT_EQ(metrics::ConnectionType::kWimax,
705 utils::GetConnectionType(kNetWimax,
706 NetworkTethering::kUnknown));
707 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
708 utils::GetConnectionType(kNetBluetooth,
709 NetworkTethering::kUnknown));
710 EXPECT_EQ(metrics::ConnectionType::kCellular,
711 utils::GetConnectionType(kNetCellular,
712 NetworkTethering::kUnknown));
713 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
714 utils::GetConnectionType(kNetEthernet,
715 NetworkTethering::kConfirmed));
716 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
717 utils::GetConnectionType(kNetWifi,
718 NetworkTethering::kConfirmed));
719
720 // Ensure that we don't report tethered ethernet unless it's confirmed.
721 EXPECT_EQ(metrics::ConnectionType::kEthernet,
722 utils::GetConnectionType(kNetEthernet,
723 NetworkTethering::kNotDetected));
724 EXPECT_EQ(metrics::ConnectionType::kEthernet,
725 utils::GetConnectionType(kNetEthernet,
726 NetworkTethering::kSuspected));
727 EXPECT_EQ(metrics::ConnectionType::kEthernet,
728 utils::GetConnectionType(kNetEthernet,
729 NetworkTethering::kUnknown));
730
731 // Ditto for tethered wifi.
732 EXPECT_EQ(metrics::ConnectionType::kWifi,
733 utils::GetConnectionType(kNetWifi,
734 NetworkTethering::kNotDetected));
735 EXPECT_EQ(metrics::ConnectionType::kWifi,
736 utils::GetConnectionType(kNetWifi,
737 NetworkTethering::kSuspected));
738 EXPECT_EQ(metrics::ConnectionType::kWifi,
739 utils::GetConnectionType(kNetWifi,
740 NetworkTethering::kUnknown));
741}
742
adlr@google.com3defe6a2009-12-04 20:57:17 +0000743} // namespace chromeos_update_engine