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   EXPECT_FALSE(CreateFileSystemURL(
     88       "filesystem:http://c.org/temporaryfoo/file").is_valid());
     89 }
     90 
     91 TEST(FileSystemURLTest, RejectMalformedURL) {
     92   EXPECT_FALSE(CreateFileSystemURL("filesystem:///foobar/file").is_valid());
     93   EXPECT_FALSE(CreateFileSystemURL("filesystem:foobar/file").is_valid());
     94 }
     95 
     96 TEST(FileSystemURLTest, CompareURLs) {
     97   const GURL urls[] = {
     98       GURL("filesystem:http://chromium.org/temporary/dir a/file a"),
     99       GURL("filesystem:http://chromium.org/temporary/dir a/file a"),
    100       GURL("filesystem:http://chromium.org/temporary/dir a/file b"),
    101       GURL("filesystem:http://chromium.org/temporary/dir a/file aa"),
    102       GURL("filesystem:http://chromium.org/temporary/dir b/file a"),
    103       GURL("filesystem:http://chromium.org/temporary/dir aa/file b"),
    104       GURL("filesystem:http://chromium.com/temporary/dir a/file a"),
    105       GURL("filesystem:https://chromium.org/temporary/dir a/file a")
    106   };
    107 
    108   FileSystemURL::Comparator compare;
    109   for (size_t i = 0; i < arraysize(urls); ++i) {
    110     for (size_t j = 0; j < arraysize(urls); ++j) {
    111       SCOPED_TRACE(testing::Message() << i << " < " << j);
    112       EXPECT_EQ(urls[i] < urls[j],
    113                 compare(FileSystemURL::CreateForTest(urls[i]),
    114                         FileSystemURL::CreateForTest(urls[j])));
    115     }
    116   }
    117 
    118   const FileSystemURL a = CreateFileSystemURL(
    119       "filesystem:http://chromium.org/temporary/dir a/file a");
    120   const FileSystemURL b = CreateFileSystemURL(
    121       "filesystem:http://chromium.org/persistent/dir a/file a");
    122   EXPECT_EQ(a.type() < b.type(), compare(a, b));
    123   EXPECT_EQ(b.type() < a.type(), compare(b, a));
    124 }
    125 
    126 TEST(FileSystemURLTest, IsParent) {
    127   const std::string root1 = GetFileSystemRootURI(
    128       GURL("http://example.com"), kFileSystemTypeTemporary).spec();
    129   const std::string root2 = GetFileSystemRootURI(
    130       GURL("http://example.com"), kFileSystemTypePersistent).spec();
    131   const std::string root3 = GetFileSystemRootURI(
    132       GURL("http://chromium.org"), kFileSystemTypeTemporary).spec();
    133 
    134   const std::string parent("dir");
    135   const std::string child("dir/child");
    136   const std::string other("other");
    137 
    138   // True cases.
    139   EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent(
    140       CreateFileSystemURL(root1 + child)));
    141   EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent(
    142       CreateFileSystemURL(root2 + child)));
    143 
    144   // False cases: the path is not a child.
    145   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    146       CreateFileSystemURL(root1 + other)));
    147   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    148       CreateFileSystemURL(root1 + parent)));
    149   EXPECT_FALSE(CreateFileSystemURL(root1 + child).IsParent(
    150       CreateFileSystemURL(root1 + parent)));
    151 
    152   // False case: different types.
    153   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    154       CreateFileSystemURL(root2 + child)));
    155 
    156   // False case: different origins.
    157   EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent(
    158       CreateFileSystemURL(root3 + child)));
    159 }
    160 
    161 TEST(FileSystemURLTest, ToGURL) {
    162   EXPECT_TRUE(FileSystemURL().ToGURL().is_empty());
    163   const char* kTestURL[] = {
    164     "filesystem:http://chromium.org/persistent/directory/file0",
    165     "filesystem:http://chromium.org/temporary/directory/file1",
    166     "filesystem:http://chromium.org/isolated/directory/file2",
    167     "filesystem:http://chromium.org/external/directory/file2",
    168     "filesystem:http://chromium.org/test/directory/file3",
    169   };
    170 
    171   for (size_t i = 0; i < arraysize(kTestURL); ++i) {
    172     EXPECT_EQ(
    173         kTestURL[i],
    174         FileSystemURL::CreateForTest(GURL(kTestURL[i])).ToGURL().spec());
    175   }
    176 }
    177 
    178 TEST(FileSystemURLTest, DebugString) {
    179   const GURL kOrigin("http://example.com");
    180   const base::FilePath kPath(FPL("dir/file"));
    181 
    182   const FileSystemURL kURL1 = FileSystemURL::CreateForTest(
    183       kOrigin, kFileSystemTypeTemporary, kPath);
    184   EXPECT_EQ("filesystem:http://example.com/temporary/" +
    185             NormalizedUTF8Path(kPath),
    186             kURL1.DebugString());
    187 }
    188 
    189 TEST(FileSystemURLTest, IsInSameFileSystem) {
    190   FileSystemURL url_foo_temp_a = FileSystemURL::CreateForTest(
    191       GURL("http://foo"), kFileSystemTypeTemporary,
    192       base::FilePath::FromUTF8Unsafe("a"));
    193   FileSystemURL url_foo_temp_b = FileSystemURL::CreateForTest(
    194       GURL("http://foo"), kFileSystemTypeTemporary,
    195       base::FilePath::FromUTF8Unsafe("b"));
    196   FileSystemURL url_foo_perm_a = FileSystemURL::CreateForTest(
    197       GURL("http://foo"), kFileSystemTypePersistent,
    198       base::FilePath::FromUTF8Unsafe("a"));
    199   FileSystemURL url_bar_temp_a = FileSystemURL::CreateForTest(
    200       GURL("http://bar"), kFileSystemTypeTemporary,
    201       base::FilePath::FromUTF8Unsafe("a"));
    202   FileSystemURL url_bar_perm_a = FileSystemURL::CreateForTest(
    203       GURL("http://bar"), kFileSystemTypePersistent,
    204       base::FilePath::FromUTF8Unsafe("a"));
    205 
    206   EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_a));
    207   EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_b));
    208   EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_foo_perm_a));
    209   EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_temp_a));
    210   EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_perm_a));
    211 }
    212 
    213 }  // namespace fileapi
    214