xpcom/tests/TestStreamUtils.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 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include <stdlib.h>
     8 #include <stdio.h>
     9 #include "nsIPipe.h"
    10 #include "nsStreamUtils.h"
    11 #include "nsString.h"
    12 #include "nsCOMPtr.h"
    14 //----
    16 static bool test_consume_stream() {
    17   const char kData[] =
    18       "Get your facts first, and then you can distort them as much as you "
    19       "please.";
    21   nsCOMPtr<nsIInputStream> input;
    22   nsCOMPtr<nsIOutputStream> output;
    23   NS_NewPipe(getter_AddRefs(input),
    24              getter_AddRefs(output),
    25              10, UINT32_MAX);
    26   if (!input || !output)
    27     return false;
    29   uint32_t n = 0;
    30   output->Write(kData, sizeof(kData) - 1, &n);
    31   if (n != (sizeof(kData) - 1))
    32     return false;
    33   output = nullptr;  // close output
    35   nsCString buf;
    36   if (NS_FAILED(NS_ConsumeStream(input, UINT32_MAX, buf)))
    37     return false;
    39   if (!buf.Equals(kData))
    40     return false;
    42   return true; 
    43 }
    45 //----
    47 typedef bool (*TestFunc)();
    48 #define DECL_TEST(name) { #name, name }
    50 static const struct Test {
    51   const char* name;
    52   TestFunc    func;
    53 } tests[] = {
    54   DECL_TEST(test_consume_stream),
    55   { nullptr, nullptr }
    56 };
    58 int main(int argc, char **argv) {
    59   int count = 1;
    60   if (argc > 1)
    61     count = atoi(argv[1]);
    63   if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
    64     return -1;
    66   while (count--) {
    67     for (const Test* t = tests; t->name != nullptr; ++t) {
    68       printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
    69     }
    70   }
    72   NS_ShutdownXPCOM(nullptr);
    73   return 0;
    74 }

mercurial