xpcom/tests/TestStringAPI.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 <stdio.h>
     6 #include "nsStringAPI.h"
     8 #define CHECK(x) \
     9   _doCheck(x, #x, __LINE__)
    11 int _doCheck(bool cond, const char* msg, int line) {
    12   if (cond) return 0;
    13   fprintf(stderr, "FAIL: line %d: %s\n", line, msg);
    14   return 1;
    15 }
    17 int testEmpty() {
    18   nsString s;
    19   return CHECK(0 == s.Length()) +
    20          CHECK(s.IsEmpty());
    21 }
    23 int testAccess() {
    24   nsString s;
    25   s.Assign(NS_LITERAL_STRING("hello"));
    26   int res = CHECK(5 == s.Length()) +
    27             CHECK(s.EqualsLiteral("hello"));
    28   const char16_t *it, *end;
    29   int len = s.BeginReading(&it, &end);
    30   res += CHECK(5 == len);
    31   res += CHECK(char16_t('h') == it[0]) +
    32          CHECK(char16_t('e') == it[1]) +
    33          CHECK(char16_t('l') == it[2]) +
    34          CHECK(char16_t('l') == it[3]) +
    35          CHECK(char16_t('o') == it[4]) +
    36          CHECK(it + len == end);
    37   res += CHECK(s[0] == s.First());
    38   for (int i = 0; i < len; ++i) {
    39     res += CHECK(s[i] == it[i]);
    40     res += CHECK(s[i] == s.CharAt(i));
    41   }
    42   res += CHECK(it == s.BeginReading());
    43   res += CHECK(end == s.EndReading());
    44   return res;
    45 }
    47 int testWrite() {
    48   nsString s(NS_LITERAL_STRING("xyzz"));
    49   char16_t *begin, *end;
    50   int res = CHECK(4 == s.Length());
    51   uint32_t len = s.BeginWriting(&begin, &end, 5);
    52   res += CHECK(5 == s.Length()) +
    53          CHECK(5 == len) +
    54          CHECK(end == begin + 5) +
    55          CHECK(begin == s.BeginWriting()) +
    56          CHECK(end == s.EndWriting());
    57   begin[4] = char16_t('y');
    58   res += CHECK(s.Equals(NS_LITERAL_STRING("xyzzy")));
    59   s.SetLength(4);
    60   res += CHECK(4 == s.Length()) +
    61          CHECK(s.Equals(NS_LITERAL_STRING("xyzz"))) +
    62          CHECK(!s.Equals(NS_LITERAL_STRING("xyzzy"))) +
    63          CHECK(!s.IsEmpty());
    64   s.Truncate();
    65   res += CHECK(0 == s.Length()) +
    66          CHECK(s.IsEmpty());
    67   const char16_t sample[] = { 's', 'a', 'm', 'p', 'l', 'e', '\0' };
    68   s.Assign(sample);
    69   res += CHECK(s.EqualsLiteral("sample"));
    70   s.Assign(sample, 4);
    71   res += CHECK(s.EqualsLiteral("samp"));
    72   s.Assign(char16_t('q'));
    73   res += CHECK(s.EqualsLiteral("q"));
    74   return res;
    75 }
    77 int testFind() {
    78   nsString str_haystack;
    79   nsString str_needle;
    80   str_needle.AssignLiteral("world");
    82   int32_t ret = 0;
    83   ret += CHECK(-1 == str_haystack.Find("world"));
    84   ret += CHECK(-1 == str_haystack.Find(str_needle));
    86   str_haystack.AssignLiteral("hello world hello world hello");
    87   ret += CHECK( 6 == str_haystack.Find("world"));
    88   ret += CHECK( 6 == str_haystack.Find(str_needle));
    89   ret += CHECK(-1 == str_haystack.Find("world", 20, false));
    90   ret += CHECK(-1 == str_haystack.Find(str_needle, 20));
    91   ret += CHECK(18 == str_haystack.Find("world", 12, false));
    92   ret += CHECK(18 == str_haystack.Find(str_needle, 12));
    94   nsCString cstr_haystack;
    95   nsCString cstr_needle;
    96   cstr_needle.AssignLiteral("world");
    98   ret += CHECK(-1 == cstr_haystack.Find("world"));
    99   ret += CHECK(-1 == cstr_haystack.Find(cstr_needle));
   101   cstr_haystack.AssignLiteral("hello world hello world hello");
   102   ret += CHECK( 6 == cstr_haystack.Find("world"));
   103   ret += CHECK( 6 == cstr_haystack.Find(cstr_needle));
   104   ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20));
   105   ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12));
   106   ret += CHECK( 6 == cstr_haystack.Find("world", 5));
   108   return ret;
   109 }
   111 int testVoid() {
   112   nsString s;
   113   int ret = CHECK(!s.IsVoid());
   114   s.SetIsVoid(false);
   115   ret += CHECK(!s.IsVoid());
   116   s.SetIsVoid(true);
   117   ret += CHECK(s.IsVoid());
   118   s.SetIsVoid(false);
   119   ret += CHECK(!s.IsVoid());
   120   s.SetIsVoid(true);
   121   s.AssignLiteral("hello");
   122   ret += CHECK(!s.IsVoid());
   123   return ret;
   124 }
   126 int testRFind() {
   127   int32_t ret = 0;
   129   // nsString.RFind
   130   nsString str_haystack;
   131   nsString str_needle;
   132   str_needle.AssignLiteral("world");
   134   ret += CHECK(-1 == str_haystack.RFind(str_needle));
   135   ret += CHECK(-1 == str_haystack.RFind("world"));
   137   str_haystack.AssignLiteral("hello world hElLo woRlD");
   139   ret += CHECK( 6 == str_haystack.RFind(str_needle));
   140   ret += CHECK( 6 == str_haystack.RFind(str_needle, -1));
   141   ret += CHECK( 6 == str_haystack.RFind(str_needle, 17));
   142   ret += CHECK( 6 == str_haystack.RFind("world", false));
   143   ret += CHECK(18 == str_haystack.RFind("world", true));
   144   ret += CHECK( 6 == str_haystack.RFind("world", -1, false));
   145   ret += CHECK(18 == str_haystack.RFind("world", -1, true));
   146   ret += CHECK( 6 == str_haystack.RFind("world", 17, false));
   147   ret += CHECK( 0 == str_haystack.RFind("hello", 0, false));
   148   ret += CHECK(18 == str_haystack.RFind("world", 19, true));
   149   ret += CHECK(18 == str_haystack.RFind("world", 22, true));
   150   ret += CHECK(18 == str_haystack.RFind("world", 23, true));
   152   // nsCString.RFind
   153   nsCString cstr_haystack;
   154   nsCString cstr_needle;
   155   cstr_needle.AssignLiteral("world");
   157   ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle));
   158   ret += CHECK(-1 == cstr_haystack.RFind("world"));
   160   cstr_haystack.AssignLiteral("hello world hElLo woRlD");
   162   ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle));
   163   ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1));
   164   ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17));
   165   ret += CHECK( 6 == cstr_haystack.RFind("world", 5));
   166   ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0));
   168   return ret;
   169 }
   171 int testCompressWhitespace() {
   172   int32_t ret = 0;
   174   // CompressWhitespace utility function
   175   nsString s;
   177   s.AssignLiteral("     ");
   178   CompressWhitespace(s);
   179   ret += CHECK(s.EqualsLiteral(""));
   181   s.AssignLiteral("  no more  leading spaces");
   182   CompressWhitespace(s);
   183   ret += CHECK(s.EqualsLiteral("no more leading spaces"));
   185   s.AssignLiteral("no    more trailing spaces ");
   186   CompressWhitespace(s);
   187   ret += CHECK(s.EqualsLiteral("no more trailing spaces"));
   189   s.AssignLiteral("   hello one    2         three    45        ");
   190   CompressWhitespace(s);
   191   ret += CHECK(s.EqualsLiteral("hello one 2 three 45"));
   193   return ret;
   194 }
   196 int main() {
   197   int rv = 0;
   198   rv += testEmpty();
   199   rv += testAccess();
   200   rv += testWrite();
   201   rv += testFind();
   202   rv += testVoid();
   203   rv += testRFind();
   204   rv += testCompressWhitespace();
   205   if (0 == rv) {
   206     fprintf(stderr, "PASS: StringAPI tests\n");
   207   }
   208   return rv;
   209 }

mercurial