xpcom/tests/TestPipe.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial