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"
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
,
20 uint32_t segmentCount
)
22 nsCOMPtr
<nsIPipe
> pipe
= do_CreateInstance("@mozilla.org/pipe;1");
24 return NS_ERROR_OUT_OF_MEMORY
;
26 nsresult rv
= pipe
->Init(nonBlockingInput
,
34 pipe
->GetInputStream(input
);
35 pipe
->GetOutputStream(output
);
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
),
50 SEGMENT_SIZE
, SEGMENT_COUNT
);
53 fail("TP_NewPipe2 failed: %x", rv
);
57 const uint32_t BUFFER_LENGTH
= 100;
58 const char written
[] =
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
;
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
);
84 char read
[BUFFER_LENGTH
];
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
);
94 if (0 != memcmp(written
, read
, BUFFER_LENGTH
))
96 fail("didn't read the written data correctly!");
97 return NS_ERROR_FAILURE
;
104 int main(int argc
, char** argv
)
106 ScopedXPCOM
xpcom("nsPipe");
112 if (NS_FAILED(TestPipe()))