Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / netwerk / test / unit / test_reopen.js
blob6ab71f17eaef8632a2af3bf52272783a5748b8a9
1 // This testcase verifies that channels can't be reopened
2 // See https://bugzilla.mozilla.org/show_bug.cgi?id=372486
4 do_import_script("netwerk/test/httpserver/httpd.js");
6 const NS_ERROR_IN_PROGRESS = 0x804b000f;
7 const NS_ERROR_ALREADY_OPENED = 0x804b0049;
9 var chan = null;
10 var httpserv = null;
12 var test_index = 0;
13 var test_array = [
14 test_data_channel,
15 test_http_channel,
16 test_file_channel,
17 // Commented by default as it relies on external ressources
18 //test_ftp_channel,
19 end
22 // Utility functions
24 function makeChan(url) {
25 var ios = Cc["@mozilla.org/network/io-service;1"]
26 .getService(Ci.nsIIOService);
27 return chan = ios.newChannel(url, null, null)
28 .QueryInterface(Ci.nsIChannel);
31 function new_file_channel(file) {
32 var ios = Cc["@mozilla.org/network/io-service;1"]
33 .getService(Ci.nsIIOService);
34 return ios.newChannelFromURI(ios.newFileURI(file));
38 function check_throws(closure, error) {
39 var thrown = false;
40 try {
41 closure();
42 } catch (e) {
43 if (error instanceof Array) {
44 do_check_neq(error.indexOf(e.result), -1);
45 } else {
46 do_check_eq(e.result, error);
48 thrown = true;
50 do_check_true(thrown);
53 function check_open_throws(error) {
54 check_throws(function() {
55 chan.open(listener, null);
56 }, error);
59 function check_async_open_throws(error) {
60 check_throws(function() {
61 chan.asyncOpen(listener, null);
62 }, error);
65 var listener = {
66 onStartRequest: function test_onStartR(request, ctx) {
67 check_async_open_throws(NS_ERROR_IN_PROGRESS);
70 onDataAvailable: function test_ODA(request, cx, inputStream,
71 offset, count) {
72 new BinaryInputStream(inputStream).readByteArray(count); // required by API
73 check_async_open_throws(NS_ERROR_IN_PROGRESS);
76 onStopRequest: function test_onStopR(request, ctx, status) {
77 // Once onStopRequest is reached, the channel is marked as having been
78 // opened
79 check_async_open_throws(NS_ERROR_ALREADY_OPENED);
80 do_timeout(0, "after_channel_closed()");
84 function after_channel_closed() {
85 check_async_open_throws(NS_ERROR_ALREADY_OPENED);
87 run_next_test();
90 function run_next_test() {
91 test_array[test_index++]();
94 function test_channel(createChanClosure) {
95 // First, synchronous reopening test
96 chan = createChanClosure();
97 var inputStream = chan.open();
98 check_open_throws(NS_ERROR_IN_PROGRESS);
99 check_async_open_throws([NS_ERROR_IN_PROGRESS, NS_ERROR_ALREADY_OPENED]);
101 // Then, asynchronous one
102 chan = createChanClosure();
103 chan.asyncOpen(listener, null);
104 check_open_throws(NS_ERROR_IN_PROGRESS);
105 check_async_open_throws(NS_ERROR_IN_PROGRESS);
108 function test_data_channel() {
109 test_channel(function() {
110 return makeChan("data:text/plain,foo");
114 function test_http_channel() {
115 test_channel(function() {
116 return makeChan("http://localhost:4444/");
120 function test_file_channel() {
121 var file = do_get_file("netwerk/test/Makefile.in");
122 test_channel(function() {
123 return new_file_channel(file);
127 // Uncomment test_ftp_channel in test_array to test this
128 function test_ftp_channel() {
129 test_channel(function() {
130 return makeChan("ftp://ftp.mozilla.org/pub/mozilla.org/README");
134 function end() {
135 httpserv.stop();
136 do_test_finished();
139 function run_test() {
140 // start server
141 httpserv = new nsHttpServer();
142 httpserv.start(4444);
144 do_test_pending();
145 run_next_test();