1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/tests/test_net_address_private_untrusted.h"
10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/cpp/private/net_address_private.h"
12 #include "ppapi/tests/test_utils.h"
13 #include "ppapi/tests/testing_instance.h"
15 REGISTER_TEST_CASE(NetAddressPrivateUntrusted
);
17 using pp::NetAddressPrivate
;
18 using pp::TCPSocketPrivate
;
20 TestNetAddressPrivateUntrusted::TestNetAddressPrivateUntrusted(
21 TestingInstance
* instance
) : TestCase(instance
), port_(0) {
24 bool TestNetAddressPrivateUntrusted::Init() {
25 bool net_address_private_is_available
= NetAddressPrivate::IsAvailable();
26 if (!net_address_private_is_available
)
27 instance_
->AppendError("PPB_NetAddress_Private interface not available");
29 bool tcp_socket_private_is_available
= TCPSocketPrivate::IsAvailable();
30 if (!tcp_socket_private_is_available
)
31 instance_
->AppendError("PPB_TCPSocket_Private interface not available");
34 GetLocalHostPort(instance_
->pp_instance(), &host_
, &port_
);
36 instance_
->AppendError("Can't init host and port");
38 return net_address_private_is_available
&&
39 tcp_socket_private_is_available
&&
41 CheckTestingInterface();
44 void TestNetAddressPrivateUntrusted::RunTests(const std::string
& filter
) {
45 RUN_TEST(AreEqual
, filter
);
46 RUN_TEST(AreHostsEqual
, filter
);
47 RUN_TEST(Describe
, filter
);
48 RUN_TEST(ReplacePort
, filter
);
49 RUN_TEST(GetAnyAddress
, filter
);
50 RUN_TEST(GetFamily
, filter
);
51 RUN_TEST(GetPort
, filter
);
52 RUN_TEST(GetAddress
, filter
);
55 int32_t TestNetAddressPrivateUntrusted::Connect(TCPSocketPrivate
* socket
,
56 const std::string
& host
,
58 TestCompletionCallback
callback(instance_
->pp_instance(), false);
60 callback
.WaitForResult(
61 socket
->Connect(host
.c_str(), port
, callback
.GetCallback()));
62 return callback
.result();
65 std::string
TestNetAddressPrivateUntrusted::TestAreEqual() {
66 pp::TCPSocketPrivate
socket(instance_
);
67 int32_t rv
= Connect(&socket
, host_
, port_
);
69 return ReportError("pp::TCPSocketPrivate::Connect", rv
);
71 PP_NetAddress_Private local_address
, remote_address
;
72 ASSERT_TRUE(socket
.GetLocalAddress(&local_address
));
73 ASSERT_TRUE(socket
.GetRemoteAddress(&remote_address
));
75 ASSERT_TRUE(NetAddressPrivate::AreEqual(local_address
, local_address
));
76 ASSERT_FALSE(NetAddressPrivate::AreEqual(local_address
, remote_address
));
82 std::string
TestNetAddressPrivateUntrusted::TestAreHostsEqual() {
83 pp::TCPSocketPrivate
socket(instance_
);
84 int32_t rv
= Connect(&socket
, host_
, port_
);
86 return ReportError("pp::TCPSocketPrivate::Connect", rv
);
88 PP_NetAddress_Private local_address
, remote_address
;
89 ASSERT_TRUE(socket
.GetLocalAddress(&local_address
));
90 ASSERT_TRUE(socket
.GetRemoteAddress(&remote_address
));
92 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(local_address
, local_address
));
93 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(local_address
, remote_address
));
99 std::string
TestNetAddressPrivateUntrusted::TestDescribe() {
100 pp::TCPSocketPrivate
socket(instance_
);
101 int32_t rv
= Connect(&socket
, host_
, port_
);
103 return ReportError("pp::TCPSocketPrivate::Connect", rv
);
105 PP_NetAddress_Private remote_address
;
106 ASSERT_TRUE(socket
.GetRemoteAddress(&remote_address
));
108 std::ostringstream os
;
111 ASSERT_EQ(os
.str(), NetAddressPrivate::Describe(remote_address
, false));
114 ASSERT_EQ(os
.str(), NetAddressPrivate::Describe(remote_address
, true));
120 std::string
TestNetAddressPrivateUntrusted::TestReplacePort() {
121 pp::TCPSocketPrivate
socket(instance_
);
122 int32_t rv
= Connect(&socket
, host_
, port_
);
124 return ReportError("pp::TCPSocketPrivate::Connect", rv
);
126 PP_NetAddress_Private src_addr
, dst_addr
;
127 ASSERT_TRUE(socket
.GetRemoteAddress(&src_addr
));
129 uint16_t nport
= port_
;
130 if (nport
== std::numeric_limits
<uint16_t>::max())
134 ASSERT_TRUE(NetAddressPrivate::ReplacePort(src_addr
, nport
, &dst_addr
));
136 std::ostringstream os
;
137 os
<< host_
<< ':' << nport
;
139 ASSERT_EQ(os
.str(), NetAddressPrivate::Describe(dst_addr
, true));
145 std::string
TestNetAddressPrivateUntrusted::TestGetAnyAddress() {
146 PP_NetAddress_Private address
;
148 NetAddressPrivate::GetAnyAddress(false, &address
);
149 ASSERT_TRUE(NetAddressPrivate::AreEqual(address
, address
));
151 NetAddressPrivate::GetAnyAddress(true, &address
);
152 ASSERT_TRUE(NetAddressPrivate::AreEqual(address
, address
));
157 std::string
TestNetAddressPrivateUntrusted::TestGetFamily() {
158 pp::TCPSocketPrivate
socket(instance_
);
159 int32_t rv
= Connect(&socket
, host_
, port_
);
161 return ReportError("pp::TCPSocketPrivate::Connect", rv
);
163 PP_NetAddress_Private remote_address
;
164 ASSERT_TRUE(socket
.GetRemoteAddress(&remote_address
));
166 ASSERT_EQ(NetAddressPrivate::GetFamily(remote_address
),
167 NetAddressPrivate::GetFamily(remote_address
));
173 std::string
TestNetAddressPrivateUntrusted::TestGetPort() {
174 pp::TCPSocketPrivate
socket(instance_
);
175 int32_t rv
= Connect(&socket
, host_
, port_
);
177 return ReportError("pp::TCPSocketPrivate::Connect", rv
);
179 PP_NetAddress_Private remote_address
;
180 ASSERT_TRUE(socket
.GetRemoteAddress(&remote_address
));
182 ASSERT_EQ(NetAddressPrivate::GetPort(remote_address
), port_
);
188 std::string
TestNetAddressPrivateUntrusted::TestGetAddress() {
189 pp::TCPSocketPrivate
socket(instance_
);
190 int32_t rv
= Connect(&socket
, host_
, port_
);
192 return ReportError("pp::TCPSocketPrivate::Connect", rv
);
194 PP_NetAddress_Private remote_address
;
195 ASSERT_TRUE(socket
.GetRemoteAddress(&remote_address
));
197 static const uint16_t buffer_size
= sizeof(remote_address
.data
);
198 char buffer
[buffer_size
];
199 ASSERT_TRUE(NetAddressPrivate::GetAddress(remote_address
, buffer
,