Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | const CC = Components.Constructor; |
michael@0 | 2 | |
michael@0 | 3 | const StreamCopier = CC("@mozilla.org/network/async-stream-copier;1", |
michael@0 | 4 | "nsIAsyncStreamCopier", |
michael@0 | 5 | "init"); |
michael@0 | 6 | |
michael@0 | 7 | const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1", |
michael@0 | 8 | "nsIScriptableInputStream", |
michael@0 | 9 | "init"); |
michael@0 | 10 | |
michael@0 | 11 | const Pipe = CC("@mozilla.org/pipe;1", |
michael@0 | 12 | "nsIPipe", |
michael@0 | 13 | "init"); |
michael@0 | 14 | |
michael@0 | 15 | var pipe1; |
michael@0 | 16 | var pipe2; |
michael@0 | 17 | var copier; |
michael@0 | 18 | var test_result; |
michael@0 | 19 | var test_content; |
michael@0 | 20 | var test_source_closed; |
michael@0 | 21 | var test_sink_closed; |
michael@0 | 22 | var test_nr; |
michael@0 | 23 | |
michael@0 | 24 | var copyObserver = |
michael@0 | 25 | { |
michael@0 | 26 | onStartRequest: function(request, context) { }, |
michael@0 | 27 | |
michael@0 | 28 | onStopRequest: function(request, cx, statusCode) |
michael@0 | 29 | { |
michael@0 | 30 | // check status code |
michael@0 | 31 | do_check_eq(statusCode, test_result); |
michael@0 | 32 | |
michael@0 | 33 | // check number of copied bytes |
michael@0 | 34 | do_check_eq(pipe2.inputStream.available(), test_content.length); |
michael@0 | 35 | |
michael@0 | 36 | // check content |
michael@0 | 37 | var scinp = new ScriptableInputStream(pipe2.inputStream); |
michael@0 | 38 | var content = scinp.read(scinp.available()); |
michael@0 | 39 | do_check_eq(content, test_content); |
michael@0 | 40 | |
michael@0 | 41 | // check closed sink |
michael@0 | 42 | try { |
michael@0 | 43 | pipe2.outputStream.write("closedSinkTest", 14); |
michael@0 | 44 | do_check_false(test_sink_closed); |
michael@0 | 45 | } |
michael@0 | 46 | catch (ex) { |
michael@0 | 47 | do_check_true(test_sink_closed); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | // check closed source |
michael@0 | 51 | try { |
michael@0 | 52 | pipe1.outputStream.write("closedSourceTest", 16); |
michael@0 | 53 | do_check_false(test_source_closed); |
michael@0 | 54 | } |
michael@0 | 55 | catch (ex) { |
michael@0 | 56 | do_check_true(test_source_closed); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | do_timeout(0, do_test); |
michael@0 | 60 | }, |
michael@0 | 61 | |
michael@0 | 62 | QueryInterface: function(aIID) |
michael@0 | 63 | { |
michael@0 | 64 | if (aIID.equals(Ci.nsIRequestObserver) || |
michael@0 | 65 | aIID.equals(Ci.nsISupports)) |
michael@0 | 66 | return this; |
michael@0 | 67 | |
michael@0 | 68 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 69 | } |
michael@0 | 70 | }; |
michael@0 | 71 | |
michael@0 | 72 | function startCopier(closeSource, closeSink) { |
michael@0 | 73 | pipe1 = new Pipe(true /* nonBlockingInput */, |
michael@0 | 74 | true /* nonBlockingOutput */, |
michael@0 | 75 | 0 /* segmentSize */, |
michael@0 | 76 | 0xffffffff /* segmentCount */, |
michael@0 | 77 | null /* segmentAllocator */); |
michael@0 | 78 | |
michael@0 | 79 | pipe2 = new Pipe(true /* nonBlockingInput */, |
michael@0 | 80 | true /* nonBlockingOutput */, |
michael@0 | 81 | 0 /* segmentSize */, |
michael@0 | 82 | 0xffffffff /* segmentCount */, |
michael@0 | 83 | null /* segmentAllocator */); |
michael@0 | 84 | |
michael@0 | 85 | copier = new StreamCopier(pipe1.inputStream /* aSource */, |
michael@0 | 86 | pipe2.outputStream /* aSink */, |
michael@0 | 87 | null /* aTarget */, |
michael@0 | 88 | true /* aSourceBuffered */, |
michael@0 | 89 | true /* aSinkBuffered */, |
michael@0 | 90 | 8192 /* aChunkSize */, |
michael@0 | 91 | closeSource /* aCloseSource */, |
michael@0 | 92 | closeSink /* aCloseSink */); |
michael@0 | 93 | |
michael@0 | 94 | copier.asyncCopy(copyObserver, null); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | function do_test() { |
michael@0 | 98 | |
michael@0 | 99 | test_nr++; |
michael@0 | 100 | test_content = "test" + test_nr; |
michael@0 | 101 | |
michael@0 | 102 | switch (test_nr) { |
michael@0 | 103 | case 1: |
michael@0 | 104 | case 2: // close sink |
michael@0 | 105 | case 3: // close source |
michael@0 | 106 | case 4: // close both |
michael@0 | 107 | // test canceling transfer |
michael@0 | 108 | // use some undefined error code to check if it is successfully passed |
michael@0 | 109 | // to the request observer |
michael@0 | 110 | test_result = 0x87654321; |
michael@0 | 111 | |
michael@0 | 112 | test_source_closed = ((test_nr-1)>>1 != 0); |
michael@0 | 113 | test_sink_closed = ((test_nr-1)%2 != 0); |
michael@0 | 114 | |
michael@0 | 115 | startCopier(test_source_closed, test_sink_closed); |
michael@0 | 116 | pipe1.outputStream.write(test_content, test_content.length); |
michael@0 | 117 | pipe1.outputStream.flush(); |
michael@0 | 118 | do_timeout(20, |
michael@0 | 119 | function(){ |
michael@0 | 120 | copier.cancel(test_result); |
michael@0 | 121 | pipe1.outputStream.write("a", 1);}); |
michael@0 | 122 | break; |
michael@0 | 123 | case 5: |
michael@0 | 124 | case 6: // close sink |
michael@0 | 125 | case 7: // close source |
michael@0 | 126 | case 8: // close both |
michael@0 | 127 | // test copying with EOF on source |
michael@0 | 128 | test_result = 0; |
michael@0 | 129 | |
michael@0 | 130 | test_source_closed = ((test_nr-5)>>1 != 0); |
michael@0 | 131 | test_sink_closed = ((test_nr-5)%2 != 0); |
michael@0 | 132 | |
michael@0 | 133 | startCopier(test_source_closed, test_sink_closed); |
michael@0 | 134 | pipe1.outputStream.write(test_content, test_content.length); |
michael@0 | 135 | // we will close the source |
michael@0 | 136 | test_source_closed = true; |
michael@0 | 137 | pipe1.outputStream.close(); |
michael@0 | 138 | break; |
michael@0 | 139 | case 9: |
michael@0 | 140 | case 10: // close sink |
michael@0 | 141 | case 11: // close source |
michael@0 | 142 | case 12: // close both |
michael@0 | 143 | // test copying with error on sink |
michael@0 | 144 | // use some undefined error code to check if it is successfully passed |
michael@0 | 145 | // to the request observer |
michael@0 | 146 | test_result = 0x87654321; |
michael@0 | 147 | |
michael@0 | 148 | test_source_closed = ((test_nr-9)>>1 != 0); |
michael@0 | 149 | test_sink_closed = ((test_nr-9)%2 != 0); |
michael@0 | 150 | |
michael@0 | 151 | startCopier(test_source_closed, test_sink_closed); |
michael@0 | 152 | pipe1.outputStream.write(test_content, test_content.length); |
michael@0 | 153 | pipe1.outputStream.flush(); |
michael@0 | 154 | // we will close the sink |
michael@0 | 155 | test_sink_closed = true; |
michael@0 | 156 | do_timeout(20, |
michael@0 | 157 | function() |
michael@0 | 158 | { |
michael@0 | 159 | pipe2.outputStream |
michael@0 | 160 | .QueryInterface(Ci.nsIAsyncOutputStream) |
michael@0 | 161 | .closeWithStatus(test_result); |
michael@0 | 162 | pipe1.outputStream.write("a", 1);}); |
michael@0 | 163 | break; |
michael@0 | 164 | case 13: |
michael@0 | 165 | do_test_finished(); |
michael@0 | 166 | break; |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | function run_test() { |
michael@0 | 171 | test_nr = 0; |
michael@0 | 172 | do_timeout(0, do_test); |
michael@0 | 173 | do_test_pending(); |
michael@0 | 174 | } |