michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestHarness.h" michael@0: michael@0: #include "nsIPipe.h" michael@0: #include "nsIMemory.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsIAsyncInputStream.h" michael@0: #include "nsIAsyncOutputStream.h" michael@0: michael@0: /** NS_NewPipe2 reimplemented, because it's not exported by XPCOM */ michael@0: nsresult TP_NewPipe2(nsIAsyncInputStream** input, michael@0: nsIAsyncOutputStream** output, michael@0: bool nonBlockingInput, michael@0: bool nonBlockingOutput, michael@0: uint32_t segmentSize, michael@0: uint32_t segmentCount) michael@0: { michael@0: nsCOMPtr pipe = do_CreateInstance("@mozilla.org/pipe;1"); michael@0: if (!pipe) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsresult rv = pipe->Init(nonBlockingInput, michael@0: nonBlockingOutput, michael@0: segmentSize, michael@0: segmentCount); michael@0: michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: pipe->GetInputStream(input); michael@0: pipe->GetOutputStream(output); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult TestPipe() michael@0: { michael@0: const uint32_t SEGMENT_COUNT = 10; michael@0: const uint32_t SEGMENT_SIZE = 10; michael@0: michael@0: nsCOMPtr input; michael@0: nsCOMPtr output; michael@0: nsresult rv = TP_NewPipe2(getter_AddRefs(input), michael@0: getter_AddRefs(output), michael@0: false, michael@0: false, michael@0: SEGMENT_SIZE, SEGMENT_COUNT); michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: fail("TP_NewPipe2 failed: %x", rv); michael@0: return rv; michael@0: } michael@0: michael@0: const uint32_t BUFFER_LENGTH = 100; michael@0: const char written[] = michael@0: "0123456789" michael@0: "1123456789" michael@0: "2123456789" michael@0: "3123456789" michael@0: "4123456789" michael@0: "5123456789" michael@0: "6123456789" michael@0: "7123456789" michael@0: "8123456789" michael@0: "9123456789"; // not just a memset, to ensure the allocator works correctly michael@0: if (sizeof(written) < BUFFER_LENGTH) michael@0: { michael@0: fail("test error with string size"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: uint32_t writeCount; michael@0: rv = output->Write(written, BUFFER_LENGTH, &writeCount); michael@0: if (NS_FAILED(rv) || writeCount != BUFFER_LENGTH) michael@0: { michael@0: fail("writing %d bytes (wrote %d bytes) to output failed: %x", michael@0: BUFFER_LENGTH, writeCount, rv); michael@0: return rv; michael@0: } michael@0: michael@0: char read[BUFFER_LENGTH]; michael@0: uint32_t readCount; michael@0: rv = input->Read(read, BUFFER_LENGTH, &readCount); michael@0: if (NS_FAILED(rv) || readCount != BUFFER_LENGTH) michael@0: { michael@0: fail("reading %d bytes (got %d bytes) from input failed: %x", michael@0: BUFFER_LENGTH, readCount, rv); michael@0: return rv; michael@0: } michael@0: michael@0: if (0 != memcmp(written, read, BUFFER_LENGTH)) michael@0: { michael@0: fail("didn't read the written data correctly!"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: passed("TestPipe"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: ScopedXPCOM xpcom("nsPipe"); michael@0: if (xpcom.failed()) michael@0: return 1; michael@0: michael@0: int rv = 0; michael@0: michael@0: if (NS_FAILED(TestPipe())) michael@0: rv = 1; michael@0: michael@0: return rv; michael@0: }