dom/system/gonk/tests/test_ril_worker_buf.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial