summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/http/tests/filesystem/script-tests/resolve-uri.js
blob: 66a7064554eb03198ad3c00cd02f2ccdf1411207 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
if (this.importScripts) {
    importScripts('../resources/fs-worker-common.js');
    importScripts('../resources/fs-test-util.js');
}

description("Tests using webkitResolveLocalFileSystemURL to obtain an Entry from a URL");

var testFileName = '/testFile';
var fileSystem = null;
var expectedPath = null;
var actualPath = null;
var errorCode = null;

function errorCallback(error) {
    if (error && error.code)
        debug("Error occurred: " + error.code);
    testFailed("Bailing out early");
    finishJSTest();
}

function expectSecurityErrAndRunNext(error) {
    errorCode = error.code;
    shouldBe("FileError.SECURITY_ERR", "errorCode");
    cleanupAndRunNext();
}

function expectEncodingErrAndRunNext(error) {
    errorCode = error.code;
    shouldBe("FileError.ENCODING_ERR", "errorCode");
    cleanupAndRunNext();
}

function createTestFile(callback) {
    fileSystem.root.getFile(testFileName, {create:true}, callback, errorCallback);
}

function assertPathsMatch(expected, actual) {
    expectedPath = expected;
    actualPath = actual;
    shouldBe("expectedPath", "actualPath");
}

var isDirectory = false;
function assertIsDirectory(entry) {
    isDirectory = entry.isDirectory;
    shouldBeTrue("isDirectory");
}

var isFile = false;
function assertIsFile(entry) {
    isFile = entry.isFile;
    shouldBeTrue("isFile");
}

// Test body functions ----------------------------------------------------

function runBasicTest() {
    debug("* Resolving a generated URL.");
    createTestFile(function(entry) {
        webkitResolveLocalFileSystemURL(entry.toURL(), function(e) {
            assertPathsMatch(entry.fullPath, e.fullPath);
            assertIsFile(e);
            cleanupAndRunNext();
        }, errorCallback);
    });
}

function runHandmadeURL() {
    debug("* Resolving test file by hand");
    createTestFile(function(entry) {
        webkitResolveLocalFileSystemURL("filesystem:http://127.0.0.1:8000/temporary/testFile", function(e) {
            assertPathsMatch(testFileName, e.fullPath);
            assertIsFile(e);
            cleanupAndRunNext();
        }, errorCallback);
    });
}

function runWrongDomain() {
    debug("* Resolving a URL with the wrong security origin (domain)");
    webkitResolveLocalFileSystemURL("filesystem:http://localhost:8000/temporary/foo", errorCallback, expectSecurityErrAndRunNext);
}

function runWrongPort() {
    debug("* Resolving a URL with the wrong security origin (port)");
    webkitResolveLocalFileSystemURL("filesystem:http://127.0.0.1:8080/temporary/foo", errorCallback, expectSecurityErrAndRunNext);
}

function runWrongScheme() {
    debug("* Resolving a URL with the wrong security origin (scheme)");
    webkitResolveLocalFileSystemURL("filesystem:https://127.0.0.1:8000/temporary/foo", errorCallback, expectSecurityErrAndRunNext);
}

function runBogusURL() {
    debug("* Resolving a completely bogus URL.");
    webkitResolveLocalFileSystemURL("foo", errorCallback, expectEncodingErrAndRunNext);
}

function runWrongProtocol() {
    debug("* Resolving a URL with the wrong protocol");
    webkitResolveLocalFileSystemURL("http://127.0.0.1:8000/foo/bar/baz", errorCallback, expectEncodingErrAndRunNext);
}

function runNotEnoughSlashes() {
    debug("* Resolving a URL with no slash between type and file");
    createTestFile(function(entry) {
        webkitResolveLocalFileSystemURL("filesystem:http://127.0.0.1:8000/temporarytestFile", errorCallback, expectEncodingErrAndRunNext);
    });
}

function runNotEnoughSlashes2() {
    debug("* Resolving a URL with no slash between protocol and type (bogus port)");
    createTestFile(function(entry) {
        webkitResolveLocalFileSystemURL("filesystem:http://127.0.0.1:8000temporary/testFile", errorCallback, expectSecurityErrAndRunNext);
    });
}

function runUseBackSlashes() {
    debug("* Resolve a path using backslashes");
    fileSystem.root.getDirectory("foo", {create:true}, function(entry) {
        entry.getFile("testFile", {create:true}, function(f) {
            webkitResolveLocalFileSystemURL("filesystem:http://127.0.0.1:8000/temporary/foo\\testFile", function(e) {
                assertPathsMatch("/foo/testFile", e.fullPath);
                assertIsFile(e);
                cleanupAndRunNext();
            }, errorCallback);
        }, errorCallback);
    }, errorCallback);
}

function runDirectory() {
    debug("* Resolve a directory");
    fileSystem.root.getDirectory("foo", {create:true}, function(entry) {
        webkitResolveLocalFileSystemURL("filesystem:http://127.0.0.1:8000/temporary/foo", function(e) {
            assertPathsMatch("/foo", e.fullPath);
            assertIsDirectory(e);
            cleanupAndRunNext();
        }, errorCallback);
    }, errorCallback);
}

function runWithTrailingSlash() {
    debug("* Resolve a path using a trailing slash");
    fileSystem.root.getDirectory("foo", {create:true}, function(entry) {
        webkitResolveLocalFileSystemURL("filesystem:http://127.0.0.1:8000/temporary/foo/", function(e) {
            assertPathsMatch("/foo", e.fullPath);
            assertIsDirectory(e);
            cleanupAndRunNext();
        }, errorCallback);
    }, errorCallback);
}

function runPersistentTest() {
    debug("* Resolving a persistent URL.");
    webkitRequestFileSystem(PERSISTENT, 100, function(fs) {
        webkitResolveLocalFileSystemURL(fs.root.toURL(), function(entry) {
            assertPathsMatch("/", entry.fullPath);
            assertIsDirectory(entry);
            cleanupAndRunNext();
        }, errorCallback);
    });
}

// End of test body functions ---------------------------------------------

var testsList = [
    runBasicTest,
    runHandmadeURL,
    runWrongDomain,
    runWrongPort,
    runWrongScheme,
    runBogusURL,
    runWrongProtocol,
    runNotEnoughSlashes,
    runNotEnoughSlashes2,
    runUseBackSlashes,
    runDirectory,
    runWithTrailingSlash,
    runPersistentTest,
];
var testCounter = 0;

function runNextTest() {
    if (testCounter == testsList.length) {
        debug("Finished running tests.");
        finishJSTest();
    } else
        testsList[testCounter++]();
}

function cleanupAndRunNext() {
    removeAllInDirectory(fileSystem.root, runNextTest, runNextTest);
}

function fileSystemCallback(fs) {
    fileSystem = fs;
    cleanupAndRunNext();
}

if (this.webkitRequestFileSystem) {
    jsTestIsAsync = true;
    webkitRequestFileSystem(this.TEMPORARY, 100, fileSystemCallback, errorCallback);
} else
    debug("This test requires FileSystem API support.");

var successfullyParsed = true;