tor-browser

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

testDeflateStringToUTF8Buffer.cpp (5918B)


      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 "jsapi-tests/tests.h"
      6 
      7 using namespace JS;
      8 
      9 BEGIN_TEST(test_DeflateStringToUTF8Buffer) {
     10  JSString* str;
     11  JSLinearString* linearStr;
     12 
     13  // DeflateStringToUTF8Buffer does not write a null terminator, so the byte
     14  // following the last byte written to the |actual| buffer should retain
     15  // the value it held before the call to DeflateStringToUTF8Buffer, which is
     16  // initialized to 0x1.
     17 
     18  char actual[100];
     19  auto span = mozilla::Span(actual);
     20 
     21  // Test with an ASCII string, which calls JSLinearString::latin1Chars
     22  // to retrieve the characters from the string and generates UTF-8 output
     23  // that is identical to the ASCII input.
     24 
     25  str = JS_NewStringCopyZ(cx, "Ohai");  // { 0x4F, 0x68, 0x61, 0x69 }
     26  MOZ_RELEASE_ASSERT(str);
     27  linearStr = JS_EnsureLinearString(cx, str);
     28 
     29  {
     30    const char expected[] = {0x4F, 0x68, 0x61, 0x69, 0x1};
     31    memset(actual, 0x1, 100);
     32    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span);
     33    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
     34    CHECK_EQUAL(dstlen, 4u);
     35  }
     36 
     37  {
     38    const char expected[] = {0x4F, 0x68, 0x61, 0x1};
     39    memset(actual, 0x1, 100);
     40    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(3));
     41    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
     42    CHECK_EQUAL(dstlen, 3u);
     43  }
     44 
     45  {
     46    const unsigned char expected[] = {0x1};
     47    memset(actual, 0x1, 100);
     48    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(0));
     49    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
     50    CHECK_EQUAL(dstlen, 0u);
     51  }
     52 
     53  // Test with a Latin-1 string, which calls JSLinearString::latin1Chars
     54  // like with the ASCII string but generates UTF-8 output that is different
     55  // from the ASCII input.
     56 
     57  str = JS_NewUCStringCopyZ(cx, u"\xD3\x68\xE3\xEF");  // u"Óhãï"
     58  MOZ_RELEASE_ASSERT(str);
     59  linearStr = JS_EnsureLinearString(cx, str);
     60 
     61  {
     62    const unsigned char expected[] = {0xC3, 0x93, 0x68, 0xC3,
     63                                      0xA3, 0xC3, 0xAF, 0x1};
     64    memset(actual, 0x1, 100);
     65    JS::DeflateStringToUTF8Buffer(linearStr, span);
     66    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
     67  }
     68 
     69  {
     70    const unsigned char expected[] = {0xC3, 0x93, 0x68, 0xC3,
     71                                      0xA3, 0xC3, 0xAF, 0x1};
     72    memset(actual, 0x1, 100);
     73    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(7));
     74    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
     75    CHECK_EQUAL(dstlen, 7u);
     76  }
     77 
     78  {
     79    // Specify a destination buffer length of 3.  That's exactly enough
     80    // space to encode the first two characters, which takes three bytes.
     81    const unsigned char expected[] = {0xC3, 0x93, 0x68, 0x1};
     82    memset(actual, 0x1, 100);
     83    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(3));
     84    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
     85    CHECK_EQUAL(dstlen, 3u);
     86  }
     87 
     88  {
     89    // Specify a destination buffer length of 4.  That's only enough space
     90    // to encode the first two characters, which takes three bytes, because
     91    // the third character would take another two bytes.
     92    const unsigned char expected[] = {0xC3, 0x93, 0x68, 0x1};
     93    memset(actual, 0x1, 100);
     94    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(4));
     95    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
     96    CHECK_EQUAL(dstlen, 3u);
     97  }
     98 
     99  {
    100    const unsigned char expected[] = {0x1};
    101    memset(actual, 0x1, 100);
    102    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(0));
    103    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
    104    CHECK_EQUAL(dstlen, 0u);
    105  }
    106 
    107  // Test with a UTF-16 string, which calls JSLinearString::twoByteChars
    108  // to retrieve the characters from the string.
    109 
    110  str = JS_NewUCStringCopyZ(cx, u"\x038C\x0068\x0203\x0457");  // u"Όhȃї"
    111  MOZ_RELEASE_ASSERT(str);
    112  linearStr = JS_EnsureLinearString(cx, str);
    113 
    114  {
    115    const unsigned char expected[] = {0xCE, 0x8C, 0x68, 0xC8,
    116                                      0x83, 0xD1, 0x97, 0x1};
    117    memset(actual, 0x1, 100);
    118    JS::DeflateStringToUTF8Buffer(linearStr, span);
    119    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
    120  }
    121 
    122  {
    123    const unsigned char expected[] = {0xCE, 0x8C, 0x68, 0xC8,
    124                                      0x83, 0xD1, 0x97, 0x1};
    125    memset(actual, 0x1, 100);
    126    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(7));
    127    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
    128    CHECK_EQUAL(dstlen, 7u);
    129  }
    130 
    131  {
    132    // Specify a destination buffer length of 3.  That's exactly enough
    133    // space to encode the first two characters, which takes three bytes.
    134    const unsigned char expected[] = {0xCE, 0x8C, 0x68, 0x1};
    135    memset(actual, 0x1, 100);
    136    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(3));
    137    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
    138    CHECK_EQUAL(dstlen, 3u);
    139  }
    140 
    141  {
    142    // Specify a destination buffer length of 4.  That's only enough space
    143    // to encode the first two characters, which takes three bytes, because
    144    // the third character would take another two bytes.
    145    const unsigned char expected[] = {0xCE, 0x8C, 0x68, 0x1};
    146    memset(actual, 0x1, 100);
    147    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(4));
    148    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
    149    CHECK_EQUAL(dstlen, 3u);
    150  }
    151 
    152  {
    153    const unsigned char expected[] = {0x1};
    154    memset(actual, 0x1, 100);
    155    size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(0));
    156    CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
    157    CHECK_EQUAL(dstlen, 0u);
    158  }
    159 
    160  return true;
    161 }
    162 END_TEST(test_DeflateStringToUTF8Buffer)