Home | History | Annotate | Download | only in fileapi
      1 // Copyright (c) 2012 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 "webkit/browser/fileapi/file_system_url.h"
      6 
      7 #include "base/files/file_path.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 #include "url/gurl.h"
     10 #include "webkit/common/fileapi/file_system_types.h"
     11 #include "webkit/common/fileapi/file_system_util.h"
     12 
     13 #define FPL FILE_PATH_LITERAL
     14 
     15 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
     16 #define DRIVE FPL("C:")
     17 #else
     18 #define DRIVE FPL("/a/")
     19 #endif
     20 
     21 namespace fileapi {
     22 
     23 namespace {
     24 
     25 FileSystemURL CreateFileSystemURL(const std::string& url_string) {
     26   FileSystemURL url = FileSystemURL::CreateForTest(GURL(url_string));
     27   EXPECT_TRUE(url.type() != kFileSystemTypeExternal &&
     28               url.type() != kFileSystemTypeIsolated);
     29   return url;
     30 }
     31 
     32 std::string NormalizedUTF8Path(const base::FilePath& path) {
     33   return path.NormalizePathSeparators().AsUTF8Unsafe();
     34 }
     35 
     36 }  // namespace
     37 
     38 TEST(FileSystemURLTest, ParsePersistent) {
     39   FileSystemURL url = CreateFileSystemURL(
     40      "filesystem:http://chromium.org/persistent/directory/file");
     41   ASSERT_TRUE(url.is_valid());
     42   EXPECT_EQ("http://chromium.org/", url.origin().spec());
     43   EXPECT_EQ(kFileSystemTypePersistent, url.type());
     44   EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value());
     45   EXPECT_EQ(FPL("directory"), url.path().DirName().value());
     46 }
     47 
     48 TEST(FileSystemURLTest, ParseTemporary) {
     49   FileSystemURL url = CreateFileSystemURL(
     50       "filesystem:http://chromium.org/temporary/directory/file");
     51   ASSERT_TRUE(url.is_valid());
     52   EXPECT_EQ("http://chromium.org/", url.origin().spec());
     53   EXPECT_EQ(kFileSystemTypeTemporary, url.type());
     54   EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value());
     55   EXPECT_EQ(FPL("directory"), url.path().DirName().value());
     56 }
     57 
     58 TEST(FileSystemURLTest, EnsureFilePathIsRelative) {
     59   FileSystemURL url = CreateFileSystemURL(
     60       "filesystem:http://chromium.org/temporary/////directory/file");
     61   ASSERT_TRUE(url.is_valid());
     62   EXPECT_EQ("http://chromium.org/", url.origin().spec());
     63   EXPECT_EQ(kFileSystemTypeTemporary, url.type());
     64   EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value());
     65   EXPECT_EQ(FPL("directory"), url.path().DirName().value());
     66   EXPECT_FALSE(url.path().IsAbsolute());
     67 }
     68 
     69 TEST(FileSystemURLTest, RejectBadSchemes) {
     70   EXPECT_FALSE(CreateFileSystemURL("http://chromium.org/").is_valid());
     71   EXPECT_FALSE(CreateFileSystemURL("https://chromium.org/").is_valid());
     72   EXPECT_FALSE(CreateFileSystemURL("file:///foo/bar").is_valid());
     73   EXPECT_FALSE(CreateFileSystemURL("foobar:///foo/bar").is_valid());
     74 }
     75 
     76 TEST(FileSystemURLTest, UnescapePath) {
     77   FileSystemURL url = CreateFileSystemURL(
     78       "filesystem:http://chromium.org/persistent/%7Echromium/space%20bar");
     79   ASSERT_TRUE(url.is_valid());
     80   EXPECT_EQ(FPL("space bar"), VirtualPath::BaseName(url.path()).value());
     81   EXPECT_EQ(FPL("~chromium"), url.path().DirName().value());
     82 }
     83 
     84 TEST(FileSystemURLTest, RejectBadType) {
     85   EXPECT_FALSE(CreateFileSystemURL(
     86       "filesystem:http://c.org/foobar/file").is_valid());
     87 }
     88 
     89 TEST(FileSystemURLTest, RejectMalformedURL) {
     90   EXPECT_FALSE(CreateFileSystemURL("filesystem:///foobar/file").is_valid());
     91   EXPECT_FALSE(CreateFileSystemURL("filesystem:foobar/file").is_valid());
     92 }
     93 
     94 TEST(FileSystemURLTest, CompareURLs) {
     95   const GURL urls[] = {
     96       GURL("filesystem:http://chromium.org/temporary/dir a/file a"),
     97       GURL("filesystem:http://chromium.org/temporary/dir a/file a"),
     98       GURL("filesystem:http://chromium.org/temporary/dir a/file b"),
     99       GURL("filesystem:http://chromium.org/temporary/dir a/file aa"),
    100       GURL("filesystem:http://chromium.org/temporary/dir b/file a"),
    101       GURL("filesystem:http://chromium.org/temporary/dir aa/file b"),
    102       GURL("filesystem:http://chromium.com/temporary/dir a/file a"),
    103       GURL("filesystem:https://chromium.org/temporary/dir a/file a")
    104   };
    105 
    106   FileSystemURL::Comparator compare;
    107   for (size_t i = 0; i < arraysize(urls); ++i) {
    108     for (size_t j = 0; j < arraysize(urls); ++j) {
    109       SCOPED_TRACE(testing::Message() << i << " < " << j);
    110       EXPECT_EQ(urls[i] < urls[j],
    111                 compare(FileSystemURL::CreateForTest(urls[i]),
    112                         FileSystemURL::CreateForTest(urls[j])));
    113     }
    114   }
    115 
    116   const FileSystemURL a = CreateFileSystemURL(
    117       "filesystem:http://chromium.org/temporary/dir a/file a");
    118   const FileSystemURL b = CreateFileSystemURL(
    119       "filesystem:http://chromium.org/persistent/dir a/file a");
    120   EXPECT_EQ(a.type() < b.type(), compare(a, b));
    121   EXPECT_EQ(b.type() < a.type(), compare(b, a));
    122 }
    123 
    124 TEST(FileSystemURLTest, IsParent) {
    125   const std::string root1 = GetFileSystemRootURI(
    126       GURL("http://example.com"), kFileSystemTypeTemporary).spec();
    127   const std::string root2 = GetFileSystemRootURI(
    128       GURL("http://example.com"), kFileSystemTypePersistent).spec();
    129   const std::string root3 = GetFileSystemRootURI(
    130       GURL("http://chromium.org"), kFileSystemTypeTemporary).spec();
    131 
    132   const std::string parent("dir");
    133   const std::string child("dir/child");
    134   const std::string other("other");
    135 
    136   // True cases.
    137   EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent(
    138       CreateFileSystemURL(root1 + child)));
    139   EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent(
    140       CreateFileSystemURL(root2 + child)));
    141 
    142   // False cases: the path is not a child.
    143   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    144       CreateFileSystemURL(root1 + other)));
    145   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    146       CreateFileSystemURL(root1 + parent)));
    147   EXPECT_FALSE(CreateFileSystemURL(root1 + child).IsParent(
    148       CreateFileSystemURL(root1 + parent)));
    149 
    150   // False case: different types.
    151   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    152       CreateFileSystemURL(root2 + child)));
    153 
    154   // False case: different origins.
    155   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    156       CreateFileSystemURL(root3 + child)));
    157 }
    158 
    159 TEST(FileSystemURLTest, DebugString) {
    160   const GURL kOrigin("http://example.com");
    161   const base::FilePath kPath(FPL("dir/file"));
    162 
    163   const FileSystemURL kURL1 = FileSystemURL::CreateForTest(
    164       kOrigin, kFileSystemTypeTemporary, kPath);
    165   EXPECT_EQ("filesystem:http://example.com/temporary/" +
    166             NormalizedUTF8Path(kPath),
    167             kURL1.DebugString());
    168 }
    169 
    170 TEST(FileSystemURLTest, IsInSameFileSystem) {
    171   FileSystemURL url_foo_temp_a = FileSystemURL::CreateForTest(
    172       GURL("http://foo"), kFileSystemTypeTemporary,
    173       base::FilePath::FromUTF8Unsafe("a"));
    174   FileSystemURL url_foo_temp_b = FileSystemURL::CreateForTest(
    175       GURL("http://foo"), kFileSystemTypeTemporary,
    176       base::FilePath::FromUTF8Unsafe("b"));
    177   FileSystemURL url_foo_perm_a = FileSystemURL::CreateForTest(
    178       GURL("http://foo"), kFileSystemTypePersistent,
    179       base::FilePath::FromUTF8Unsafe("a"));
    180   FileSystemURL url_bar_temp_a = FileSystemURL::CreateForTest(
    181       GURL("http://bar"), kFileSystemTypeTemporary,
    182       base::FilePath::FromUTF8Unsafe("a"));
    183   FileSystemURL url_bar_perm_a = FileSystemURL::CreateForTest(
    184       GURL("http://bar"), kFileSystemTypePersistent,
    185       base::FilePath::FromUTF8Unsafe("a"));
    186 
    187   EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_a));
    188   EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_b));
    189   EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_foo_perm_a));
    190   EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_temp_a));
    191   EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_perm_a));
    192 }
    193 
    194 }  // namespace fileapi
    195