tor-browser

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

testStringBuffers.cpp (5344B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "mozilla/StringBuffer.h"
      6 
      7 #include "jsapi.h"
      8 
      9 #include "gc/Zone.h"
     10 #include "js/String.h"
     11 #include "jsapi-tests/tests.h"
     12 #include "util/Text.h"
     13 
     14 BEGIN_TEST(testStringBuffersLatin1) {
     15  static const JS::Latin1Char chars[] = "This is just some random string";
     16  static const size_t len = js_strlen(chars);
     17 
     18  RefPtr<mozilla::StringBuffer> buffer =
     19      mozilla::StringBuffer::Create(chars, len);
     20  CHECK(buffer);
     21 
     22  auto* bufferChars = static_cast<const JS::Latin1Char*>(buffer->Data());
     23 
     24  // Don't purge the ExternalStringCache.
     25  js::gc::AutoSuppressGC suppress(cx);
     26 
     27  JS::Rooted<JSString*> str1(cx,
     28                             JS::NewStringFromLatin1Buffer(cx, buffer, len));
     29  CHECK(str1);
     30  CHECK_EQUAL(JS_GetStringLength(str1), len);
     31  {
     32    JS::AutoCheckCannotGC nogc;
     33    size_t strLen;
     34    const JS::Latin1Char* strChars =
     35        JS_GetLatin1StringCharsAndLength(cx, nogc, str1, &strLen);
     36    CHECK_EQUAL(strLen, len);
     37    CHECK_EQUAL(strChars, bufferChars);
     38  }
     39 
     40  JS::Rooted<JSString*> str2(cx,
     41                             JS::NewStringFromLatin1Buffer(cx, buffer, len));
     42  CHECK(str2);
     43 
     44  cx->zone()->externalStringCache().purge();
     45 
     46  JS::Rooted<JSString*> str3(
     47      cx, JS::NewStringFromKnownLiveLatin1Buffer(cx, buffer, len));
     48  CHECK(str3);
     49 
     50  // Check the ExternalStringCache works.
     51  CHECK_EQUAL(str1, str2);
     52 
     53 #ifdef DEBUG
     54  // Three references: buffer, str1/str2, str3.
     55  CHECK_EQUAL(buffer->RefCount(), 3u);
     56 #endif
     57 
     58  mozilla::StringBuffer* buf;
     59  CHECK(!JS::IsTwoByteStringWithStringBuffer(str2, &buf));
     60  CHECK(JS::IsLatin1StringWithStringBuffer(str2, &buf));
     61  CHECK_EQUAL(buf, buffer);
     62 
     63  return true;
     64 }
     65 END_TEST(testStringBuffersLatin1)
     66 
     67 BEGIN_TEST(testStringBuffersTwoByte) {
     68  static const char16_t chars[] = u"This is just some random string";
     69  static const size_t len = js_strlen(chars);
     70 
     71  RefPtr<mozilla::StringBuffer> buffer =
     72      mozilla::StringBuffer::Create(chars, len);
     73  CHECK(buffer);
     74 
     75  auto* bufferChars = static_cast<const char16_t*>(buffer->Data());
     76 
     77  // Don't purge the ExternalStringCache.
     78  js::gc::AutoSuppressGC suppress(cx);
     79 
     80  JS::Rooted<JSString*> str1(cx,
     81                             JS::NewStringFromTwoByteBuffer(cx, buffer, len));
     82  CHECK(str1);
     83  CHECK_EQUAL(JS_GetStringLength(str1), len);
     84  {
     85    JS::AutoCheckCannotGC nogc;
     86    size_t strLen;
     87    const char16_t* strChars =
     88        JS_GetTwoByteStringCharsAndLength(cx, nogc, str1, &strLen);
     89    CHECK_EQUAL(strLen, len);
     90    CHECK_EQUAL(strChars, bufferChars);
     91  }
     92 
     93  JS::Rooted<JSString*> str2(cx,
     94                             JS::NewStringFromTwoByteBuffer(cx, buffer, len));
     95  CHECK(str2);
     96 
     97  cx->zone()->externalStringCache().purge();
     98 
     99  JS::Rooted<JSString*> str3(
    100      cx, JS::NewStringFromKnownLiveTwoByteBuffer(cx, buffer, len));
    101  CHECK(str3);
    102 
    103  // Check the ExternalStringCache works.
    104  CHECK_EQUAL(str1, str2);
    105 
    106 #ifdef DEBUG
    107  // Three references: buffer, str1/str2, str3.
    108  CHECK_EQUAL(buffer->RefCount(), 3u);
    109 #endif
    110 
    111  mozilla::StringBuffer* buf;
    112  CHECK(!JS::IsLatin1StringWithStringBuffer(str2, &buf));
    113  CHECK(JS::IsTwoByteStringWithStringBuffer(str2, &buf));
    114  CHECK_EQUAL(buf, buffer);
    115 
    116  return true;
    117 }
    118 END_TEST(testStringBuffersTwoByte)
    119 
    120 BEGIN_TEST(testStringBuffersUTF8) {
    121  // UTF8 ASCII string buffer.
    122  {
    123    static const char chars[] = "This is a UTF-8 string but also ASCII";
    124    static const size_t len = js_strlen(chars);
    125 
    126    RefPtr<mozilla::StringBuffer> buffer =
    127        mozilla::StringBuffer::Create(chars, len);
    128    CHECK(buffer);
    129 
    130    // Don't purge the ExternalStringCache.
    131    js::gc::AutoSuppressGC suppress(cx);
    132 
    133    JS::Rooted<JSString*> str1(cx,
    134                               JS::NewStringFromUTF8Buffer(cx, buffer, len));
    135    CHECK(str1);
    136    CHECK_EQUAL(JS_GetStringLength(str1), len);
    137 
    138    mozilla::StringBuffer* buf;
    139    CHECK(!JS::IsTwoByteStringWithStringBuffer(str1, &buf));
    140    CHECK(JS::IsLatin1StringWithStringBuffer(str1, &buf));
    141    CHECK_EQUAL(buf, buffer);
    142 
    143    JS::Rooted<JSString*> str2(
    144        cx, JS::NewStringFromKnownLiveUTF8Buffer(cx, buf, len));
    145    CHECK(str2);
    146 
    147    // Check the ExternalStringCache works.
    148    CHECK_EQUAL(str1, str2);
    149 
    150 #ifdef DEBUG
    151    CHECK_EQUAL(buffer->RefCount(), 2u);  // |buffer| and str1/str2.
    152 #endif
    153  }
    154 
    155  // UTF8 non-ASCII string buffer. The passed in buffer isn't used.
    156  {
    157    static const char chars[] =
    158        "This is a UTF-\xEF\xBC\x98 string but not ASCII";
    159    static const size_t len = js_strlen(chars);
    160 
    161    RefPtr<mozilla::StringBuffer> buffer =
    162        mozilla::StringBuffer::Create(chars, len);
    163    CHECK(buffer);
    164 
    165    JS::Rooted<JSString*> str1(cx,
    166                               JS::NewStringFromUTF8Buffer(cx, buffer, len));
    167    CHECK(str1);
    168    CHECK_EQUAL(JS_GetStringLength(str1), 36u);
    169 
    170    mozilla::StringBuffer* buf;
    171    CHECK(!JS::IsLatin1StringWithStringBuffer(str1, &buf));
    172    CHECK(!JS::IsTwoByteStringWithStringBuffer(str1, &buf));
    173 
    174    JS::Rooted<JSString*> str2(
    175        cx, JS::NewStringFromKnownLiveUTF8Buffer(cx, buffer, len));
    176    CHECK(str2);
    177 
    178 #ifdef DEBUG
    179    CHECK_EQUAL(buffer->RefCount(), 1u);  // Just |buffer|.
    180 #endif
    181  }
    182 
    183  return true;
    184 }
    185 END_TEST(testStringBuffersUTF8)