Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / xpcom / tests / TestPipe.cpp
blob5376cc414d04c6d6e8003cad870661b8d9db72da
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)
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;
39 nsresult TestPipe()
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))
53 fail("TP_NewPipe2 failed: %x", rv);
54 return rv;
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)
71 fail("test error with string size");
72 return NS_ERROR_FAILURE;
75 uint32_t writeCount;
76 rv = output->Write(written, BUFFER_LENGTH, &writeCount);
77 if (NS_FAILED(rv) || writeCount != BUFFER_LENGTH)
79 fail("writing %d bytes (wrote %d bytes) to output failed: %x",
80 BUFFER_LENGTH, writeCount, rv);
81 return rv;
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)
89 fail("reading %d bytes (got %d bytes) from input failed: %x",
90 BUFFER_LENGTH, readCount, rv);
91 return rv;
94 if (0 != memcmp(written, read, BUFFER_LENGTH))
96 fail("didn't read the written data correctly!");
97 return NS_ERROR_FAILURE;
100 passed("TestPipe");
101 return NS_OK;
104 int main(int argc, char** argv)
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;