xpcom/tests/TestUTF.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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/. */
     5 #include "mozilla/ArrayUtils.h"
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 #include "nsString.h"
    10 #include "nsStringBuffer.h"
    11 #include "nsReadableUtils.h"
    12 #include "UTFStrings.h"
    13 #include "nsUnicharUtils.h"
    14 #include "mozilla/HashFunctions.h"
    16 using namespace mozilla;
    18 namespace TestUTF {
    20 bool
    21 test_valid()
    22 {
    23   for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
    24     nsDependentCString str8(ValidStrings[i].m8);
    25     nsDependentString str16(ValidStrings[i].m16);
    27     if (!NS_ConvertUTF16toUTF8(str16).Equals(str8))
    28       return false;
    30     if (!NS_ConvertUTF8toUTF16(str8).Equals(str16))
    31       return false;
    33     nsCString tmp8("string ");
    34     AppendUTF16toUTF8(str16, tmp8);
    35     if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8))
    36       return false;
    38     nsString tmp16(NS_LITERAL_STRING("string "));
    39     AppendUTF8toUTF16(str8, tmp16);
    40     if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16))
    41       return false;
    43     if (CompareUTF8toUTF16(str8, str16) != 0)
    44       return false;
    45   }
    47   return true;
    48 }
    50 bool
    51 test_invalid16()
    52 {
    53   for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) {
    54     nsDependentString str16(Invalid16Strings[i].m16);
    55     nsDependentCString str8(Invalid16Strings[i].m8);
    57     if (!NS_ConvertUTF16toUTF8(str16).Equals(str8))
    58       return false;
    60     nsCString tmp8("string ");
    61     AppendUTF16toUTF8(str16, tmp8);
    62     if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8))
    63       return false;
    65     if (CompareUTF8toUTF16(str8, str16) != 0)
    66       return false;
    67   }
    69   return true;
    70 }
    72 bool
    73 test_invalid8()
    74 {
    75   for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
    76     nsDependentString str16(Invalid8Strings[i].m16);
    77     nsDependentCString str8(Invalid8Strings[i].m8);
    79     if (!NS_ConvertUTF8toUTF16(str8).Equals(str16))
    80       return false;
    82     nsString tmp16(NS_LITERAL_STRING("string "));
    83     AppendUTF8toUTF16(str8, tmp16);
    84     if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16))
    85       return false;
    87     if (CompareUTF8toUTF16(str8, str16) != 0)
    88       return false;
    89   }
    91   return true;
    92 }
    94 bool
    95 test_malformed8()
    96 {
    97 // Don't run this test in debug builds as that intentionally asserts.
    98 #ifndef DEBUG
    99   for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
   100     nsDependentCString str8(Malformed8Strings[i]);
   102     if (!NS_ConvertUTF8toUTF16(str8).IsEmpty())
   103       return false;
   105     nsString tmp16(NS_LITERAL_STRING("string"));
   106     AppendUTF8toUTF16(str8, tmp16);
   107     if (!tmp16.Equals(NS_LITERAL_STRING("string")))
   108       return false;
   110     if (CompareUTF8toUTF16(str8, EmptyString()) == 0)
   111       return false;
   112   }
   113 #endif
   115   return true;
   116 }
   118 bool
   119 test_hashas16()
   120 {
   121   for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
   122     nsDependentCString str8(ValidStrings[i].m8);
   123     bool err;
   124     if (HashString(ValidStrings[i].m16) !=
   125         HashUTF8AsUTF16(str8.get(), str8.Length(), &err) ||
   126         err)
   127       return false;
   128   }
   130   for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
   131     nsDependentCString str8(Invalid8Strings[i].m8);
   132     bool err;
   133     if (HashString(Invalid8Strings[i].m16) !=
   134         HashUTF8AsUTF16(str8.get(), str8.Length(), &err) ||
   135         err)
   136       return false;
   137   }
   139 // Don't run this test in debug builds as that intentionally asserts.
   140 #ifndef DEBUG
   141   for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
   142     nsDependentCString str8(Malformed8Strings[i]);
   143     bool err;
   144     if (HashUTF8AsUTF16(str8.get(), str8.Length(), &err) != 0 ||
   145         !err)
   146       return false;
   147   }
   148 #endif
   150   return true;
   151 }
   153 typedef bool (*TestFunc)();
   155 static const struct Test
   156   {
   157     const char* name;
   158     TestFunc    func;
   159   }
   160 tests[] =
   161   {
   162     { "test_valid", test_valid },
   163     { "test_invalid16", test_invalid16 },
   164     { "test_invalid8", test_invalid8 },
   165     { "test_malformed8", test_malformed8 },
   166     { "test_hashas16", test_hashas16 },
   167     { nullptr, nullptr }
   168   };
   170 }
   172 using namespace TestUTF;
   174 int main(int argc, char **argv)
   175   {
   176     int count = 1;
   177     if (argc > 1)
   178       count = atoi(argv[1]);
   180     while (count--)
   181       {
   182         for (const Test* t = tests; t->name != nullptr; ++t)
   183           {
   184             printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
   185           }
   186       }
   188     return 0;
   189   }

mercurial