1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_NetUtil.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,700 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 sts=2 et 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/** 1.11 + * This file tests the methods on NetUtil.jsm. 1.12 + */ 1.13 + 1.14 +Cu.import("resource://testing-common/httpd.js"); 1.15 + 1.16 +Cu.import("resource://gre/modules/NetUtil.jsm"); 1.17 +Cu.import("resource://gre/modules/Task.jsm"); 1.18 +Cu.import("resource://gre/modules/Promise.jsm"); 1.19 + 1.20 +// We need the profile directory so the test harness will clean up our test 1.21 +// files. 1.22 +do_get_profile(); 1.23 + 1.24 +const OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/file-output-stream;1"; 1.25 +const SAFE_OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/safe-file-output-stream;1"; 1.26 + 1.27 +//////////////////////////////////////////////////////////////////////////////// 1.28 +//// Helper Methods 1.29 + 1.30 +/** 1.31 + * Reads the contents of a file and returns it as a string. 1.32 + * 1.33 + * @param aFile 1.34 + * The file to return from. 1.35 + * @return the contents of the file in the form of a string. 1.36 + */ 1.37 +function getFileContents(aFile) 1.38 +{ 1.39 + "use strict"; 1.40 + 1.41 + let fstream = Cc["@mozilla.org/network/file-input-stream;1"]. 1.42 + createInstance(Ci.nsIFileInputStream); 1.43 + fstream.init(aFile, -1, 0, 0); 1.44 + 1.45 + let cstream = Cc["@mozilla.org/intl/converter-input-stream;1"]. 1.46 + createInstance(Ci.nsIConverterInputStream); 1.47 + cstream.init(fstream, "UTF-8", 0, 0); 1.48 + 1.49 + let string = {}; 1.50 + cstream.readString(-1, string); 1.51 + cstream.close(); 1.52 + return string.value; 1.53 +} 1.54 + 1.55 + 1.56 +/** 1.57 + * Tests asynchronously writing a file using NetUtil.asyncCopy. 1.58 + * 1.59 + * @param aContractId 1.60 + * The contract ID to use for the output stream 1.61 + * @param aDeferOpen 1.62 + * Whether to use DEFER_OPEN in the output stream. 1.63 + */ 1.64 +function async_write_file(aContractId, aDeferOpen) 1.65 +{ 1.66 + do_test_pending(); 1.67 + 1.68 + // First, we need an output file to write to. 1.69 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.70 + getService(Ci.nsIProperties). 1.71 + get("ProfD", Ci.nsIFile); 1.72 + file.append("NetUtil-async-test-file.tmp"); 1.73 + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 1.74 + 1.75 + // Then, we need an output stream to our output file. 1.76 + let ostream = Cc[aContractId].createInstance(Ci.nsIFileOutputStream); 1.77 + ostream.init(file, -1, -1, aDeferOpen ? Ci.nsIFileOutputStream.DEFER_OPEN : 0); 1.78 + 1.79 + // Finally, we need an input stream to take data from. 1.80 + const TEST_DATA = "this is a test string"; 1.81 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.82 + createInstance(Ci.nsIStringInputStream); 1.83 + istream.setData(TEST_DATA, TEST_DATA.length); 1.84 + 1.85 + NetUtil.asyncCopy(istream, ostream, function(aResult) { 1.86 + // Make sure the copy was successful! 1.87 + do_check_true(Components.isSuccessCode(aResult)); 1.88 + 1.89 + // Check the file contents. 1.90 + do_check_eq(TEST_DATA, getFileContents(file)); 1.91 + 1.92 + // Finish the test. 1.93 + do_test_finished(); 1.94 + run_next_test(); 1.95 + }); 1.96 +} 1.97 + 1.98 +//////////////////////////////////////////////////////////////////////////////// 1.99 +//// Tests 1.100 + 1.101 +// Test NetUtil.asyncCopy for all possible buffering scenarios 1.102 +function test_async_copy() 1.103 +{ 1.104 + // Create a data sample 1.105 + function make_sample(text) { 1.106 + let data = []; 1.107 + for (let i = 0; i <= 100; ++i) { 1.108 + data.push(text); 1.109 + } 1.110 + return data.join(); 1.111 + } 1.112 + 1.113 + // Create an input buffer holding some data 1.114 + function make_input(isBuffered, data) { 1.115 + if (isBuffered) { 1.116 + // String input streams are buffered 1.117 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.118 + createInstance(Ci.nsIStringInputStream); 1.119 + istream.setData(data, data.length); 1.120 + return istream; 1.121 + } 1.122 + 1.123 + // File input streams are not buffered, so let's create a file 1.124 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.125 + getService(Ci.nsIProperties). 1.126 + get("ProfD", Ci.nsIFile); 1.127 + file.append("NetUtil-asyncFetch-test-file.tmp"); 1.128 + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 1.129 + 1.130 + let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. 1.131 + createInstance(Ci.nsIFileOutputStream); 1.132 + ostream.init(file, -1, -1, 0); 1.133 + ostream.write(data, data.length); 1.134 + ostream.close(); 1.135 + 1.136 + let istream = Cc["@mozilla.org/network/file-input-stream;1"]. 1.137 + createInstance(Ci.nsIFileInputStream); 1.138 + istream.init(file, -1, 0, 0); 1.139 + 1.140 + return istream; 1.141 + } 1.142 + 1.143 + // Create an output buffer holding some data 1.144 + function make_output(isBuffered) { 1.145 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.146 + getService(Ci.nsIProperties). 1.147 + get("ProfD", Ci.nsIFile); 1.148 + file.append("NetUtil-asyncFetch-test-file.tmp"); 1.149 + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 1.150 + 1.151 + let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. 1.152 + createInstance(Ci.nsIFileOutputStream); 1.153 + ostream.init(file, -1, -1, 0); 1.154 + 1.155 + if (!isBuffered) { 1.156 + return {file: file, sink: ostream}; 1.157 + } 1.158 + 1.159 + let bstream = Cc["@mozilla.org/network/buffered-output-stream;1"]. 1.160 + createInstance(Ci.nsIBufferedOutputStream); 1.161 + bstream.init(ostream, 256); 1.162 + return {file: file, sink: bstream}; 1.163 + } 1.164 + Task.spawn(function*() { 1.165 + do_test_pending(); 1.166 + for (let bufferedInput of [true, false]) { 1.167 + for (let bufferedOutput of [true, false]) { 1.168 + let text = "test_async_copy with " 1.169 + + (bufferedInput?"buffered input":"unbuffered input") 1.170 + + ", " 1.171 + + (bufferedOutput?"buffered output":"unbuffered output"); 1.172 + do_print(text); 1.173 + let TEST_DATA = "[" + make_sample(text) + "]"; 1.174 + let source = make_input(bufferedInput, TEST_DATA); 1.175 + let {file, sink} = make_output(bufferedOutput); 1.176 + let deferred = Promise.defer(); 1.177 + NetUtil.asyncCopy(source, sink, deferred.resolve); 1.178 + let result = yield deferred.promise; 1.179 + 1.180 + // Make sure the copy was successful! 1.181 + if (!Components.isSuccessCode(result)) { 1.182 + do_throw(new Components.Exception("asyncCopy error", result)); 1.183 + } 1.184 + 1.185 + // Check the file contents. 1.186 + do_check_eq(TEST_DATA, getFileContents(file)); 1.187 + } 1.188 + } 1.189 + 1.190 + do_test_finished(); 1.191 + run_next_test(); 1.192 + }); 1.193 +} 1.194 + 1.195 +function test_async_write_file() { 1.196 + async_write_file(OUTPUT_STREAM_CONTRACT_ID); 1.197 +} 1.198 + 1.199 +function test_async_write_file_deferred() { 1.200 + async_write_file(OUTPUT_STREAM_CONTRACT_ID, true); 1.201 +} 1.202 + 1.203 +function test_async_write_file_safe() { 1.204 + async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID); 1.205 +} 1.206 + 1.207 +function test_async_write_file_safe_deferred() { 1.208 + async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID, true); 1.209 +} 1.210 + 1.211 +function test_newURI_no_spec_throws() 1.212 +{ 1.213 + try { 1.214 + NetUtil.newURI(); 1.215 + do_throw("should throw!"); 1.216 + } 1.217 + catch (e) { 1.218 + do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); 1.219 + } 1.220 + 1.221 + run_next_test(); 1.222 +} 1.223 + 1.224 +function test_newURI() 1.225 +{ 1.226 + let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); 1.227 + 1.228 + // Check that we get the same URI back from the IO service and the utility 1.229 + // method. 1.230 + const TEST_URI = "http://mozilla.org"; 1.231 + let iosURI = ios.newURI(TEST_URI, null, null); 1.232 + let NetUtilURI = NetUtil.newURI(TEST_URI); 1.233 + do_check_true(iosURI.equals(NetUtilURI)); 1.234 + 1.235 + run_next_test(); 1.236 +} 1.237 + 1.238 +function test_newURI_takes_nsIFile() 1.239 +{ 1.240 + let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); 1.241 + 1.242 + // Create a test file that we can pass into NetUtil.newURI 1.243 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.244 + getService(Ci.nsIProperties). 1.245 + get("ProfD", Ci.nsIFile); 1.246 + file.append("NetUtil-test-file.tmp"); 1.247 + 1.248 + // Check that we get the same URI back from the IO service and the utility 1.249 + // method. 1.250 + let iosURI = ios.newFileURI(file); 1.251 + let NetUtilURI = NetUtil.newURI(file); 1.252 + do_check_true(iosURI.equals(NetUtilURI)); 1.253 + 1.254 + run_next_test(); 1.255 +} 1.256 + 1.257 +function test_ioService() 1.258 +{ 1.259 + do_check_true(NetUtil.ioService instanceof Ci.nsIIOService); 1.260 + run_next_test(); 1.261 +} 1.262 + 1.263 +function test_asyncFetch_no_channel() 1.264 +{ 1.265 + try { 1.266 + NetUtil.asyncFetch(null, function() { }); 1.267 + do_throw("should throw!"); 1.268 + } 1.269 + catch (e) { 1.270 + do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); 1.271 + } 1.272 + 1.273 + run_next_test(); 1.274 +} 1.275 + 1.276 +function test_asyncFetch_no_callback() 1.277 +{ 1.278 + try { 1.279 + NetUtil.asyncFetch({ }); 1.280 + do_throw("should throw!"); 1.281 + } 1.282 + catch (e) { 1.283 + do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); 1.284 + } 1.285 + 1.286 + run_next_test(); 1.287 +} 1.288 + 1.289 +function test_asyncFetch_with_nsIChannel() 1.290 +{ 1.291 + const TEST_DATA = "this is a test string"; 1.292 + 1.293 + // Start the http server, and register our handler. 1.294 + let server = new HttpServer(); 1.295 + server.registerPathHandler("/test", function(aRequest, aResponse) { 1.296 + aResponse.setStatusLine(aRequest.httpVersion, 200, "OK"); 1.297 + aResponse.setHeader("Content-Type", "text/plain", false); 1.298 + aResponse.write(TEST_DATA); 1.299 + }); 1.300 + server.start(-1); 1.301 + 1.302 + // Create our channel. 1.303 + let channel = NetUtil.ioService. 1.304 + newChannel("http://localhost:" + 1.305 + server.identity.primaryPort + "/test", null, null); 1.306 + 1.307 + // Open our channel asynchronously. 1.308 + NetUtil.asyncFetch(channel, function(aInputStream, aResult) { 1.309 + // Check that we had success. 1.310 + do_check_true(Components.isSuccessCode(aResult)); 1.311 + 1.312 + // Check that we got the right data. 1.313 + do_check_eq(aInputStream.available(), TEST_DATA.length); 1.314 + let is = Cc["@mozilla.org/scriptableinputstream;1"]. 1.315 + createInstance(Ci.nsIScriptableInputStream); 1.316 + is.init(aInputStream); 1.317 + let result = is.read(TEST_DATA.length); 1.318 + do_check_eq(TEST_DATA, result); 1.319 + 1.320 + server.stop(run_next_test); 1.321 + }); 1.322 +} 1.323 + 1.324 +function test_asyncFetch_with_nsIURI() 1.325 +{ 1.326 + const TEST_DATA = "this is a test string"; 1.327 + 1.328 + // Start the http server, and register our handler. 1.329 + let server = new HttpServer(); 1.330 + server.registerPathHandler("/test", function(aRequest, aResponse) { 1.331 + aResponse.setStatusLine(aRequest.httpVersion, 200, "OK"); 1.332 + aResponse.setHeader("Content-Type", "text/plain", false); 1.333 + aResponse.write(TEST_DATA); 1.334 + }); 1.335 + server.start(-1); 1.336 + 1.337 + // Create our URI. 1.338 + let uri = NetUtil.newURI("http://localhost:" + 1.339 + server.identity.primaryPort + "/test"); 1.340 + 1.341 + // Open our URI asynchronously. 1.342 + NetUtil.asyncFetch(uri, function(aInputStream, aResult) { 1.343 + // Check that we had success. 1.344 + do_check_true(Components.isSuccessCode(aResult)); 1.345 + 1.346 + // Check that we got the right data. 1.347 + do_check_eq(aInputStream.available(), TEST_DATA.length); 1.348 + let is = Cc["@mozilla.org/scriptableinputstream;1"]. 1.349 + createInstance(Ci.nsIScriptableInputStream); 1.350 + is.init(aInputStream); 1.351 + let result = is.read(TEST_DATA.length); 1.352 + do_check_eq(TEST_DATA, result); 1.353 + 1.354 + server.stop(run_next_test); 1.355 + }); 1.356 +} 1.357 + 1.358 +function test_asyncFetch_with_string() 1.359 +{ 1.360 + const TEST_DATA = "this is a test string"; 1.361 + 1.362 + // Start the http server, and register our handler. 1.363 + let server = new HttpServer(); 1.364 + server.registerPathHandler("/test", function(aRequest, aResponse) { 1.365 + aResponse.setStatusLine(aRequest.httpVersion, 200, "OK"); 1.366 + aResponse.setHeader("Content-Type", "text/plain", false); 1.367 + aResponse.write(TEST_DATA); 1.368 + }); 1.369 + server.start(-1); 1.370 + 1.371 + // Open our location asynchronously. 1.372 + NetUtil.asyncFetch("http://localhost:" + 1.373 + server.identity.primaryPort + "/test", 1.374 + function(aInputStream, aResult) { 1.375 + // Check that we had success. 1.376 + do_check_true(Components.isSuccessCode(aResult)); 1.377 + 1.378 + // Check that we got the right data. 1.379 + do_check_eq(aInputStream.available(), TEST_DATA.length); 1.380 + let is = Cc["@mozilla.org/scriptableinputstream;1"]. 1.381 + createInstance(Ci.nsIScriptableInputStream); 1.382 + is.init(aInputStream); 1.383 + let result = is.read(TEST_DATA.length); 1.384 + do_check_eq(TEST_DATA, result); 1.385 + 1.386 + server.stop(run_next_test); 1.387 + }); 1.388 +} 1.389 + 1.390 +function test_asyncFetch_with_nsIFile() 1.391 +{ 1.392 + const TEST_DATA = "this is a test string"; 1.393 + 1.394 + // First we need a file to read from. 1.395 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.396 + getService(Ci.nsIProperties). 1.397 + get("ProfD", Ci.nsIFile); 1.398 + file.append("NetUtil-asyncFetch-test-file.tmp"); 1.399 + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 1.400 + 1.401 + // Write the test data to the file. 1.402 + let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. 1.403 + createInstance(Ci.nsIFileOutputStream); 1.404 + ostream.init(file, -1, -1, 0); 1.405 + ostream.write(TEST_DATA, TEST_DATA.length); 1.406 + 1.407 + // Sanity check to make sure the data was written. 1.408 + do_check_eq(TEST_DATA, getFileContents(file)); 1.409 + 1.410 + // Open our file asynchronously. 1.411 + NetUtil.asyncFetch(file, function(aInputStream, aResult) { 1.412 + // Check that we had success. 1.413 + do_check_true(Components.isSuccessCode(aResult)); 1.414 + 1.415 + // Check that we got the right data. 1.416 + do_check_eq(aInputStream.available(), TEST_DATA.length); 1.417 + let is = Cc["@mozilla.org/scriptableinputstream;1"]. 1.418 + createInstance(Ci.nsIScriptableInputStream); 1.419 + is.init(aInputStream); 1.420 + let result = is.read(TEST_DATA.length); 1.421 + do_check_eq(TEST_DATA, result); 1.422 + 1.423 + run_next_test(); 1.424 + }); 1.425 +} 1.426 + 1.427 +function test_asyncFetch_with_nsIInputString() 1.428 +{ 1.429 + const TEST_DATA = "this is a test string"; 1.430 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.431 + createInstance(Ci.nsIStringInputStream); 1.432 + istream.setData(TEST_DATA, TEST_DATA.length); 1.433 + 1.434 + // Read the input stream asynchronously. 1.435 + NetUtil.asyncFetch(istream, function(aInputStream, aResult) { 1.436 + // Check that we had success. 1.437 + do_check_true(Components.isSuccessCode(aResult)); 1.438 + 1.439 + // Check that we got the right data. 1.440 + do_check_eq(aInputStream.available(), TEST_DATA.length); 1.441 + do_check_eq(NetUtil.readInputStreamToString(aInputStream, TEST_DATA.length), 1.442 + TEST_DATA); 1.443 + 1.444 + run_next_test(); 1.445 + }); 1.446 +} 1.447 + 1.448 +function test_asyncFetch_does_not_block() 1.449 +{ 1.450 + // Create our channel that has no data. 1.451 + let channel = NetUtil.ioService. 1.452 + newChannel("data:text/plain,", null, null); 1.453 + 1.454 + // Open our channel asynchronously. 1.455 + NetUtil.asyncFetch(channel, function(aInputStream, aResult) { 1.456 + // Check that we had success. 1.457 + do_check_true(Components.isSuccessCode(aResult)); 1.458 + 1.459 + // Check that reading a byte throws that the stream was closed (as opposed 1.460 + // saying it would block). 1.461 + let is = Cc["@mozilla.org/scriptableinputstream;1"]. 1.462 + createInstance(Ci.nsIScriptableInputStream); 1.463 + is.init(aInputStream); 1.464 + try { 1.465 + is.read(1); 1.466 + do_throw("should throw!"); 1.467 + } 1.468 + catch (e) { 1.469 + do_check_eq(e.result, Cr.NS_BASE_STREAM_CLOSED); 1.470 + } 1.471 + 1.472 + run_next_test(); 1.473 + }); 1.474 +} 1.475 + 1.476 +function test_newChannel_no_specifier() 1.477 +{ 1.478 + try { 1.479 + NetUtil.newChannel(); 1.480 + do_throw("should throw!"); 1.481 + } 1.482 + catch (e) { 1.483 + do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); 1.484 + } 1.485 + 1.486 + run_next_test(); 1.487 +} 1.488 + 1.489 +function test_newChannel_with_string() 1.490 +{ 1.491 + const TEST_SPEC = "http://mozilla.org"; 1.492 + 1.493 + // Check that we get the same URI back from channel the IO service creates and 1.494 + // the channel the utility method creates. 1.495 + let ios = NetUtil.ioService; 1.496 + let iosChannel = ios.newChannel(TEST_SPEC, null, null); 1.497 + let NetUtilChannel = NetUtil.newChannel(TEST_SPEC); 1.498 + do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); 1.499 + 1.500 + run_next_test(); 1.501 +} 1.502 + 1.503 +function test_newChannel_with_nsIURI() 1.504 +{ 1.505 + const TEST_SPEC = "http://mozilla.org"; 1.506 + 1.507 + // Check that we get the same URI back from channel the IO service creates and 1.508 + // the channel the utility method creates. 1.509 + let uri = NetUtil.newURI(TEST_SPEC); 1.510 + let iosChannel = NetUtil.ioService.newChannelFromURI(uri); 1.511 + let NetUtilChannel = NetUtil.newChannel(uri); 1.512 + do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); 1.513 + 1.514 + run_next_test(); 1.515 +} 1.516 + 1.517 +function test_newChannel_with_nsIFile() 1.518 +{ 1.519 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.520 + getService(Ci.nsIProperties). 1.521 + get("ProfD", Ci.nsIFile); 1.522 + file.append("NetUtil-test-file.tmp"); 1.523 + 1.524 + // Check that we get the same URI back from channel the IO service creates and 1.525 + // the channel the utility method creates. 1.526 + let uri = NetUtil.newURI(file); 1.527 + let iosChannel = NetUtil.ioService.newChannelFromURI(uri); 1.528 + let NetUtilChannel = NetUtil.newChannel(uri); 1.529 + do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); 1.530 + 1.531 + run_next_test(); 1.532 +} 1.533 + 1.534 +function test_readInputStreamToString() 1.535 +{ 1.536 + const TEST_DATA = "this is a test string\0 with an embedded null"; 1.537 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.538 + createInstance(Ci.nsISupportsCString); 1.539 + istream.data = TEST_DATA; 1.540 + 1.541 + do_check_eq(NetUtil.readInputStreamToString(istream, TEST_DATA.length), 1.542 + TEST_DATA); 1.543 + 1.544 + run_next_test(); 1.545 +} 1.546 + 1.547 +function test_readInputStreamToString_no_input_stream() 1.548 +{ 1.549 + try { 1.550 + NetUtil.readInputStreamToString("hi", 2); 1.551 + do_throw("should throw!"); 1.552 + } 1.553 + catch (e) { 1.554 + do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); 1.555 + } 1.556 + 1.557 + run_next_test(); 1.558 +} 1.559 + 1.560 +function test_readInputStreamToString_no_bytes_arg() 1.561 +{ 1.562 + const TEST_DATA = "this is a test string"; 1.563 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.564 + createInstance(Ci.nsIStringInputStream); 1.565 + istream.setData(TEST_DATA, TEST_DATA.length); 1.566 + 1.567 + try { 1.568 + NetUtil.readInputStreamToString(istream); 1.569 + do_throw("should throw!"); 1.570 + } 1.571 + catch (e) { 1.572 + do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); 1.573 + } 1.574 + 1.575 + run_next_test(); 1.576 +} 1.577 + 1.578 +function test_readInputStreamToString_blocking_stream() 1.579 +{ 1.580 + let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe); 1.581 + pipe.init(true, true, 0, 0, null); 1.582 + 1.583 + try { 1.584 + NetUtil.readInputStreamToString(pipe.inputStream, 10); 1.585 + do_throw("should throw!"); 1.586 + } 1.587 + catch (e) { 1.588 + do_check_eq(e.result, Cr.NS_BASE_STREAM_WOULD_BLOCK); 1.589 + } 1.590 + run_next_test(); 1.591 +} 1.592 + 1.593 +function test_readInputStreamToString_too_many_bytes() 1.594 +{ 1.595 + const TEST_DATA = "this is a test string"; 1.596 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.597 + createInstance(Ci.nsIStringInputStream); 1.598 + istream.setData(TEST_DATA, TEST_DATA.length); 1.599 + 1.600 + try { 1.601 + NetUtil.readInputStreamToString(istream, TEST_DATA.length + 10); 1.602 + do_throw("should throw!"); 1.603 + } 1.604 + catch (e) { 1.605 + do_check_eq(e.result, Cr.NS_ERROR_FAILURE); 1.606 + } 1.607 + 1.608 + run_next_test(); 1.609 +} 1.610 + 1.611 +function test_readInputStreamToString_with_charset() 1.612 +{ 1.613 + const TEST_DATA = "\uff10\uff11\uff12\uff13"; 1.614 + const TEST_DATA_UTF8 = "\xef\xbc\x90\xef\xbc\x91\xef\xbc\x92\xef\xbc\x93"; 1.615 + const TEST_DATA_SJIS = "\x82\x4f\x82\x50\x82\x51\x82\x52"; 1.616 + 1.617 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.618 + createInstance(Ci.nsIStringInputStream); 1.619 + 1.620 + istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length); 1.621 + do_check_eq(NetUtil.readInputStreamToString(istream, 1.622 + TEST_DATA_UTF8.length, 1.623 + { charset: "UTF-8"}), 1.624 + TEST_DATA); 1.625 + 1.626 + istream.setData(TEST_DATA_SJIS, TEST_DATA_SJIS.length); 1.627 + do_check_eq(NetUtil.readInputStreamToString(istream, 1.628 + TEST_DATA_SJIS.length, 1.629 + { charset: "Shift_JIS"}), 1.630 + TEST_DATA); 1.631 + 1.632 + run_next_test(); 1.633 +} 1.634 + 1.635 +function test_readInputStreamToString_invalid_sequence() 1.636 +{ 1.637 + const TEST_DATA = "\ufffd\ufffd\ufffd\ufffd"; 1.638 + const TEST_DATA_UTF8 = "\xaa\xaa\xaa\xaa"; 1.639 + 1.640 + let istream = Cc["@mozilla.org/io/string-input-stream;1"]. 1.641 + createInstance(Ci.nsIStringInputStream); 1.642 + 1.643 + istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length); 1.644 + try { 1.645 + NetUtil.readInputStreamToString(istream, 1.646 + TEST_DATA_UTF8.length, 1.647 + { charset: "UTF-8" }); 1.648 + do_throw("should throw!"); 1.649 + } catch (e) { 1.650 + do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_INPUT); 1.651 + } 1.652 + 1.653 + istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length); 1.654 + do_check_eq(NetUtil.readInputStreamToString(istream, 1.655 + TEST_DATA_UTF8.length, { 1.656 + charset: "UTF-8", 1.657 + replacement: Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER}), 1.658 + TEST_DATA); 1.659 + 1.660 + run_next_test(); 1.661 +} 1.662 + 1.663 + 1.664 +//////////////////////////////////////////////////////////////////////////////// 1.665 +//// Test Runner 1.666 + 1.667 +[ 1.668 + test_async_copy, 1.669 + test_async_write_file, 1.670 + test_async_write_file_deferred, 1.671 + test_async_write_file_safe, 1.672 + test_async_write_file_safe_deferred, 1.673 + test_newURI_no_spec_throws, 1.674 + test_newURI, 1.675 + test_newURI_takes_nsIFile, 1.676 + test_ioService, 1.677 + test_asyncFetch_no_channel, 1.678 + test_asyncFetch_no_callback, 1.679 + test_asyncFetch_with_nsIChannel, 1.680 + test_asyncFetch_with_nsIURI, 1.681 + test_asyncFetch_with_string, 1.682 + test_asyncFetch_with_nsIFile, 1.683 + test_asyncFetch_with_nsIInputString, 1.684 + test_asyncFetch_does_not_block, 1.685 + test_newChannel_no_specifier, 1.686 + test_newChannel_with_string, 1.687 + test_newChannel_with_nsIURI, 1.688 + test_newChannel_with_nsIFile, 1.689 + test_readInputStreamToString, 1.690 + test_readInputStreamToString_no_input_stream, 1.691 + test_readInputStreamToString_no_bytes_arg, 1.692 + test_readInputStreamToString_blocking_stream, 1.693 + test_readInputStreamToString_too_many_bytes, 1.694 + test_readInputStreamToString_with_charset, 1.695 + test_readInputStreamToString_invalid_sequence, 1.696 +].forEach(add_test); 1.697 +let index = 0; 1.698 + 1.699 +function run_test() 1.700 +{ 1.701 + run_next_test(); 1.702 +} 1.703 +