xpcom/tests/TestCRT.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 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsCRT.h"
michael@0 7 #include "nsString.h"
michael@0 8 #include "plstr.h"
michael@0 9 #include <stdlib.h>
michael@0 10
michael@0 11 namespace TestCRT {
michael@0 12
michael@0 13 // The return from strcmp etc is only defined to be postive, zero or
michael@0 14 // negative. The magnitude of a non-zero return is irrelevant.
michael@0 15 int sign(int val) {
michael@0 16 if (val == 0)
michael@0 17 return 0;
michael@0 18 else {
michael@0 19 if (val > 0)
michael@0 20 return 1;
michael@0 21 else
michael@0 22 return -1;
michael@0 23 }
michael@0 24 }
michael@0 25
michael@0 26
michael@0 27 // Verify that nsCRT versions of string comparison routines get the
michael@0 28 // same answers as the native non-unicode versions. We only pass in
michael@0 29 // iso-latin-1 strings, so the comparison must be valid.
michael@0 30 static void Check(const char* s1, const char* s2, int n)
michael@0 31 {
michael@0 32 #ifdef DEBUG
michael@0 33 int clib =
michael@0 34 #endif
michael@0 35 PL_strcmp(s1, s2);
michael@0 36
michael@0 37 #ifdef DEBUG
michael@0 38 int clib_n =
michael@0 39 #endif
michael@0 40 PL_strncmp(s1, s2, n);
michael@0 41
michael@0 42 nsAutoString t1,t2;
michael@0 43 t1.AssignWithConversion(s1);
michael@0 44 t2.AssignWithConversion(s2);
michael@0 45 const char16_t* us1 = t1.get();
michael@0 46 const char16_t* us2 = t2.get();
michael@0 47
michael@0 48 #ifdef DEBUG
michael@0 49 int u2 =
michael@0 50 #endif
michael@0 51 nsCRT::strcmp(us1, us2);
michael@0 52
michael@0 53 #ifdef DEBUG
michael@0 54 int u2_n =
michael@0 55 #endif
michael@0 56 nsCRT::strncmp(us1, us2, n);
michael@0 57
michael@0 58 NS_ASSERTION(sign(clib) == sign(u2), "strcmp");
michael@0 59 NS_ASSERTION(sign(clib_n) == sign(u2_n), "strncmp");
michael@0 60 }
michael@0 61
michael@0 62 struct Test {
michael@0 63 const char* s1;
michael@0 64 const char* s2;
michael@0 65 int n;
michael@0 66 };
michael@0 67
michael@0 68 static Test tests[] = {
michael@0 69 { "foo", "foo", 3 },
michael@0 70 { "foo", "fo", 3 },
michael@0 71
michael@0 72 { "foo", "bar", 3 },
michael@0 73 { "foo", "ba", 3 },
michael@0 74
michael@0 75 { "foo", "zap", 3 },
michael@0 76 { "foo", "za", 3 },
michael@0 77
michael@0 78 { "bar", "foo", 3 },
michael@0 79 { "bar", "fo", 3 },
michael@0 80
michael@0 81 { "bar", "foo", 3 },
michael@0 82 { "bar", "fo", 3 },
michael@0 83 };
michael@0 84 #define NUM_TESTS int((sizeof(tests) / sizeof(tests[0])))
michael@0 85
michael@0 86 }
michael@0 87
michael@0 88 using namespace TestCRT;
michael@0 89
michael@0 90 int main()
michael@0 91 {
michael@0 92 Test* tp = tests;
michael@0 93 for (int i = 0; i < NUM_TESTS; i++, tp++) {
michael@0 94 Check(tp->s1, tp->s2, tp->n);
michael@0 95 }
michael@0 96
michael@0 97 return 0;
michael@0 98 }

mercurial