root/webkit/common/fileapi/file_system_util_unittest.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. TEST_F
  2. TEST_F
  3. TEST_F
  4. TEST_F
  5. TEST_F
  6. TEST_F
  7. TEST_F
  8. TEST_F
  9. TEST_F
  10. TEST_F
  11. TEST_F
  12. TEST_F
  13. TEST_F

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/common/fileapi/file_system_util.h"

#include "base/files/file_path.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace fileapi {
namespace {

class FileSystemUtilTest : public testing::Test {};

TEST_F(FileSystemUtilTest, ParseFileSystemSchemeURL) {
  GURL uri("filesystem:http://chromium.org/temporary/foo/bar");
  GURL origin_url;
  FileSystemType type;
  base::FilePath virtual_path;
  ParseFileSystemSchemeURL(uri, &origin_url, &type, &virtual_path);
  EXPECT_EQ(GURL("http://chromium.org"), origin_url);
  EXPECT_EQ(fileapi::kFileSystemTypeTemporary, type);
#if defined(FILE_PATH_USES_WIN_SEPARATORS)
  base::FilePath expected_path(FILE_PATH_LITERAL("foo\\bar"));
#else
  base::FilePath expected_path(FILE_PATH_LITERAL("foo/bar"));
#endif
  EXPECT_EQ(expected_path, virtual_path);
}

TEST_F(FileSystemUtilTest, GetTempFileSystemRootURI) {
  GURL origin_url("http://chromium.org");
  fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary;
  GURL uri = GURL("filesystem:http://chromium.org/temporary/");
  EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
}

TEST_F(FileSystemUtilTest, GetPersistentFileSystemRootURI) {
  GURL origin_url("http://chromium.org");
  fileapi::FileSystemType type = fileapi::kFileSystemTypePersistent;
  GURL uri = GURL("filesystem:http://chromium.org/persistent/");
  EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
}

TEST_F(FileSystemUtilTest, VirtualPathBaseName) {
  struct test_data {
    const base::FilePath::StringType path;
    const base::FilePath::StringType base_name;
  } test_cases[] = {
    { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("b:bar") },
    { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("") },
    { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
    { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("bar") },
    { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL("bar") }
  };
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
    base::FilePath input = base::FilePath(test_cases[i].path);
    base::FilePath base_name = VirtualPath::BaseName(input);
    EXPECT_EQ(test_cases[i].base_name, base_name.value());
  }
}

TEST_F(FileSystemUtilTest, VirtualPathDirName) {
  struct test_data {
    const base::FilePath::StringType path;
    const base::FilePath::StringType dir_name;
  } test_cases[] = {
    { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".") },
    { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
    { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("/") },
    { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL(".") },
    { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL(".") },
    { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/") },
    { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("/") },
    { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL(".") },
    { FILE_PATH_LITERAL("c:bar"), FILE_PATH_LITERAL(".") },
#ifdef FILE_PATH_USES_WIN_SEPARATORS
    { FILE_PATH_LITERAL("foo\\bar"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("foo\\b:bar"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("\\") },
    { FILE_PATH_LITERAL("foo\\\\\\\\\\\\bar"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("foo\\bar\\"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("foo\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("foo") },
    { FILE_PATH_LITERAL("\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("\\") },
    { FILE_PATH_LITERAL("bar\\\\\\\\\\"), FILE_PATH_LITERAL(".") },
    { FILE_PATH_LITERAL("bar\\"), FILE_PATH_LITERAL(".") },
    { FILE_PATH_LITERAL("\\bar"), FILE_PATH_LITERAL("\\") },
    { FILE_PATH_LITERAL("\\\\\\\\bar"), FILE_PATH_LITERAL("\\") },
#endif
  };
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
    base::FilePath input = base::FilePath(test_cases[i].path);
    base::FilePath dir_name = VirtualPath::DirName(input);
    EXPECT_EQ(test_cases[i].dir_name, dir_name.value());
  }
}

TEST_F(FileSystemUtilTest, GetNormalizedFilePath) {
  struct test_data {
    const base::FilePath::StringType path;
    const base::FilePath::StringType normalized_path;
  } test_cases[] = {
    { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("/") },
    { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
    { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
    { FILE_PATH_LITERAL("/foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
#if defined(FILE_PATH_USES_WIN_SEPARATORS)
    { FILE_PATH_LITERAL("\\foo"), FILE_PATH_LITERAL("/foo") },
#endif
  };
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
    base::FilePath input = base::FilePath(test_cases[i].path);
    base::FilePath::StringType normalized_path_string =
        VirtualPath::GetNormalizedFilePath(input);
    EXPECT_EQ(test_cases[i].normalized_path, normalized_path_string);
  }
}

TEST_F(FileSystemUtilTest, IsAbsolutePath) {
  EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/")));
  EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/foo/bar")));
  EXPECT_FALSE(VirtualPath::IsAbsolute(base::FilePath::StringType()));
  EXPECT_FALSE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("foo/bar")));
}

TEST_F(FileSystemUtilTest, IsRootPath) {
  EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL(""))));
  EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath()));
  EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("/"))));
  EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("//"))));
  EXPECT_FALSE(VirtualPath::IsRootPath(
      base::FilePath(FILE_PATH_LITERAL("c:/"))));
#if defined(FILE_PATH_USES_WIN_SEPARATORS)
  EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("\\"))));
  EXPECT_FALSE(VirtualPath::IsRootPath(
      base::FilePath(FILE_PATH_LITERAL("c:\\"))));
#endif
}

