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
205
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/tests/test_net_address_private_untrusted.h"
#include <limits>
#include <sstream>
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/private/net_address_private.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
REGISTER_TEST_CASE(NetAddressPrivateUntrusted);
using pp::NetAddressPrivate;
using pp::TCPSocketPrivate;
TestNetAddressPrivateUntrusted::TestNetAddressPrivateUntrusted(
TestingInstance* instance) : TestCase(instance), port_(0) {
}
bool TestNetAddressPrivateUntrusted::Init() {
bool net_address_private_is_available = NetAddressPrivate::IsAvailable();
if (!net_address_private_is_available)
instance_->AppendError("PPB_NetAddress_Private interface not available");
bool tcp_socket_private_is_available = TCPSocketPrivate::IsAvailable();
if (!tcp_socket_private_is_available)
instance_->AppendError("PPB_TCPSocket_Private interface not available");
bool init_host_port =
GetLocalHostPort(instance_->pp_instance(), &host_, &port_);
if (!init_host_port)
instance_->AppendError("Can't init host and port");
return net_address_private_is_available &&
tcp_socket_private_is_available &&
init_host_port &&
CheckTestingInterface();
}
void TestNetAddressPrivateUntrusted::RunTests(const std::string& filter) {
RUN_TEST(AreEqual, filter);
RUN_TEST(AreHostsEqual, filter);
RUN_TEST(Describe, filter);
RUN_TEST(ReplacePort, filter);
RUN_TEST(GetAnyAddress, filter);
RUN_TEST(GetFamily, filter);
RUN_TEST(GetPort, filter);
RUN_TEST(GetAddress, filter);
}
int32_t TestNetAddressPrivateUntrusted::Connect(TCPSocketPrivate* socket,
const std::string& host,
uint16_t port) {
TestCompletionCallback callback(instance_->pp_instance(), false);
int32_t rv = socket->Connect(host.c_str(), port, callback.GetCallback());
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
return rv;
}
std::string TestNetAddressPrivateUntrusted::TestAreEqual() {
pp::TCPSocketPrivate socket(instance_);
int32_t rv = Connect(&socket, host_, port_);
if (rv != PP_OK)
return ReportError("pp::TCPSocketPrivate::Connect", rv);
PP_NetAddress_Private local_address, remote_address;
ASSERT_TRUE(socket.GetLocalAddress(&local_address));
ASSERT_TRUE(socket.GetRemoteAddress(&remote_address));
ASSERT_TRUE(NetAddressPrivate::AreEqual(local_address, local_address));
ASSERT_FALSE(NetAddressPrivate::AreEqual(local_address, remote_address));
socket.Disconnect();
PASS();
}
std::string TestNetAddressPrivateUntrusted::TestAreHostsEqual() {
pp::TCPSocketPrivate socket(instance_);
int32_t rv = Connect(&socket, host_, port_);
if (rv != PP_OK)
return ReportError("pp::TCPSocketPrivate::Connect", rv);
PP_NetAddress_Private local_address, remote_address;
ASSERT_TRUE(socket.GetLocalAddress(&local_address));
ASSERT_TRUE(socket.GetRemoteAddress(&remote_address));
ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(local_address, local_address));
ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(local_address, remote_address));
socket.Disconnect();
PASS();
}
std::string TestNetAddressPrivateUntrusted::TestDescribe() {
pp::TCPSocketPrivate socket(instance_);
int32_t rv = Connect(&socket, host_, port_);
if (rv != PP_OK)
return ReportError("pp::TCPSocketPrivate::Connect", rv);
PP_NetAddress_Private remote_address;
ASSERT_TRUE(socket.GetRemoteAddress(&remote_address));
std::ostringstream os;
os << host_;
ASSERT_EQ(os.str(), NetAddressPrivate::Describe(remote_address, false));
os << ':' << port_;
ASSERT_EQ(os.str(), NetAddressPrivate::Describe(remote_address, true));
socket.Disconnect();
PASS();
}
std::string TestNetAddressPrivateUntrusted::TestReplacePort() {
pp::TCPSocketPrivate socket(instance_);
int32_t rv = Connect(&socket, host_, port_);
if (rv != PP_OK)
return ReportError("pp::TCPSocketPrivate::Connect", rv);
PP_NetAddress_Private src_addr, dst_addr;
ASSERT_TRUE(socket.GetRemoteAddress(&src_addr));
uint16_t nport = port_;
if (nport == std::numeric_limits<uint16_t>::max())
--nport;
else
++nport;
ASSERT_TRUE(NetAddressPrivate::ReplacePort(src_addr, nport, &dst_addr));
std::ostringstream os;
os << host_ << ':' << nport;
ASSERT_EQ(os.str(), NetAddressPrivate::Describe(dst_addr, true));
socket.Disconnect();
PASS();
}
std::string TestNetAddressPrivateUntrusted::TestGetAnyAddress() {
PP_NetAddress_Private address;
NetAddressPrivate::GetAnyAddress(false, &address);
ASSERT_TRUE(NetAddressPrivate::AreEqual(address, address));
NetAddressPrivate::GetAnyAddress(true, &address);
ASSERT_TRUE(NetAddressPrivate::AreEqual(address, address));
PASS();
}
std::string TestNetAddressPrivateUntrusted::TestGetFamily() {
pp::TCPSocketPrivate socket(instance_);
int32_t rv = Connect(&socket, host_, port_);
if (rv != PP_OK)
return ReportError("pp::TCPSocketPrivate::Connect", rv);
PP_NetAddress_Private remote_address;
ASSERT_TRUE(socket.GetRemoteAddress(&remote_address));
ASSERT_EQ(NetAddressPrivate::GetFamily(remote_address),
NetAddressPrivate::GetFamily(remote_address));
socket.Disconnect();
PASS();
}
std::string TestNetAddressPrivateUntrusted::TestGetPort() {
pp::TCPSocketPrivate socket(instance_);
int32_t rv = Connect(&socket, host_, port_);
if (rv != PP_OK)
return ReportError("pp::TCPSocketPrivate::Connect", rv);
PP_NetAddress_Private remote_address;
ASSERT_TRUE(socket.GetRemoteAddress(&remote_address));
ASSERT_EQ(NetAddressPrivate::GetPort(remote_address), port_);
socket.Disconnect();
PASS();
}
std::string TestNetAddressPrivateUntrusted::TestGetAddress() {
pp::TCPSocketPrivate socket(instance_);
int32_t rv = Connect(&socket, host_, port_);
if (rv != PP_OK)
return ReportError("pp::TCPSocketPrivate::Connect", rv);
PP_NetAddress_Private remote_address;
ASSERT_TRUE(socket.GetRemoteAddress(&remote_address));
static const uint16_t buffer_size = sizeof(remote_address.data);
char buffer[buffer_size];
ASSERT_TRUE(NetAddressPrivate::GetAddress(remote_address, buffer,
buffer_size));
socket.Disconnect();
PASS();
}
|