xpcom/tests/TestPipe.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: 4 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "TestHarness.h"
     8 #include "nsIPipe.h"
     9 #include "nsIMemory.h"
    10 #include "mozilla/Attributes.h"
    11 #include "nsIAsyncInputStream.h"
    12 #include "nsIAsyncOutputStream.h"
    14 /** NS_NewPipe2 reimplemented, because it's not exported by XPCOM */
    15 nsresult TP_NewPipe2(nsIAsyncInputStream** input,
    16                      nsIAsyncOutputStream** output,
    17                      bool nonBlockingInput,
    18                      bool nonBlockingOutput,
    19                      uint32_t segmentSize,
    20                      uint32_t segmentCount)
    21 {
    22   nsCOMPtr<nsIPipe> pipe = do_CreateInstance("@mozilla.org/pipe;1");
    23   if (!pipe)
    24     return NS_ERROR_OUT_OF_MEMORY;
    26   nsresult rv = pipe->Init(nonBlockingInput,
    27                            nonBlockingOutput,
    28                            segmentSize,
    29                            segmentCount);
    31   if (NS_FAILED(rv))
    32     return rv;
    34   pipe->GetInputStream(input);
    35   pipe->GetOutputStream(output);
    36   return NS_OK;
    37 }
    39 nsresult TestPipe()
    40 {
    41   const uint32_t SEGMENT_COUNT = 10;
    42   const uint32_t SEGMENT_SIZE = 10;
    44   nsCOMPtr<nsIAsyncInputStream> input;
    45   nsCOMPtr<nsIAsyncOutputStream> output;
    46   nsresult rv = TP_NewPipe2(getter_AddRefs(input),
    47                    getter_AddRefs(output),
    48                    false,
    49                    false,
    50                    SEGMENT_SIZE, SEGMENT_COUNT); 
    51   if (NS_FAILED(rv))
    52   {
    53     fail("TP_NewPipe2 failed: %x", rv);
    54     return rv;
    55   }
    57   const uint32_t BUFFER_LENGTH = 100;
    58   const char written[] =
    59     "0123456789"
    60     "1123456789"
    61     "2123456789"
    62     "3123456789"
    63     "4123456789"
    64     "5123456789"
    65     "6123456789"
    66     "7123456789"
    67     "8123456789"
    68     "9123456789"; // not just a memset, to ensure the allocator works correctly
    69   if (sizeof(written) < BUFFER_LENGTH)
    70   {
    71     fail("test error with string size");
    72     return NS_ERROR_FAILURE;
    73   }
    75   uint32_t writeCount;
    76   rv = output->Write(written, BUFFER_LENGTH, &writeCount);
    77   if (NS_FAILED(rv) || writeCount != BUFFER_LENGTH)
    78   {
    79     fail("writing %d bytes (wrote %d bytes) to output failed: %x",
    80          BUFFER_LENGTH, writeCount, rv);
    81     return rv;
    82   }
    84   char read[BUFFER_LENGTH];
    85   uint32_t readCount;
    86   rv = input->Read(read, BUFFER_LENGTH, &readCount);
    87   if (NS_FAILED(rv) || readCount != BUFFER_LENGTH)
    88   {
    89     fail("reading %d bytes (got %d bytes) from input failed: %x",
    90          BUFFER_LENGTH, readCount,  rv);
    91     return rv;
    92   }
    94   if (0 != memcmp(written, read, BUFFER_LENGTH))
    95   {
    96     fail("didn't read the written data correctly!");
    97     return NS_ERROR_FAILURE;
    98   }
   100   passed("TestPipe");
   101   return NS_OK;
   102 }
   104 int main(int argc, char** argv)
   105 {
   106   ScopedXPCOM xpcom("nsPipe");
   107   if (xpcom.failed())
   108     return 1;
   110   int rv = 0;
   112   if (NS_FAILED(TestPipe()))
   113     rv = 1;
   115   return rv;
   116 }

mercurial