blob: 44d1b870fe5130ed4d062eec22bb503cbb58ca22 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo2c0db7b2014-11-04 12:23:39 -08005#include "update_engine/utils.h"
6
Alex Vakulenko44cab302014-07-23 13:12:15 -07007#include <errno.h>
Ben Chan9abb7632014-08-07 00:10:53 -07008#include <stdint.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <sys/stat.h>
10#include <sys/types.h>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070011
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070012#include <map>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080013#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include <vector>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070015
Alex Deymoc1711e22014-08-08 13:16:23 -070016#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070018#include <base/strings/string_util.h>
19#include <base/strings/stringprintf.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070020#include <gtest/gtest.h>
21
David Zeuthen33bae492014-02-25 16:16:18 -080022#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080023#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070024#include "update_engine/fake_system_state.h"
David Zeuthen33bae492014-02-25 16:16:18 -080025#include "update_engine/prefs.h"
Darin Petkovd3f8c892010-10-12 21:38:45 -070026#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000027
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070028using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080029using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000030using std::vector;
31
32namespace chromeos_update_engine {
33
34class UtilsTest : public ::testing::Test { };
35
Chris Sosac1972482013-04-30 22:31:10 -070036TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070037 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070038 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
39 EXPECT_EQ("123456", utils::ParseECVersion(
40 "b=1231a fw_version=123456 a=fasd2"));
41 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
42 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070043 "vendor=\"sam\" fw_version=\"00VFA616\""));
44
45 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070046 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070047}
48
J. Richard Barnette30842932013-10-28 15:04:23 -070049
50TEST(UtilsTest, KernelDeviceOfBootDevice) {
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -070051 EXPECT_EQ("", utils::KernelDeviceOfBootDevice(""));
J. Richard Barnette30842932013-10-28 15:04:23 -070052 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo"));
53 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0"));
54 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1"));
55 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2"));
56 EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3"));
57 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4"));
58 EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5"));
59 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6"));
60 EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7"));
61 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8"));
62 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9"));
63
64 EXPECT_EQ("/dev/mmcblk0p2",
65 utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3"));
66 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4"));
67
68 EXPECT_EQ("/dev/ubi2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
69 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
70
71 EXPECT_EQ("/dev/mtdblock2",
72 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
73 EXPECT_EQ("/dev/mtdblock4",
74 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
75 EXPECT_EQ("/dev/mtdblock6",
76 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
77 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
78}
79
80
adlr@google.com3defe6a2009-12-04 20:57:17 +000081TEST(UtilsTest, NormalizePathTest) {
82 EXPECT_EQ("", utils::NormalizePath("", false));
83 EXPECT_EQ("", utils::NormalizePath("", true));
84 EXPECT_EQ("/", utils::NormalizePath("/", false));
85 EXPECT_EQ("", utils::NormalizePath("/", true));
86 EXPECT_EQ("/", utils::NormalizePath("//", false));
87 EXPECT_EQ("", utils::NormalizePath("//", true));
88 EXPECT_EQ("foo", utils::NormalizePath("foo", false));
89 EXPECT_EQ("foo", utils::NormalizePath("foo", true));
90 EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false));
91 EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true));
92 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
93 false));
94 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
95 true));
96 EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/",
97 false));
98 EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/",
99 true));
100 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false));
101 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700102 EXPECT_EQ("\\:/;$PATH\n\\",
103 utils::NormalizePath("\\://;$PATH\n\\", false));
104 EXPECT_EQ("\\:/;$PATH\n\\",
105 utils::NormalizePath("\\://;$PATH\n\\", true));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000106 EXPECT_EQ("/spaces s/ ok/s / / /",
107 utils::NormalizePath("/spaces s/ ok/s / / /", false));
108 EXPECT_EQ("/spaces s/ ok/s / / ",
109 utils::NormalizePath("/spaces s/ ok/s / / /", true));
110}
111
112TEST(UtilsTest, ReadFileFailure) {
113 vector<char> empty;
114 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
115}
116
Darin Petkov8e447e02013-04-16 16:23:50 +0200117TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700118 base::FilePath file;
119 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +0200120 ScopedPathUnlinker unlinker(file.value());
121 vector<char> data;
122 const size_t kSize = 1024 * 1024;
123 for (size_t i = 0; i < kSize; i++) {
124 data.push_back(i % 255);
125 }
126 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), &data[0], data.size()));
127 vector<char> in_data;
128 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
129 EXPECT_TRUE(in_data.empty());
130 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
131 EXPECT_TRUE(data == in_data);
132 in_data.clear();
133 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
134 EXPECT_TRUE(vector<char>(data.begin() + 10, data.begin() + 10 + 20) ==
135 in_data);
136}
137
adlr@google.com3defe6a2009-12-04 20:57:17 +0000138TEST(UtilsTest, ErrnoNumberAsStringTest) {
139 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
140}
141
142TEST(UtilsTest, StringHasSuffixTest) {
143 EXPECT_TRUE(utils::StringHasSuffix("foo", "foo"));
144 EXPECT_TRUE(utils::StringHasSuffix("foo", "o"));
145 EXPECT_TRUE(utils::StringHasSuffix("", ""));
146 EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc"));
147 EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere"));
148 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh"));
149 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", ""));
150 EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo"));
151 EXPECT_FALSE(utils::StringHasSuffix("", "x"));
152 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg"));
153 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab"));
154}
155
156TEST(UtilsTest, StringHasPrefixTest) {
157 EXPECT_TRUE(utils::StringHasPrefix("foo", "foo"));
158 EXPECT_TRUE(utils::StringHasPrefix("foo", "f"));
159 EXPECT_TRUE(utils::StringHasPrefix("", ""));
160 EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc"));
161 EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl"));
162 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab"));
163 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", ""));
164 EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa"));
165 EXPECT_FALSE(utils::StringHasPrefix("", "x"));
166 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc"));
167 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh"));
168}
169
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170TEST(UtilsTest, RecursiveUnlinkDirTest) {
Gilad Arnoldb4346a22013-07-16 06:44:45 -0700171 string first_dir_name;
172 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-a-XXXXXX",
173 &first_dir_name));
174 ASSERT_EQ(0, Chmod(first_dir_name, 0755));
175 string second_dir_name;
176 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-b-XXXXXX",
177 &second_dir_name));
178 ASSERT_EQ(0, Chmod(second_dir_name, 0755));
179
180 EXPECT_EQ(0, Symlink(string("../") + first_dir_name,
181 second_dir_name + "/link"));
182 EXPECT_EQ(0, System(string("echo hi > ") + second_dir_name + "/file"));
183 EXPECT_EQ(0, Mkdir(second_dir_name + "/dir", 0755));
184 EXPECT_EQ(0, System(string("echo ok > ") + second_dir_name + "/dir/subfile"));
185 EXPECT_TRUE(utils::RecursiveUnlinkDir(second_dir_name));
186 EXPECT_TRUE(utils::FileExists(first_dir_name.c_str()));
187 EXPECT_EQ(0, System(string("rm -rf ") + first_dir_name));
188 EXPECT_FALSE(utils::FileExists(second_dir_name.c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000189 EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist"));
190}
191
Darin Petkov002b2fe2010-11-22 13:53:22 -0800192TEST(UtilsTest, IsSymlinkTest) {
193 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800194 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700195 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800196 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700197 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800198 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
199 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
200 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
201 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
202 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
203 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
204}
205
Alex Deymo7dc4c502014-05-20 20:09:58 -0700206TEST(UtilsTest, IsDirTest) {
207 string temp_dir;
208 EXPECT_TRUE(utils::MakeTempDirectory("isdir-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -0700209 string temp_file = temp_dir + "/temp-file";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700210 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700211 string temp_symlink = temp_dir + "/temp-symlink";
Alex Deymo7dc4c502014-05-20 20:09:58 -0700212 EXPECT_EQ(0, symlink(temp_dir.c_str(), temp_symlink.c_str()));
213 EXPECT_TRUE(utils::IsDir(temp_dir.c_str()));
214 EXPECT_FALSE(utils::IsDir(temp_file.c_str()));
215 EXPECT_FALSE(utils::IsDir(temp_symlink.c_str()));
216 EXPECT_FALSE(utils::IsDir("/non/existent/path"));
217 ASSERT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
218}
219
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800220TEST(UtilsTest, GetDiskNameTest) {
221 EXPECT_EQ("/dev/sda", utils::GetDiskName("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700222 EXPECT_EQ("/dev/sdp", utils::GetDiskName("/dev/sdp1234"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800223 EXPECT_EQ("/dev/mmcblk0", utils::GetDiskName("/dev/mmcblk0p3"));
224 EXPECT_EQ("", utils::GetDiskName("/dev/mmcblk0p"));
225 EXPECT_EQ("", utils::GetDiskName("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700226 EXPECT_EQ("/dev/ubiblock", utils::GetDiskName("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800227 EXPECT_EQ("", utils::GetDiskName("/dev/foo/bar"));
228 EXPECT_EQ("", utils::GetDiskName("/"));
229 EXPECT_EQ("", utils::GetDiskName(""));
Darin Petkovf74eb652010-08-04 12:08:38 -0700230}
231
232TEST(UtilsTest, SysfsBlockDeviceTest) {
233 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
234 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
235 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
236 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
237 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
238 EXPECT_EQ("", utils::SysfsBlockDevice(""));
239}
240
241TEST(UtilsTest, IsRemovableDeviceTest) {
242 EXPECT_FALSE(utils::IsRemovableDevice(""));
243 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700244}
245
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800246TEST(UtilsTest, GetPartitionNumberTest) {
247 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sda3"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700248 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/sdz3"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800249 EXPECT_EQ(123, utils::GetPartitionNumber("/dev/sda123"));
250 EXPECT_EQ(2, utils::GetPartitionNumber("/dev/mmcblk0p2"));
251 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/mmcblk0p"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700252 EXPECT_EQ(3, utils::GetPartitionNumber("/dev/ubiblock3_2"));
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800253 EXPECT_EQ(0, utils::GetPartitionNumber(""));
254 EXPECT_EQ(0, utils::GetPartitionNumber("/"));
255 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/"));
256 EXPECT_EQ(0, utils::GetPartitionNumber("/dev/sda"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700257 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10"));
258 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11"));
259 EXPECT_EQ(10, utils::GetPartitionNumber("/dev/loop10_0"));
260 EXPECT_EQ(11, utils::GetPartitionNumber("/dev/loop28p11_0"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700261}
262
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700263TEST(UtilsTest, MakePartitionNameTest) {
264 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
265 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
266 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
267 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
268 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
269 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
270 EXPECT_EQ("/dev/ubiblock3_0", utils::MakePartitionName("/dev/ubiblock", 3));
271}
272
273
Chris Sosa4f8ee272012-11-30 13:01:54 -0800274TEST(UtilsTest, CompareCpuSharesTest) {
275 EXPECT_LT(utils::CompareCpuShares(utils::kCpuSharesLow,
276 utils::kCpuSharesNormal), 0);
277 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesNormal,
278 utils::kCpuSharesLow), 0);
279 EXPECT_EQ(utils::CompareCpuShares(utils::kCpuSharesNormal,
280 utils::kCpuSharesNormal), 0);
281 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesHigh,
282 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700283}
284
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700285TEST(UtilsTest, FuzzIntTest) {
286 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
287 for (size_t r = 0; r < arraysize(kRanges); ++r) {
288 unsigned int range = kRanges[r];
289 const int kValue = 50;
290 for (int tries = 0; tries < 100; ++tries) {
291 int value = utils::FuzzInt(kValue, range);
292 EXPECT_GE(value, kValue - range / 2);
293 EXPECT_LE(value, kValue + range - range / 2);
294 }
295 }
296}
297
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700298TEST(UtilsTest, ApplyMapTest) {
299 int initial_values[] = {1, 2, 3, 4, 6};
300 vector<int> collection(&initial_values[0],
301 initial_values + arraysize(initial_values));
302 EXPECT_EQ(arraysize(initial_values), collection.size());
303 int expected_values[] = {1, 2, 5, 4, 8};
304 map<int, int> value_map;
305 value_map[3] = 5;
306 value_map[6] = 8;
307 value_map[5] = 10;
308
309 utils::ApplyMap(&collection, value_map);
310
311 size_t index = 0;
312 for (vector<int>::iterator it = collection.begin(), e = collection.end();
313 it != e; ++it) {
314 EXPECT_EQ(expected_values[index++], *it);
315 }
316}
317
Darin Petkovd3f8c892010-10-12 21:38:45 -0700318TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
319 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700320 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700321 ScopedPathUnlinker img_unlinker(img);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700322 CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700323 // Extend the "partition" holding the file system from 10MiB to 20MiB.
324 EXPECT_EQ(0, System(base::StringPrintf(
325 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
326 img.c_str())));
327 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
328 int block_count = 0;
329 int block_size = 0;
330 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
331 EXPECT_EQ(4096, block_size);
332 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
333}
334
Chris Sosad317e402013-06-12 13:47:09 -0700335TEST(UtilsTest, GetInstallDevTest) {
336 string boot_dev = "/dev/sda5";
337 string install_dev;
338 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
339 EXPECT_EQ(install_dev, "/dev/sda3");
340
341 boot_dev = "/dev/sda3";
342 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
343 EXPECT_EQ(install_dev, "/dev/sda5");
344
345 boot_dev = "/dev/sda12";
346 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700347
348 boot_dev = "/dev/ubiblock3_0";
349 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
350 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
351
352 boot_dev = "/dev/ubiblock5_0";
353 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
354 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
355
356 boot_dev = "/dev/ubiblock12_0";
357 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700358}
359
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800360namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700361void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700362 const vector<uint8_t>& contents) {
Alex Deymo032e7722014-03-25 17:53:56 -0700363 ScopedTempFile file;
364 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
365 reinterpret_cast<const char*>(contents.data()),
366 contents.size()));
367 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
368}
369}
370
371TEST(UtilsTest, GetFileFormatTest) {
372 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700373 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
374 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700375
376 // Real tests from cros_installer on different boards.
377 // ELF 32-bit LSB executable, Intel 80386
378 GetFileFormatTester(
379 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700380 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
381 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
383 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700384
Alex Deymoc1711e22014-08-08 13:16:23 -0700385 // ELF 32-bit LSB executable, MIPS
386 GetFileFormatTester(
387 "ELF 32-bit little-endian mips",
388 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
389 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
390 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
391 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
392
Alex Deymo032e7722014-03-25 17:53:56 -0700393 // ELF 32-bit LSB executable, ARM
394 GetFileFormatTester(
395 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700396 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
397 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
398 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
399 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700400
401 // ELF 64-bit LSB executable, x86-64
402 GetFileFormatTester(
403 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700404 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
407 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700408}
409
410namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800411gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
412 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
413 g_main_loop_quit(loop);
414 return FALSE; // Don't call this callback again
415}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700416} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800417
418TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
419 // Not much to test. At least this tests for memory leaks, crashes,
420 // log errors.
421 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
422 utils::ScheduleCrashReporterUpload();
423 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
424 g_main_loop_run(loop);
425 g_main_loop_unref(loop);
426}
427
David Zeuthen674c3182013-04-18 14:05:20 -0700428TEST(UtilsTest, FormatTimeDeltaTest) {
429 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
430 // which is not localized) so we only need to test the C locale
431 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
432 "0.1s");
433 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
434 "0s");
435 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
436 "1s");
437 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
438 "59s");
439 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
440 "1m0s");
441 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
442 "1m1s");
443 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
444 "1m30s");
445 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
446 "20m5s");
447 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
448 "1h0m0s");
449 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
450 "1h0m1s");
451 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
452 "1h1m1s");
453 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
454 "2h1m1s");
455 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
456 "1d0h0m0s");
457 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
458 "1d0h0m1s");
459 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
460 "2d7h33m20s");
461 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
462 base::TimeDelta::FromMilliseconds(1)),
463 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400464 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
465 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700466}
467
David Zeuthen27a48bc2013-08-06 12:06:29 -0700468TEST(UtilsTest, TimeFromStructTimespecTest) {
469 struct timespec ts;
470
471 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
472 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
473 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
474
475 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
476 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
477 .tv_nsec = 42 * 1000 * 1000};
478 base::Time::Exploded exploded = (base::Time::Exploded) {
479 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
480 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
481 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
482 utils::TimeFromStructTimespec(&ts));
483}
484
David Zeuthene7f89172013-10-31 10:21:04 -0700485TEST(UtilsTest, DecodeAndStoreBase64String) {
486 base::FilePath path;
487
488 // Ensure we return false on empty strings or invalid base64.
489 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
490 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
491
492 // Pass known base64 and check that it matches. This string was generated
493 // the following way:
494 //
495 // $ echo "Update Engine" | base64
496 // VXBkYXRlIEVuZ2luZQo=
497 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
498 &path));
499 ScopedPathUnlinker unlinker(path.value());
500 string expected_contents = "Update Engine\n";
501 string contents;
502 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
503 EXPECT_EQ(contents, expected_contents);
504 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
505}
506
David Zeuthen639aa362014-02-03 16:23:44 -0800507TEST(UtilsTest, ConvertToOmahaInstallDate) {
508 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
509 // Monday. In Unix time, this point in time is easily obtained via
510 // the date(1) command like this:
511 //
512 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
513 const time_t omaha_epoch = 1167638400;
514 int value;
515
516 // Points in time *on and after* the Omaha epoch should not fail.
517 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
518 base::Time::FromTimeT(omaha_epoch), &value));
519 EXPECT_GE(value, 0);
520
521 // Anything before the Omaha epoch should fail. We test it for two points.
522 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
523 base::Time::FromTimeT(omaha_epoch - 1), &value));
524 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
525 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
526
527 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
528 // on Jan 8, 2007 0:00 PST.
529 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
530 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
531 EXPECT_EQ(value, 0);
532 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
533 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
534 EXPECT_EQ(value, 7);
535
536 // Check a couple of more values.
537 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
538 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
539 EXPECT_EQ(value, 7);
540 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
541 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
542 EXPECT_EQ(value, 14);
543 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
544 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
545 EXPECT_EQ(value, 21);
546 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
547 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
548 EXPECT_EQ(value, 28);
549
550 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
551 // where the Omaha InstallDate jumps 7 days. Its unix time is
552 // 1180940400. Notably, this is a point in time where Daylight
553 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
554 //
555 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
556 // ignores DST (as it's hard to implement in a thread-safe way using
557 // glibc, see comments in utils.h) we have to fudge by the DST
558 // offset which is one hour. Conveniently, if the function were
559 // someday modified to be DST aware, this test would have to be
560 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700561 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800562 const time_t fudge = 3600;
563 int value1, value2;
564 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
565 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
566 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
567 base::Time::FromTimeT(dst_time + fudge), &value2));
568 EXPECT_EQ(value1, value2 - 7);
569}
570
David Zeuthen33bae492014-02-25 16:16:18 -0800571TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700572 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800573 FakeClock fake_clock;
574 base::TimeDelta duration;
575 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800576 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800577
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700578 fake_system_state.set_clock(&fake_clock);
579 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800580
581 // Initialize wallclock to 1 sec.
582 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
583
584 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700585 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800586 state_variable_key,
587 &duration));
588
589 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700590 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800591 state_variable_key,
592 &duration));
593 EXPECT_EQ(duration.InSeconds(), 0);
594
595 // We can also call it as many times as we want with it being
596 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700597 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800598 state_variable_key,
599 &duration));
600 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700601 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800602 state_variable_key,
603 &duration));
604 EXPECT_EQ(duration.InSeconds(), 0);
605
606 // Advance the clock one second, then we should get 1 sec on the
607 // next call and 0 sec on the subsequent call.
608 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700609 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800610 state_variable_key,
611 &duration));
612 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700613 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800614 state_variable_key,
615 &duration));
616 EXPECT_EQ(duration.InSeconds(), 0);
617
618 // Advance clock two seconds and we should get 2 sec and then 0 sec.
619 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700620 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800621 state_variable_key,
622 &duration));
623 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700624 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800625 state_variable_key,
626 &duration));
627 EXPECT_EQ(duration.InSeconds(), 0);
628
629 // There's a possibility that the wallclock can go backwards (NTP
630 // adjustments, for example) so check that we properly handle this
631 // case.
632 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700633 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800634 state_variable_key,
635 &duration));
636 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700637 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800638 state_variable_key,
639 &duration));
640 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800641}
642
643TEST(UtilsTest, MonotonicDurationHelper) {
644 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700645 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800646 FakeClock fake_clock;
647 base::TimeDelta duration;
648
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700649 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800650
651 // Initialize monotonic clock to 1 sec.
652 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
653
654 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700655 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800656 &storage,
657 &duration));
658
659 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700660 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800661 &storage,
662 &duration));
663 EXPECT_EQ(duration.InSeconds(), 0);
664
665 // We can also call it as many times as we want with it being
666 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700667 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800668 &storage,
669 &duration));
670 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700671 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800672 &storage,
673 &duration));
674 EXPECT_EQ(duration.InSeconds(), 0);
675
676 // Advance the clock one second, then we should get 1 sec on the
677 // next call and 0 sec on the subsequent call.
678 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700679 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800680 &storage,
681 &duration));
682 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700683 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800684 &storage,
685 &duration));
686 EXPECT_EQ(duration.InSeconds(), 0);
687
688 // Advance clock two seconds and we should get 2 sec and then 0 sec.
689 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700690 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800691 &storage,
692 &duration));
693 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700694 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800695 &storage,
696 &duration));
697 EXPECT_EQ(duration.InSeconds(), 0);
698}
699
David Zeuthenb281f072014-04-02 10:20:19 -0700700TEST(UtilsTest, GetConnectionType) {
701 // Check that expected combinations map to the right value.
702 EXPECT_EQ(metrics::ConnectionType::kUnknown,
703 utils::GetConnectionType(kNetUnknown,
704 NetworkTethering::kUnknown));
705 EXPECT_EQ(metrics::ConnectionType::kEthernet,
706 utils::GetConnectionType(kNetEthernet,
707 NetworkTethering::kUnknown));
708 EXPECT_EQ(metrics::ConnectionType::kWifi,
709 utils::GetConnectionType(kNetWifi,
710 NetworkTethering::kUnknown));
711 EXPECT_EQ(metrics::ConnectionType::kWimax,
712 utils::GetConnectionType(kNetWimax,
713 NetworkTethering::kUnknown));
714 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
715 utils::GetConnectionType(kNetBluetooth,
716 NetworkTethering::kUnknown));
717 EXPECT_EQ(metrics::ConnectionType::kCellular,
718 utils::GetConnectionType(kNetCellular,
719 NetworkTethering::kUnknown));
720 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
721 utils::GetConnectionType(kNetEthernet,
722 NetworkTethering::kConfirmed));
723 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
724 utils::GetConnectionType(kNetWifi,
725 NetworkTethering::kConfirmed));
726
727 // Ensure that we don't report tethered ethernet unless it's confirmed.
728 EXPECT_EQ(metrics::ConnectionType::kEthernet,
729 utils::GetConnectionType(kNetEthernet,
730 NetworkTethering::kNotDetected));
731 EXPECT_EQ(metrics::ConnectionType::kEthernet,
732 utils::GetConnectionType(kNetEthernet,
733 NetworkTethering::kSuspected));
734 EXPECT_EQ(metrics::ConnectionType::kEthernet,
735 utils::GetConnectionType(kNetEthernet,
736 NetworkTethering::kUnknown));
737
738 // Ditto for tethered wifi.
739 EXPECT_EQ(metrics::ConnectionType::kWifi,
740 utils::GetConnectionType(kNetWifi,
741 NetworkTethering::kNotDetected));
742 EXPECT_EQ(metrics::ConnectionType::kWifi,
743 utils::GetConnectionType(kNetWifi,
744 NetworkTethering::kSuspected));
745 EXPECT_EQ(metrics::ConnectionType::kWifi,
746 utils::GetConnectionType(kNetWifi,
747 NetworkTethering::kUnknown));
748}
749
adlr@google.com3defe6a2009-12-04 20:57:17 +0000750} // namespace chromeos_update_engine