Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | function run_test() { |
michael@0 | 5 | run_next_test(); |
michael@0 | 6 | } |
michael@0 | 7 | |
michael@0 | 8 | /** |
michael@0 | 9 | * Add test function with specified parcel and request handler. |
michael@0 | 10 | * |
michael@0 | 11 | * @param parcel |
michael@0 | 12 | * Incoming parcel to be tested. |
michael@0 | 13 | * @param handler |
michael@0 | 14 | * Handler to be invoked as RIL request handler. |
michael@0 | 15 | */ |
michael@0 | 16 | function add_test_incoming_parcel(parcel, handler) { |
michael@0 | 17 | add_test(function test_incoming_parcel() { |
michael@0 | 18 | let worker = newWorker({ |
michael@0 | 19 | postRILMessage: function(data) { |
michael@0 | 20 | // do nothing |
michael@0 | 21 | }, |
michael@0 | 22 | postMessage: function(message) { |
michael@0 | 23 | // do nothing |
michael@0 | 24 | } |
michael@0 | 25 | }); |
michael@0 | 26 | |
michael@0 | 27 | if (!parcel) { |
michael@0 | 28 | parcel = newIncomingParcel(-1, |
michael@0 | 29 | worker.RESPONSE_TYPE_UNSOLICITED, |
michael@0 | 30 | worker.REQUEST_VOICE_REGISTRATION_STATE, |
michael@0 | 31 | [0, 0, 0, 0]); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | let context = worker.ContextPool._contexts[0]; |
michael@0 | 35 | // supports only requests less or equal than UINT8_MAX(255). |
michael@0 | 36 | let buf = context.Buf; |
michael@0 | 37 | let request = parcel[buf.PARCEL_SIZE_SIZE + buf.UINT32_SIZE]; |
michael@0 | 38 | context.RIL[request] = function ril_request_handler() { |
michael@0 | 39 | handler.apply(this, arguments); |
michael@0 | 40 | }; |
michael@0 | 41 | |
michael@0 | 42 | worker.onRILMessage(0, parcel); |
michael@0 | 43 | |
michael@0 | 44 | // end of incoming parcel's trip, let's do next test. |
michael@0 | 45 | run_next_test(); |
michael@0 | 46 | }); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // Test normal parcel handling. |
michael@0 | 50 | add_test_incoming_parcel(null, |
michael@0 | 51 | function test_normal_parcel_handling() { |
michael@0 | 52 | let self = this; |
michael@0 | 53 | do_check_throws(function normal_handler() { |
michael@0 | 54 | // reads exactly the same size, should not throw anything. |
michael@0 | 55 | self.context.Buf.readInt32(); |
michael@0 | 56 | }); |
michael@0 | 57 | } |
michael@0 | 58 | ); |
michael@0 | 59 | |
michael@0 | 60 | // Test parcel under read. |
michael@0 | 61 | add_test_incoming_parcel(null, |
michael@0 | 62 | function test_parcel_under_read() { |
michael@0 | 63 | let self = this; |
michael@0 | 64 | do_check_throws(function under_read_handler() { |
michael@0 | 65 | // reads less than parcel size, should not throw. |
michael@0 | 66 | self.context.Buf.readUint16(); |
michael@0 | 67 | }); |
michael@0 | 68 | } |
michael@0 | 69 | ); |
michael@0 | 70 | |
michael@0 | 71 | // Test parcel over read. |
michael@0 | 72 | add_test_incoming_parcel(null, |
michael@0 | 73 | function test_parcel_over_read() { |
michael@0 | 74 | let buf = this.context.Buf; |
michael@0 | 75 | |
michael@0 | 76 | // read all data available |
michael@0 | 77 | while (buf.readAvailable > 0) { |
michael@0 | 78 | buf.readUint8(); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | do_check_throws(function over_read_handler() { |
michael@0 | 82 | // reads more than parcel size, should throw an error. |
michael@0 | 83 | buf.readUint8(); |
michael@0 | 84 | },"Trying to read data beyond the parcel end!"); |
michael@0 | 85 | } |
michael@0 | 86 | ); |
michael@0 | 87 | |
michael@0 | 88 | // Test Bug 814761: buffer overwritten |
michael@0 | 89 | add_test(function test_incoming_parcel_buffer_overwritten() { |
michael@0 | 90 | let worker = newWorker({ |
michael@0 | 91 | postRILMessage: function(data) { |
michael@0 | 92 | // do nothing |
michael@0 | 93 | }, |
michael@0 | 94 | postMessage: function(message) { |
michael@0 | 95 | // do nothing |
michael@0 | 96 | } |
michael@0 | 97 | }); |
michael@0 | 98 | |
michael@0 | 99 | let context = worker.ContextPool._contexts[0]; |
michael@0 | 100 | // A convenient alias. |
michael@0 | 101 | let buf = context.Buf; |
michael@0 | 102 | |
michael@0 | 103 | // Allocate an array of specified size and set each of its elements to value. |
michael@0 | 104 | function calloc(length, value) { |
michael@0 | 105 | let array = new Array(length); |
michael@0 | 106 | for (let i = 0; i < length; i++) { |
michael@0 | 107 | array[i] = value; |
michael@0 | 108 | } |
michael@0 | 109 | return array; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // Do nothing in handleParcel(). |
michael@0 | 113 | let request = worker.REQUEST_VOICE_REGISTRATION_STATE; |
michael@0 | 114 | context.RIL[request] = null; |
michael@0 | 115 | |
michael@0 | 116 | // Prepare two parcels, whose sizes are both smaller than the incoming buffer |
michael@0 | 117 | // size but larger when combined, to trigger the bug. |
michael@0 | 118 | let pA_dataLength = buf.incomingBufferLength / 2; |
michael@0 | 119 | let pA = newIncomingParcel(-1, |
michael@0 | 120 | worker.RESPONSE_TYPE_UNSOLICITED, |
michael@0 | 121 | request, |
michael@0 | 122 | calloc(pA_dataLength, 1)); |
michael@0 | 123 | let pA_parcelSize = pA.length - buf.PARCEL_SIZE_SIZE; |
michael@0 | 124 | |
michael@0 | 125 | let pB_dataLength = buf.incomingBufferLength * 3 / 4; |
michael@0 | 126 | let pB = newIncomingParcel(-1, |
michael@0 | 127 | worker.RESPONSE_TYPE_UNSOLICITED, |
michael@0 | 128 | request, |
michael@0 | 129 | calloc(pB_dataLength, 1)); |
michael@0 | 130 | let pB_parcelSize = pB.length - buf.PARCEL_SIZE_SIZE; |
michael@0 | 131 | |
michael@0 | 132 | // First, send an incomplete pA and verifies related data pointer: |
michael@0 | 133 | let p1 = pA.subarray(0, pA.length - 1); |
michael@0 | 134 | worker.onRILMessage(0, p1); |
michael@0 | 135 | // The parcel should not have been processed. |
michael@0 | 136 | do_check_eq(buf.readAvailable, 0); |
michael@0 | 137 | // buf.currentParcelSize should have been set because incoming data has more |
michael@0 | 138 | // than 4 octets. |
michael@0 | 139 | do_check_eq(buf.currentParcelSize, pA_parcelSize); |
michael@0 | 140 | // buf.readIncoming should contains remaining unconsumed octets count. |
michael@0 | 141 | do_check_eq(buf.readIncoming, p1.length - buf.PARCEL_SIZE_SIZE); |
michael@0 | 142 | // buf.incomingWriteIndex should be ready to accept the last octet. |
michael@0 | 143 | do_check_eq(buf.incomingWriteIndex, p1.length); |
michael@0 | 144 | |
michael@0 | 145 | // Second, send the last octet of pA and whole pB. The Buf should now expand |
michael@0 | 146 | // to cover both pA & pB. |
michael@0 | 147 | let p2 = new Uint8Array(1 + pB.length); |
michael@0 | 148 | p2.set(pA.subarray(pA.length - 1), 0); |
michael@0 | 149 | p2.set(pB, 1); |
michael@0 | 150 | worker.onRILMessage(0, p2); |
michael@0 | 151 | // The parcels should have been both consumed. |
michael@0 | 152 | do_check_eq(buf.readAvailable, 0); |
michael@0 | 153 | // No parcel data remains. |
michael@0 | 154 | do_check_eq(buf.currentParcelSize, 0); |
michael@0 | 155 | // No parcel data remains. |
michael@0 | 156 | do_check_eq(buf.readIncoming, 0); |
michael@0 | 157 | // The Buf should now expand to cover both pA & pB. |
michael@0 | 158 | do_check_eq(buf.incomingWriteIndex, pA.length + pB.length); |
michael@0 | 159 | |
michael@0 | 160 | // end of incoming parcel's trip, let's do next test. |
michael@0 | 161 | run_next_test(); |
michael@0 | 162 | }); |
michael@0 | 163 | |
michael@0 | 164 | // Test Buf.readUint8Array. |
michael@0 | 165 | add_test_incoming_parcel(null, |
michael@0 | 166 | function test_buf_readUint8Array() { |
michael@0 | 167 | let buf = this.context.Buf; |
michael@0 | 168 | |
michael@0 | 169 | let u8array = buf.readUint8Array(1); |
michael@0 | 170 | do_check_eq(u8array instanceof Uint8Array, true); |
michael@0 | 171 | do_check_eq(u8array.length, 1); |
michael@0 | 172 | do_check_eq(buf.readAvailable, 3); |
michael@0 | 173 | |
michael@0 | 174 | u8array = buf.readUint8Array(2); |
michael@0 | 175 | do_check_eq(u8array.length, 2); |
michael@0 | 176 | do_check_eq(buf.readAvailable, 1); |
michael@0 | 177 | |
michael@0 | 178 | do_check_throws(function over_read_handler() { |
michael@0 | 179 | // reads more than parcel size, should throw an error. |
michael@0 | 180 | u8array = buf.readUint8Array(2); |
michael@0 | 181 | }, "Trying to read data beyond the parcel end!"); |
michael@0 | 182 | } |
michael@0 | 183 | ); |