Thu, 15 Jan 2015 21:03:48 +0100
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.)
1 // This testcase verifies that channels can't be reopened
2 // See https://bugzilla.mozilla.org/show_bug.cgi?id=372486
4 Cu.import("resource://testing-common/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 [
13 test_data_channel,
14 test_http_channel,
15 test_file_channel,
16 // Commented by default as it relies on external ressources
17 //test_ftp_channel,
18 end
19 ].forEach(add_test);
21 // Utility functions
23 function makeChan(url) {
24 var ios = Cc["@mozilla.org/network/io-service;1"]
25 .getService(Ci.nsIIOService);
26 return chan = ios.newChannel(url, null, null)
27 .QueryInterface(Ci.nsIChannel);
28 }
30 function new_file_channel(file) {
31 var ios = Cc["@mozilla.org/network/io-service;1"]
32 .getService(Ci.nsIIOService);
33 return ios.newChannelFromURI(ios.newFileURI(file));
34 }
37 function check_throws(closure, error) {
38 var thrown = false;
39 try {
40 closure();
41 } catch (e) {
42 if (error instanceof Array) {
43 do_check_neq(error.indexOf(e.result), -1);
44 } else {
45 do_check_eq(e.result, error);
46 }
47 thrown = true;
48 }
49 do_check_true(thrown);
50 }
52 function check_open_throws(error) {
53 check_throws(function() {
54 chan.open(listener, null);
55 }, error);
56 }
58 function check_async_open_throws(error) {
59 check_throws(function() {
60 chan.asyncOpen(listener, null);
61 }, error);
62 }
64 var listener = {
65 onStartRequest: function test_onStartR(request, ctx) {
66 check_async_open_throws(NS_ERROR_IN_PROGRESS);
67 },
69 onDataAvailable: function test_ODA(request, cx, inputStream,
70 offset, count) {
71 new BinaryInputStream(inputStream).readByteArray(count); // required by API
72 check_async_open_throws(NS_ERROR_IN_PROGRESS);
73 },
75 onStopRequest: function test_onStopR(request, ctx, status) {
76 // Once onStopRequest is reached, the channel is marked as having been
77 // opened
78 check_async_open_throws(NS_ERROR_ALREADY_OPENED);
79 do_timeout(0, after_channel_closed);
80 }
81 };
83 function after_channel_closed() {
84 check_async_open_throws(NS_ERROR_ALREADY_OPENED);
86 run_next_test();
87 }
89 function test_channel(createChanClosure) {
90 // First, synchronous reopening test
91 chan = createChanClosure();
92 var inputStream = chan.open();
93 check_open_throws(NS_ERROR_IN_PROGRESS);
94 check_async_open_throws([NS_ERROR_IN_PROGRESS, NS_ERROR_ALREADY_OPENED]);
96 // Then, asynchronous one
97 chan = createChanClosure();
98 chan.asyncOpen(listener, null);
99 check_open_throws(NS_ERROR_IN_PROGRESS);
100 check_async_open_throws(NS_ERROR_IN_PROGRESS);
101 }
103 function test_data_channel() {
104 test_channel(function() {
105 return makeChan("data:text/plain,foo");
106 });
107 }
109 function test_http_channel() {
110 test_channel(function() {
111 return makeChan("http://localhost:" + httpserv.identity.primaryPort + "/");
112 });
113 }
115 function test_file_channel() {
116 var file = do_get_file("data/test_readline1.txt");
117 test_channel(function() {
118 return new_file_channel(file);
119 });
120 }
122 // Uncomment test_ftp_channel in test_array to test this
123 function test_ftp_channel() {
124 test_channel(function() {
125 return makeChan("ftp://ftp.mozilla.org/pub/mozilla.org/README");
126 });
127 }
129 function end() {
130 httpserv.stop(do_test_finished);
131 }
133 function run_test() {
134 // start server
135 httpserv = new HttpServer();
136 httpserv.start(-1);
138 run_next_test();
139 }