blob: 0e2911359a72515bcab9e4bb0c441316dd9a8a58 [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;
Alex Deymo020600d2014-11-05 21:05:55 -0800312 for (const int value : collection) {
313 EXPECT_EQ(expected_values[index++], value);
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700314 }
315}
316
Darin Petkovd3f8c892010-10-12 21:38:45 -0700317TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
318 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700319 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700320 ScopedPathUnlinker img_unlinker(img);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700321 CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700322 // Extend the "partition" holding the file system from 10MiB to 20MiB.
323 EXPECT_EQ(0, System(base::StringPrintf(
324 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
325 img.c_str())));
326 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
327 int block_count = 0;
328 int block_size = 0;
329 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
330 EXPECT_EQ(4096, block_size);
331 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
332}
333
Chris Sosad317e402013-06-12 13:47:09 -0700334TEST(UtilsTest, GetInstallDevTest) {
335 string boot_dev = "/dev/sda5";
336 string install_dev;
337 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
338 EXPECT_EQ(install_dev, "/dev/sda3");
339
340 boot_dev = "/dev/sda3";
341 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
342 EXPECT_EQ(install_dev, "/dev/sda5");
343
344 boot_dev = "/dev/sda12";
345 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700346
347 boot_dev = "/dev/ubiblock3_0";
348 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
349 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
350
351 boot_dev = "/dev/ubiblock5_0";
352 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
353 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
354
355 boot_dev = "/dev/ubiblock12_0";
356 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700357}
358
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800359namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700360void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700361 const vector<uint8_t>& contents) {
Alex Deymo032e7722014-03-25 17:53:56 -0700362 ScopedTempFile file;
363 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
364 reinterpret_cast<const char*>(contents.data()),
365 contents.size()));
366 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
367}
368}
369
370TEST(UtilsTest, GetFileFormatTest) {
371 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700372 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
373 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700374
375 // Real tests from cros_installer on different boards.
376 // ELF 32-bit LSB executable, Intel 80386
377 GetFileFormatTester(
378 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700379 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
380 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
381 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
382 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700383
Alex Deymoc1711e22014-08-08 13:16:23 -0700384 // ELF 32-bit LSB executable, MIPS
385 GetFileFormatTester(
386 "ELF 32-bit little-endian mips",
387 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
388 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
390 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
391
Alex Deymo032e7722014-03-25 17:53:56 -0700392 // ELF 32-bit LSB executable, ARM
393 GetFileFormatTester(
394 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700395 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
398 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700399
400 // ELF 64-bit LSB executable, x86-64
401 GetFileFormatTester(
402 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700403 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
404 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
405 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
406 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700407}
408
409namespace {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800410gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
411 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
412 g_main_loop_quit(loop);
413 return FALSE; // Don't call this callback again
414}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700415} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800416
417TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
418 // Not much to test. At least this tests for memory leaks, crashes,
419 // log errors.
420 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
421 utils::ScheduleCrashReporterUpload();
422 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
423 g_main_loop_run(loop);
424 g_main_loop_unref(loop);
425}
426
David Zeuthen674c3182013-04-18 14:05:20 -0700427TEST(UtilsTest, FormatTimeDeltaTest) {
428 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
429 // which is not localized) so we only need to test the C locale
430 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
431 "0.1s");
432 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
433 "0s");
434 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
435 "1s");
436 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
437 "59s");
438 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
439 "1m0s");
440 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
441 "1m1s");
442 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
443 "1m30s");
444 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
445 "20m5s");
446 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
447 "1h0m0s");
448 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
449 "1h0m1s");
450 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
451 "1h1m1s");
452 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
453 "2h1m1s");
454 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
455 "1d0h0m0s");
456 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
457 "1d0h0m1s");
458 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
459 "2d7h33m20s");
460 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
461 base::TimeDelta::FromMilliseconds(1)),
462 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400463 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
464 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700465}
466
David Zeuthen27a48bc2013-08-06 12:06:29 -0700467TEST(UtilsTest, TimeFromStructTimespecTest) {
468 struct timespec ts;
469
470 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
471 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
472 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
473
474 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
475 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
476 .tv_nsec = 42 * 1000 * 1000};
477 base::Time::Exploded exploded = (base::Time::Exploded) {
478 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
479 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
480 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
481 utils::TimeFromStructTimespec(&ts));
482}
483
David Zeuthene7f89172013-10-31 10:21:04 -0700484TEST(UtilsTest, DecodeAndStoreBase64String) {
485 base::FilePath path;
486
487 // Ensure we return false on empty strings or invalid base64.
488 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
489 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
490
491 // Pass known base64 and check that it matches. This string was generated
492 // the following way:
493 //
494 // $ echo "Update Engine" | base64
495 // VXBkYXRlIEVuZ2luZQo=
496 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
497 &path));
498 ScopedPathUnlinker unlinker(path.value());
499 string expected_contents = "Update Engine\n";
500 string contents;
501 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
502 EXPECT_EQ(contents, expected_contents);
503 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
504}
505
David Zeuthen639aa362014-02-03 16:23:44 -0800506TEST(UtilsTest, ConvertToOmahaInstallDate) {
507 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
508 // Monday. In Unix time, this point in time is easily obtained via
509 // the date(1) command like this:
510 //
511 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
512 const time_t omaha_epoch = 1167638400;
513 int value;
514
515 // Points in time *on and after* the Omaha epoch should not fail.
516 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
517 base::Time::FromTimeT(omaha_epoch), &value));
518 EXPECT_GE(value, 0);
519
520 // Anything before the Omaha epoch should fail. We test it for two points.
521 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
522 base::Time::FromTimeT(omaha_epoch - 1), &value));
523 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
524 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
525
526 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
527 // on Jan 8, 2007 0:00 PST.
528 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
529 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
530 EXPECT_EQ(value, 0);
531 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
532 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
533 EXPECT_EQ(value, 7);
534
535 // Check a couple of more values.
536 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
537 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
538 EXPECT_EQ(value, 7);
539 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
540 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
541 EXPECT_EQ(value, 14);
542 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
543 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
544 EXPECT_EQ(value, 21);
545 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
546 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
547 EXPECT_EQ(value, 28);
548
549 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
550 // where the Omaha InstallDate jumps 7 days. Its unix time is
551 // 1180940400. Notably, this is a point in time where Daylight
552 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
553 //
554 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
555 // ignores DST (as it's hard to implement in a thread-safe way using
556 // glibc, see comments in utils.h) we have to fudge by the DST
557 // offset which is one hour. Conveniently, if the function were
558 // someday modified to be DST aware, this test would have to be
559 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700560 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800561 const time_t fudge = 3600;
562 int value1, value2;
563 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
564 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
565 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
566 base::Time::FromTimeT(dst_time + fudge), &value2));
567 EXPECT_EQ(value1, value2 - 7);
568}
569
David Zeuthen33bae492014-02-25 16:16:18 -0800570TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700571 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800572 FakeClock fake_clock;
573 base::TimeDelta duration;
574 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800575 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800576
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700577 fake_system_state.set_clock(&fake_clock);
578 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800579
580 // Initialize wallclock to 1 sec.
581 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
582
583 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700584 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800585 state_variable_key,
586 &duration));
587
588 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700589 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800590 state_variable_key,
591 &duration));
592 EXPECT_EQ(duration.InSeconds(), 0);
593
594 // We can also call it as many times as we want with it being
595 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700596 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800597 state_variable_key,
598 &duration));
599 EXPECT_EQ(duration.InSeconds(), 0);
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(), 0);
604
605 // Advance the clock one second, then we should get 1 sec on the
606 // next call and 0 sec on the subsequent call.
607 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700608 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800609 state_variable_key,
610 &duration));
611 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700612 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800613 state_variable_key,
614 &duration));
615 EXPECT_EQ(duration.InSeconds(), 0);
616
617 // Advance clock two seconds and we should get 2 sec and then 0 sec.
618 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700619 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800620 state_variable_key,
621 &duration));
622 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700623 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800624 state_variable_key,
625 &duration));
626 EXPECT_EQ(duration.InSeconds(), 0);
627
628 // There's a possibility that the wallclock can go backwards (NTP
629 // adjustments, for example) so check that we properly handle this
630 // case.
631 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700632 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800633 state_variable_key,
634 &duration));
635 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700636 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800637 state_variable_key,
638 &duration));
639 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800640}
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