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