Revert "Only store leading 13 bits of password hash."
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / file_system_provider / thumbnail / test.js
blob087de223fa5d25866d90e3b16e75085ea3dc6ac6
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.
5 'use strict';
7 /**
8 * @type {Object}
9 * @const
11 var TESTING_ROOT = Object.freeze({
12 isDirectory: true,
13 name: '',
14 size: 0,
15 modificationTime: new Date(2013, 3, 27, 9, 38, 14)
16 });
18 /**
19 * @type {Object}
20 * @const
22 var TESTING_WITH_VALID_THUMBNAIL_FILE = Object.freeze({
23 isDirectory: false,
24 name: 'valid-thumbnail.txt',
25 size: 4096,
26 modificationTime: new Date(2014, 4, 28, 10, 39, 15),
27 thumbnail: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' +
28 'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' +
29 '9TXL0Y4OHwAAAABJRU5ErkJggg=='
30 });
32 /**
33 * @type {Object}
34 * @const
36 var TESTING_ALWAYS_WITH_THUMBNAIL_FILE = Object.freeze({
37 isDirectory: false,
38 name: 'always-with-thumbnail.txt',
39 size: 4096,
40 modificationTime: new Date(2014, 4, 28, 10, 39, 15),
41 thumbnail: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' +
42 'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' +
43 '9TXL0Y4OHwAAAABJRU5ErkJggg=='
44 });
46 /**
47 * @type {Object}
48 * @const
50 var TESTING_WITH_INVALID_THUMBNAIL_FILE = Object.freeze({
51 isDirectory: false,
52 name: 'invalid-thumbnail.txt',
53 size: 4096,
54 modificationTime: new Date(2014, 4, 28, 10, 39, 15),
55 thumbnail: 'https://www.foobar.com/evil'
56 });
58 /**
59 * Returns metadata for a requested entry.
61 * @param {GetMetadataRequestedOptions} options Options.
62 * @param {function(Object)} onSuccess Success callback with metadata passed
63 * an argument.
64 * @param {function(string)} onError Error callback with an error code.
66 function onGetMetadataRequested(options, onSuccess, onError) {
67 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
68 onError('SECURITY'); // enum ProviderError.
69 return;
72 // Metadata to be returned.
73 var metadata;
75 switch (options.entryPath) {
76 case '/':
77 metadata = TESTING_ROOT;
78 break;
80 case '/' + TESTING_WITH_VALID_THUMBNAIL_FILE.name:
81 metadata = TESTING_WITH_VALID_THUMBNAIL_FILE;
82 break;
84 case '/' + TESTING_ALWAYS_WITH_THUMBNAIL_FILE.name:
85 metadata = TESTING_ALWAYS_WITH_THUMBNAIL_FILE;
86 break;
88 case '/' + TESTING_WITH_INVALID_THUMBNAIL_FILE.name:
89 metadata = TESTING_WITH_INVALID_THUMBNAIL_FILE;
90 break;
92 default:
93 onError('NOT_FOUND'); // enum ProviderError.
94 return;
97 // Returning a thumbnail while not requested is not allowed for performance
98 // reasons. Remove the field if needed. However, do not remove it for one
99 // file, to simulate an error.
100 if (!options.thumbnail && metadata.thumbnail &&
101 options.entryPath != '/' + TESTING_ALWAYS_WITH_THUMBNAIL_FILE.name) {
102 var metadataWithoutThumbnail = {
103 isDirectory: metadata.isDirectory,
104 name: metadata.name,
105 size: metadata.size,
106 modificationTime: metadata.modificationTime
108 onSuccess(metadataWithoutThumbnail);
109 } else {
110 onSuccess(metadata);
115 * Sets up the tests. Called once per all test cases. In case of a failure,
116 * the callback is not called.
118 * @param {function()} callback Success callback.
120 function setUp(callback) {
121 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
122 onGetMetadataRequested);
123 test_util.mountFileSystem(callback);
127 * Runs all of the test cases, one by one.
129 function runTests() {
130 chrome.test.runTests([
131 // Test if providers are notified that no thumbnail is requested when normal
132 // metadata is requested.
133 function notRequestedAndNotProvidedThumbnailSuccess() {
134 test_util.fileSystem.root.getFile(
135 TESTING_WITH_VALID_THUMBNAIL_FILE.name,
136 {create: false},
137 chrome.test.callbackPass(),
138 function(error) {
139 chrome.test.fail(error.name);
143 // If providers return a thumbnail data despite not being requested for
144 // that, then the operation must fail.
145 function notRequestedButProvidedThumbnailError() {
146 test_util.fileSystem.root.getFile(
147 TESTING_ALWAYS_WITH_THUMBNAIL_FILE.name,
148 {create: false},
149 function(fileEntry) {
150 chrome.test.fail(
151 'Thumbnail returned when not requested should result in an ' +
152 'error, but the operation succeeded.');
153 }, chrome.test.callbackPass(function(error) {
154 chrome.test.assertEq('InvalidStateError', error.name);
155 }));
158 // Thumbnails should be returned when available for private API request.
159 function getEntryPropertiesWithThumbnailSuccess() {
160 test_util.fileSystem.root.getFile(
161 TESTING_WITH_VALID_THUMBNAIL_FILE.name,
162 {create: false},
163 chrome.test.callbackPass(function(fileEntry) {
164 chrome.fileManagerPrivate.getEntryProperties(
165 [fileEntry.toURL()],
166 chrome.test.callbackPass(function(fileProperties) {
167 chrome.test.assertEq(1, fileProperties.length);
168 chrome.test.assertEq(
169 TESTING_WITH_VALID_THUMBNAIL_FILE.thumbnail,
170 fileProperties[0].thumbnailUrl);
171 chrome.test.assertEq(
172 TESTING_WITH_VALID_THUMBNAIL_FILE.fileSize,
173 fileProperties[0].size);
174 chrome.test.assertEq(
175 TESTING_WITH_VALID_THUMBNAIL_FILE.modificationTime,
176 new Date(fileProperties[0].lastModifiedTime));
177 }));
179 function(error) {
180 chrome.test.fail(error.name);
184 // Confirm that extensions are not able to pass an invalid thumbnail url,
185 // including evil urls.
186 function getEntryPropertiesWithInvalidThumbnail() {
187 test_util.fileSystem.root.getFile(
188 TESTING_WITH_INVALID_THUMBNAIL_FILE.name,
189 {create: false},
190 chrome.test.callbackPass(function(fileEntry) {
191 chrome.fileManagerPrivate.getEntryProperties(
192 [fileEntry.toURL()],
193 chrome.test.callbackPass(function(fileProperties) {
194 chrome.test.assertEq(1, fileProperties.length);
195 // The results for an entry is an empty dictionary in case of
196 // an error.
197 chrome.test.assertEq(
198 0, Object.keys(fileProperties[0]).length);
199 }));
201 function(error) {
202 chrome.test.fail(error.name);
208 // Setup and run all of the test cases.
209 setUp(runTests);