• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 //
16 
17 #include "update_engine/common/utils.h"
18 
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <sys/mount.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 
25 #include <limits>
26 #include <string>
27 #include <vector>
28 
29 #include <base/files/file_path.h>
30 #include <base/files/file_util.h>
31 #include <base/files/scoped_temp_dir.h>
32 #include <gtest/gtest.h>
33 
34 #include "update_engine/common/test_utils.h"
35 
36 using std::numeric_limits;
37 using std::string;
38 using std::vector;
39 
40 namespace chromeos_update_engine {
41 
42 class UtilsTest : public ::testing::Test {};
43 
TEST(UtilsTest,CanParseECVersion)44 TEST(UtilsTest, CanParseECVersion) {
45   // Should be able to parse and valid key value line.
46   EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
47   EXPECT_EQ("123456",
48             utils::ParseECVersion("b=1231a fw_version=123456 a=fasd2"));
49   EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
50   EXPECT_EQ("00VFA616",
51             utils::ParseECVersion("vendor=\"sam\" fw_version=\"00VFA616\""));
52 
53   // For invalid entries, should return the empty string.
54   EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
55 }
56 
TEST(UtilsTest,WriteFileOpenFailure)57 TEST(UtilsTest, WriteFileOpenFailure) {
58   EXPECT_FALSE(utils::WriteFile("/this/doesn't/exist", "hello", 5));
59 }
60 
TEST(UtilsTest,WriteFileReadFile)61 TEST(UtilsTest, WriteFileReadFile) {
62   test_utils::ScopedTempFile file;
63   EXPECT_TRUE(utils::WriteFile(file.path().c_str(), "hello", 5));
64 
65   brillo::Blob readback;
66   EXPECT_TRUE(utils::ReadFile(file.path().c_str(), &readback));
67   EXPECT_EQ("hello", string(readback.begin(), readback.end()));
68 }
69 
TEST(UtilsTest,ReadFileFailure)70 TEST(UtilsTest, ReadFileFailure) {
71   brillo::Blob empty;
72   EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
73 }
74 
TEST(UtilsTest,ReadFileChunk)75 TEST(UtilsTest, ReadFileChunk) {
76   test_utils::ScopedTempFile file;
77   brillo::Blob data;
78   const size_t kSize = 1024 * 1024;
79   for (size_t i = 0; i < kSize; i++) {
80     data.push_back(i % 255);
81   }
82   EXPECT_TRUE(test_utils::WriteFileVector(file.path(), data));
83   brillo::Blob in_data;
84   EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), kSize, 10, &in_data));
85   EXPECT_TRUE(in_data.empty());
86   EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 0, -1, &in_data));
87   EXPECT_EQ(data, in_data);
88   in_data.clear();
89   EXPECT_TRUE(utils::ReadFileChunk(file.path().c_str(), 10, 20, &in_data));
90   EXPECT_EQ(brillo::Blob(data.begin() + 10, data.begin() + 10 + 20), in_data);
91 }
92 
TEST(UtilsTest,ErrnoNumberAsStringTest)93 TEST(UtilsTest, ErrnoNumberAsStringTest) {
94   EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
95 }
96 
TEST(UtilsTest,IsSymlinkTest)97 TEST(UtilsTest, IsSymlinkTest) {
98   base::ScopedTempDir temp_dir;
99   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
100   string temp_file = temp_dir.GetPath().Append("temp-file").value();
101   EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
102   string temp_symlink = temp_dir.GetPath().Append("temp-symlink").value();
103   EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
104   EXPECT_FALSE(utils::IsSymlink(temp_dir.GetPath().value().c_str()));
105   EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
106   EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
107   EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
108 }
109 
TEST(UtilsTest,SplitPartitionNameTest)110 TEST(UtilsTest, SplitPartitionNameTest) {
111   string disk;
112   int part_num;
113 
114   EXPECT_TRUE(utils::SplitPartitionName("/dev/sda3", &disk, &part_num));
115   EXPECT_EQ("/dev/sda", disk);
116   EXPECT_EQ(3, part_num);
117 
118   EXPECT_TRUE(utils::SplitPartitionName("/dev/sdp1234", &disk, &part_num));
119   EXPECT_EQ("/dev/sdp", disk);
120   EXPECT_EQ(1234, part_num);
121 
122   EXPECT_TRUE(utils::SplitPartitionName("/dev/mmcblk0p3", &disk, &part_num));
123   EXPECT_EQ("/dev/mmcblk0", disk);
124   EXPECT_EQ(3, part_num);
125 
126   EXPECT_TRUE(utils::SplitPartitionName("/dev/ubiblock3_2", &disk, &part_num));
127   EXPECT_EQ("/dev/ubiblock", disk);
128   EXPECT_EQ(3, part_num);
129 
130   EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10", &disk, &part_num));
131   EXPECT_EQ("/dev/loop", disk);
132   EXPECT_EQ(10, part_num);
133 
134   EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11", &disk, &part_num));
135   EXPECT_EQ("/dev/loop28", disk);
136   EXPECT_EQ(11, part_num);
137 
138   EXPECT_TRUE(utils::SplitPartitionName("/dev/loop10_0", &disk, &part_num));
139   EXPECT_EQ("/dev/loop", disk);
140   EXPECT_EQ(10, part_num);
141 
142   EXPECT_TRUE(utils::SplitPartitionName("/dev/loop28p11_0", &disk, &part_num));
143   EXPECT_EQ("/dev/loop28", disk);
144   EXPECT_EQ(11, part_num);
145 
146   EXPECT_FALSE(utils::SplitPartitionName("/dev/mmcblk0p", &disk, &part_num));
147   EXPECT_FALSE(utils::SplitPartitionName("/dev/sda", &disk, &part_num));
148   EXPECT_FALSE(utils::SplitPartitionName("/dev/foo/bar", &disk, &part_num));
149   EXPECT_FALSE(utils::SplitPartitionName("/", &disk, &part_num));
150   EXPECT_FALSE(utils::SplitPartitionName("", &disk, &part_num));
151 }
152 
TEST(UtilsTest,MakePartitionNameTest)153 TEST(UtilsTest, MakePartitionNameTest) {
154   EXPECT_EQ("/dev/sda4", utils::MakePartitionName("/dev/sda", 4));
155   EXPECT_EQ("/dev/sda123", utils::MakePartitionName("/dev/sda", 123));
156   EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionName("/dev/mmcblk", 2));
157   EXPECT_EQ("/dev/mmcblk0p2", utils::MakePartitionName("/dev/mmcblk0", 2));
158   EXPECT_EQ("/dev/loop8", utils::MakePartitionName("/dev/loop", 8));
159   EXPECT_EQ("/dev/loop12p2", utils::MakePartitionName("/dev/loop12", 2));
160   EXPECT_EQ("/dev/ubi5_0", utils::MakePartitionName("/dev/ubiblock", 5));
161   EXPECT_EQ("/dev/mtd4", utils::MakePartitionName("/dev/ubiblock", 4));
162   EXPECT_EQ("/dev/ubi3_0", utils::MakePartitionName("/dev/ubiblock", 3));
163   EXPECT_EQ("/dev/mtd2", utils::MakePartitionName("/dev/ubiblock", 2));
164   EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionName("/dev/ubiblock", 1));
165 }
166 
TEST(UtilsTest,MakePartitionNameForMountTest)167 TEST(UtilsTest, MakePartitionNameForMountTest) {
168   EXPECT_EQ("/dev/sda4", utils::MakePartitionNameForMount("/dev/sda4"));
169   EXPECT_EQ("/dev/sda123", utils::MakePartitionNameForMount("/dev/sda123"));
170   EXPECT_EQ("/dev/mmcblk2", utils::MakePartitionNameForMount("/dev/mmcblk2"));
171   EXPECT_EQ("/dev/mmcblk0p2",
172             utils::MakePartitionNameForMount("/dev/mmcblk0p2"));
173   EXPECT_EQ("/dev/loop0", utils::MakePartitionNameForMount("/dev/loop0"));
174   EXPECT_EQ("/dev/loop8", utils::MakePartitionNameForMount("/dev/loop8"));
175   EXPECT_EQ("/dev/loop12p2", utils::MakePartitionNameForMount("/dev/loop12p2"));
176   EXPECT_EQ("/dev/ubiblock5_0",
177             utils::MakePartitionNameForMount("/dev/ubiblock5_0"));
178   EXPECT_EQ("/dev/mtd4", utils::MakePartitionNameForMount("/dev/ubi4_0"));
179   EXPECT_EQ("/dev/ubiblock3_0",
180             utils::MakePartitionNameForMount("/dev/ubiblock3"));
181   EXPECT_EQ("/dev/mtd2", utils::MakePartitionNameForMount("/dev/ubi2"));
182   EXPECT_EQ("/dev/ubi1_0", utils::MakePartitionNameForMount("/dev/ubiblock1"));
183 }
184 
TEST(UtilsTest,FuzzIntTest)185 TEST(UtilsTest, FuzzIntTest) {
186   static const uint32_t kRanges[] = {0, 1, 2, 20};
187   for (uint32_t range : kRanges) {
188     const int kValue = 50;
189     for (int tries = 0; tries < 100; ++tries) {
190       uint32_t value = utils::FuzzInt(kValue, range);
191       EXPECT_GE(value, kValue - range / 2);
192       EXPECT_LE(value, kValue + range - range / 2);
193     }
194   }
195 }
196 
197 namespace {
GetFileFormatTester(const string & expected,const vector<uint8_t> & contents)198 void GetFileFormatTester(const string& expected,
199                          const vector<uint8_t>& contents) {
200   test_utils::ScopedTempFile file;
201   ASSERT_TRUE(utils::WriteFile(file.path().c_str(),
202                                reinterpret_cast<const char*>(contents.data()),
203                                contents.size()));
204   EXPECT_EQ(expected, utils::GetFileFormat(file.path()));
205 }
206 }  // namespace
207 
TEST(UtilsTest,GetFileFormatTest)208 TEST(UtilsTest, GetFileFormatTest) {
209   EXPECT_EQ("File not found.", utils::GetFileFormat("/path/to/nowhere"));
210   GetFileFormatTester("data", vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8});
211   GetFileFormatTester("ELF", vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46});
212 
213   // Real tests from cros_installer on different boards.
214   // ELF 32-bit LSB executable, Intel 80386
215   GetFileFormatTester(
216       "ELF 32-bit little-endian x86",
217       vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
218                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
219                       0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
220                       0x90, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00});
221 
222   // ELF 32-bit LSB executable, MIPS
223   GetFileFormatTester(
224       "ELF 32-bit little-endian mips",
225       vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
226                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227                       0x03, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00,
228                       0xc0, 0x12, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
229 
230   // ELF 32-bit LSB executable, ARM
231   GetFileFormatTester(
232       "ELF 32-bit little-endian arm",
233       vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00,
234                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235                       0x02, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
236                       0x85, 0x8b, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00});
237 
238   // ELF 64-bit LSB executable, x86-64
239   GetFileFormatTester(
240       "ELF 64-bit little-endian x86-64",
241       vector<uint8_t>{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
242                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243                       0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
244                       0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
245 }
246 
TEST(UtilsTest,FormatTimeDeltaTest)247 TEST(UtilsTest, FormatTimeDeltaTest) {
248   // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
249   // which is not localized) so we only need to test the C locale
250   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
251             "0.1s");
252   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)), "0s");
253   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)), "1s");
254   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)), "59s");
255   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)), "1m0s");
256   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)), "1m1s");
257   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)), "1m30s");
258   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
259             "20m5s");
260   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
261             "1h0m0s");
262   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
263             "1h0m1s");
264   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
265             "1h1m1s");
266   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
267             "2h1m1s");
268   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
269             "1d0h0m0s");
270   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
271             "1d0h0m1s");
272   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
273             "2d7h33m20s");
274   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
275                                    base::TimeDelta::FromMilliseconds(1)),
276             "2d7h33m20.001s");
277   EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(-1)), "-1s");
278 }
279 
TEST(UtilsTest,ConvertToOmahaInstallDate)280 TEST(UtilsTest, ConvertToOmahaInstallDate) {
281   // The Omaha Epoch starts at Jan 1, 2007 0:00 PST which is a
282   // Monday. In Unix time, this point in time is easily obtained via
283   // the date(1) command like this:
284   //
285   //  $ date +"%s" --date="Jan 1, 2007 0:00 PST"
286   const time_t omaha_epoch = 1167638400;
287   int value;
288 
289   // Points in time *on and after* the Omaha epoch should not fail.
290   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
291       base::Time::FromTimeT(omaha_epoch), &value));
292   EXPECT_GE(value, 0);
293 
294   // Anything before the Omaha epoch should fail. We test it for two points.
295   EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
296       base::Time::FromTimeT(omaha_epoch - 1), &value));
297   EXPECT_FALSE(utils::ConvertToOmahaInstallDate(
298       base::Time::FromTimeT(omaha_epoch - 100 * 24 * 3600), &value));
299 
300   // Check that we jump from 0 to 7 exactly on the one-week mark, e.g.
301   // on Jan 8, 2007 0:00 PST.
302   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
303       base::Time::FromTimeT(omaha_epoch + 7 * 24 * 3600 - 1), &value));
304   EXPECT_EQ(value, 0);
305   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
306       base::Time::FromTimeT(omaha_epoch + 7 * 24 * 3600), &value));
307   EXPECT_EQ(value, 7);
308 
309   // Check a couple of more values.
310   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
311       base::Time::FromTimeT(omaha_epoch + 10 * 24 * 3600), &value));
312   EXPECT_EQ(value, 7);
313   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
314       base::Time::FromTimeT(omaha_epoch + 20 * 24 * 3600), &value));
315   EXPECT_EQ(value, 14);
316   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
317       base::Time::FromTimeT(omaha_epoch + 26 * 24 * 3600), &value));
318   EXPECT_EQ(value, 21);
319   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
320       base::Time::FromTimeT(omaha_epoch + 29 * 24 * 3600), &value));
321   EXPECT_EQ(value, 28);
322 
323   // The date Jun 4, 2007 0:00 PDT is a Monday and is hence a point
324   // where the Omaha InstallDate jumps 7 days. Its unix time is
325   // 1180940400. Notably, this is a point in time where Daylight
326   // Savings Time (DST) was is in effect (e.g. it's PDT, not PST).
327   //
328   // Note that as utils::ConvertToOmahaInstallDate() _deliberately_
329   // ignores DST (as it's hard to implement in a thread-safe way using
330   // glibc, see comments in utils.h) we have to fudge by the DST
331   // offset which is one hour. Conveniently, if the function were
332   // someday modified to be DST aware, this test would have to be
333   // modified as well.
334   const time_t dst_time = 1180940400;  // Jun 4, 2007 0:00 PDT.
335   const time_t fudge = 3600;
336   int value1, value2;
337   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
338       base::Time::FromTimeT(dst_time + fudge - 1), &value1));
339   EXPECT_TRUE(utils::ConvertToOmahaInstallDate(
340       base::Time::FromTimeT(dst_time + fudge), &value2));
341   EXPECT_EQ(value1, value2 - 7);
342 }
343 
TEST(UtilsTest,GetMinorVersion)344 TEST(UtilsTest, GetMinorVersion) {
345   // Test GetMinorVersion by verifying that it parses the conf file and returns
346   // the correct value.
347   uint32_t minor_version;
348 
349   brillo::KeyValueStore store;
350   EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
351 
352   EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=one-two-three\n"));
353   EXPECT_FALSE(utils::GetMinorVersion(store, &minor_version));
354 
355   EXPECT_TRUE(store.LoadFromString("PAYLOAD_MINOR_VERSION=123\n"));
356   EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
357   EXPECT_EQ(123U, minor_version);
358 }
359 
BoolMacroTestHelper()360 static bool BoolMacroTestHelper() {
361   int i = 1;
362   unsigned int ui = 1;
363   bool b = 1;
364   std::unique_ptr<char> cptr(new char);
365 
366   TEST_AND_RETURN_FALSE(i);
367   TEST_AND_RETURN_FALSE(ui);
368   TEST_AND_RETURN_FALSE(b);
369   TEST_AND_RETURN_FALSE(cptr);
370 
371   TEST_AND_RETURN_FALSE_ERRNO(i);
372   TEST_AND_RETURN_FALSE_ERRNO(ui);
373   TEST_AND_RETURN_FALSE_ERRNO(b);
374   TEST_AND_RETURN_FALSE_ERRNO(cptr);
375 
376   return true;
377 }
378 
VoidMacroTestHelper(bool * ret)379 static void VoidMacroTestHelper(bool* ret) {
380   int i = 1;
381   unsigned int ui = 1;
382   bool b = 1;
383   std::unique_ptr<char> cptr(new char);
384 
385   *ret = false;
386 
387   TEST_AND_RETURN(i);
388   TEST_AND_RETURN(ui);
389   TEST_AND_RETURN(b);
390   TEST_AND_RETURN(cptr);
391 
392   TEST_AND_RETURN_ERRNO(i);
393   TEST_AND_RETURN_ERRNO(ui);
394   TEST_AND_RETURN_ERRNO(b);
395   TEST_AND_RETURN_ERRNO(cptr);
396 
397   *ret = true;
398 }
399 
ExpectParseRollbackKeyVersion(const string & version,uint16_t expected_high,uint16_t expected_low)400 static void ExpectParseRollbackKeyVersion(const string& version,
401                                           uint16_t expected_high,
402                                           uint16_t expected_low) {
403   uint16_t actual_high;
404   uint16_t actual_low;
405   utils::ParseRollbackKeyVersion(version, &actual_high, &actual_low);
406   EXPECT_EQ(expected_high, actual_high);
407   EXPECT_EQ(expected_low, actual_low);
408 }
409 
ExpectInvalidParseRollbackKeyVersion(const string & version)410 static void ExpectInvalidParseRollbackKeyVersion(const string& version) {
411   ExpectParseRollbackKeyVersion(version,
412                                 numeric_limits<uint16_t>::max(),
413                                 numeric_limits<uint16_t>::max());
414 }
415 
TEST(UtilsTest,TestMacros)416 TEST(UtilsTest, TestMacros) {
417   bool void_test = false;
418   VoidMacroTestHelper(&void_test);
419   EXPECT_TRUE(void_test);
420 
421   EXPECT_TRUE(BoolMacroTestHelper());
422 }
423 
TEST(UtilsTest,RunAsRootUnmountFilesystemFailureTest)424 TEST(UtilsTest, RunAsRootUnmountFilesystemFailureTest) {
425   EXPECT_FALSE(utils::UnmountFilesystem("/path/to/non-existing-dir"));
426 }
427 
TEST(UtilsTest,RunAsRootUnmountFilesystemBusyFailureTest)428 TEST(UtilsTest, RunAsRootUnmountFilesystemBusyFailureTest) {
429   test_utils::ScopedTempFile tmp_image("img.XXXXXX");
430 
431   EXPECT_TRUE(base::CopyFile(
432       test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
433       base::FilePath(tmp_image.path())));
434 
435   base::ScopedTempDir mnt_dir;
436   EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
437 
438   string loop_dev;
439   test_utils::ScopedLoopbackDeviceBinder loop_binder(
440       tmp_image.path(), true, &loop_dev);
441 
442   EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
443   // This is the actual test part. While we hold a file descriptor open for the
444   // mounted filesystem, umount should still succeed.
445   EXPECT_TRUE(utils::MountFilesystem(
446       loop_dev, mnt_dir.GetPath().value(), MS_RDONLY, "ext4", ""));
447   // Verify the directory is a mount point now.
448   EXPECT_TRUE(utils::IsMountpoint(mnt_dir.GetPath().value()));
449 
450   string target_file = mnt_dir.GetPath().Append("empty-file").value();
451   int fd = HANDLE_EINTR(open(target_file.c_str(), O_RDONLY));
452   EXPECT_GE(fd, 0);
453   EXPECT_TRUE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
454   // The filesystem should be already unmounted at this point.
455   EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
456   IGNORE_EINTR(close(fd));
457   // The filesystem was already unmounted so this call should fail.
458   EXPECT_FALSE(utils::UnmountFilesystem(mnt_dir.GetPath().value()));
459 }
460 
TEST(UtilsTest,IsMountpointTest)461 TEST(UtilsTest, IsMountpointTest) {
462   EXPECT_TRUE(utils::IsMountpoint("/"));
463   EXPECT_FALSE(utils::IsMountpoint("/path/to/nowhere"));
464 
465   base::ScopedTempDir mnt_dir;
466   EXPECT_TRUE(mnt_dir.CreateUniqueTempDir());
467   EXPECT_FALSE(utils::IsMountpoint(mnt_dir.GetPath().value()));
468 
469   test_utils::ScopedTempFile file;
470   EXPECT_FALSE(utils::IsMountpoint(file.path()));
471 }
472 
TEST(UtilsTest,VersionPrefix)473 TEST(UtilsTest, VersionPrefix) {
474   EXPECT_EQ(10575, utils::VersionPrefix("10575.39."));
475   EXPECT_EQ(10575, utils::VersionPrefix("10575.39"));
476   EXPECT_EQ(10575, utils::VersionPrefix("10575.x"));
477   EXPECT_EQ(10575, utils::VersionPrefix("10575."));
478   EXPECT_EQ(10575, utils::VersionPrefix("10575"));
479   EXPECT_EQ(0, utils::VersionPrefix(""));
480   EXPECT_EQ(-1, utils::VersionPrefix("x"));
481   EXPECT_EQ(-1, utils::VersionPrefix("1x"));
482   EXPECT_EQ(-1, utils::VersionPrefix("x.1"));
483 }
484 
TEST(UtilsTest,ParseDottedVersion)485 TEST(UtilsTest, ParseDottedVersion) {
486   // Valid case.
487   ExpectParseRollbackKeyVersion("2.3", 2, 3);
488   ExpectParseRollbackKeyVersion("65535.65535", 65535, 65535);
489 
490   // Zero is technically allowed but never actually used.
491   ExpectParseRollbackKeyVersion("0.0", 0, 0);
492 
493   // Invalid cases.
494   ExpectInvalidParseRollbackKeyVersion("");
495   ExpectInvalidParseRollbackKeyVersion("2");
496   ExpectInvalidParseRollbackKeyVersion("2.");
497   ExpectInvalidParseRollbackKeyVersion(".2");
498   ExpectInvalidParseRollbackKeyVersion("2.2.");
499   ExpectInvalidParseRollbackKeyVersion("2.2.3");
500   ExpectInvalidParseRollbackKeyVersion(".2.2");
501   ExpectInvalidParseRollbackKeyVersion("a.b");
502   ExpectInvalidParseRollbackKeyVersion("1.b");
503   ExpectInvalidParseRollbackKeyVersion("a.2");
504   ExpectInvalidParseRollbackKeyVersion("65536.65536");
505   ExpectInvalidParseRollbackKeyVersion("99999.99999");
506   ExpectInvalidParseRollbackKeyVersion("99999.1");
507   ExpectInvalidParseRollbackKeyVersion("1.99999");
508 }
509 
510 }  // namespace chromeos_update_engine
511