1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#include "gtest/gtest.h"
#include "nsIStreamTransportService.h"
#include "nsStreamUtils.h"
#include "nsThreadUtils.h"
#include "Helpers.h"
#include "nsNetCID.h"
#include "nsServiceManagerUtils.h"
#include "nsITransport.h"
#include "nsNetUtil.h"
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
void CreateStream(already_AddRefed<nsIInputStream> aSource,
nsIAsyncInputStream** aStream) {
nsCOMPtr<nsIInputStream> source = std::move(aSource);
nsresult rv;
nsCOMPtr<nsIStreamTransportService> sts =
do_GetService(kStreamTransportServiceCID, &rv);
ASSERT_EQ(NS_OK, rv);
nsCOMPtr<nsITransport> transport;
rv = sts->CreateInputTransport(source, true, getter_AddRefs(transport));
ASSERT_EQ(NS_OK, rv);
nsCOMPtr<nsIInputStream> wrapper;
rv = transport->OpenInputStream(0, 0, 0, getter_AddRefs(wrapper));
ASSERT_EQ(NS_OK, rv);
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(wrapper);
MOZ_RELEASE_ASSERT(asyncStream);
asyncStream.forget(aStream);
}
class BlockingSyncStream final : public nsIInputStream {
nsCOMPtr<nsIInputStream> mStream;
public:
NS_DECL_THREADSAFE_ISUPPORTS
explicit BlockingSyncStream(const nsACString& aBuffer) {
NS_NewCStringInputStream(getter_AddRefs(mStream), aBuffer);
}
NS_IMETHOD
Available(uint64_t* aLength) override { return mStream->Available(aLength); }
NS_IMETHOD
StreamStatus() override { return mStream->StreamStatus(); }
NS_IMETHOD
Read(char* aBuffer, uint32_t aCount, uint32_t* aReadCount) override {
return mStream->Read(aBuffer, aCount, aReadCount);
}
NS_IMETHOD
ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, uint32_t aCount,
uint32_t* aResult) override {
return mStream->ReadSegments(aWriter, aClosure, aCount, aResult);
}
NS_IMETHOD
Close() override { return mStream->Close(); }
NS_IMETHOD
IsNonBlocking(bool* aNonBlocking) override {
*aNonBlocking = false;
return NS_OK;
}
private:
~BlockingSyncStream() = default;
};
NS_IMPL_ISUPPORTS(BlockingSyncStream, nsIInputStream)
// Testing a simple blocking stream.
TEST(TestInputStreamTransport, BlockingNotAsync)
{
RefPtr<BlockingSyncStream> stream = new BlockingSyncStream("Hello world"_ns);
nsCOMPtr<nsIAsyncInputStream> ais;
CreateStream(stream.forget(), getter_AddRefs(ais));
ASSERT_TRUE(!!ais);
nsAutoCString data;
nsresult rv = NS_ReadInputStreamToString(ais, data, -1);
ASSERT_EQ(NS_OK, rv);
ASSERT_TRUE(data.EqualsLiteral("Hello world"));
}
class BlockingAsyncStream final : public nsIAsyncInputStream {
nsCOMPtr<nsIInputStream> mStream;
bool mPending;
public:
NS_DECL_THREADSAFE_ISUPPORTS
explicit BlockingAsyncStream(const nsACString& aBuffer) : mPending(false) {
NS_NewCStringInputStream(getter_AddRefs(mStream), aBuffer);
}
NS_IMETHOD
Available(uint64_t* aLength) override {
mStream->Available(aLength);
// 1 char at the time, just to test the asyncWait+Read loop a bit more.
if (*aLength > 0) {
*aLength = 1;
}
return NS_OK;
}
NS_IMETHOD
StreamStatus() override { return mStream->StreamStatus(); }
NS_IMETHOD
Read(char* aBuffer, uint32_t aCount, uint32_t* aReadCount) override {
mPending = !mPending;
if (mPending) {
return NS_BASE_STREAM_WOULD_BLOCK;
}
// 1 char at the time, just to test the asyncWait+Read loop a bit more.
aCount = 1;
return mStream->Read(aBuffer, aCount, aReadCount);
}
NS_IMETHOD
ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, uint32_t aCount,
uint32_t* aResult) override {
mPending = !mPending;
if (mPending) {
return NS_BASE_STREAM_WOULD_BLOCK;
}
// 1 char at the time, just to test the asyncWait+Read loop a bit more.
aCount = 1;
return mStream->ReadSegments(aWriter, aClosure, aCount, aResult);
}
NS_IMETHOD
Close() override { return mStream->Close(); }
NS_IMETHOD
IsNonBlocking(bool* aNonBlocking) override {
*aNonBlocking = false;
return NS_OK;
}
NS_IMETHOD
CloseWithStatus(nsresult aStatus) override { return Close(); }
NS_IMETHOD
AsyncWait(nsIInputStreamCallback* aCallback, uint32_t aFlags,
uint32_t aRequestedCount, nsIEventTarget* aEventTarget) override {
if (!aCallback) {
return NS_OK;
}
RefPtr<BlockingAsyncStream> self = this;
nsCOMPtr<nsIInputStreamCallback> callback = aCallback;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
"gtest-asyncwait",
[self, callback]() { callback->OnInputStreamReady(self); });
if (aEventTarget) {
aEventTarget->Dispatch(r.forget());
} else {
r->Run();
}
return NS_OK;
}
private:
~BlockingAsyncStream() = default;
};
NS_IMPL_ISUPPORTS(BlockingAsyncStream, nsIInputStream, nsIAsyncInputStream)
// Testing an async blocking stream.
TEST(TestInputStreamTransport, BlockingAsync)
{
RefPtr<BlockingAsyncStream> stream =
new BlockingAsyncStream("Hello world"_ns);
nsCOMPtr<nsIAsyncInputStream> ais;
CreateStream(stream.forget(), getter_AddRefs(ais));
ASSERT_TRUE(!!ais);
nsAutoCString data;
nsresult rv = NS_ReadInputStreamToString(ais, data, -1);
ASSERT_EQ(NS_OK, rv);
ASSERT_TRUE(data.EqualsLiteral("Hello world"));
}
|