tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

array-buffers.js (3044B)


      1 // Tests for ArrayBuffer, TypedArray, and DataView encoding/decoding. 
      2 
      3 var clonebuffer = serialize("dummy");
      4 
      5 // ========= V2 =========
      6 
      7 function testV2Int32Array() {
      8    var buf = new Uint8Array([3,0,0,0,0,0,241,255,3,0,0,0,16,0,255,255,4,0,0,0,0,0,0,0,12,0,0,0,9,0,255,255,1,0,0,0,177,127,57,5,133,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0]);
      9    clonebuffer.clonebuffer = buf.buffer;
     10    var ta = deserialize(clonebuffer);
     11    assertEq(ta instanceof Int32Array, true);
     12    assertEq(ta.toString(), "1,87654321,-123");
     13 }
     14 testV2Int32Array();
     15 
     16 function testV2Float64Array() {
     17    var buf = new Uint8Array([3,0,0,0,0,0,241,255,4,0,0,0,16,0,255,255,7,0,0,0,0,0,0,0,32,0,0,0,9,0,255,255,0,0,0,0,0,0,248,127,31,133,235,81,184,30,9,64,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
     18    clonebuffer.clonebuffer = buf.buffer;
     19    var ta = deserialize(clonebuffer);
     20    assertEq(ta instanceof Float64Array, true);
     21    assertEq(ta.toString(), "NaN,3.14,0,0");
     22 }
     23 testV2Float64Array();
     24 
     25 function testV2DataView() {
     26    var buf = new Uint8Array([3,0,0,0,0,0,241,255,3,0,0,0,21,0,255,255,3,0,0,0,9,0,255,255,5,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0]);
     27    clonebuffer.clonebuffer = buf.buffer;
     28    var dv = deserialize(clonebuffer);
     29    assertEq(dv instanceof DataView, true);
     30    assertEq(new Uint8Array(dv.buffer).toString(), "5,0,255");
     31 }
     32 testV2DataView();
     33 
     34 function testV2ArrayBuffer() {
     35    var buf = new Uint8Array([3,0,0,0,0,0,241,255,4,0,0,0,9,0,255,255,33,44,55,66,0,0,0,0]);
     36    clonebuffer.clonebuffer = buf.buffer;
     37    var ab = deserialize(clonebuffer);
     38    assertEq(ab instanceof ArrayBuffer, true);
     39    assertEq(new Uint8Array(ab).toString(), "33,44,55,66");
     40 }
     41 testV2ArrayBuffer();
     42 
     43 // ========= Current =========
     44 
     45 function testInt32Array() {
     46    var ta1 = new Int32Array([1, 87654321, -123]);
     47    var clonebuf = serialize(ta1, undefined, {scope: "DifferentProcessForIndexedDB"});
     48    var ta2 = deserialize(clonebuf);
     49    assertEq(ta2 instanceof Int32Array, true);
     50    assertEq(ta2.toString(), "1,87654321,-123");
     51 }
     52 testInt32Array();
     53 
     54 function testFloat64Array() {
     55    var ta1 = new Float64Array([NaN, 3.14, 0, 0]);
     56    var clonebuf = serialize(ta1, undefined, {scope: "DifferentProcessForIndexedDB"});
     57    var ta2 = deserialize(clonebuf);
     58    assertEq(ta2 instanceof Float64Array, true);
     59    assertEq(ta2.toString(), "NaN,3.14,0,0");
     60 }
     61 testFloat64Array();
     62 
     63 function testDataView() {
     64    var ta = new Uint8Array([5, 0, 255]);
     65    var dv1 = new DataView(ta.buffer);
     66    var clonebuf = serialize(dv1, undefined, {scope: "DifferentProcessForIndexedDB"});
     67    var dv2 = deserialize(clonebuf);
     68    assertEq(dv2 instanceof DataView, true);
     69    assertEq(new Uint8Array(dv2.buffer).toString(), "5,0,255");
     70 }
     71 testDataView();
     72 
     73 function testArrayBuffer() {
     74    var ta = new Uint8Array([33, 44, 55, 66]);
     75    var clonebuf = serialize(ta.buffer, undefined, {scope: "DifferentProcessForIndexedDB"});
     76    var ab = deserialize(clonebuf);
     77    assertEq(ab instanceof ArrayBuffer, true);
     78    assertEq(new Uint8Array(ab).toString(), "33,44,55,66");
     79 }
     80 testArrayBuffer();