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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "TestCommon.h"
#include "gtest/gtest.h"
#include "nsISocketTransportService.h"
#include "nsISocketTransport.h"
#include "nsIServerSocket.h"
#include "nsIAsyncInputStream.h"
#include "mozilla/net/DNS.h"
#include "prerror.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
using namespace mozilla::net;
using namespace mozilla;
class ServerListener : public nsIServerSocketListener {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISERVERSOCKETLISTENER
explicit ServerListener(WaitForCondition* waiter);
// Port that is got from server side will be store here.
uint32_t mClientPort;
bool mFailed;
RefPtr<WaitForCondition> mWaiter;
private:
virtual ~ServerListener();
};
NS_IMPL_ISUPPORTS(ServerListener, nsIServerSocketListener)
ServerListener::ServerListener(WaitForCondition* waiter)
: mClientPort(-1), mFailed(false), mWaiter(waiter) {}
ServerListener::~ServerListener() = default;
NS_IMETHODIMP
ServerListener::OnSocketAccepted(nsIServerSocket* aServ,
nsISocketTransport* aTransport) {
// Run on STS thread.
NetAddr peerAddr;
nsresult rv = aTransport->GetPeerAddr(&peerAddr);
if (NS_FAILED(rv)) {
mFailed = true;
mWaiter->Notify();
return NS_OK;
}
mClientPort = PR_ntohs(peerAddr.inet.port);
mWaiter->Notify();
return NS_OK;
}
NS_IMETHODIMP
ServerListener::OnStopListening(nsIServerSocket* aServ, nsresult aStatus) {
return NS_OK;
}
class ClientInputCallback : public nsIInputStreamCallback {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINPUTSTREAMCALLBACK
explicit ClientInputCallback(WaitForCondition* waiter);
bool mFailed;
RefPtr<WaitForCondition> mWaiter;
private:
virtual ~ClientInputCallback();
};
NS_IMPL_ISUPPORTS(ClientInputCallback, nsIInputStreamCallback)
ClientInputCallback::ClientInputCallback(WaitForCondition* waiter)
: mFailed(false), mWaiter(waiter) {}
ClientInputCallback::~ClientInputCallback() = default;
NS_IMETHODIMP
ClientInputCallback::OnInputStreamReady(nsIAsyncInputStream* aStream) {
// Server doesn't send. That means if we are here, we probably have run into
// an error.
uint64_t avail;
nsresult rv = aStream->Available(&avail);
if (NS_FAILED(rv)) {
mFailed = true;
}
mWaiter->Notify();
return NS_OK;
}
TEST(TestBind, MainTest)
{
//
// Server side.
//
nsCOMPtr<nsIServerSocket> server =
do_CreateInstance("@mozilla.org/network/server-socket;1");
ASSERT_TRUE(server);
nsresult rv = server->Init(-1, true, -1);
ASSERT_TRUE(NS_SUCCEEDED(rv));
int32_t serverPort;
rv = server->GetPort(&serverPort);
ASSERT_TRUE(NS_SUCCEEDED(rv));
RefPtr<WaitForCondition> waiter = new WaitForCondition();
// Listening.
RefPtr<ServerListener> serverListener = new ServerListener(waiter);
rv = server->AsyncListen(serverListener);
ASSERT_TRUE(NS_SUCCEEDED(rv));
//
// Client side
//
uint32_t bindingPort = 20000;
nsCOMPtr<nsISocketTransportService> sts =
do_GetService("@mozilla.org/network/socket-transport-service;1", &rv);
ASSERT_TRUE(NS_SUCCEEDED(rv));
nsCOMPtr<nsIInputStream> inputStream;
RefPtr<ClientInputCallback> clientCallback;
for (int32_t tried = 0; tried < 100; tried++) {
nsCOMPtr<nsISocketTransport> client;
rv = sts->CreateTransport(nsTArray<nsCString>(), "127.0.0.1"_ns, serverPort,
nullptr, getter_AddRefs(client));
ASSERT_TRUE(NS_SUCCEEDED(rv));
// Bind to a port. It's possible that we are binding to a port that is
// currently in use. If we failed to bind, we try next port.
NetAddr bindingAddr;
bindingAddr.inet.family = AF_INET;
bindingAddr.inet.ip = 0;
bindingAddr.inet.port = PR_htons(bindingPort);
rv = client->Bind(&bindingAddr);
ASSERT_TRUE(NS_SUCCEEDED(rv));
// Open IO streams, to make client SocketTransport connect to server.
clientCallback = new ClientInputCallback(waiter);
rv = client->OpenInputStream(nsITransport::OPEN_UNBUFFERED, 0, 0,
getter_AddRefs(inputStream));
ASSERT_TRUE(NS_SUCCEEDED(rv));
nsCOMPtr<nsIAsyncInputStream> asyncInputStream =
do_QueryInterface(inputStream);
rv = asyncInputStream->AsyncWait(clientCallback, 0, 0, nullptr);
// Wait for server's response or callback of input stream.
waiter->Wait(1);
if (clientCallback->mFailed) {
// if client received error, we likely have bound a port that is in use.
// we can try another port.
bindingPort++;
} else {
// We are unlocked by server side, leave the loop and check result.
break;
}
}
ASSERT_FALSE(serverListener->mFailed);
ASSERT_EQ(serverListener->mClientPort, bindingPort);
inputStream->Close();
waiter->Wait(1);
ASSERT_TRUE(clientCallback->mFailed);
server->Close();
}
|