1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_reopen.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +// This testcase verifies that channels can't be reopened 1.5 +// See https://bugzilla.mozilla.org/show_bug.cgi?id=372486 1.6 + 1.7 +Cu.import("resource://testing-common/httpd.js"); 1.8 + 1.9 +const NS_ERROR_IN_PROGRESS = 0x804b000f; 1.10 +const NS_ERROR_ALREADY_OPENED = 0x804b0049; 1.11 + 1.12 +var chan = null; 1.13 +var httpserv = null; 1.14 + 1.15 +[ 1.16 + test_data_channel, 1.17 + test_http_channel, 1.18 + test_file_channel, 1.19 + // Commented by default as it relies on external ressources 1.20 + //test_ftp_channel, 1.21 + end 1.22 +].forEach(add_test); 1.23 + 1.24 +// Utility functions 1.25 + 1.26 +function makeChan(url) { 1.27 + var ios = Cc["@mozilla.org/network/io-service;1"] 1.28 + .getService(Ci.nsIIOService); 1.29 + return chan = ios.newChannel(url, null, null) 1.30 + .QueryInterface(Ci.nsIChannel); 1.31 +} 1.32 + 1.33 +function new_file_channel(file) { 1.34 + var ios = Cc["@mozilla.org/network/io-service;1"] 1.35 + .getService(Ci.nsIIOService); 1.36 + return ios.newChannelFromURI(ios.newFileURI(file)); 1.37 +} 1.38 + 1.39 + 1.40 +function check_throws(closure, error) { 1.41 + var thrown = false; 1.42 + try { 1.43 + closure(); 1.44 + } catch (e) { 1.45 + if (error instanceof Array) { 1.46 + do_check_neq(error.indexOf(e.result), -1); 1.47 + } else { 1.48 + do_check_eq(e.result, error); 1.49 + } 1.50 + thrown = true; 1.51 + } 1.52 + do_check_true(thrown); 1.53 +} 1.54 + 1.55 +function check_open_throws(error) { 1.56 + check_throws(function() { 1.57 + chan.open(listener, null); 1.58 + }, error); 1.59 +} 1.60 + 1.61 +function check_async_open_throws(error) { 1.62 + check_throws(function() { 1.63 + chan.asyncOpen(listener, null); 1.64 + }, error); 1.65 +} 1.66 + 1.67 +var listener = { 1.68 + onStartRequest: function test_onStartR(request, ctx) { 1.69 + check_async_open_throws(NS_ERROR_IN_PROGRESS); 1.70 + }, 1.71 + 1.72 + onDataAvailable: function test_ODA(request, cx, inputStream, 1.73 + offset, count) { 1.74 + new BinaryInputStream(inputStream).readByteArray(count); // required by API 1.75 + check_async_open_throws(NS_ERROR_IN_PROGRESS); 1.76 + }, 1.77 + 1.78 + onStopRequest: function test_onStopR(request, ctx, status) { 1.79 + // Once onStopRequest is reached, the channel is marked as having been 1.80 + // opened 1.81 + check_async_open_throws(NS_ERROR_ALREADY_OPENED); 1.82 + do_timeout(0, after_channel_closed); 1.83 + } 1.84 +}; 1.85 + 1.86 +function after_channel_closed() { 1.87 + check_async_open_throws(NS_ERROR_ALREADY_OPENED); 1.88 + 1.89 + run_next_test(); 1.90 +} 1.91 + 1.92 +function test_channel(createChanClosure) { 1.93 + // First, synchronous reopening test 1.94 + chan = createChanClosure(); 1.95 + var inputStream = chan.open(); 1.96 + check_open_throws(NS_ERROR_IN_PROGRESS); 1.97 + check_async_open_throws([NS_ERROR_IN_PROGRESS, NS_ERROR_ALREADY_OPENED]); 1.98 + 1.99 + // Then, asynchronous one 1.100 + chan = createChanClosure(); 1.101 + chan.asyncOpen(listener, null); 1.102 + check_open_throws(NS_ERROR_IN_PROGRESS); 1.103 + check_async_open_throws(NS_ERROR_IN_PROGRESS); 1.104 +} 1.105 + 1.106 +function test_data_channel() { 1.107 + test_channel(function() { 1.108 + return makeChan("data:text/plain,foo"); 1.109 + }); 1.110 +} 1.111 + 1.112 +function test_http_channel() { 1.113 + test_channel(function() { 1.114 + return makeChan("http://localhost:" + httpserv.identity.primaryPort + "/"); 1.115 + }); 1.116 +} 1.117 + 1.118 +function test_file_channel() { 1.119 + var file = do_get_file("data/test_readline1.txt"); 1.120 + test_channel(function() { 1.121 + return new_file_channel(file); 1.122 + }); 1.123 +} 1.124 + 1.125 +// Uncomment test_ftp_channel in test_array to test this 1.126 +function test_ftp_channel() { 1.127 + test_channel(function() { 1.128 + return makeChan("ftp://ftp.mozilla.org/pub/mozilla.org/README"); 1.129 + }); 1.130 +} 1.131 + 1.132 +function end() { 1.133 + httpserv.stop(do_test_finished); 1.134 +} 1.135 + 1.136 +function run_test() { 1.137 + // start server 1.138 + httpserv = new HttpServer(); 1.139 + httpserv.start(-1); 1.140 + 1.141 + run_next_test(); 1.142 +}