Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / evil / test.js
1 // Copyright 2014 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 'use strict';
6
7 /**
8  * Map of opened files, from a <code>openRequestId</code> to <code>filePath
9  * </code>.
10  * @type {Object.<number, string>}
11  */
12 var openedFiles = {};
13
14 /**
15  * Metadata for a testing file.
16  * @type {Object}
17  * @const
18  */
19 var TESTING_TOO_LARGE_CHUNK_FILE = Object.freeze({
20   isDirectory: false,
21   name: 'too-large-chunk.txt',
22   size: 2 * 1024 * 1024,  // 2MB
23   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
24 });
25
26 /**
27  * Metadata for a testing file.
28  * @type {Object}
29  * @const
30  */
31 var TESTING_INVALID_CALLBACK_FILE = Object.freeze({
32   isDirectory: false,
33   name: 'invalid-request.txt',
34   size: 1 * 1024 * 1024,  // 1MB
35   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
36 });
37
38 /**
39  * Metadata for a testing file.
40  * @type {Object}
41  * @const
42  */
43 var TESTING_NEGATIVE_SIZE_FILE = Object.freeze({
44   isDirectory: false,
45   name: 'negative-size.txt',
46   size: -1 * 1024 * 1024,  // -1MB
47   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
48 });
49
50 /**
51  * Metadata for a testing file.
52  * @type {Object}
53  * @const
54  */
55 var TESTING_RELATIVE_NAME_FILE = Object.freeze({
56   isDirectory: false,
57   name: '../../../b.txt',
58   size: 1 * 1024 * 1024,  // 1MB
59   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
60 });
61
62 /**
63  * Requests opening a file at <code>filePath</code>. Further file operations
64  * will be associated with the <code>requestId</code>
65  *
66  * @param {OpenFileRequestedOptions} options Options.
67  * @param {function()} onSuccess Success callback.
68  * @param {function(string)} onError Error callback.
69  */
70 function onOpenFileRequested(options, onSuccess, onError) {
71   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
72     onError('INVALID_OPERATION');  // enum ProviderError.
73     return;
74   }
75
76   if (options.mode != 'READ') {
77     onError('ACCESS_DENIED');  // enum ProviderError.
78     return;
79   }
80
81   if (options.filePath != '/' + TESTING_TOO_LARGE_CHUNK_FILE.name ||
82       options.filePath != '/' + TESTING_INVALID_CALLBACK_FILE.name ||
83       options.filePath != '/' + TESTING_NEGATIVE_SIZE_FILE.name ||
84       options.filePath != '/' + TESTING_RELATIVE_NAME_FILE.name) {
85     onError('NOT_FOUND');  // enum ProviderError.
86     return;
87   }
88
89   openedFiles[options.requestId] = options.filePath;
90   onSuccess();
91 }
92
93 /**
94  * Requests closing a file previously opened with <code>openRequestId</code>.
95  *
96  * @param {CloseFileRequestedOptions} options Options.
97  * @param {function()} onSuccess Success callback.
98  * @param {function(string)} onError Error callback.
99  */
100 function onCloseFileRequested(options, onSuccess, onError) {
101   if (options.fileSystemId != test_util.FILE_SYSTEM_ID ||
102       !openedFiles[options.openRequestId]) {
103     onError('INVALID_OPERATION');  // enum ProviderError.
104     return;
105   }
106
107   delete openedFiles[options.openRequestId];
108   onSuccess();
109 }
110
111 /**
112  * Requests reading contents of a file, previously opened with <code>
113  * openRequestId</code>.
114  *
115  * @param {ReadFileRequestedOptions} options Options.
116  * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a
117  *     chunk of data, and information if more data will be provided later.
118  * @param {function(string)} onError Error callback.
119  */
120 function onReadFileRequested(options, onSuccess, onError) {
121   var filePath = openedFiles[options.openRequestId];
122   if (options.fileSystemId != test_util.FILE_SYSTEM_ID || !filePath) {
123     onError('INVALID_OPERATION');  // enum ProviderError.
124     return;
125   }
126
127   if (filePath == '/' + TESTING_TOO_LARGE_CHUNK_FILE.name) {
128     var buffer = '';
129     while (buffer.length < 4 * TESTING_TOO_LARGE_CHUNK_FILE.size) {
130       buffer += 'I-LIKE-ICE-CREAM!';
131     }
132     var reader = new FileReader();
133     reader.onload = function(e) {
134       onSuccess(e.target.result, true /* hasMore */);
135       onSuccess(e.target.result, true /* hasMore */);
136       onSuccess(e.target.result, true /* hasMore */);
137       onSuccess(e.target.result, false /* hasMore */);
138     };
139     reader.readAsArrayBuffer(new Blob([buffer]));
140     return;
141   }
142
143   if (filePath == '/' + TESTING_INVALID_CALLBACK_FILE.name) {
144     // Calling onSuccess after onError is unexpected. After handling the error
145     // the request should be removed.
146     onError('NOT_FOUND');
147     onSuccess(new ArrayBuffer(options.length * 4), false /* hasMore */);
148     return;
149   }
150
151   if (filePath == '/' + TESTING_NEGATIVE_SIZE_FILE.name) {
152     onSuccess(new ArrayBuffer(-TESTING_NEGATIVE_SIZE_FILE.size * 2),
153               false /* hasMore */);
154     return;
155   }
156
157   if (filePath == '/' + TESTING_RELATIVE_NAME_FILE.name) {
158     onSuccess(new ArrayBuffer(options.length), false /* hasMore */);
159     return;
160   }
161
162   onError('INVALID_OPERATION');  // enum ProviderError.
163 }
164
165 /**
166  * Sets up the tests. Called once per all test cases. In case of a failure,
167  * the callback is not called.
168  *
169  * @param {function()} callback Success callback.
170  */
171 function setUp(callback) {
172   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
173       test_util.onGetMetadataRequestedDefault);
174
175   test_util.defaultMetadata['/' + TESTING_TOO_LARGE_CHUNK_FILE.name] =
176     TESTING_TOO_LARGE_CHUNK_FILE;
177   test_util.defaultMetadata['/' + TESTING_INVALID_CALLBACK_FILE.name] =
178     TESTING_INVALID_CALLBACK_FILE;
179   test_util.defaultMetadata['/' + TESTING_NEGATIVE_SIZE_FILE.name] =
180     TESTING_NEGATIVE_SIZE_FILE;
181   test_util.defaultMetadata['/' + TESTING_RELATIVE_NAME_FILE.name] =
182     TESTING_RELATIVE_NAME_FILE;
183
184   chrome.fileSystemProvider.onOpenFileRequested.addListener(
185       onOpenFileRequested);
186   chrome.fileSystemProvider.onReadFileRequested.addListener(
187       onReadFileRequested);
188   chrome.fileSystemProvider.onCloseFileRequested.addListener(
189       onCloseFileRequested);
190
191   test_util.mountFileSystem(callback);
192 }
193
194 /**
195  * Runs all of the test cases, one by one.
196  */
197 function runTests() {
198   chrome.test.runTests([
199     // Tests that returning a too big chunk (4 times larger than the file size,
200     // and also much more than requested 1 KB of data).
201     function returnTooLargeChunk() {
202       test_util.fileSystem.root.getFile(
203           TESTING_TOO_LARGE_CHUNK_FILE.name,
204           {create: false},
205           chrome.test.callbackPass(function(fileEntry) {
206             fileEntry.file(chrome.test.callbackPass(function(file) {
207               // Read 1 KB of data.
208               var fileSlice = file.slice(0, 1024);
209               var fileReader = new FileReader();
210               fileReader.onload = function(e) {
211                 chrome.test.fail('Reading should fail.');
212               };
213               fileReader.onerror = chrome.test.callbackPass();
214               fileReader.readAsText(fileSlice);
215             }),
216             function(error) {
217               chrome.test.fail(error.name);
218             });
219           }),
220           function(error) {
221             chrome.test.fail(error.name);
222           });
223     },
224
225     // Tests that calling a success callback with a non-existing request id
226     // doesn't cause any harm.
227     function invalidCallback() {
228       test_util.fileSystem.root.getFile(
229           TESTING_INVALID_CALLBACK_FILE.name,
230           {create: false},
231           chrome.test.callbackPass(function(fileEntry) {
232             fileEntry.file(chrome.test.callbackPass(function(file) {
233               // Read 1 KB of data.
234               var fileSlice = file.slice(0, 1024);
235               var fileReader = new FileReader();
236               fileReader.onload = function(e) {
237                 chrome.test.fail('Reading should fail.');
238               };
239               fileReader.onerror = chrome.test.callbackPass();
240               fileReader.readAsText(fileSlice);
241             }),
242             function(error) {
243               chrome.test.fail(error.name);
244             });
245           }),
246           function(error) {
247             chrome.test.fail(error.name);
248           });
249     },
250
251     // Test that reading from files with negative size is not allowed.
252     function negativeSize() {
253       test_util.fileSystem.root.getFile(
254           TESTING_NEGATIVE_SIZE_FILE.name,
255           {create: false},
256           chrome.test.callbackPass(function(fileEntry) {
257             fileEntry.file(chrome.test.callbackPass(function(file) {
258               // Read 1 KB of data.
259               var fileSlice = file.slice(0, 1024);
260               var fileReader = new FileReader();
261               fileReader.onload = chrome.test.callbackPass(function(e) {
262                 var text = fileReader.result;
263                 chrome.test.assertEq(0, text.length);
264               });
265               fileReader.readAsText(fileSlice);
266             }),
267             function(error) {
268               chrome.test.fail(error.name);
269             });
270           }),
271           function(error) {
272             chrome.test.fail(error.name);
273           });
274     },
275
276     // Tests that URLs generated from a file containing .. inside is properly
277     // escaped.
278     function relativeName() {
279       test_util.fileSystem.root.getFile(
280           TESTING_RELATIVE_NAME_FILE.name,
281           {create: false},
282           function(fileEntry) {
283             chrome.test.fail('Opening a file should fail.');
284           },
285           chrome.test.callbackPass(function(error) {
286             chrome.test.assertEq('NotFoundError', error.name);
287           }));
288     }
289   ]);
290 }
291
292 // Setup and run all of the test cases.
293 setUp(runTests);