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.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "mozilla/ArrayUtils.h"
michael@0 6
michael@0 7 #include <stdio.h>
michael@0 8 #include <stdlib.h>
michael@0 9 #include "nsString.h"
michael@0 10 #include "nsStringBuffer.h"
michael@0 11 #include "nsReadableUtils.h"
michael@0 12 #include "UTFStrings.h"
michael@0 13 #include "nsUnicharUtils.h"
michael@0 14 #include "mozilla/HashFunctions.h"
michael@0 15
michael@0 16 using namespace mozilla;
michael@0 17
michael@0 18 namespace TestUTF {
michael@0 19
michael@0 20 bool
michael@0 21 test_valid()
michael@0 22 {
michael@0 23 for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
michael@0 24 nsDependentCString str8(ValidStrings[i].m8);
michael@0 25 nsDependentString str16(ValidStrings[i].m16);
michael@0 26
michael@0 27 if (!NS_ConvertUTF16toUTF8(str16).Equals(str8))
michael@0 28 return false;
michael@0 29
michael@0 30 if (!NS_ConvertUTF8toUTF16(str8).Equals(str16))
michael@0 31 return false;
michael@0 32
michael@0 33 nsCString tmp8("string ");
michael@0 34 AppendUTF16toUTF8(str16, tmp8);
michael@0 35 if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8))
michael@0 36 return false;
michael@0 37
michael@0 38 nsString tmp16(NS_LITERAL_STRING("string "));
michael@0 39 AppendUTF8toUTF16(str8, tmp16);
michael@0 40 if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16))
michael@0 41 return false;
michael@0 42
michael@0 43 if (CompareUTF8toUTF16(str8, str16) != 0)
michael@0 44 return false;
michael@0 45 }
michael@0 46
michael@0 47 return true;
michael@0 48 }
michael@0 49
michael@0 50 bool
michael@0 51 test_invalid16()
michael@0 52 {
michael@0 53 for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) {
michael@0 54 nsDependentString str16(Invalid16Strings[i].m16);
michael@0 55 nsDependentCString str8(Invalid16Strings[i].m8);
michael@0 56
michael@0 57 if (!NS_ConvertUTF16toUTF8(str16).Equals(str8))
michael@0 58 return false;
michael@0 59
michael@0 60 nsCString tmp8("string ");
michael@0 61 AppendUTF16toUTF8(str16, tmp8);
michael@0 62 if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8))
michael@0 63 return false;
michael@0 64
michael@0 65 if (CompareUTF8toUTF16(str8, str16) != 0)
michael@0 66 return false;
michael@0 67 }
michael@0 68
michael@0 69 return true;
michael@0 70 }
michael@0 71
michael@0 72 bool
michael@0 73 test_invalid8()
michael@0 74 {
michael@0 75 for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
michael@0 76 nsDependentString str16(Invalid8Strings[i].m16);
michael@0 77 nsDependentCString str8(Invalid8Strings[i].m8);
michael@0 78
michael@0 79 if (!NS_ConvertUTF8toUTF16(str8).Equals(str16))
michael@0 80 return false;
michael@0 81
michael@0 82 nsString tmp16(NS_LITERAL_STRING("string "));
michael@0 83 AppendUTF8toUTF16(str8, tmp16);
michael@0 84 if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16))
michael@0 85 return false;
michael@0 86
michael@0 87 if (CompareUTF8toUTF16(str8, str16) != 0)
michael@0 88 return false;
michael@0 89 }
michael@0 90
michael@0 91 return true;
michael@0 92 }
michael@0 93
michael@0 94 bool
michael@0 95 test_malformed8()
michael@0 96 {
michael@0 97 // Don't run this test in debug builds as that intentionally asserts.
michael@0 98 #ifndef DEBUG
michael@0 99 for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
michael@0 100 nsDependentCString str8(Malformed8Strings[i]);
michael@0 101
michael@0 102 if (!NS_ConvertUTF8toUTF16(str8).IsEmpty())
michael@0 103 return false;
michael@0 104
michael@0 105 nsString tmp16(NS_LITERAL_STRING("string"));
michael@0 106 AppendUTF8toUTF16(str8, tmp16);
michael@0 107 if (!tmp16.Equals(NS_LITERAL_STRING("string")))
michael@0 108 return false;
michael@0 109
michael@0 110 if (CompareUTF8toUTF16(str8, EmptyString()) == 0)
michael@0 111 return false;
michael@0 112 }
michael@0 113 #endif
michael@0 114
michael@0 115 return true;
michael@0 116 }
michael@0 117
michael@0 118 bool
michael@0 119 test_hashas16()
michael@0 120 {
michael@0 121 for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
michael@0 122 nsDependentCString str8(ValidStrings[i].m8);
michael@0 123 bool err;
michael@0 124 if (HashString(ValidStrings[i].m16) !=
michael@0 125 HashUTF8AsUTF16(str8.get(), str8.Length(), &err) ||
michael@0 126 err)
michael@0 127 return false;
michael@0 128 }
michael@0 129
michael@0 130 for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
michael@0 131 nsDependentCString str8(Invalid8Strings[i].m8);
michael@0 132 bool err;
michael@0 133 if (HashString(Invalid8Strings[i].m16) !=
michael@0 134 HashUTF8AsUTF16(str8.get(), str8.Length(), &err) ||
michael@0 135 err)
michael@0 136 return false;
michael@0 137 }
michael@0 138
michael@0 139 // Don't run this test in debug builds as that intentionally asserts.
michael@0 140 #ifndef DEBUG
michael@0 141 for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
michael@0 142 nsDependentCString str8(Malformed8Strings[i]);
michael@0 143 bool err;
michael@0 144 if (HashUTF8AsUTF16(str8.get(), str8.Length(), &err) != 0 ||
michael@0 145 !err)
michael@0 146 return false;
michael@0 147 }
michael@0 148 #endif
michael@0 149
michael@0 150 return true;
michael@0 151 }
michael@0 152
michael@0 153 typedef bool (*TestFunc)();
michael@0 154
michael@0 155 static const struct Test
michael@0 156 {
michael@0 157 const char* name;
michael@0 158 TestFunc func;
michael@0 159 }
michael@0 160 tests[] =
michael@0 161 {
michael@0 162 { "test_valid", test_valid },
michael@0 163 { "test_invalid16", test_invalid16 },
michael@0 164 { "test_invalid8", test_invalid8 },
michael@0 165 { "test_malformed8", test_malformed8 },
michael@0 166 { "test_hashas16", test_hashas16 },
michael@0 167 { nullptr, nullptr }
michael@0 168 };
michael@0 169
michael@0 170 }
michael@0 171
michael@0 172 using namespace TestUTF;
michael@0 173
michael@0 174 int main(int argc, char **argv)
michael@0 175 {
michael@0 176 int count = 1;
michael@0 177 if (argc > 1)
michael@0 178 count = atoi(argv[1]);
michael@0 179
michael@0 180 while (count--)
michael@0 181 {
michael@0 182 for (const Test* t = tests; t->name != nullptr; ++t)
michael@0 183 {
michael@0 184 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 return 0;
michael@0 189 }

mercurial