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