Address some additional cleanup work needed for the component flash updates on Linux.
[chromium-blink-merge.git] / ppapi / tests / test_net_address_private_untrusted.cc
blob06db002788e73b5ff2f2ee6e08c14fa7a5908947
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"
7 #include <limits>
8 #include <sstream>
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");
33 bool init_host_port =
34 GetLocalHostPort(instance_->pp_instance(), &host_, &port_);
35 if (!init_host_port)
36 instance_->AppendError("Can't init host and port");
38 return net_address_private_is_available &&
39 tcp_socket_private_is_available &&
40 init_host_port &&
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,
57 uint16_t port) {
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_);
68 if (rv != PP_OK)
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));
78 socket.Disconnect();
79 PASS();
82 std::string TestNetAddressPrivateUntrusted::TestAreHostsEqual() {
83 pp::TCPSocketPrivate socket(instance_);
84 int32_t rv = Connect(&socket, host_, port_);
85 if (rv != PP_OK)
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));
95 socket.Disconnect();
96 PASS();
99 std::string TestNetAddressPrivateUntrusted::TestDescribe() {
100 pp::TCPSocketPrivate socket(instance_);
101 int32_t rv = Connect(&socket, host_, port_);
102 if (rv != PP_OK)
103 return ReportError("pp::TCPSocketPrivate::Connect", rv);
105 PP_NetAddress_Private remote_address;
106 ASSERT_TRUE(socket.GetRemoteAddress(&remote_address));
108 std::ostringstream os;
110 os << host_;
111 ASSERT_EQ(os.str(), NetAddressPrivate::Describe(remote_address, false));
113 os << ':' << port_;
114 ASSERT_EQ(os.str(), NetAddressPrivate::Describe(remote_address, true));
116 socket.Disconnect();
117 PASS();
120 std::string TestNetAddressPrivateUntrusted::TestReplacePort() {
121 pp::TCPSocketPrivate socket(instance_);
122 int32_t rv = Connect(&socket, host_, port_);
123 if (rv != PP_OK)
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())
131 --nport;
132 else
133 ++nport;
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));
141 socket.Disconnect();
142 PASS();
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));
154 PASS();
157 std::string TestNetAddressPrivateUntrusted::TestGetFamily() {
158 pp::TCPSocketPrivate socket(instance_);
159 int32_t rv = Connect(&socket, host_, port_);
160 if (rv != PP_OK)
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));
169 socket.Disconnect();
170 PASS();
173 std::string TestNetAddressPrivateUntrusted::TestGetPort() {
174 pp::TCPSocketPrivate socket(instance_);
175 int32_t rv = Connect(&socket, host_, port_);
176 if (rv != PP_OK)
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_);
184 socket.Disconnect();
185 PASS();
188 std::string TestNetAddressPrivateUntrusted::TestGetAddress() {
189 pp::TCPSocketPrivate socket(instance_);
190 int32_t rv = Connect(&socket, host_, port_);
191 if (rv != PP_OK)
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,
200 buffer_size));
202 socket.Disconnect();
203 PASS();