blob: 2bdb76b04a99455d72f04fcf08fc608084be205a [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/utils.h"
18
Alex Vakulenko44cab302014-07-23 13:12:15 -070019#include <errno.h>
Ben Chan9abb7632014-08-07 00:10:53 -070020#include <stdint.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <sys/stat.h>
22#include <sys/types.h>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070023
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070024#include <map>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080025#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000026#include <vector>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070027
Alex Deymoc1711e22014-08-08 13:16:23 -070028#include <base/files/file_path.h>
Alex Deymo2c0db7b2014-11-04 12:23:39 -080029#include <base/files/file_util.h>
Allie Wood78750a42015-02-11 15:42:11 -080030#include <base/files/scoped_temp_dir.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070031#include <base/strings/string_util.h>
32#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070033#include <brillo/message_loops/fake_message_loop.h>
34#include <brillo/message_loops/message_loop_utils.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070035#include <gtest/gtest.h>
36
David Zeuthen33bae492014-02-25 16:16:18 -080037#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080038#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070039#include "update_engine/fake_system_state.h"
David Zeuthen33bae492014-02-25 16:16:18 -080040#include "update_engine/prefs.h"
Darin Petkovd3f8c892010-10-12 21:38:45 -070041#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000042
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070043using brillo::FakeMessageLoop;
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070044using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080045using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000046using std::vector;
47
48namespace chromeos_update_engine {
49
50class UtilsTest : public ::testing::Test { };
51
Chris Sosac1972482013-04-30 22:31:10 -070052TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070053 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070054 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
55 EXPECT_EQ("123456", utils::ParseECVersion(
56 "b=1231a fw_version=123456 a=fasd2"));
57 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
58 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070059 "vendor=\"sam\" fw_version=\"00VFA616\""));
60
61 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070062 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070063}
64
adlr@google.com3defe6a2009-12-04 20:57:17 +000065TEST(UtilsTest, ReadFileFailure) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070066 brillo::Blob empty;
adlr@google.com3defe6a2009-12-04 20:57:17 +000067 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
68}
69
Darin Petkov8e447e02013-04-16 16:23:50 +020070TEST(UtilsTest, ReadFileChunk) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070071 base::FilePath file;
72 EXPECT_TRUE(base::CreateTemporaryFile(&file));
Darin Petkov8e447e02013-04-16 16:23:50 +020073 ScopedPathUnlinker unlinker(file.value());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070074 brillo::Blob data;
Darin Petkov8e447e02013-04-16 16:23:50 +020075 const size_t kSize = 1024 * 1024;
76 for (size_t i = 0; i < kSize; i++) {
77 data.push_back(i % 255);
78 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080079 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), data.data(), data.size()));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070080 brillo::Blob in_data;
Darin Petkov8e447e02013-04-16 16:23:50 +020081 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
82 EXPECT_TRUE(in_data.empty());
83 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
84 EXPECT_TRUE(data == in_data);
85 in_data.clear();
86 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070087 EXPECT_TRUE(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20) ==
Darin Petkov8e447e02013-04-16 16:23:50 +020088 in_data);
89}
90
adlr@google.com3defe6a2009-12-04 20:57:17 +000091TEST(UtilsTest, ErrnoNumberAsStringTest) {
92 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
93}
94
Darin Petkov002b2fe2010-11-22 13:53:22 -080095TEST(UtilsTest, IsSymlinkTest) {
96 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -080097 EXPECT_TRUE(utils::MakeTempDirectory("symlink-test.XXXXXX", &temp_dir));
Alex Deymo8d925292014-05-21 19:15:25 -070098 string temp_file = temp_dir + "/temp-file";
Darin Petkov002b2fe2010-11-22 13:53:22 -080099 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
Alex Deymo8d925292014-05-21 19:15:25 -0700100 string temp_symlink = temp_dir + "/temp-symlink";
Darin Petkov002b2fe2010-11-22 13:53:22 -0800101 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
102 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
103 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
104 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
105 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
Alex Deymo110e0302015-10-19 20:35:21 -0700106 EXPECT_TRUE(base::DeleteFile(base::FilePath(temp_dir), true));
Darin Petkov002b2fe2010-11-22 13:53:22 -0800107}
108
Alex Deymo763e7db2015-08-27 21:08:08 -0700109TEST(UtilsTest, SplitPartitionNameTest) {
110 string disk;
111 int part_num;
Darin Petkovf74eb652010-08-04 12:08:38 -0700112
Alex Deymo763e7db2015-08-27 21:08:08 -0700113 EXPECT_TRUE(utils::SplitPartitionName("/dev/sda3", &disk, &part_num));
114 EXPECT_EQ("/dev/sda", disk);
115 EXPECT_EQ(3, part_num);
Darin Petkovf74eb652010-08-04 12:08:38 -0700116
Alex Deymo763e7db2015-08-27 21:08:08 -0700117 EXPECT_TRUE(utils::SplitPartitionName("/dev/sdp1234", &disk, &part_num));
118 EXPECT_EQ("/dev/sdp", disk);
119 EXPECT_EQ(1234, part_num);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700120
Alex Deymo763e7db2015-08-27 21:08:08 -0700121 EXPECT_TRUE(utils::SplitPartitionName("/dev/mmcblk0p3", &disk, &part_num));
122 EXPECT_EQ("/dev/mmcblk0", disk);
123 EXPECT_EQ(3, part_num);
124
125 EXPECT_TRUE(utils::SplitPartitionName("/dev/ubiblock3_2", &disk, &part_num));
126 EXPECT_EQ("/dev/ubiblock", disk);
127 EXPECT_EQ(3, part_num);
128
129 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10", &disk, &part_num));
130 EXPECT_EQ("/dev/loop", disk);
131 EXPECT_EQ(10, part_num);
132
133 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11", &disk, &part_num));
134 EXPECT_EQ("/dev/loop28", disk);
135 EXPECT_EQ(11, part_num);
136
137 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10_0", &disk, &part_num));
138 EXPECT_EQ("/dev/loop", disk);
139 EXPECT_EQ(10, part_num);
140
141 EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11_0", &disk, &part_num));
142 EXPECT_EQ("/dev/loop28", disk);
143 EXPECT_EQ(11, part_num);
144
145 EXPECT_FALSE(utils::SplitPartitionName("/dev/mmcblk0p", &disk, &part_num));
146 EXPECT_FALSE(utils::SplitPartitionName("/dev/sda", &disk, &part_num));
147 EXPECT_FALSE(utils::SplitPartitionName("/dev/foo/bar", &disk, &part_num));
148 EXPECT_FALSE(utils::SplitPartitionName("/", &disk, &part_num));
149 EXPECT_FALSE(utils::SplitPartitionName("", &disk, &part_num));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700150}
151
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700152TEST(UtilsTest, MakePartitionNameTest) {
153 EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
154 EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
155 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
156 EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
157 EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
158 EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800159 EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5));
160 EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4));
161 EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3));
162 EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2));
163 EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1));
164}
165
166TEST(UtilsTest, MakePartitionNameForMountTest) {
167 EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4"));
168 EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123"));
169 EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2"));
170 EXPECT_EQ("/dev/mmcblk0p2",
171 utils::MakePartitionNameForMount("/dev/mmcblk0p2"));
172 EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0"));
173 EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8"));
174 EXPECT_EQ("/dev/loop12p2",
175 utils::MakePartitionNameForMount("/dev/loop12p2"));
176 EXPECT_EQ("/dev/ubiblock5_0",
177 utils::MakePartitionNameForMount("/dev/ubiblock5_0"));
178 EXPECT_EQ("/dev/mtd4",
179 utils::MakePartitionNameForMount("/dev/ubi4_0"));
180 EXPECT_EQ("/dev/ubiblock3_0",
181 utils::MakePartitionNameForMount("/dev/ubiblock3"));
182 EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2"));
183 EXPECT_EQ("/dev/ubi1_0",
184 utils::MakePartitionNameForMount("/dev/ubiblock1"));
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700185}
186
Alex Deymo10875d92014-11-10 21:52:57 -0800187namespace {
188// Compares cpu shares and returns an integer that is less
189// than, equal to or greater than 0 if |shares_lhs| is,
190// respectively, lower than, same as or higher than |shares_rhs|.
191int CompareCpuShares(utils::CpuShares shares_lhs,
192 utils::CpuShares shares_rhs) {
193 return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs);
194}
195} // namespace
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700196
Alex Deymo10875d92014-11-10 21:52:57 -0800197// Tests the CPU shares enum is in the order we expect it.
Chris Sosa4f8ee272012-11-30 13:01:54 -0800198TEST(UtilsTest, CompareCpuSharesTest) {
Alex Deymo10875d92014-11-10 21:52:57 -0800199 EXPECT_LT(CompareCpuShares(utils::kCpuSharesLow,
200 utils::kCpuSharesNormal), 0);
201 EXPECT_GT(CompareCpuShares(utils::kCpuSharesNormal,
202 utils::kCpuSharesLow), 0);
203 EXPECT_EQ(CompareCpuShares(utils::kCpuSharesNormal,
204 utils::kCpuSharesNormal), 0);
205 EXPECT_GT(CompareCpuShares(utils::kCpuSharesHigh,
206 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700207}
208
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700209TEST(UtilsTest, FuzzIntTest) {
210 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800211 for (unsigned int range : kRanges) {
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700212 const int kValue = 50;
213 for (int tries = 0; tries < 100; ++tries) {
214 int value = utils::FuzzInt(kValue, range);
215 EXPECT_GE(value, kValue - range / 2);
216 EXPECT_LE(value, kValue + range - range / 2);
217 }
218 }
219}
220
Darin Petkovd3f8c892010-10-12 21:38:45 -0700221TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
222 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700223 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkovd3f8c892010-10-12 21:38:45 -0700224 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800225 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700226 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800227 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Alex Deymo1f93d032015-03-10 18:58:32 -0700228 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none",
Darin Petkovd3f8c892010-10-12 21:38:45 -0700229 img.c_str())));
230 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
231 int block_count = 0;
232 int block_size = 0;
233 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
234 EXPECT_EQ(4096, block_size);
235 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
236}
237
Alex Deymo192393b2014-11-10 15:58:38 -0800238// Squashfs example filesystem, generated with:
239// echo hola>hola
240// mksquashfs hola hola.sqfs -noappend -nopad
241// hexdump hola.sqfs -e '16/1 "%02x, " "\n"'
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800242const uint8_t kSquashfsFile[] = {
Alex Deymo192393b2014-11-10 15:58:38 -0800243 0x68, 0x73, 0x71, 0x73, 0x02, 0x00, 0x00, 0x00, // magic, inodes
244 0x3e, 0x49, 0x61, 0x54, 0x00, 0x00, 0x02, 0x00,
245 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x00,
246 0xc0, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, // flags, noids, major, minor
247 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // root_inode
248 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes_used
249 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
250 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
251 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254 0xd5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
255 0x68, 0x6f, 0x6c, 0x61, 0x0a, 0x2c, 0x00, 0x78,
256 0xda, 0x63, 0x62, 0x58, 0xc2, 0xc8, 0xc0, 0xc0,
257 0xc8, 0xd0, 0x6b, 0x91, 0x18, 0x02, 0x64, 0xa0,
258 0x00, 0x56, 0x06, 0x90, 0xcc, 0x7f, 0xb0, 0xbc,
259 0x9d, 0x67, 0x62, 0x08, 0x13, 0x54, 0x1c, 0x44,
260 0x4b, 0x03, 0x31, 0x33, 0x10, 0x03, 0x00, 0xb5,
261 0x87, 0x04, 0x89, 0x16, 0x00, 0x78, 0xda, 0x63,
262 0x60, 0x80, 0x00, 0x46, 0x28, 0xcd, 0xc4, 0xc0,
263 0xcc, 0x90, 0x91, 0x9f, 0x93, 0x08, 0x00, 0x04,
264 0x70, 0x01, 0xab, 0x10, 0x80, 0x60, 0x00, 0x00,
265 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
266 0x01, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00,
267 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x78,
268 0xda, 0x63, 0x60, 0x80, 0x00, 0x05, 0x28, 0x0d,
269 0x00, 0x01, 0x10, 0x00, 0x21, 0xc5, 0x00, 0x00,
270 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x99,
271 0xcd, 0x02, 0x00, 0x88, 0x13, 0x00, 0x00, 0xdd,
272 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
273};
274
275TEST(UtilsTest, GetSquashfs4Size) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800276 uint8_t buffer[sizeof(kSquashfsFile)];
Alex Deymo192393b2014-11-10 15:58:38 -0800277 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
278
279 int block_count = -1;
280 int block_size = -1;
281 // Not enough bytes passed.
282 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
283
284 // The whole file system is passed, which is enough for parsing.
285 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
286 &block_count, &block_size));
287 EXPECT_EQ(4096, block_size);
288 EXPECT_EQ(1, block_count);
289
290 // Modify the major version to 5.
291 uint16_t* s_major = reinterpret_cast<uint16_t*>(buffer + 0x1c);
292 *s_major = 5;
293 EXPECT_FALSE(utils::GetSquashfs4Size(buffer, 10, nullptr, nullptr));
294 memcpy(buffer, kSquashfsFile, sizeof(kSquashfsFile));
295
296 // Modify the bytes_used to have 6 blocks.
297 int64_t* bytes_used = reinterpret_cast<int64_t*>(buffer + 0x28);
298 *bytes_used = 4096 * 5 + 1; // 6 "blocks".
299 EXPECT_TRUE(utils::GetSquashfs4Size(buffer, sizeof(kSquashfsFile),
300 &block_count, &block_size));
301 EXPECT_EQ(4096, block_size);
302 EXPECT_EQ(6, block_count);
303}
304
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800305namespace {
Alex Deymo032e7722014-03-25 17:53:56 -0700306void GetFileFormatTester(const string& expected,
Ben Chan9abb7632014-08-07 00:10:53 -0700307 const vector<uint8_t>& contents) {
Alex Deymo10875d92014-11-10 21:52:57 -0800308 test_utils::ScopedTempFile file;
Alex Deymo032e7722014-03-25 17:53:56 -0700309 ASSERT_TRUE(utils::WriteFile(file.GetPath().c_str(),
310 reinterpret_cast<const char*>(contents.data()),
311 contents.size()));
312 EXPECT_EQ(expected, utils::GetFileFormat(file.GetPath()));
313}
Alex Deymo10875d92014-11-10 21:52:57 -0800314} // namespace
Alex Deymo032e7722014-03-25 17:53:56 -0700315
316TEST(UtilsTest, GetFileFormatTest) {
317 EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
Ben Chan9abb7632014-08-07 00:10:53 -0700318 GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
319 GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
Alex Deymo032e7722014-03-25 17:53:56 -0700320
321 // Real tests from cros_installer on different boards.
322 // ELF 32-bit LSB executable, Intel 80386
323 GetFileFormatTester(
324 "ELF 32-bit little-endian x86",
Ben Chan9abb7632014-08-07 00:10:53 -0700325 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
328 0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700329
Alex Deymoc1711e22014-08-08 13:16:23 -0700330 // ELF 32-bit LSB executable, MIPS
331 GetFileFormatTester(
332 "ELF 32-bit little-endian mips",
333 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
334 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335 0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
336 0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
337
Alex Deymo032e7722014-03-25 17:53:56 -0700338 // ELF 32-bit LSB executable, ARM
339 GetFileFormatTester(
340 "ELF 32-bit little-endian arm",
Ben Chan9abb7632014-08-07 00:10:53 -0700341 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343 0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
344 0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700345
346 // ELF 64-bit LSB executable, x86-64
347 GetFileFormatTester(
348 "ELF 64-bit little-endian x86-64",
Ben Chan9abb7632014-08-07 00:10:53 -0700349 vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
350 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
352 0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
Alex Deymo032e7722014-03-25 17:53:56 -0700353}
354
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800355TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
356 // Not much to test. At least this tests for memory leaks, crashes,
357 // log errors.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700358 FakeMessageLoop loop(nullptr);
359 loop.SetAsCurrent();
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800360 utils::ScheduleCrashReporterUpload();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700361 // Test that we scheduled one callback from the crash reporter.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700362 EXPECT_EQ(1, brillo::MessageLoopRunMaxIterations(&loop, 100));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700363 EXPECT_FALSE(loop.PendingTasks());
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800364}
365
David Zeuthen674c3182013-04-18 14:05:20 -0700366TEST(UtilsTest, FormatTimeDeltaTest) {
367 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
368 // which is not localized) so we only need to test the C locale
369 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
370 "0.1s");
371 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
372 "0s");
373 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
374 "1s");
375 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
376 "59s");
377 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
378 "1m0s");
379 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
380 "1m1s");
381 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
382 "1m30s");
383 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
384 "20m5s");
385 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
386 "1h0m0s");
387 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
388 "1h0m1s");
389 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
390 "1h1m1s");
391 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
392 "2h1m1s");
393 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
394 "1d0h0m0s");
395 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
396 "1d0h0m1s");
397 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
398 "2d7h33m20s");
399 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
400 base::TimeDelta::FromMilliseconds(1)),
401 "2d7h33m20.001s");
David Zeuthen973449e2014-08-18 16:18:23 -0400402 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)),
403 "-1s");
David Zeuthen674c3182013-04-18 14:05:20 -0700404}
405
David Zeuthen27a48bc2013-08-06 12:06:29 -0700406TEST(UtilsTest, TimeFromStructTimespecTest) {
407 struct timespec ts;
408
409 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
410 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
411 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
412
413 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
414 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
415 .tv_nsec = 42 * 1000 * 1000};
416 base::Time::Exploded exploded = (base::Time::Exploded) {
417 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
418 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
419 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
420 utils::TimeFromStructTimespec(&ts));
421}
422
David Zeuthene7f89172013-10-31 10:21:04 -0700423TEST(UtilsTest, DecodeAndStoreBase64String) {
424 base::FilePath path;
425
426 // Ensure we return false on empty strings or invalid base64.
427 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
428 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
429
430 // Pass known base64 and check that it matches. This string was generated
431 // the following way:
432 //
433 // $ echo "Update Engine" | base64
434 // VXBkYXRlIEVuZ2luZQo=
435 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
436 &path));
437 ScopedPathUnlinker unlinker(path.value());
438 string expected_contents = "Update Engine\n";
439 string contents;
440 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
441 EXPECT_EQ(contents, expected_contents);
442 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
443}
444
David Zeuthen639aa362014-02-03 16:23:44 -0800445TEST(UtilsTest, ConvertToOmahaInstallDate) {
446 // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
447 // Monday. In Unix time, this point in time is easily obtained via
448 // the date(1) command like this:
449 //
450 // $ date +"%s" --date="Jan 1, 2007 0:00 PST"
451 const time_t omaha_epoch = 1167638400;
452 int value;
453
454 // Points in time *on and after* the Omaha epoch should not fail.
455 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
456 base::Time::FromTimeT(omaha_epoch), &value));
457 EXPECT_GE(value, 0);
458
459 // Anything before the Omaha epoch should fail. We test it for two points.
460 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
461 base::Time::FromTimeT(omaha_epoch - 1), &value));
462 EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
463 base::Time::FromTimeT(omaha_epoch - 100*24*3600), &value));
464
465 // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
466 // on Jan 8, 2007 0:00 PST.
467 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
468 base::Time::FromTimeT(omaha_epoch + 7*24*3600 - 1), &value));
469 EXPECT_EQ(value, 0);
470 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
471 base::Time::FromTimeT(omaha_epoch + 7*24*3600), &value));
472 EXPECT_EQ(value, 7);
473
474 // Check a couple of more values.
475 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
476 base::Time::FromTimeT(omaha_epoch + 10*24*3600), &value));
477 EXPECT_EQ(value, 7);
478 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
479 base::Time::FromTimeT(omaha_epoch + 20*24*3600), &value));
480 EXPECT_EQ(value, 14);
481 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
482 base::Time::FromTimeT(omaha_epoch + 26*24*3600), &value));
483 EXPECT_EQ(value, 21);
484 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
485 base::Time::FromTimeT(omaha_epoch + 29*24*3600), &value));
486 EXPECT_EQ(value, 28);
487
488 // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
489 // where the Omaha InstallDate jumps 7 days. Its unix time is
490 // 1180940400. Notably, this is a point in time where Daylight
491 // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
492 //
493 // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
494 // ignores DST (as it's hard to implement in a thread-safe way using
495 // glibc, see comments in utils.h) we have to fudge by the DST
496 // offset which is one hour. Conveniently, if the function were
497 // someday modified to be DST aware, this test would have to be
498 // modified as well.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700499 const time_t dst_time = 1180940400; // Jun 4, 2007 0:00 PDT.
David Zeuthen639aa362014-02-03 16:23:44 -0800500 const time_t fudge = 3600;
501 int value1, value2;
502 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
503 base::Time::FromTimeT(dst_time + fudge - 1), &value1));
504 EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
505 base::Time::FromTimeT(dst_time + fudge), &value2));
506 EXPECT_EQ(value1, value2 - 7);
507}
508
David Zeuthen33bae492014-02-25 16:16:18 -0800509TEST(UtilsTest, WallclockDurationHelper) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700510 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800511 FakeClock fake_clock;
512 base::TimeDelta duration;
513 string state_variable_key = "test-prefs";
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800514 FakePrefs fake_prefs;
David Zeuthen33bae492014-02-25 16:16:18 -0800515
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700516 fake_system_state.set_clock(&fake_clock);
517 fake_system_state.set_prefs(&fake_prefs);
David Zeuthen33bae492014-02-25 16:16:18 -0800518
519 // Initialize wallclock to 1 sec.
520 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
521
522 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700523 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800524 state_variable_key,
525 &duration));
526
527 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700528 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800529 state_variable_key,
530 &duration));
531 EXPECT_EQ(duration.InSeconds(), 0);
532
533 // We can also call it as many times as we want with it being
534 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700535 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800536 state_variable_key,
537 &duration));
538 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700539 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800540 state_variable_key,
541 &duration));
542 EXPECT_EQ(duration.InSeconds(), 0);
543
544 // Advance the clock one second, then we should get 1 sec on the
545 // next call and 0 sec on the subsequent call.
546 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700547 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800548 state_variable_key,
549 &duration));
550 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700551 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800552 state_variable_key,
553 &duration));
554 EXPECT_EQ(duration.InSeconds(), 0);
555
556 // Advance clock two seconds and we should get 2 sec and then 0 sec.
557 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700558 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800559 state_variable_key,
560 &duration));
561 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700562 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800563 state_variable_key,
564 &duration));
565 EXPECT_EQ(duration.InSeconds(), 0);
566
567 // There's a possibility that the wallclock can go backwards (NTP
568 // adjustments, for example) so check that we properly handle this
569 // case.
570 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700571 EXPECT_FALSE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800572 state_variable_key,
573 &duration));
574 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700575 EXPECT_TRUE(utils::WallclockDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800576 state_variable_key,
577 &duration));
578 EXPECT_EQ(duration.InSeconds(), 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800579}
580
581TEST(UtilsTest, MonotonicDurationHelper) {
582 int64_t storage = 0;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700583 FakeSystemState fake_system_state;
David Zeuthen33bae492014-02-25 16:16:18 -0800584 FakeClock fake_clock;
585 base::TimeDelta duration;
586
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700587 fake_system_state.set_clock(&fake_clock);
David Zeuthen33bae492014-02-25 16:16:18 -0800588
589 // Initialize monotonic clock to 1 sec.
590 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
591
592 // First time called so no previous measurement available.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700593 EXPECT_FALSE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800594 &storage,
595 &duration));
596
597 // Next time, we should get zero since the clock didn't advance.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700598 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800599 &storage,
600 &duration));
601 EXPECT_EQ(duration.InSeconds(), 0);
602
603 // We can also call it as many times as we want with it being
604 // considered a failure.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700605 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800606 &storage,
607 &duration));
608 EXPECT_EQ(duration.InSeconds(), 0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700609 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800610 &storage,
611 &duration));
612 EXPECT_EQ(duration.InSeconds(), 0);
613
614 // Advance the clock one second, then we should get 1 sec on the
615 // next call and 0 sec on the subsequent call.
616 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700617 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800618 &storage,
619 &duration));
620 EXPECT_EQ(duration.InSeconds(), 1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700621 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800622 &storage,
623 &duration));
624 EXPECT_EQ(duration.InSeconds(), 0);
625
626 // Advance clock two seconds and we should get 2 sec and then 0 sec.
627 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700628 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800629 &storage,
630 &duration));
631 EXPECT_EQ(duration.InSeconds(), 2);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700632 EXPECT_TRUE(utils::MonotonicDurationHelper(&fake_system_state,
David Zeuthen33bae492014-02-25 16:16:18 -0800633 &storage,
634 &duration));
635 EXPECT_EQ(duration.InSeconds(), 0);
636}
637
David Zeuthenb281f072014-04-02 10:20:19 -0700638TEST(UtilsTest, GetConnectionType) {
639 // Check that expected combinations map to the right value.
640 EXPECT_EQ(metrics::ConnectionType::kUnknown,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700641 utils::GetConnectionType(NetworkConnectionType::kUnknown,
David Zeuthenb281f072014-04-02 10:20:19 -0700642 NetworkTethering::kUnknown));
643 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700644 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700645 NetworkTethering::kUnknown));
646 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700647 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700648 NetworkTethering::kUnknown));
649 EXPECT_EQ(metrics::ConnectionType::kWimax,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700650 utils::GetConnectionType(NetworkConnectionType::kWimax,
David Zeuthenb281f072014-04-02 10:20:19 -0700651 NetworkTethering::kUnknown));
652 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700653 utils::GetConnectionType(NetworkConnectionType::kBluetooth,
David Zeuthenb281f072014-04-02 10:20:19 -0700654 NetworkTethering::kUnknown));
655 EXPECT_EQ(metrics::ConnectionType::kCellular,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700656 utils::GetConnectionType(NetworkConnectionType::kCellular,
David Zeuthenb281f072014-04-02 10:20:19 -0700657 NetworkTethering::kUnknown));
658 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700659 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700660 NetworkTethering::kConfirmed));
661 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700662 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700663 NetworkTethering::kConfirmed));
664
665 // Ensure that we don't report tethered ethernet unless it's confirmed.
666 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700667 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700668 NetworkTethering::kNotDetected));
669 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700670 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700671 NetworkTethering::kSuspected));
672 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700673 utils::GetConnectionType(NetworkConnectionType::kEthernet,
David Zeuthenb281f072014-04-02 10:20:19 -0700674 NetworkTethering::kUnknown));
675
676 // Ditto for tethered wifi.
677 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700678 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700679 NetworkTethering::kNotDetected));
680 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700681 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700682 NetworkTethering::kSuspected));
683 EXPECT_EQ(metrics::ConnectionType::kWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700684 utils::GetConnectionType(NetworkConnectionType::kWifi,
David Zeuthenb281f072014-04-02 10:20:19 -0700685 NetworkTethering::kUnknown));
686}
687
Allie Wood78750a42015-02-11 15:42:11 -0800688TEST(UtilsTest, GetMinorVersion) {
689 // Test GetMinorVersion by verifying that it parses the conf file and returns
690 // the correct value.
Allie Wood78750a42015-02-11 15:42:11 -0800691 uint32_t minor_version;
692
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700693 brillo::KeyValueStore store;
Alex Deymob42b98d2015-07-06 17:42:38 -0700694 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800695
Alex Deymob42b98d2015-07-06 17:42:38 -0700696 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n"));
697 EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
Allie Wood78750a42015-02-11 15:42:11 -0800698
Alex Deymob42b98d2015-07-06 17:42:38 -0700699 EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n"));
700 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
701 EXPECT_EQ(minor_version, 123);
Allie Wood78750a42015-02-11 15:42:11 -0800702}
703
Nam T. Nguyen2b67a592014-12-03 14:56:00 -0800704static bool BoolMacroTestHelper() {
705 int i = 1;
706 unsigned int ui = 1;
707 bool b = 1;
708 std::unique_ptr<char> cptr(new char);
709
710 TEST_AND_RETURN_FALSE(i);
711 TEST_AND_RETURN_FALSE(ui);
712 TEST_AND_RETURN_FALSE(b);
713 TEST_AND_RETURN_FALSE(cptr);
714
715 TEST_AND_RETURN_FALSE_ERRNO(i);
716 TEST_AND_RETURN_FALSE_ERRNO(ui);
717 TEST_AND_RETURN_FALSE_ERRNO(b);
718 TEST_AND_RETURN_FALSE_ERRNO(cptr);
719
720 return true;
721}
722
723static void VoidMacroTestHelper(bool* ret) {
724 int i = 1;
725 unsigned int ui = 1;
726 bool b = 1;
727 std::unique_ptr<char> cptr(new char);
728
729 *ret = false;
730
731 TEST_AND_RETURN(i);
732 TEST_AND_RETURN(ui);
733 TEST_AND_RETURN(b);
734 TEST_AND_RETURN(cptr);
735
736 TEST_AND_RETURN_ERRNO(i);
737 TEST_AND_RETURN_ERRNO(ui);
738 TEST_AND_RETURN_ERRNO(b);
739 TEST_AND_RETURN_ERRNO(cptr);
740
741 *ret = true;
742}
743
744TEST(UtilsTest, TestMacros) {
745 bool void_test = false;
746 VoidMacroTestHelper(&void_test);
747 EXPECT_TRUE(void_test);
748
749 EXPECT_TRUE(BoolMacroTestHelper());
750}
751
adlr@google.com3defe6a2009-12-04 20:57:17 +0000752} // namespace chromeos_update_engine