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> |
| 8 | #include <vector> |
| 9 | #include <gtest/gtest.h> |
| 10 | #include "update_engine/utils.h" |
| 11 | |
| 12 | using std::vector; |
| 13 | |
| 14 | namespace chromeos_update_engine { |
| 15 | |
| 16 | class UtilsTest : public ::testing::Test { }; |
| 17 | |
| 18 | TEST(UtilsTest, NormalizePathTest) { |
| 19 | EXPECT_EQ("", utils::NormalizePath("", false)); |
| 20 | EXPECT_EQ("", utils::NormalizePath("", true)); |
| 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("foo", utils::NormalizePath("foo", false)); |
| 26 | EXPECT_EQ("foo", utils::NormalizePath("foo", true)); |
| 27 | EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false)); |
| 28 | EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true)); |
| 29 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 30 | false)); |
| 31 | EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr", |
| 32 | true)); |
| 33 | EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 34 | false)); |
| 35 | EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/", |
| 36 | true)); |
| 37 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false)); |
| 38 | EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true)); |
| 39 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", false)); |
| 40 | EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", true)); |
| 41 | EXPECT_EQ("/spaces s/ ok/s / / /", |
| 42 | utils::NormalizePath("/spaces s/ ok/s / / /", false)); |
| 43 | EXPECT_EQ("/spaces s/ ok/s / / ", |
| 44 | utils::NormalizePath("/spaces s/ ok/s / / /", true)); |
| 45 | } |
| 46 | |
| 47 | TEST(UtilsTest, ReadFileFailure) { |
| 48 | vector<char> empty; |
| 49 | EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty)); |
| 50 | } |
| 51 | |
| 52 | TEST(UtilsTest, ErrnoNumberAsStringTest) { |
| 53 | EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT)); |
| 54 | } |
| 55 | |
| 56 | TEST(UtilsTest, StringHasSuffixTest) { |
| 57 | EXPECT_TRUE(utils::StringHasSuffix("foo", "foo")); |
| 58 | EXPECT_TRUE(utils::StringHasSuffix("foo", "o")); |
| 59 | EXPECT_TRUE(utils::StringHasSuffix("", "")); |
| 60 | EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc")); |
| 61 | EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere")); |
| 62 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh")); |
| 63 | EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "")); |
| 64 | EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo")); |
| 65 | EXPECT_FALSE(utils::StringHasSuffix("", "x")); |
| 66 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg")); |
| 67 | EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab")); |
| 68 | } |
| 69 | |
| 70 | TEST(UtilsTest, StringHasPrefixTest) { |
| 71 | EXPECT_TRUE(utils::StringHasPrefix("foo", "foo")); |
| 72 | EXPECT_TRUE(utils::StringHasPrefix("foo", "f")); |
| 73 | EXPECT_TRUE(utils::StringHasPrefix("", "")); |
| 74 | EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc")); |
| 75 | EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl")); |
| 76 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab")); |
| 77 | EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "")); |
| 78 | EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa")); |
| 79 | EXPECT_FALSE(utils::StringHasPrefix("", "x")); |
| 80 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc")); |
| 81 | EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh")); |
| 82 | } |
| 83 | |
| 84 | TEST(UtilsTest, BootDeviceTest) { |
| 85 | // Pretty lame test... |
| 86 | EXPECT_FALSE(utils::BootDevice().empty()); |
| 87 | } |
| 88 | |
| 89 | TEST(UtilsTest, RecursiveUnlinkDirTest) { |
| 90 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-a", 0755)); |
| 91 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-b", 0755)); |
| 92 | EXPECT_EQ(0, symlink("../RecursiveUnlinkDirTest-a", |
| 93 | "RecursiveUnlinkDirTest-b/link")); |
| 94 | EXPECT_EQ(0, system("echo hi > RecursiveUnlinkDirTest-b/file")); |
| 95 | EXPECT_EQ(0, mkdir("RecursiveUnlinkDirTest-b/dir", 0755)); |
| 96 | EXPECT_EQ(0, system("echo ok > RecursiveUnlinkDirTest-b/dir/subfile")); |
| 97 | EXPECT_TRUE(utils::RecursiveUnlinkDir("RecursiveUnlinkDirTest-b")); |
| 98 | EXPECT_TRUE(utils::FileExists("RecursiveUnlinkDirTest-a")); |
| 99 | EXPECT_EQ(0, system("rm -rf RecursiveUnlinkDirTest-a")); |
| 100 | EXPECT_FALSE(utils::FileExists("RecursiveUnlinkDirTest-b")); |
| 101 | EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist")); |
| 102 | } |
| 103 | |
| 104 | TEST(UtilsTest, TempFilenameTest) { |
| 105 | const string original = "/foo.XXXXXX"; |
| 106 | const string result = utils::TempFilename(original); |
| 107 | EXPECT_EQ(original.size(), result.size()); |
| 108 | EXPECT_TRUE(utils::StringHasPrefix(result, "/foo.")); |
| 109 | EXPECT_FALSE(utils::StringHasSuffix(result, "XXXXXX")); |
| 110 | } |
| 111 | |
| 112 | } // namespace chromeos_update_engine |