adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 8 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 9 | #include <vector> |
| 10 | #include <gtest/gtest.h> |
| 11 | #include "update_engine/utils.h" |
| 12 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 13 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 14 | using std::vector; |
| 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
| 18 | class UtilsTest : public ::testing::Test { }; |
| 19 | |
| 20 | TEST(UtilsTest, NormalizePathTest) { |
| 21 | EXPECT_EQ("", utils::NormalizePath("", false)); |
| 22 | EXPECT_EQ("", utils::NormalizePath("", true)); |
| 23 | EXPECT_EQ("/", utils::NormalizePath("/", false)); |
| 24 | EXPECT_EQ("", utils::NormalizePath("/", true)); |
| 25 | EXPECT_EQ("/", utils::NormalizePath("//", false)); |
| 26 | EXPECT_EQ("", utils::NormalizePath("//", true)); |
| 27 | EXPECT_EQ("foo", utils::NormalizePath("foo", false)); |
| 28 | EXPECT_EQ("foo", utils::NormalizePath("foo", true)); |
| 29 | EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false)); |
| 30 | EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true)); |
| 31 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 32 | false)); |
| 33 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 34 | true)); |
| 35 | EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 36 | false)); |
| 37 | EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 38 | true)); |
| 39 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false)); |
| 40 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true)); |
| 41 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", false)); |
| 42 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", true)); |
| 43 | EXPECT_EQ("/spaces s/ ok/s / / /", |
| 44 | utils::NormalizePath("/spaces s/ ok/s / / /", false)); |
| 45 | EXPECT_EQ("/spaces s/ ok/s / / ", |
| 46 | utils::NormalizePath("/spaces s/ ok/s / / /", true)); |
| 47 | } |
| 48 | |
| 49 | TEST(UtilsTest, ReadFileFailure) { |
| 50 | vector<char> empty; |
| 51 | EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty)); |
| 52 | } |
| 53 | |
| 54 | TEST(UtilsTest, ErrnoNumberAsStringTest) { |
| 55 | EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT)); |
| 56 | } |
| 57 | |
| 58 | TEST(UtilsTest, StringHasSuffixTest) { |
| 59 | EXPECT_TRUE(utils::StringHasSuffix("foo", "foo")); |
| 60 | EXPECT_TRUE(utils::StringHasSuffix("foo", "o")); |
| 61 | EXPECT_TRUE(utils::StringHasSuffix("", "")); |
| 62 | EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc")); |
| 63 | EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere")); |
| 64 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh")); |
| 65 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "")); |
| 66 | EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo")); |
| 67 | EXPECT_FALSE(utils::StringHasSuffix("", "x")); |
| 68 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg")); |
| 69 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab")); |
| 70 | } |
| 71 | |
| 72 | TEST(UtilsTest, StringHasPrefixTest) { |
| 73 | EXPECT_TRUE(utils::StringHasPrefix("foo", "foo")); |
| 74 | EXPECT_TRUE(utils::StringHasPrefix("foo", "f")); |
| 75 | EXPECT_TRUE(utils::StringHasPrefix("", "")); |
| 76 | EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc")); |
| 77 | EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl")); |
| 78 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab")); |
| 79 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "")); |
| 80 | EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa")); |
| 81 | EXPECT_FALSE(utils::StringHasPrefix("", "x")); |
| 82 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc")); |
| 83 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh")); |
| 84 | } |
| 85 | |
| 86 | TEST(UtilsTest, BootDeviceTest) { |
| 87 | // Pretty lame test... |
| 88 | EXPECT_FALSE(utils::BootDevice().empty()); |
| 89 | } |
| 90 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 91 | TEST(UtilsTest, BootKernelDeviceTest) { |
| 92 | EXPECT_EQ("", utils::BootKernelDevice("foo")); |
| 93 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda0")); |
| 94 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda1")); |
| 95 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda2")); |
| 96 | EXPECT_EQ("/dev/sda2", utils::BootKernelDevice("/dev/sda3")); |
| 97 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda4")); |
| 98 | EXPECT_EQ("/dev/sda4", utils::BootKernelDevice("/dev/sda5")); |
| 99 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda6")); |
| 100 | EXPECT_EQ("/dev/sda6", utils::BootKernelDevice("/dev/sda7")); |
| 101 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda8")); |
| 102 | EXPECT_EQ("", utils::BootKernelDevice("/dev/sda9")); |
| 103 | } |
| 104 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 105 | TEST(UtilsTest, RecursiveUnlinkDirTest) { |
| 106 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-a", 0755)); |
| 107 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-b", 0755)); |
| 108 | EXPECT_EQ(0, symlink("../RecursiveUnlinkDirTest-a", |
| 109 | "RecursiveUnlinkDirTest-b/link")); |
| 110 | EXPECT_EQ(0, system("echo hi > RecursiveUnlinkDirTest-b/file")); |
| 111 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-b/dir", 0755)); |
| 112 | EXPECT_EQ(0, system("echo ok > RecursiveUnlinkDirTest-b/dir/subfile")); |
| 113 | EXPECT_TRUE(utils::RecursiveUnlinkDir("RecursiveUnlinkDirTest-b")); |
| 114 | EXPECT_TRUE(utils::FileExists("RecursiveUnlinkDirTest-a")); |
| 115 | EXPECT_EQ(0, system("rm -rf RecursiveUnlinkDirTest-a")); |
| 116 | EXPECT_FALSE(utils::FileExists("RecursiveUnlinkDirTest-b")); |
| 117 | EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist")); |
| 118 | } |
| 119 | |
| 120 | TEST(UtilsTest, TempFilenameTest) { |
| 121 | const string original = "/foo.XXXXXX"; |
| 122 | const string result = utils::TempFilename(original); |
| 123 | EXPECT_EQ(original.size(), result.size()); |
| 124 | EXPECT_TRUE(utils::StringHasPrefix(result, "/foo.")); |
| 125 | EXPECT_FALSE(utils::StringHasSuffix(result, "XXXXXX")); |
| 126 | } |
| 127 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame^] | 128 | TEST(UtilsTest, RootDeviceTest) { |
| 129 | EXPECT_EQ("/dev/sda", utils::RootDevice("/dev/sda3")); |
| 130 | EXPECT_EQ("/dev/mmc0", utils::RootDevice("/dev/mmc0p3")); |
| 131 | } |
| 132 | |
| 133 | TEST(UtilsTest, PartitionNumberTest) { |
| 134 | EXPECT_EQ("3", utils::PartitionNumber("/dev/sda3")); |
| 135 | EXPECT_EQ("3", utils::PartitionNumber("/dev/mmc0p3")); |
| 136 | } |
| 137 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 138 | } // namespace chromeos_update_engine |