blob: 3a4ec6ad822e0b62b736f03fbf33de6166db0236 [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));
99 EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", false));
100 EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", true));
101 EXPECT_EQ("/spaces s/ ok/s / / /",
102 utils::NormalizePath("/spaces s/ ok/s / / /", false));
103 EXPECT_EQ("/spaces s/ ok/s / / ",
104 utils::NormalizePath("/spaces s/ ok/s / / /", true));
105}
106
107TEST(UtilsTest, ReadFileFailure) {
108 vector<char> empty;
109 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
110}
111
Darin Petkov8e447e02013-04-16 16:23:50 +0200112TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700113 base::FilePath file;
114 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +0200115 ScopedPathUnlinker unlinker(file.value());
116 vector<char> data;
117 const size_t kSize = 1024 * 1024;
118 for (size_t i = 0; i < kSize; i++) {
119 data.push_back(i % 255);
120 }
121 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), &data[0], data.size()));
122 vector<char> in_data;
123 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
124 EXPECT_TRUE(in_data.empty());
125 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
126 EXPECT_TRUE(data == in_data);
127 in_data.clear();
128 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
129 EXPECT_TRUE(vector<char>(data.begin() + 10, data.begin() + 10 + 20) ==
130 in_data);
131}
132
adlr@google.com3defe6a2009-12-04 20:57:17 +0000133TEST(UtilsTest, ErrnoNumberAsStringTest) {
134 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
135}
136
137TEST(UtilsTest, StringHasSuffixTest) {
138 EXPECT_TRUE(utils::StringHasSuffix("foo", "foo"));
139 EXPECT_TRUE(utils::StringHasSuffix("foo", "o"));
140 EXPECT_TRUE(utils::StringHasSuffix("", ""));
141 EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc"));
142 EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere"));
143 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh"));
144 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", ""));
145 EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo"));
146 EXPECT_FALSE(utils::StringHasSuffix("", "x"));
147 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg"));
148 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab"));
149}
150
151TEST(UtilsTest, StringHasPrefixTest) {
152 EXPECT_TRUE(utils::StringHasPrefix("foo", "foo"));
153 EXPECT_TRUE(utils::StringHasPrefix("foo", "f"));
154 EXPECT_TRUE(utils::StringHasPrefix("", ""));
155 EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc"));
156 EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl"));
157 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab"));
158 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", ""));
159 EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa"));
160 EXPECT_FALSE(utils::StringHasPrefix("", "x"));
161 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc"));
162 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh"));
163}
164
adlr@google.com3defe6a2009-12-04 20:57:17 +0000165TEST(UtilsTest, RecursiveUnlinkDirTest) {
Gilad Arnoldb4346a22013-07-16 06:44:45 -0700166 string first_dir_name;
167 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-a-XXXXXX",
168 &first_dir_name));
169 ASSERT_EQ(0, Chmod(first_dir_name, 0755));
170 string second_dir_name;
171 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-b-XXXXXX",
172 &second_dir_name));
173 ASSERT_EQ(0, Chmod(second_dir_name, 0755));
174
175 EXPECT_EQ(0, Symlink(string("../") + first_dir_name,
176 second_dir_name + "/link"));
177 EXPECT_EQ(0, System(string("echo hi > ") + second_dir_name + "/file"));
178 EXPECT_EQ(0, Mkdir(second_dir_name + "/dir", 0755));
179 EXPECT_EQ(0, System(string("echo ok > ") + second_dir_name + "/dir/subfile"));
180 EXPECT_TRUE(utils::RecursiveUnlinkDir(second_dir_name));
181 EXPECT_TRUE(utils::FileExists(first_dir_name.c_str()));
182 EXPECT_EQ(0, System(string("rm -rf ") + first_dir_name));
183 EXPECT_FALSE(utils::FileExists(second_dir_name.c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000184 EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist"));
185}
186
Darin Petkov002b2fe2010-11-22 13:53:22 -0800187TEST(UtilsTest, IsSymlinkTest) {
188 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800189 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700190 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800191 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700192 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800193 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
194 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
195 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
196 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
197 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
198 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
199}
200
Alex Deymo7dc4c502014-05-20 20:09:58 -0700201TEST(UtilsTest, IsDirTest) {
202 string temp_dir;
203 EXPECT_TRUE(utils::MakeTempDirectory("isdir-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700204 string temp_file = temp_dir + "/temp-file";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700205 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700206 string temp_symlink = temp_dir + "/temp-symlink";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700207 EXPECT_EQ(0, symlink(temp_dir.c_str(), temp_symlink.c_str()));
208 EXPECT_TRUE(utils::IsDir(temp_dir.c_str()));
209 EXPECT_FALSE(utils::IsDir(temp_file.c_str()));
210 EXPECT_FALSE(utils::IsDir(temp_symlink.c_str()));
211 EXPECT_FALSE(utils::IsDir("/non/existent/path"));
212 ASSERT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
213}
214
adlr@google.com3defe6a2009-12-04 20:57:17 +0000215TEST(UtilsTest, TempFilenameTest) {
216 const string original = "/foo.XXXXXX";
217 const string result = utils::TempFilename(original);
218 EXPECT_EQ(original.size(), result.size());
219 EXPECT_TRUE(utils::StringHasPrefix(result, "/foo."));
220 EXPECT_FALSE(utils::StringHasSuffix(result, "XXXXXX"));
221}
222
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800223TEST(UtilsTest, GetDiskNameTest) {
224 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700225 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800226 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
227 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
228 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700229 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800230 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
231 EXPECT_EQ("", utils::GetDiskName("/"));
232 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700233}
234
235TEST(UtilsTest, SysfsBlockDeviceTest) {
236 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
237 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
238 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
239 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
240 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
241 EXPECT_EQ("", utils::SysfsBlockDevice(""));
242}
243
244TEST(UtilsTest, IsRemovableDeviceTest) {
245 EXPECT_FALSE(utils::IsRemovableDevice(""));
246 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700247}
248
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800249TEST(UtilsTest, GetPartitionNumberTest) {
250 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700251 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800252 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
253 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
254 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700255 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800256 EXPECT_EQ(0, utils::GetPartitionNumber(""));
257 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
258 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
259 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700260 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
261 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
262 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
263 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700264}
265
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700266TEST(UtilsTest, MakePartitionNameTest) {
267 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
268 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
269 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
270 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
271 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
272 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
273 EXPECT_EQ("/dev/ubiblock3_0", utils::MakePartitionName("/dev/ubiblock", 3));
274}
275
276
Chris Sosa4f8ee272012-11-30 13:01:54 -0800277TEST(UtilsTest, CompareCpuSharesTest) {
278 EXPECT_LT(utils::CompareCpuShares(utils::kCpuSharesLow,
279 utils::kCpuSharesNormal), 0);
280 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesNormal,
281 utils::kCpuSharesLow), 0);
282 EXPECT_EQ(utils::CompareCpuShares(utils::kCpuSharesNormal,
283 utils::kCpuSharesNormal), 0);
284 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesHigh,
285 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700286}
287
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700288TEST(UtilsTest, FuzzIntTest) {
289 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
290 for (size_t r = 0; r < arraysize(kRanges); ++r) {
291 unsigned int range = kRanges[r];
292 const int kValue = 50;
293 for (int tries = 0; tries < 100; ++tries) {
294 int value = utils::FuzzInt(kValue, range);
295 EXPECT_GE(value, kValue - range / 2);
296 EXPECT_LE(value, kValue + range - range / 2);
297 }
298 }
299}
300
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700301TEST(UtilsTest, ApplyMapTest) {
302 int initial_values[] = {1, 2, 3, 4, 6};
303 vector<int> collection(&initial_values[0],
304 initial_values + arraysize(initial_values));
305 EXPECT_EQ(arraysize(initial_values), collection.size());
306 int expected_values[] = {1, 2, 5, 4, 8};
307 map<int, int> value_map;
308 value_map[3] = 5;
309 value_map[6] = 8;
310 value_map[5] = 10;
311
312 utils::ApplyMap(&collection, value_map);
313
314 size_t index = 0;
315 for (vector<int>::iterator it = collection.begin(), e = collection.end();
316 it != e; ++it) {
317 EXPECT_EQ(expected_values[index++], *it);
318 }
319}
320
Darin Petkovd3f8c892010-10-12 21:38:45 -0700321TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
322 string img;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800323 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, NULL));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700324 ScopedPathUnlinker img_unlinker(img);
325 CreateExtImageAtPath(img, NULL);
326 // Extend the "partition" holding the file system from 10MiB to 20MiB.
327 EXPECT_EQ(0, System(base::StringPrintf(
328 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
329 img.c_str())));
330 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
331 int block_count = 0;
332 int block_size = 0;
333 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
334 EXPECT_EQ(4096, block_size);
335 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
336}
337
Chris Sosad317e402013-06-12 13:47:09 -0700338TEST(UtilsTest, GetInstallDevTest) {
339 string boot_dev = "/dev/sda5";
340 string install_dev;
341 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
342 EXPECT_EQ(install_dev, "/dev/sda3");
343
344 boot_dev = "/dev/sda3";
345 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
346 EXPECT_EQ(install_dev, "/dev/sda5");
347
348 boot_dev = "/dev/sda12";
349 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700350
351 boot_dev = "/dev/ubiblock3_0";
352 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
353 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
354
355 boot_dev = "/dev/ubiblock5_0";
356 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
357 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
358
359 boot_dev = "/dev/ubiblock12_0";
360 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700361}
362
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800363namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700364void GetFileFormatTester(const string& expected,
365 const vector<uint8>& contents) {
366 ScopedTempFile file;
367 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
368 reinterpret_cast<const char*>(contents.data()),
369 contents.size()));
370 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
371}
372}
373
374TEST(UtilsTest, GetFileFormatTest) {
375 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
376 GetFileFormatTester("data", vector<uint8>{1, 2, 3, 4, 5, 6, 7, 8});
377 GetFileFormatTester("ELF", vector<uint8>{0x7f, 0x45, 0x4c, 0x46});
378
379 // Real tests from cros_installer on different boards.
380 // ELF 32-bit LSB executable, Intel 80386
381 GetFileFormatTester(
382 "ELF 32-bit little-endian x86",
383 vector<uint8>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
384 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
385 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
386 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
387
388 // ELF 32-bit LSB executable, ARM
389 GetFileFormatTester(
390 "ELF 32-bit little-endian arm",
391 vector<uint8>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
392 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
393 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
394 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
395
396 // ELF 64-bit LSB executable, x86-64
397 GetFileFormatTester(
398 "ELF 64-bit little-endian x86-64",
399 vector<uint8>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
400 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
401 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
402 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
403}
404
405namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800406gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
407 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
408 g_main_loop_quit(loop);
409 return FALSE; // Don't call this callback again
410}
411} // namespace {}
412
413TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
414 // Not much to test. At least this tests for memory leaks, crashes,
415 // log errors.
416 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
417 utils::ScheduleCrashReporterUpload();
418 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
419 g_main_loop_run(loop);
420 g_main_loop_unref(loop);
421}
422
David Zeuthen674c3182013-04-18 14:05:20 -0700423TEST(UtilsTest, FormatTimeDeltaTest) {
424 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
425 // which is not localized) so we only need to test the C locale
426 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
427 "0.1s");
428 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
429 "0s");
430 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
431 "1s");
432 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
433 "59s");
434 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
435 "1m0s");
436 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
437 "1m1s");
438 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
439 "1m30s");
440 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
441 "20m5s");
442 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
443 "1h0m0s");
444 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
445 "1h0m1s");
446 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
447 "1h1m1s");
448 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
449 "2h1m1s");
450 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
451 "1d0h0m0s");
452 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
453 "1d0h0m1s");
454 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
455 "2d7h33m20s");
456 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
457 base::TimeDelta::FromMilliseconds(1)),
458 "2d7h33m20.001s");
459}
460
David Zeuthen27a48bc2013-08-06 12:06:29 -0700461TEST(UtilsTest, TimeFromStructTimespecTest) {
462 struct timespec ts;
463
464 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
465 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
466 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
467
468 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
469 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
470 .tv_nsec = 42 * 1000 * 1000};
471 base::Time::Exploded exploded = (base::Time::Exploded) {
472 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
473 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
474 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
475 utils::TimeFromStructTimespec(&ts));
476}
477
David Zeuthene7f89172013-10-31 10:21:04 -0700478TEST(UtilsTest, DecodeAndStoreBase64String) {
479 base::FilePath path;
480
481 // Ensure we return false on empty strings or invalid base64.
482 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
483 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
484
485 // Pass known base64 and check that it matches. This string was generated
486 // the following way:
487 //
488 // $ echo "Update Engine" | base64
489 // VXBkYXRlIEVuZ2luZQo=
490 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
491 &path));
492 ScopedPathUnlinker unlinker(path.value());
493 string expected_contents = "Update Engine\n";
494 string contents;
495 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
496 EXPECT_EQ(contents, expected_contents);
497 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
498}
499
David Zeuthen639aa362014-02-03 16:23:44 -0800500TEST(UtilsTest, ConvertToOmahaInstallDate) {
501 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
502 // Monday. In Unix time, this point in time is easily obtained via
503 // the date(1) command like this:
504 //
505 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
506 const time_t omaha_epoch = 1167638400;
507 int value;
508
509 // Points in time *on and after* the Omaha epoch should not fail.
510 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
511 base::Time::FromTimeT(omaha_epoch), &value));
512 EXPECT_GE(value, 0);
513
514 // Anything before the Omaha epoch should fail. We test it for two points.
515 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
516 base::Time::FromTimeT(omaha_epoch - 1), &value));
517 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
518 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
519
520 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
521 // on Jan 8, 2007 0:00 PST.
522 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
523 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
524 EXPECT_EQ(value, 0);
525 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
526 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
527 EXPECT_EQ(value, 7);
528
529 // Check a couple of more values.
530 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
531 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
532 EXPECT_EQ(value, 7);
533 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
534 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
535 EXPECT_EQ(value, 14);
536 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
537 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
538 EXPECT_EQ(value, 21);
539 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
540 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
541 EXPECT_EQ(value, 28);
542
543 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
544 // where the Omaha InstallDate jumps 7 days. Its unix time is
545 // 1180940400. Notably, this is a point in time where Daylight
546 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
547 //
548 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
549 // ignores DST (as it's hard to implement in a thread-safe way using
550 // glibc, see comments in utils.h) we have to fudge by the DST
551 // offset which is one hour. Conveniently, if the function were
552 // someday modified to be DST aware, this test would have to be
553 // modified as well.
554 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
555 const time_t fudge = 3600;
556 int value1, value2;
557 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
558 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
559 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
560 base::Time::FromTimeT(dst_time + fudge), &value2));
561 EXPECT_EQ(value1, value2 - 7);
562}
563
David Zeuthen33bae492014-02-25 16:16:18 -0800564TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700565 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800566 FakeClock fake_clock;
567 base::TimeDelta duration;
568 string state_variable_key = "test-prefs";
569 string temp_dir;
570 Prefs fake_prefs;
571
572 EXPECT_TRUE(utils::MakeTempDirectory("DurationPrefs.XXXXXX", &temp_dir));
573 fake_prefs.Init(base::FilePath(temp_dir));
574
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700575 fake_system_state.set_clock(&fake_clock);
576 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800577
578 // Initialize wallclock to 1 sec.
579 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
580
581 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700582 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800583 state_variable_key,
584 &duration));
585
586 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700587 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800588 state_variable_key,
589 &duration));
590 EXPECT_EQ(duration.InSeconds(), 0);
591
592 // We can also call it as many times as we want with it being
593 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700594 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800595 state_variable_key,
596 &duration));
597 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700598 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800599 state_variable_key,
600 &duration));
601 EXPECT_EQ(duration.InSeconds(), 0);
602
603 // Advance the clock one second, then we should get 1 sec on the
604 // next call and 0 sec on the subsequent call.
605 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
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(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700610 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800611 state_variable_key,
612 &duration));
613 EXPECT_EQ(duration.InSeconds(), 0);
614
615 // Advance clock two seconds and we should get 2 sec and then 0 sec.
616 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700617 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800618 state_variable_key,
619 &duration));
620 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700621 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800622 state_variable_key,
623 &duration));
624 EXPECT_EQ(duration.InSeconds(), 0);
625
626 // There's a possibility that the wallclock can go backwards (NTP
627 // adjustments, for example) so check that we properly handle this
628 // case.
629 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700630 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800631 state_variable_key,
632 &duration));
633 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700634 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800635 state_variable_key,
636 &duration));
637 EXPECT_EQ(duration.InSeconds(), 1);
638
639 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
640}
641
642TEST(UtilsTest, MonotonicDurationHelper) {
643 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700644 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800645 FakeClock fake_clock;
646 base::TimeDelta duration;
647
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700648 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800649
650 // Initialize monotonic clock to 1 sec.
651 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
652
653 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700654 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800655 &storage,
656 &duration));
657
658 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700659 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800660 &storage,
661 &duration));
662 EXPECT_EQ(duration.InSeconds(), 0);
663
664 // We can also call it as many times as we want with it being
665 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700666 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800667 &storage,
668 &duration));
669 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700670 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800671 &storage,
672 &duration));
673 EXPECT_EQ(duration.InSeconds(), 0);
674
675 // Advance the clock one second, then we should get 1 sec on the
676 // next call and 0 sec on the subsequent call.
677 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700678 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800679 &storage,
680 &duration));
681 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700682 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800683 &storage,
684 &duration));
685 EXPECT_EQ(duration.InSeconds(), 0);
686
687 // Advance clock two seconds and we should get 2 sec and then 0 sec.
688 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700689 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800690 &storage,
691 &duration));
692 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700693 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800694 &storage,
695 &duration));
696 EXPECT_EQ(duration.InSeconds(), 0);
697}
698
David Zeuthenb281f072014-04-02 10:20:19 -0700699TEST(UtilsTest, GetConnectionType) {
700 // Check that expected combinations map to the right value.
701 EXPECT_EQ(metrics::ConnectionType::kUnknown,
702 utils::GetConnectionType(kNetUnknown,
703 NetworkTethering::kUnknown));
704 EXPECT_EQ(metrics::ConnectionType::kEthernet,
705 utils::GetConnectionType(kNetEthernet,
706 NetworkTethering::kUnknown));
707 EXPECT_EQ(metrics::ConnectionType::kWifi,
708 utils::GetConnectionType(kNetWifi,
709 NetworkTethering::kUnknown));
710 EXPECT_EQ(metrics::ConnectionType::kWimax,
711 utils::GetConnectionType(kNetWimax,
712 NetworkTethering::kUnknown));
713 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
714 utils::GetConnectionType(kNetBluetooth,
715 NetworkTethering::kUnknown));
716 EXPECT_EQ(metrics::ConnectionType::kCellular,
717 utils::GetConnectionType(kNetCellular,
718 NetworkTethering::kUnknown));
719 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
720 utils::GetConnectionType(kNetEthernet,
721 NetworkTethering::kConfirmed));
722 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
723 utils::GetConnectionType(kNetWifi,
724 NetworkTethering::kConfirmed));
725
726 // Ensure that we don't report tethered ethernet unless it's confirmed.
727 EXPECT_EQ(metrics::ConnectionType::kEthernet,
728 utils::GetConnectionType(kNetEthernet,
729 NetworkTethering::kNotDetected));
730 EXPECT_EQ(metrics::ConnectionType::kEthernet,
731 utils::GetConnectionType(kNetEthernet,
732 NetworkTethering::kSuspected));
733 EXPECT_EQ(metrics::ConnectionType::kEthernet,
734 utils::GetConnectionType(kNetEthernet,
735 NetworkTethering::kUnknown));
736
737 // Ditto for tethered wifi.
738 EXPECT_EQ(metrics::ConnectionType::kWifi,
739 utils::GetConnectionType(kNetWifi,
740 NetworkTethering::kNotDetected));
741 EXPECT_EQ(metrics::ConnectionType::kWifi,
742 utils::GetConnectionType(kNetWifi,
743 NetworkTethering::kSuspected));
744 EXPECT_EQ(metrics::ConnectionType::kWifi,
745 utils::GetConnectionType(kNetWifi,
746 NetworkTethering::kUnknown));
747}
748
adlr@google.com3defe6a2009-12-04 20:57:17 +0000749} // namespace chromeos_update_engine