summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/file_system_util_unittest.cc
blob: e08fec79bf998e521db81dcae9546bb2d4761c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2011 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/fileapi/file_system_util.h"

#include "base/file_path.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/fileapi/file_system_types.h"

namespace fileapi {
namespace {

class FileSystemUtilTest : public testing::Test {
 protected:
  bool CrackFileSystemURL(const char* url) {
    return fileapi::CrackFileSystemURL(
        GURL(url), &origin_url_, &type_, &file_path_);
  }

  GURL origin_url_;
  FileSystemType type_;
  FilePath file_path_;
};

TEST_F(FileSystemUtilTest, ParsePersistent) {
  ASSERT_TRUE(CrackFileSystemURL(
      "filesystem:http://chromium.org/persistent/directory/file"));
  EXPECT_EQ("http://chromium.org/", origin_url_.spec());
  EXPECT_EQ(kFileSystemTypePersistent, type_);
  EXPECT_EQ(FILE_PATH_LITERAL("directory/file"), file_path_.value());
}

TEST_F(FileSystemUtilTest, ParseTemporary) {
  ASSERT_TRUE(CrackFileSystemURL(
      "filesystem:http://chromium.org/temporary/directory/file"));
  EXPECT_EQ("http://chromium.org/", origin_url_.spec());
  EXPECT_EQ(kFileSystemTypeTemporary, type_);
  EXPECT_EQ(FILE_PATH_LITERAL("directory/file"), file_path_.value());
}

TEST_F(FileSystemUtilTest, EnsureFilePathIsRelative) {
  ASSERT_TRUE(CrackFileSystemURL(
      "filesystem:http://chromium.org/temporary/////directory/file"));
  EXPECT_EQ("http://chromium.org/", origin_url_.spec());
  EXPECT_EQ(kFileSystemTypeTemporary, type_);
  EXPECT_EQ(FILE_PATH_LITERAL("directory/file"), file_path_.value());
  EXPECT_FALSE(file_path_.IsAbsolute());
}

TEST_F(FileSystemUtilTest, RejectBadSchemes) {
  EXPECT_FALSE(CrackFileSystemURL("http://chromium.org/"));
  EXPECT_FALSE(CrackFileSystemURL("https://chromium.org/"));
  EXPECT_FALSE(CrackFileSystemURL("file:///foo/bar"));
  EXPECT_FALSE(CrackFileSystemURL("foobar:///foo/bar"));
}

TEST_F(FileSystemUtilTest, UnescapePath) {
  ASSERT_TRUE(CrackFileSystemURL(
      "filesystem:http://chromium.org/persistent/%7Echromium/space%20bar"));
  EXPECT_EQ(FILE_PATH_LITERAL("~chromium/space bar"), file_path_.value());
}

TEST_F(FileSystemUtilTest, RejectBadType) {
  EXPECT_FALSE(CrackFileSystemURL("filesystem:http://c.org/foobar/file"));
}

TEST_F(FileSystemUtilTest, RejectMalformedURL) {
  EXPECT_FALSE(CrackFileSystemURL("filesystem:///foobar/file"));
  EXPECT_FALSE(CrackFileSystemURL("filesystem:foobar/file"));
}

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));
}

}  // namespace (anonymous)
}  // namespace fileapi