Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: chrome/test/data/extensions/api_test/downloads/test.js

Issue 10213002: Make downloads.download() respect host permissions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // downloads api test 5 // downloads api test
6 // browser_tests.exe --gtest_filter=DownloadsApiTest.Downloads 6 // browser_tests.exe --gtest_filter=DownloadsApiTest.Downloads
7 7
8 // Comment this out to enable debugging. 8 // Comment this out to enable debugging.
9 console.debug = function() {}; 9 console.debug = function() {};
10 10
11 function debugObject(obj) { 11 function debugObject(obj) {
12 for (var property in obj) { 12 for (var property in obj) {
13 console.debug(property + ': ' + obj[property]); 13 console.debug(property + ': ' + obj[property]);
14 } 14 }
15 } 15 }
16 16
17 var downloads = chrome.experimental.downloads; 17 var downloads = chrome.experimental.downloads;
18 window.requestFileSystem = (window.requestFileSystem ||
19 window.webkitRequestFileSystem);
20 window.BlobBuilder = (window.BlobBuilder ||
21 window.WebKitBlobBuilder);
18 22
19 chrome.test.getConfig(function(testConfig) { 23 chrome.test.getConfig(function(testConfig) {
20 function getURL(path) { 24 function getURL(path) {
21 return 'http://localhost:' + testConfig.testServer.port + '/' + path; 25 return 'http://localhost:' + testConfig.testServer.port + '/' + path;
22 } 26 }
23 27
24 var nextId = 0; 28 var nextId = 0;
25 function getNextId() { 29 function getNextId() {
26 return nextId++; 30 return nextId++;
27 } 31 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 // callbackCompleted(); 91 // callbackCompleted();
88 // } 92 // }
89 // downloads.onChanged.addListener(myListener); 93 // downloads.onChanged.addListener(myListener);
90 // downloads.download( 94 // downloads.download(
91 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, 95 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'},
92 // chrome.test.callback(function(id) { 96 // chrome.test.callback(function(id) {
93 // chrome.test.assertEq(downloadId, id); 97 // chrome.test.assertEq(downloadId, id);
94 // })); 98 // }));
95 // }, 99 // },
96 100
101 function downloadBlob() {
102 // Test that we can begin a download for a blob.
103 var downloadId = getNextId();
104 console.debug(downloadId);
105 function getBlobURL(data, filename, callback) {
106 var dirname = '' + Math.random();
107 function fileSystemError(operation, data) {
108 return function(fileError) {
109 callback(null, {operation: operation,
110 data: data,
111 code: fileError.code});
112 }
113 }
114 window.requestFileSystem(TEMPORARY, 5*1024*1024, function(fs) {
115 fs.root.getDirectory(dirname, {create: true, exclusive: true},
116 function(dirEntry) {
117 dirEntry.getFile(filename, {create: true, exclusive: true},
118 function(fileEntry) {
119 fileEntry.createWriter(function(fileWriter) {
120 fileWriter.onwriteend = function(e) {
121 callback(fileEntry.toURL(), null);
122 };
123 fileWriter.onerror = function(e) {
124 callback(null, ('Write failed: ' + e.toString()));
125 };
126 var bb = new window.BlobBuilder();
127 bb.append(data);
128 fileWriter.write(bb.getBlob());
129 }, fileSystemError('createWriter'));
130 }, fileSystemError('getFile', filename));
131 }, fileSystemError('getDirectory', dirname));
132 }, fileSystemError('requestFileSystem'));
133 }
134
135 getBlobURL('Lorem ipsum', downloadId + '.txt',
136 chrome.test.callback(function(blobUrl, blobError) {
137 if (blobError)
138 throw blobError;
139 console.debug(blobUrl);
140 downloads.download(
141 {'url': blobUrl},
142 chrome.test.callback(function(id) {
143 console.debug(id);
144 chrome.test.assertEq(downloadId, id);
145 }));
146 }));
147 },
148
97 function downloadSimple() { 149 function downloadSimple() {
98 // Test that we can begin a download. 150 // Test that we can begin a download.
99 var downloadId = getNextId(); 151 var downloadId = getNextId();
100 console.debug(downloadId); 152 console.debug(downloadId);
101 downloads.download( 153 downloads.download(
102 {'url': SAFE_FAST_URL}, 154 {'url': SAFE_FAST_URL},
103 chrome.test.callback(function(id) { 155 chrome.test.callback(function(id) {
104 chrome.test.assertEq(downloadId, id); 156 chrome.test.assertEq(downloadId, id);
105 })); 157 }));
106 }, 158 },
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 // Valid file URLs are valid URLs. 979 // Valid file URLs are valid URLs.
928 var downloadId = getNextId(); 980 var downloadId = getNextId();
929 console.debug(downloadId); 981 console.debug(downloadId);
930 downloads.download( 982 downloads.download(
931 {'url': 'file:///'}, 983 {'url': 'file:///'},
932 chrome.test.callback(function(id) { 984 chrome.test.callback(function(id) {
933 chrome.test.assertEq(downloadId, id); 985 chrome.test.assertEq(downloadId, id);
934 })); 986 }));
935 }, 987 },
936 988
937 // TODO(benjhayden): Set up a test ftp server. 989 function downloadInvalidURL7() {
990 // Test that download() rejects javascript urls.
991 downloads.download(
992 {'url': 'javascript:document.write("hello");'},
993 chrome.test.callbackFail(downloads.ERROR_INVALID_URL));
994 },
995
996 function downloadInvalidURL8() {
997 // Test that download() rejects javascript urls.
998 downloads.download(
999 {'url': 'javascript:return false;'},
1000 chrome.test.callbackFail(downloads.ERROR_INVALID_URL));
1001 },
1002
1003 function downloadInvalidURL9() {
1004 // Test that download() rejects otherwise-valid URLs that fail the host
1005 // permissions check.
1006 downloads.download(
1007 {'url': 'ftp://example.com/example.txt'},
1008 chrome.test.callbackFail(downloads.ERROR_INVALID_URL));
1009 },
1010
1011 // TODO(benjhayden): Set up a test ftp server, add ftp://localhost* to
1012 // permissions, maybe update downloadInvalidURL9.
938 // function downloadAllowFTPURLs() { 1013 // function downloadAllowFTPURLs() {
939 // // Valid ftp URLs are valid URLs. 1014 // // Valid ftp URLs are valid URLs.
940 // var downloadId = getNextId(); 1015 // var downloadId = getNextId();
941 // console.debug(downloadId); 1016 // console.debug(downloadId);
942 // downloads.download( 1017 // downloads.download(
943 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'}, 1018 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'},
944 // chrome.test.callback(function(id) { 1019 // chrome.test.callback(function(id) {
945 // chrome.test.assertEq(downloadId, id); 1020 // chrome.test.assertEq(downloadId, id);
946 // })); 1021 // }));
947 // }, 1022 // },
948 1023
949 function downloadInvalidURL7() {
950 // Test that download() rejects javascript urls.
951 downloads.download(
952 {'url': 'javascript:document.write("hello");'},
953 chrome.test.callbackFail('net::ERR_ACCESS_DENIED'));
954 },
955
956 function downloadInvalidURL8() {
957 // Test that download() rejects javascript urls.
958 downloads.download(
959 {'url': 'javascript:return false;'},
960 chrome.test.callbackFail('net::ERR_ACCESS_DENIED'));
961 },
962
963 function downloadInvalidMethod() { 1024 function downloadInvalidMethod() {
964 assertThrows(('Invalid value for argument 1. Property \'method\': ' + 1025 assertThrows(('Invalid value for argument 1. Property \'method\': ' +
965 'Value must be one of: [GET, POST].'), 1026 'Value must be one of: [GET, POST].'),
966 downloads.download, 1027 downloads.download,
967 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); 1028 {'url': SAFE_FAST_URL, 'method': 'GOAT'});
968 }, 1029 },
969 1030
970 function downloadInvalidHeader() { 1031 function downloadInvalidHeader() {
971 // Test that download() disallows setting the Cookie header. 1032 // Test that download() disallows setting the Cookie header.
972 downloads.download( 1033 downloads.download(
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 }, 1122 },
1062 1123
1063 function callNotifyPass() { 1124 function callNotifyPass() {
1064 chrome.test.notifyPass(); 1125 chrome.test.notifyPass();
1065 setTimeout(chrome.test.callback(function() { 1126 setTimeout(chrome.test.callback(function() {
1066 console.debug(''); 1127 console.debug('');
1067 }), 0); 1128 }), 0);
1068 } 1129 }
1069 ]); 1130 ]);
1070 }); 1131 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698