tor-browser

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

testArrayBufferWithUserOwnedContents.cpp (1565B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 */
      4 
      5 #include <stdint.h>  // uint32_t
      6 
      7 #include "js/ArrayBuffer.h"  // JS::{DetachArrayBuffer,GetArrayBuffer{ByteLength,Data},IsArrayBufferObject,NewArrayBufferWithUserOwnedContents}
      8 #include "js/GCAPI.h"        // JS::AutoCheckCannotGC, JS_GC
      9 #include "js/RootingAPI.h"   // JS::Rooted
     10 #include "jsapi-tests/tests.h"
     11 #include "vm/ArrayBufferObject.h"
     12 
     13 char testData[] =
     14    "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     15 
     16 constexpr size_t testDataLength = sizeof(testData);
     17 
     18 static void GC(JSContext* cx) {
     19  JS_GC(cx);
     20  // Trigger another to wait for background finalization to end.
     21  JS_GC(cx);
     22 }
     23 
     24 BEGIN_TEST(testArrayBufferWithUserOwnedContents) {
     25  JS::Rooted<JSObject*> obj(cx, JS::NewArrayBufferWithUserOwnedContents(
     26                                    cx, testDataLength, testData));
     27  GC(cx);
     28  CHECK(VerifyObject(obj, testDataLength));
     29  GC(cx);
     30  JS::DetachArrayBuffer(cx, obj);
     31  GC(cx);
     32  CHECK(VerifyObject(obj, 0));
     33 
     34  return true;
     35 }
     36 
     37 bool VerifyObject(JS::HandleObject obj, uint32_t length) {
     38  JS::AutoCheckCannotGC nogc;
     39 
     40  CHECK(obj);
     41  CHECK(JS::IsArrayBufferObject(obj));
     42  CHECK_EQUAL(JS::GetArrayBufferByteLength(obj), length);
     43  bool sharedDummy;
     44  const char* data = reinterpret_cast<const char*>(
     45      JS::GetArrayBufferData(obj, &sharedDummy, nogc));
     46  if (length == testDataLength) {
     47    CHECK(data);
     48    CHECK(testData == data);
     49  }
     50 
     51  return true;
     52 }
     53 
     54 END_TEST(testArrayBufferWithUserOwnedContents)