netwerk/test/unit/test_NetUtil.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 sts=2 et
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * This file tests the methods on NetUtil.jsm.
michael@0 9 */
michael@0 10
michael@0 11 Cu.import("resource://testing-common/httpd.js");
michael@0 12
michael@0 13 Cu.import("resource://gre/modules/NetUtil.jsm");
michael@0 14 Cu.import("resource://gre/modules/Task.jsm");
michael@0 15 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 16
michael@0 17 // We need the profile directory so the test harness will clean up our test
michael@0 18 // files.
michael@0 19 do_get_profile();
michael@0 20
michael@0 21 const OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/file-output-stream;1";
michael@0 22 const SAFE_OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/safe-file-output-stream;1";
michael@0 23
michael@0 24 ////////////////////////////////////////////////////////////////////////////////
michael@0 25 //// Helper Methods
michael@0 26
michael@0 27 /**
michael@0 28 * Reads the contents of a file and returns it as a string.
michael@0 29 *
michael@0 30 * @param aFile
michael@0 31 * The file to return from.
michael@0 32 * @return the contents of the file in the form of a string.
michael@0 33 */
michael@0 34 function getFileContents(aFile)
michael@0 35 {
michael@0 36 "use strict";
michael@0 37
michael@0 38 let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
michael@0 39 createInstance(Ci.nsIFileInputStream);
michael@0 40 fstream.init(aFile, -1, 0, 0);
michael@0 41
michael@0 42 let cstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
michael@0 43 createInstance(Ci.nsIConverterInputStream);
michael@0 44 cstream.init(fstream, "UTF-8", 0, 0);
michael@0 45
michael@0 46 let string = {};
michael@0 47 cstream.readString(-1, string);
michael@0 48 cstream.close();
michael@0 49 return string.value;
michael@0 50 }
michael@0 51
michael@0 52
michael@0 53 /**
michael@0 54 * Tests asynchronously writing a file using NetUtil.asyncCopy.
michael@0 55 *
michael@0 56 * @param aContractId
michael@0 57 * The contract ID to use for the output stream
michael@0 58 * @param aDeferOpen
michael@0 59 * Whether to use DEFER_OPEN in the output stream.
michael@0 60 */
michael@0 61 function async_write_file(aContractId, aDeferOpen)
michael@0 62 {
michael@0 63 do_test_pending();
michael@0 64
michael@0 65 // First, we need an output file to write to.
michael@0 66 let file = Cc["@mozilla.org/file/directory_service;1"].
michael@0 67 getService(Ci.nsIProperties).
michael@0 68 get("ProfD", Ci.nsIFile);
michael@0 69 file.append("NetUtil-async-test-file.tmp");
michael@0 70 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 71
michael@0 72 // Then, we need an output stream to our output file.
michael@0 73 let ostream = Cc[aContractId].createInstance(Ci.nsIFileOutputStream);
michael@0 74 ostream.init(file, -1, -1, aDeferOpen ? Ci.nsIFileOutputStream.DEFER_OPEN : 0);
michael@0 75
michael@0 76 // Finally, we need an input stream to take data from.
michael@0 77 const TEST_DATA = "this is a test string";
michael@0 78 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 79 createInstance(Ci.nsIStringInputStream);
michael@0 80 istream.setData(TEST_DATA, TEST_DATA.length);
michael@0 81
michael@0 82 NetUtil.asyncCopy(istream, ostream, function(aResult) {
michael@0 83 // Make sure the copy was successful!
michael@0 84 do_check_true(Components.isSuccessCode(aResult));
michael@0 85
michael@0 86 // Check the file contents.
michael@0 87 do_check_eq(TEST_DATA, getFileContents(file));
michael@0 88
michael@0 89 // Finish the test.
michael@0 90 do_test_finished();
michael@0 91 run_next_test();
michael@0 92 });
michael@0 93 }
michael@0 94
michael@0 95 ////////////////////////////////////////////////////////////////////////////////
michael@0 96 //// Tests
michael@0 97
michael@0 98 // Test NetUtil.asyncCopy for all possible buffering scenarios
michael@0 99 function test_async_copy()
michael@0 100 {
michael@0 101 // Create a data sample
michael@0 102 function make_sample(text) {
michael@0 103 let data = [];
michael@0 104 for (let i = 0; i <= 100; ++i) {
michael@0 105 data.push(text);
michael@0 106 }
michael@0 107 return data.join();
michael@0 108 }
michael@0 109
michael@0 110 // Create an input buffer holding some data
michael@0 111 function make_input(isBuffered, data) {
michael@0 112 if (isBuffered) {
michael@0 113 // String input streams are buffered
michael@0 114 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 115 createInstance(Ci.nsIStringInputStream);
michael@0 116 istream.setData(data, data.length);
michael@0 117 return istream;
michael@0 118 }
michael@0 119
michael@0 120 // File input streams are not buffered, so let's create a file
michael@0 121 let file = Cc["@mozilla.org/file/directory_service;1"].
michael@0 122 getService(Ci.nsIProperties).
michael@0 123 get("ProfD", Ci.nsIFile);
michael@0 124 file.append("NetUtil-asyncFetch-test-file.tmp");
michael@0 125 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 126
michael@0 127 let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 128 createInstance(Ci.nsIFileOutputStream);
michael@0 129 ostream.init(file, -1, -1, 0);
michael@0 130 ostream.write(data, data.length);
michael@0 131 ostream.close();
michael@0 132
michael@0 133 let istream = Cc["@mozilla.org/network/file-input-stream;1"].
michael@0 134 createInstance(Ci.nsIFileInputStream);
michael@0 135 istream.init(file, -1, 0, 0);
michael@0 136
michael@0 137 return istream;
michael@0 138 }
michael@0 139
michael@0 140 // Create an output buffer holding some data
michael@0 141 function make_output(isBuffered) {
michael@0 142 let file = Cc["@mozilla.org/file/directory_service;1"].
michael@0 143 getService(Ci.nsIProperties).
michael@0 144 get("ProfD", Ci.nsIFile);
michael@0 145 file.append("NetUtil-asyncFetch-test-file.tmp");
michael@0 146 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 147
michael@0 148 let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 149 createInstance(Ci.nsIFileOutputStream);
michael@0 150 ostream.init(file, -1, -1, 0);
michael@0 151
michael@0 152 if (!isBuffered) {
michael@0 153 return {file: file, sink: ostream};
michael@0 154 }
michael@0 155
michael@0 156 let bstream = Cc["@mozilla.org/network/buffered-output-stream;1"].
michael@0 157 createInstance(Ci.nsIBufferedOutputStream);
michael@0 158 bstream.init(ostream, 256);
michael@0 159 return {file: file, sink: bstream};
michael@0 160 }
michael@0 161 Task.spawn(function*() {
michael@0 162 do_test_pending();
michael@0 163 for (let bufferedInput of [true, false]) {
michael@0 164 for (let bufferedOutput of [true, false]) {
michael@0 165 let text = "test_async_copy with "
michael@0 166 + (bufferedInput?"buffered input":"unbuffered input")
michael@0 167 + ", "
michael@0 168 + (bufferedOutput?"buffered output":"unbuffered output");
michael@0 169 do_print(text);
michael@0 170 let TEST_DATA = "[" + make_sample(text) + "]";
michael@0 171 let source = make_input(bufferedInput, TEST_DATA);
michael@0 172 let {file, sink} = make_output(bufferedOutput);
michael@0 173 let deferred = Promise.defer();
michael@0 174 NetUtil.asyncCopy(source, sink, deferred.resolve);
michael@0 175 let result = yield deferred.promise;
michael@0 176
michael@0 177 // Make sure the copy was successful!
michael@0 178 if (!Components.isSuccessCode(result)) {
michael@0 179 do_throw(new Components.Exception("asyncCopy error", result));
michael@0 180 }
michael@0 181
michael@0 182 // Check the file contents.
michael@0 183 do_check_eq(TEST_DATA, getFileContents(file));
michael@0 184 }
michael@0 185 }
michael@0 186
michael@0 187 do_test_finished();
michael@0 188 run_next_test();
michael@0 189 });
michael@0 190 }
michael@0 191
michael@0 192 function test_async_write_file() {
michael@0 193 async_write_file(OUTPUT_STREAM_CONTRACT_ID);
michael@0 194 }
michael@0 195
michael@0 196 function test_async_write_file_deferred() {
michael@0 197 async_write_file(OUTPUT_STREAM_CONTRACT_ID, true);
michael@0 198 }
michael@0 199
michael@0 200 function test_async_write_file_safe() {
michael@0 201 async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID);
michael@0 202 }
michael@0 203
michael@0 204 function test_async_write_file_safe_deferred() {
michael@0 205 async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID, true);
michael@0 206 }
michael@0 207
michael@0 208 function test_newURI_no_spec_throws()
michael@0 209 {
michael@0 210 try {
michael@0 211 NetUtil.newURI();
michael@0 212 do_throw("should throw!");
michael@0 213 }
michael@0 214 catch (e) {
michael@0 215 do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
michael@0 216 }
michael@0 217
michael@0 218 run_next_test();
michael@0 219 }
michael@0 220
michael@0 221 function test_newURI()
michael@0 222 {
michael@0 223 let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 224
michael@0 225 // Check that we get the same URI back from the IO service and the utility
michael@0 226 // method.
michael@0 227 const TEST_URI = "http://mozilla.org";
michael@0 228 let iosURI = ios.newURI(TEST_URI, null, null);
michael@0 229 let NetUtilURI = NetUtil.newURI(TEST_URI);
michael@0 230 do_check_true(iosURI.equals(NetUtilURI));
michael@0 231
michael@0 232 run_next_test();
michael@0 233 }
michael@0 234
michael@0 235 function test_newURI_takes_nsIFile()
michael@0 236 {
michael@0 237 let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 238
michael@0 239 // Create a test file that we can pass into NetUtil.newURI
michael@0 240 let file = Cc["@mozilla.org/file/directory_service;1"].
michael@0 241 getService(Ci.nsIProperties).
michael@0 242 get("ProfD", Ci.nsIFile);
michael@0 243 file.append("NetUtil-test-file.tmp");
michael@0 244
michael@0 245 // Check that we get the same URI back from the IO service and the utility
michael@0 246 // method.
michael@0 247 let iosURI = ios.newFileURI(file);
michael@0 248 let NetUtilURI = NetUtil.newURI(file);
michael@0 249 do_check_true(iosURI.equals(NetUtilURI));
michael@0 250
michael@0 251 run_next_test();
michael@0 252 }
michael@0 253
michael@0 254 function test_ioService()
michael@0 255 {
michael@0 256 do_check_true(NetUtil.ioService instanceof Ci.nsIIOService);
michael@0 257 run_next_test();
michael@0 258 }
michael@0 259
michael@0 260 function test_asyncFetch_no_channel()
michael@0 261 {
michael@0 262 try {
michael@0 263 NetUtil.asyncFetch(null, function() { });
michael@0 264 do_throw("should throw!");
michael@0 265 }
michael@0 266 catch (e) {
michael@0 267 do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
michael@0 268 }
michael@0 269
michael@0 270 run_next_test();
michael@0 271 }
michael@0 272
michael@0 273 function test_asyncFetch_no_callback()
michael@0 274 {
michael@0 275 try {
michael@0 276 NetUtil.asyncFetch({ });
michael@0 277 do_throw("should throw!");
michael@0 278 }
michael@0 279 catch (e) {
michael@0 280 do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
michael@0 281 }
michael@0 282
michael@0 283 run_next_test();
michael@0 284 }
michael@0 285
michael@0 286 function test_asyncFetch_with_nsIChannel()
michael@0 287 {
michael@0 288 const TEST_DATA = "this is a test string";
michael@0 289
michael@0 290 // Start the http server, and register our handler.
michael@0 291 let server = new HttpServer();
michael@0 292 server.registerPathHandler("/test", function(aRequest, aResponse) {
michael@0 293 aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
michael@0 294 aResponse.setHeader("Content-Type", "text/plain", false);
michael@0 295 aResponse.write(TEST_DATA);
michael@0 296 });
michael@0 297 server.start(-1);
michael@0 298
michael@0 299 // Create our channel.
michael@0 300 let channel = NetUtil.ioService.
michael@0 301 newChannel("http://localhost:" +
michael@0 302 server.identity.primaryPort + "/test", null, null);
michael@0 303
michael@0 304 // Open our channel asynchronously.
michael@0 305 NetUtil.asyncFetch(channel, function(aInputStream, aResult) {
michael@0 306 // Check that we had success.
michael@0 307 do_check_true(Components.isSuccessCode(aResult));
michael@0 308
michael@0 309 // Check that we got the right data.
michael@0 310 do_check_eq(aInputStream.available(), TEST_DATA.length);
michael@0 311 let is = Cc["@mozilla.org/scriptableinputstream;1"].
michael@0 312 createInstance(Ci.nsIScriptableInputStream);
michael@0 313 is.init(aInputStream);
michael@0 314 let result = is.read(TEST_DATA.length);
michael@0 315 do_check_eq(TEST_DATA, result);
michael@0 316
michael@0 317 server.stop(run_next_test);
michael@0 318 });
michael@0 319 }
michael@0 320
michael@0 321 function test_asyncFetch_with_nsIURI()
michael@0 322 {
michael@0 323 const TEST_DATA = "this is a test string";
michael@0 324
michael@0 325 // Start the http server, and register our handler.
michael@0 326 let server = new HttpServer();
michael@0 327 server.registerPathHandler("/test", function(aRequest, aResponse) {
michael@0 328 aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
michael@0 329 aResponse.setHeader("Content-Type", "text/plain", false);
michael@0 330 aResponse.write(TEST_DATA);
michael@0 331 });
michael@0 332 server.start(-1);
michael@0 333
michael@0 334 // Create our URI.
michael@0 335 let uri = NetUtil.newURI("http://localhost:" +
michael@0 336 server.identity.primaryPort + "/test");
michael@0 337
michael@0 338 // Open our URI asynchronously.
michael@0 339 NetUtil.asyncFetch(uri, function(aInputStream, aResult) {
michael@0 340 // Check that we had success.
michael@0 341 do_check_true(Components.isSuccessCode(aResult));
michael@0 342
michael@0 343 // Check that we got the right data.
michael@0 344 do_check_eq(aInputStream.available(), TEST_DATA.length);
michael@0 345 let is = Cc["@mozilla.org/scriptableinputstream;1"].
michael@0 346 createInstance(Ci.nsIScriptableInputStream);
michael@0 347 is.init(aInputStream);
michael@0 348 let result = is.read(TEST_DATA.length);
michael@0 349 do_check_eq(TEST_DATA, result);
michael@0 350
michael@0 351 server.stop(run_next_test);
michael@0 352 });
michael@0 353 }
michael@0 354
michael@0 355 function test_asyncFetch_with_string()
michael@0 356 {
michael@0 357 const TEST_DATA = "this is a test string";
michael@0 358
michael@0 359 // Start the http server, and register our handler.
michael@0 360 let server = new HttpServer();
michael@0 361 server.registerPathHandler("/test", function(aRequest, aResponse) {
michael@0 362 aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
michael@0 363 aResponse.setHeader("Content-Type", "text/plain", false);
michael@0 364 aResponse.write(TEST_DATA);
michael@0 365 });
michael@0 366 server.start(-1);
michael@0 367
michael@0 368 // Open our location asynchronously.
michael@0 369 NetUtil.asyncFetch("http://localhost:" +
michael@0 370 server.identity.primaryPort + "/test",
michael@0 371 function(aInputStream, aResult) {
michael@0 372 // Check that we had success.
michael@0 373 do_check_true(Components.isSuccessCode(aResult));
michael@0 374
michael@0 375 // Check that we got the right data.
michael@0 376 do_check_eq(aInputStream.available(), TEST_DATA.length);
michael@0 377 let is = Cc["@mozilla.org/scriptableinputstream;1"].
michael@0 378 createInstance(Ci.nsIScriptableInputStream);
michael@0 379 is.init(aInputStream);
michael@0 380 let result = is.read(TEST_DATA.length);
michael@0 381 do_check_eq(TEST_DATA, result);
michael@0 382
michael@0 383 server.stop(run_next_test);
michael@0 384 });
michael@0 385 }
michael@0 386
michael@0 387 function test_asyncFetch_with_nsIFile()
michael@0 388 {
michael@0 389 const TEST_DATA = "this is a test string";
michael@0 390
michael@0 391 // First we need a file to read from.
michael@0 392 let file = Cc["@mozilla.org/file/directory_service;1"].
michael@0 393 getService(Ci.nsIProperties).
michael@0 394 get("ProfD", Ci.nsIFile);
michael@0 395 file.append("NetUtil-asyncFetch-test-file.tmp");
michael@0 396 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 397
michael@0 398 // Write the test data to the file.
michael@0 399 let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 400 createInstance(Ci.nsIFileOutputStream);
michael@0 401 ostream.init(file, -1, -1, 0);
michael@0 402 ostream.write(TEST_DATA, TEST_DATA.length);
michael@0 403
michael@0 404 // Sanity check to make sure the data was written.
michael@0 405 do_check_eq(TEST_DATA, getFileContents(file));
michael@0 406
michael@0 407 // Open our file asynchronously.
michael@0 408 NetUtil.asyncFetch(file, function(aInputStream, aResult) {
michael@0 409 // Check that we had success.
michael@0 410 do_check_true(Components.isSuccessCode(aResult));
michael@0 411
michael@0 412 // Check that we got the right data.
michael@0 413 do_check_eq(aInputStream.available(), TEST_DATA.length);
michael@0 414 let is = Cc["@mozilla.org/scriptableinputstream;1"].
michael@0 415 createInstance(Ci.nsIScriptableInputStream);
michael@0 416 is.init(aInputStream);
michael@0 417 let result = is.read(TEST_DATA.length);
michael@0 418 do_check_eq(TEST_DATA, result);
michael@0 419
michael@0 420 run_next_test();
michael@0 421 });
michael@0 422 }
michael@0 423
michael@0 424 function test_asyncFetch_with_nsIInputString()
michael@0 425 {
michael@0 426 const TEST_DATA = "this is a test string";
michael@0 427 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 428 createInstance(Ci.nsIStringInputStream);
michael@0 429 istream.setData(TEST_DATA, TEST_DATA.length);
michael@0 430
michael@0 431 // Read the input stream asynchronously.
michael@0 432 NetUtil.asyncFetch(istream, function(aInputStream, aResult) {
michael@0 433 // Check that we had success.
michael@0 434 do_check_true(Components.isSuccessCode(aResult));
michael@0 435
michael@0 436 // Check that we got the right data.
michael@0 437 do_check_eq(aInputStream.available(), TEST_DATA.length);
michael@0 438 do_check_eq(NetUtil.readInputStreamToString(aInputStream, TEST_DATA.length),
michael@0 439 TEST_DATA);
michael@0 440
michael@0 441 run_next_test();
michael@0 442 });
michael@0 443 }
michael@0 444
michael@0 445 function test_asyncFetch_does_not_block()
michael@0 446 {
michael@0 447 // Create our channel that has no data.
michael@0 448 let channel = NetUtil.ioService.
michael@0 449 newChannel("data:text/plain,", null, null);
michael@0 450
michael@0 451 // Open our channel asynchronously.
michael@0 452 NetUtil.asyncFetch(channel, function(aInputStream, aResult) {
michael@0 453 // Check that we had success.
michael@0 454 do_check_true(Components.isSuccessCode(aResult));
michael@0 455
michael@0 456 // Check that reading a byte throws that the stream was closed (as opposed
michael@0 457 // saying it would block).
michael@0 458 let is = Cc["@mozilla.org/scriptableinputstream;1"].
michael@0 459 createInstance(Ci.nsIScriptableInputStream);
michael@0 460 is.init(aInputStream);
michael@0 461 try {
michael@0 462 is.read(1);
michael@0 463 do_throw("should throw!");
michael@0 464 }
michael@0 465 catch (e) {
michael@0 466 do_check_eq(e.result, Cr.NS_BASE_STREAM_CLOSED);
michael@0 467 }
michael@0 468
michael@0 469 run_next_test();
michael@0 470 });
michael@0 471 }
michael@0 472
michael@0 473 function test_newChannel_no_specifier()
michael@0 474 {
michael@0 475 try {
michael@0 476 NetUtil.newChannel();
michael@0 477 do_throw("should throw!");
michael@0 478 }
michael@0 479 catch (e) {
michael@0 480 do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
michael@0 481 }
michael@0 482
michael@0 483 run_next_test();
michael@0 484 }
michael@0 485
michael@0 486 function test_newChannel_with_string()
michael@0 487 {
michael@0 488 const TEST_SPEC = "http://mozilla.org";
michael@0 489
michael@0 490 // Check that we get the same URI back from channel the IO service creates and
michael@0 491 // the channel the utility method creates.
michael@0 492 let ios = NetUtil.ioService;
michael@0 493 let iosChannel = ios.newChannel(TEST_SPEC, null, null);
michael@0 494 let NetUtilChannel = NetUtil.newChannel(TEST_SPEC);
michael@0 495 do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
michael@0 496
michael@0 497 run_next_test();
michael@0 498 }
michael@0 499
michael@0 500 function test_newChannel_with_nsIURI()
michael@0 501 {
michael@0 502 const TEST_SPEC = "http://mozilla.org";
michael@0 503
michael@0 504 // Check that we get the same URI back from channel the IO service creates and
michael@0 505 // the channel the utility method creates.
michael@0 506 let uri = NetUtil.newURI(TEST_SPEC);
michael@0 507 let iosChannel = NetUtil.ioService.newChannelFromURI(uri);
michael@0 508 let NetUtilChannel = NetUtil.newChannel(uri);
michael@0 509 do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
michael@0 510
michael@0 511 run_next_test();
michael@0 512 }
michael@0 513
michael@0 514 function test_newChannel_with_nsIFile()
michael@0 515 {
michael@0 516 let file = Cc["@mozilla.org/file/directory_service;1"].
michael@0 517 getService(Ci.nsIProperties).
michael@0 518 get("ProfD", Ci.nsIFile);
michael@0 519 file.append("NetUtil-test-file.tmp");
michael@0 520
michael@0 521 // Check that we get the same URI back from channel the IO service creates and
michael@0 522 // the channel the utility method creates.
michael@0 523 let uri = NetUtil.newURI(file);
michael@0 524 let iosChannel = NetUtil.ioService.newChannelFromURI(uri);
michael@0 525 let NetUtilChannel = NetUtil.newChannel(uri);
michael@0 526 do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
michael@0 527
michael@0 528 run_next_test();
michael@0 529 }
michael@0 530
michael@0 531 function test_readInputStreamToString()
michael@0 532 {
michael@0 533 const TEST_DATA = "this is a test string\0 with an embedded null";
michael@0 534 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 535 createInstance(Ci.nsISupportsCString);
michael@0 536 istream.data = TEST_DATA;
michael@0 537
michael@0 538 do_check_eq(NetUtil.readInputStreamToString(istream, TEST_DATA.length),
michael@0 539 TEST_DATA);
michael@0 540
michael@0 541 run_next_test();
michael@0 542 }
michael@0 543
michael@0 544 function test_readInputStreamToString_no_input_stream()
michael@0 545 {
michael@0 546 try {
michael@0 547 NetUtil.readInputStreamToString("hi", 2);
michael@0 548 do_throw("should throw!");
michael@0 549 }
michael@0 550 catch (e) {
michael@0 551 do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
michael@0 552 }
michael@0 553
michael@0 554 run_next_test();
michael@0 555 }
michael@0 556
michael@0 557 function test_readInputStreamToString_no_bytes_arg()
michael@0 558 {
michael@0 559 const TEST_DATA = "this is a test string";
michael@0 560 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 561 createInstance(Ci.nsIStringInputStream);
michael@0 562 istream.setData(TEST_DATA, TEST_DATA.length);
michael@0 563
michael@0 564 try {
michael@0 565 NetUtil.readInputStreamToString(istream);
michael@0 566 do_throw("should throw!");
michael@0 567 }
michael@0 568 catch (e) {
michael@0 569 do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
michael@0 570 }
michael@0 571
michael@0 572 run_next_test();
michael@0 573 }
michael@0 574
michael@0 575 function test_readInputStreamToString_blocking_stream()
michael@0 576 {
michael@0 577 let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
michael@0 578 pipe.init(true, true, 0, 0, null);
michael@0 579
michael@0 580 try {
michael@0 581 NetUtil.readInputStreamToString(pipe.inputStream, 10);
michael@0 582 do_throw("should throw!");
michael@0 583 }
michael@0 584 catch (e) {
michael@0 585 do_check_eq(e.result, Cr.NS_BASE_STREAM_WOULD_BLOCK);
michael@0 586 }
michael@0 587 run_next_test();
michael@0 588 }
michael@0 589
michael@0 590 function test_readInputStreamToString_too_many_bytes()
michael@0 591 {
michael@0 592 const TEST_DATA = "this is a test string";
michael@0 593 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 594 createInstance(Ci.nsIStringInputStream);
michael@0 595 istream.setData(TEST_DATA, TEST_DATA.length);
michael@0 596
michael@0 597 try {
michael@0 598 NetUtil.readInputStreamToString(istream, TEST_DATA.length + 10);
michael@0 599 do_throw("should throw!");
michael@0 600 }
michael@0 601 catch (e) {
michael@0 602 do_check_eq(e.result, Cr.NS_ERROR_FAILURE);
michael@0 603 }
michael@0 604
michael@0 605 run_next_test();
michael@0 606 }
michael@0 607
michael@0 608 function test_readInputStreamToString_with_charset()
michael@0 609 {
michael@0 610 const TEST_DATA = "\uff10\uff11\uff12\uff13";
michael@0 611 const TEST_DATA_UTF8 = "\xef\xbc\x90\xef\xbc\x91\xef\xbc\x92\xef\xbc\x93";
michael@0 612 const TEST_DATA_SJIS = "\x82\x4f\x82\x50\x82\x51\x82\x52";
michael@0 613
michael@0 614 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 615 createInstance(Ci.nsIStringInputStream);
michael@0 616
michael@0 617 istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length);
michael@0 618 do_check_eq(NetUtil.readInputStreamToString(istream,
michael@0 619 TEST_DATA_UTF8.length,
michael@0 620 { charset: "UTF-8"}),
michael@0 621 TEST_DATA);
michael@0 622
michael@0 623 istream.setData(TEST_DATA_SJIS, TEST_DATA_SJIS.length);
michael@0 624 do_check_eq(NetUtil.readInputStreamToString(istream,
michael@0 625 TEST_DATA_SJIS.length,
michael@0 626 { charset: "Shift_JIS"}),
michael@0 627 TEST_DATA);
michael@0 628
michael@0 629 run_next_test();
michael@0 630 }
michael@0 631
michael@0 632 function test_readInputStreamToString_invalid_sequence()
michael@0 633 {
michael@0 634 const TEST_DATA = "\ufffd\ufffd\ufffd\ufffd";
michael@0 635 const TEST_DATA_UTF8 = "\xaa\xaa\xaa\xaa";
michael@0 636
michael@0 637 let istream = Cc["@mozilla.org/io/string-input-stream;1"].
michael@0 638 createInstance(Ci.nsIStringInputStream);
michael@0 639
michael@0 640 istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length);
michael@0 641 try {
michael@0 642 NetUtil.readInputStreamToString(istream,
michael@0 643 TEST_DATA_UTF8.length,
michael@0 644 { charset: "UTF-8" });
michael@0 645 do_throw("should throw!");
michael@0 646 } catch (e) {
michael@0 647 do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_INPUT);
michael@0 648 }
michael@0 649
michael@0 650 istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length);
michael@0 651 do_check_eq(NetUtil.readInputStreamToString(istream,
michael@0 652 TEST_DATA_UTF8.length, {
michael@0 653 charset: "UTF-8",
michael@0 654 replacement: Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER}),
michael@0 655 TEST_DATA);
michael@0 656
michael@0 657 run_next_test();
michael@0 658 }
michael@0 659
michael@0 660
michael@0 661 ////////////////////////////////////////////////////////////////////////////////
michael@0 662 //// Test Runner
michael@0 663
michael@0 664 [
michael@0 665 test_async_copy,
michael@0 666 test_async_write_file,
michael@0 667 test_async_write_file_deferred,
michael@0 668 test_async_write_file_safe,
michael@0 669 test_async_write_file_safe_deferred,
michael@0 670 test_newURI_no_spec_throws,
michael@0 671 test_newURI,
michael@0 672 test_newURI_takes_nsIFile,
michael@0 673 test_ioService,
michael@0 674 test_asyncFetch_no_channel,
michael@0 675 test_asyncFetch_no_callback,
michael@0 676 test_asyncFetch_with_nsIChannel,
michael@0 677 test_asyncFetch_with_nsIURI,
michael@0 678 test_asyncFetch_with_string,
michael@0 679 test_asyncFetch_with_nsIFile,
michael@0 680 test_asyncFetch_with_nsIInputString,
michael@0 681 test_asyncFetch_does_not_block,
michael@0 682 test_newChannel_no_specifier,
michael@0 683 test_newChannel_with_string,
michael@0 684 test_newChannel_with_nsIURI,
michael@0 685 test_newChannel_with_nsIFile,
michael@0 686 test_readInputStreamToString,
michael@0 687 test_readInputStreamToString_no_input_stream,
michael@0 688 test_readInputStreamToString_no_bytes_arg,
michael@0 689 test_readInputStreamToString_blocking_stream,
michael@0 690 test_readInputStreamToString_too_many_bytes,
michael@0 691 test_readInputStreamToString_with_charset,
michael@0 692 test_readInputStreamToString_invalid_sequence,
michael@0 693 ].forEach(add_test);
michael@0 694 let index = 0;
michael@0 695
michael@0 696 function run_test()
michael@0 697 {
michael@0 698 run_next_test();
michael@0 699 }
michael@0 700

mercurial