Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <errno.h> |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 8 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 9 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include <vector> |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 11 | |
| 12 | #include "gtest/gtest.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include "update_engine/utils.h" |
| 14 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 15 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | using std::vector; |
| 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
| 20 | class UtilsTest : public ::testing::Test { }; |
| 21 | |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 22 | TEST(UtilsTest, IsOfficialBuild) { |
| 23 | // Pretty lame test... |
| 24 | EXPECT_TRUE(utils::IsOfficialBuild()); |
| 25 | } |
| 26 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | TEST(UtilsTest, NormalizePathTest) { |
| 28 | EXPECT_EQ("", utils::NormalizePath("", false)); |
| 29 | EXPECT_EQ("", utils::NormalizePath("", true)); |
| 30 | EXPECT_EQ("/", utils::NormalizePath("/", false)); |
| 31 | EXPECT_EQ("", utils::NormalizePath("/", true)); |
| 32 | EXPECT_EQ("/", utils::NormalizePath("//", false)); |
| 33 | EXPECT_EQ("", utils::NormalizePath("//", true)); |
| 34 | EXPECT_EQ("foo", utils::NormalizePath("foo", false)); |
| 35 | EXPECT_EQ("foo", utils::NormalizePath("foo", true)); |
| 36 | EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false)); |
| 37 | EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true)); |
| 38 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 39 | false)); |
| 40 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 41 | true)); |
| 42 | EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 43 | false)); |
| 44 | EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 45 | true)); |
| 46 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false)); |
| 47 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true)); |
| 48 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", false)); |
| 49 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", true)); |
| 50 | EXPECT_EQ("/spaces s/ ok/s / / /", |
| 51 | utils::NormalizePath("/spaces s/ ok/s / / /", false)); |
| 52 | EXPECT_EQ("/spaces s/ ok/s / / ", |
| 53 | utils::NormalizePath("/spaces s/ ok/s / / /", true)); |
| 54 | } |
| 55 | |
| 56 | TEST(UtilsTest, ReadFileFailure) { |
| 57 | vector<char> empty; |
| 58 | EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty)); |
| 59 | } |
| 60 | |
| 61 | TEST(UtilsTest, ErrnoNumberAsStringTest) { |
| 62 | EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT)); |
| 63 | } |
| 64 | |
| 65 | TEST(UtilsTest, StringHasSuffixTest) { |
| 66 | EXPECT_TRUE(utils::StringHasSuffix("foo", "foo")); |
| 67 | EXPECT_TRUE(utils::StringHasSuffix("foo", "o")); |
| 68 | EXPECT_TRUE(utils::StringHasSuffix("", "")); |
| 69 | EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc")); |
| 70 | EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere")); |
| 71 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh")); |
| 72 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "")); |
| 73 | EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo")); |
| 74 | EXPECT_FALSE(utils::StringHasSuffix("", "x")); |
| 75 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg")); |
| 76 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab")); |
| 77 | } |
| 78 | |
| 79 | TEST(UtilsTest, StringHasPrefixTest) { |
| 80 | EXPECT_TRUE(utils::StringHasPrefix("foo", "foo")); |
| 81 | EXPECT_TRUE(utils::StringHasPrefix("foo", "f")); |
| 82 | EXPECT_TRUE(utils::StringHasPrefix("", "")); |
| 83 | EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc")); |
| 84 | EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl")); |
| 85 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab")); |
| 86 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "")); |
| 87 | EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa")); |
| 88 | EXPECT_FALSE(utils::StringHasPrefix("", "x")); |
| 89 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc")); |
| 90 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh")); |
| 91 | } |
| 92 | |
| 93 | TEST(UtilsTest, BootDeviceTest) { |
| 94 | // Pretty lame test... |
| 95 | EXPECT_FALSE(utils::BootDevice().empty()); |
| 96 | } |
| 97 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 98 | TEST(UtilsTest, BootKernelDeviceTest) { |
| 99 | EXPECT_EQ("", utils::BootKernelDevice("foo")); |
| 100 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda0")); |
| 101 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda1")); |
| 102 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda2")); |
| 103 | EXPECT_EQ("/dev/sda2", utils::BootKernelDevice("/dev/sda3")); |
| 104 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda4")); |
| 105 | EXPECT_EQ("/dev/sda4", utils::BootKernelDevice("/dev/sda5")); |
| 106 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda6")); |
| 107 | EXPECT_EQ("/dev/sda6", utils::BootKernelDevice("/dev/sda7")); |
| 108 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda8")); |
| 109 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda9")); |
| 110 | } |
| 111 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 112 | TEST(UtilsTest, RecursiveUnlinkDirTest) { |
| 113 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-a", 0755)); |
| 114 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-b", 0755)); |
| 115 | EXPECT_EQ(0, symlink("../RecursiveUnlinkDirTest-a", |
| 116 | "RecursiveUnlinkDirTest-b/link")); |
| 117 | EXPECT_EQ(0, system("echo hi > RecursiveUnlinkDirTest-b/file")); |
| 118 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-b/dir", 0755)); |
| 119 | EXPECT_EQ(0, system("echo ok > RecursiveUnlinkDirTest-b/dir/subfile")); |
| 120 | EXPECT_TRUE(utils::RecursiveUnlinkDir("RecursiveUnlinkDirTest-b")); |
| 121 | EXPECT_TRUE(utils::FileExists("RecursiveUnlinkDirTest-a")); |
| 122 | EXPECT_EQ(0, system("rm -rf RecursiveUnlinkDirTest-a")); |
| 123 | EXPECT_FALSE(utils::FileExists("RecursiveUnlinkDirTest-b")); |
| 124 | EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist")); |
| 125 | } |
| 126 | |
| 127 | TEST(UtilsTest, TempFilenameTest) { |
| 128 | const string original = "/foo.XXXXXX"; |
| 129 | const string result = utils::TempFilename(original); |
| 130 | EXPECT_EQ(original.size(), result.size()); |
| 131 | EXPECT_TRUE(utils::StringHasPrefix(result, "/foo.")); |
| 132 | EXPECT_FALSE(utils::StringHasSuffix(result, "XXXXXX")); |
| 133 | } |
| 134 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 135 | TEST(UtilsTest, RootDeviceTest) { |
| 136 | EXPECT_EQ("/dev/sda", utils::RootDevice("/dev/sda3")); |
| 137 | EXPECT_EQ("/dev/mmc0", utils::RootDevice("/dev/mmc0p3")); |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 138 | EXPECT_EQ("", utils::RootDevice("/dev/foo/bar")); |
| 139 | EXPECT_EQ("", utils::RootDevice("/")); |
| 140 | EXPECT_EQ("", utils::RootDevice("")); |
| 141 | } |
| 142 | |
| 143 | TEST(UtilsTest, SysfsBlockDeviceTest) { |
| 144 | EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda")); |
| 145 | EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda")); |
| 146 | EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar")); |
| 147 | EXPECT_EQ("", utils::SysfsBlockDevice("/")); |
| 148 | EXPECT_EQ("", utils::SysfsBlockDevice("./")); |
| 149 | EXPECT_EQ("", utils::SysfsBlockDevice("")); |
| 150 | } |
| 151 | |
| 152 | TEST(UtilsTest, IsRemovableDeviceTest) { |
| 153 | EXPECT_FALSE(utils::IsRemovableDevice("")); |
| 154 | EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device")); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | TEST(UtilsTest, PartitionNumberTest) { |
| 158 | EXPECT_EQ("3", utils::PartitionNumber("/dev/sda3")); |
| 159 | EXPECT_EQ("3", utils::PartitionNumber("/dev/mmc0p3")); |
| 160 | } |
| 161 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 162 | TEST(UtilsTest, ComparePrioritiesTest) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 163 | EXPECT_LT(utils::ComparePriorities(utils::kProcessPriorityLow, |
| 164 | utils::kProcessPriorityNormal), 0); |
| 165 | EXPECT_GT(utils::ComparePriorities(utils::kProcessPriorityNormal, |
| 166 | utils::kProcessPriorityLow), 0); |
| 167 | EXPECT_EQ(utils::ComparePriorities(utils::kProcessPriorityNormal, |
| 168 | utils::kProcessPriorityNormal), 0); |
| 169 | EXPECT_GT(utils::ComparePriorities(utils::kProcessPriorityHigh, |
| 170 | utils::kProcessPriorityNormal), 0); |
| 171 | } |
| 172 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 173 | TEST(UtilsTest, FuzzIntTest) { |
| 174 | static const unsigned int kRanges[] = { 0, 1, 2, 20 }; |
| 175 | for (size_t r = 0; r < arraysize(kRanges); ++r) { |
| 176 | unsigned int range = kRanges[r]; |
| 177 | const int kValue = 50; |
| 178 | for (int tries = 0; tries < 100; ++tries) { |
| 179 | int value = utils::FuzzInt(kValue, range); |
| 180 | EXPECT_GE(value, kValue - range / 2); |
| 181 | EXPECT_LE(value, kValue + range - range / 2); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 186 | } // namespace chromeos_update_engine |