TEST_F(FileSystemUtilTest, VirtualPathGetComponents) {
  struct test_data {
    const base::FilePath::StringType path;
    size_t count;
    const base::FilePath::StringType components[3];
  } test_cases[] = {
    { FILE_PATH_LITERAL("foo/bar"),
      2,
      { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
    { FILE_PATH_LITERAL("foo"),
      1,
      { FILE_PATH_LITERAL("foo") } },
    { FILE_PATH_LITERAL("foo////bar"),
      2,
      { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
    { FILE_PATH_LITERAL("foo/c:bar"),
      2,
      { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("c:bar") } },
    { FILE_PATH_LITERAL("c:foo/bar"),
      2,
      { FILE_PATH_LITERAL("c:foo"), FILE_PATH_LITERAL("bar") } },
    { FILE_PATH_LITERAL("foo/bar"),
      2,
      { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
    { FILE_PATH_LITERAL("/foo/bar"),
      2,
      { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
    { FILE_PATH_LITERAL("c:/bar"),
      2,
      { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
#ifdef FILE_PATH_USES_WIN_SEPARATORS
    { FILE_PATH_LITERAL("c:\\bar"),
      2,
      { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
#endif
  };
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
    base::FilePath input = base::FilePath(test_cases[i].path);
    std::vector<base::FilePath::StringType> components;
    VirtualPath::GetComponents(input, &components);
    EXPECT_EQ(test_cases[i].count, components.size());
    for (size_t j = 0; j < components.size(); ++j)
      EXPECT_EQ(test_cases[i].components[j], components[j]);
  }
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
    base::FilePath input = base::FilePath(test_cases[i].path);
    std::vector<std::string> components;
    VirtualPath::GetComponentsUTF8Unsafe(input, &components);
    EXPECT_EQ(test_cases[i].count, components.size());
    for (size_t j = 0; j < components.size(); ++j) {
      EXPECT_EQ(base::FilePath(test_cases[i].components[j]).AsUTF8Unsafe(),
                components[j]);
    }
  }
}

TEST_F(FileSystemUtilTest, GetIsolatedFileSystemName) {
  GURL origin_url("http://foo");
  std::string fsname1 = GetIsolatedFileSystemName(origin_url, "bar");
  EXPECT_EQ("http_foo_0:Isolated_bar", fsname1);
}

TEST_F(FileSystemUtilTest, CrackIsolatedFileSystemName) {
  std::string fsid;
  EXPECT_TRUE(CrackIsolatedFileSystemName("foo:Isolated_bar", &fsid));
  EXPECT_EQ("bar", fsid);
  EXPECT_TRUE(CrackIsolatedFileSystemName("foo:isolated_bar", &fsid));
  EXPECT_EQ("bar", fsid);
  EXPECT_TRUE(CrackIsolatedFileSystemName("foo:Isolated__bar", &fsid));
  EXPECT_EQ("_bar", fsid);
  EXPECT_TRUE(CrackIsolatedFileSystemName("foo::Isolated_bar", &fsid));
  EXPECT_EQ("bar", fsid);
}

TEST_F(FileSystemUtilTest, RejectBadIsolatedFileSystemName) {
  std::string fsid;
  EXPECT_FALSE(CrackIsolatedFileSystemName("foobar", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName("foo:_bar", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolatedbar", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName("fooIsolatedbar", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Persistent", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Temporary", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName("foo:External", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName(":Isolated_bar", &fsid));
  EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolated_", &fsid));
}

TEST_F(FileSystemUtilTest, ValidateIsolatedFileSystemId) {
  EXPECT_TRUE(ValidateIsolatedFileSystemId("ABCDEF0123456789ABCDEF0123456789"));
  EXPECT_TRUE(ValidateIsolatedFileSystemId("ABCDEFABCDEFABCDEFABCDEFABCDEFAB"));
  EXPECT_TRUE(ValidateIsolatedFileSystemId("01234567890123456789012345678901"));

  const size_t kExpectedFileSystemIdSize = 32;

  // Should not contain lowercase characters.
  const std::string kLowercaseId = "abcdef0123456789abcdef0123456789";
  EXPECT_EQ(kExpectedFileSystemIdSize, kLowercaseId.size());
  EXPECT_FALSE(ValidateIsolatedFileSystemId(kLowercaseId));

  // Should not be shorter/longer than expected.
  EXPECT_FALSE(ValidateIsolatedFileSystemId(std::string()));

  const std::string kShorterId = "ABCDEF0123456789ABCDEF";
  EXPECT_GT(kExpectedFileSystemIdSize, kShorterId.size());
  EXPECT_FALSE(ValidateIsolatedFileSystemId(kShorterId));

  const std::string kLongerId = "ABCDEF0123456789ABCDEF0123456789ABCDEF";
  EXPECT_LT(kExpectedFileSystemIdSize, kLongerId.size());
  EXPECT_FALSE(ValidateIsolatedFileSystemId(kLongerId));

  // Should not contain not alphabetical nor numerical characters.
  const std::string kSlashId = "ABCD/EFGH/IJKL/MNOP/QRST/UVWX/YZ";
  EXPECT_EQ(kExpectedFileSystemIdSize, kSlashId.size());
  EXPECT_FALSE(ValidateIsolatedFileSystemId(kSlashId));

  const std::string kBackslashId = "ABCD\\EFGH\\IJKL\\MNOP\\QRST\\UVWX\\YZ";
  EXPECT_EQ(kExpectedFileSystemIdSize, kBackslashId.size());
  EXPECT_FALSE(ValidateIsolatedFileSystemId(kBackslashId));

  const std::string kSpaceId = "ABCD EFGH IJKL MNOP QRST UVWX YZ";
  EXPECT_EQ(kExpectedFileSystemIdSize, kSpaceId.size());
  EXPECT_FALSE(ValidateIsolatedFileSystemId(kSpaceId));
}

}  // namespace
}  // namespace fileapi

/* [<][>][^][v][top][bottom][index][help] */