Extend the enterprise policy for SSL overrides to the "danger" command
[chromium-blink-merge.git] / ppapi / tests / test_udp_socket_private.cc
blob74dd8ba8cea55520d5920796bb4b42028b2887d9
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 <cstring>
6 #include <vector>
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/private/net_address_private.h"
10 #include "ppapi/cpp/private/tcp_socket_private.h"
11 #include "ppapi/cpp/var.h"
12 #include "ppapi/tests/test_udp_socket_private.h"
13 #include "ppapi/tests/test_utils.h"
14 #include "ppapi/tests/testing_instance.h"
16 REGISTER_TEST_CASE(UDPSocketPrivate);
18 namespace {
20 const uint16_t kPortScanFrom = 1024;
21 const uint16_t kPortScanTo = 4096;
23 } // namespace
25 TestUDPSocketPrivate::TestUDPSocketPrivate(
26 TestingInstance* instance)
27 : TestCase(instance) {
30 bool TestUDPSocketPrivate::Init() {
31 bool tcp_socket_private_is_available = pp::TCPSocketPrivate::IsAvailable();
32 if (!tcp_socket_private_is_available)
33 instance_->AppendError("PPB_TCPSocket_Private interface not available");
35 bool udp_socket_private_is_available = pp::UDPSocketPrivate::IsAvailable();
36 if (!udp_socket_private_is_available)
37 instance_->AppendError("PPB_UDPSocket_Private interface not available");
39 bool net_address_private_is_available = pp::NetAddressPrivate::IsAvailable();
40 if (!net_address_private_is_available)
41 instance_->AppendError("PPB_NetAddress_Private interface not available");
43 bool init_host_port = GetLocalHostPort(instance_->pp_instance(),
44 &host_, &port_);
45 if (!init_host_port)
46 instance_->AppendError("Can't init host and port");
48 return tcp_socket_private_is_available &&
49 udp_socket_private_is_available &&
50 net_address_private_is_available &&
51 init_host_port &&
52 CheckTestingInterface() &&
53 EnsureRunningOverHTTP();
56 void TestUDPSocketPrivate::RunTests(const std::string& filter) {
57 RUN_CALLBACK_TEST(TestUDPSocketPrivate, Connect, filter);
58 RUN_CALLBACK_TEST(TestUDPSocketPrivate, ConnectFailure, filter);
59 RUN_CALLBACK_TEST(TestUDPSocketPrivate, Broadcast, filter);
60 RUN_CALLBACK_TEST(TestUDPSocketPrivate, SetSocketFeatureErrors, filter);
63 std::string TestUDPSocketPrivate::GetLocalAddress(
64 PP_NetAddress_Private* address) {
65 pp::TCPSocketPrivate socket(instance_);
66 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
67 callback.WaitForResult(
68 socket.Connect(host_.c_str(), port_, callback.GetCallback()));
69 CHECK_CALLBACK_BEHAVIOR(callback);
70 ASSERT_EQ(PP_OK, callback.result());
71 ASSERT_TRUE(socket.GetLocalAddress(address));
72 socket.Disconnect();
73 PASS();
76 std::string TestUDPSocketPrivate::SetBroadcastOptions(
77 pp::UDPSocketPrivate* socket) {
78 int32_t rv = socket->SetSocketFeature(
79 PP_UDPSOCKETFEATURE_PRIVATE_ADDRESS_REUSE, pp::Var(true));
80 if (rv != PP_OK)
81 return ReportError("PPB_UDPSocket_Private::SetSocketFeature", rv);
83 rv = socket->SetSocketFeature(PP_UDPSOCKETFEATURE_PRIVATE_BROADCAST,
84 pp::Var(true));
85 if (rv != PP_OK)
86 return ReportError("PPB_UDPSocket_Private::SetSocketFeature", rv);
88 PASS();
91 std::string TestUDPSocketPrivate::BindUDPSocket(
92 pp::UDPSocketPrivate* socket,
93 PP_NetAddress_Private* address) {
94 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
95 callback.WaitForResult(socket->Bind(address, callback.GetCallback()));
96 CHECK_CALLBACK_BEHAVIOR(callback);
97 ASSERT_EQ(PP_OK, callback.result());
98 PASS();
101 std::string TestUDPSocketPrivate::LookupPortAndBindUDPSocket(
102 pp::UDPSocketPrivate* socket,
103 PP_NetAddress_Private *address) {
104 PP_NetAddress_Private base_address;
105 ASSERT_SUBTEST_SUCCESS(GetLocalAddress(&base_address));
107 bool is_free_port_found = false;
108 for (uint16_t port = kPortScanFrom; port < kPortScanTo; ++port) {
109 if (!pp::NetAddressPrivate::ReplacePort(base_address, port, address))
110 return "PPB_NetAddress_Private::ReplacePort: Failed";
111 if (BindUDPSocket(socket, address).empty()) {
112 is_free_port_found = true;
113 break;
116 if (!is_free_port_found)
117 return "Can't find available port";
118 if (!socket->GetBoundAddress(address))
119 return "PPB_UDPSocket_Private::GetBoundAddress: Failed";
120 PASS();
123 std::string TestUDPSocketPrivate::BindUDPSocketFailure(
124 pp::UDPSocketPrivate* socket,
125 PP_NetAddress_Private *address) {
126 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
127 callback.WaitForResult(socket->Bind(address, callback.GetCallback()));
128 CHECK_CALLBACK_BEHAVIOR(callback);
129 ASSERT_NE(PP_OK, callback.result());
130 ASSERT_FALSE(socket->GetBoundAddress(address));
131 PASS();
134 std::string TestUDPSocketPrivate::ReadSocket(pp::UDPSocketPrivate* socket,
135 PP_NetAddress_Private* address,
136 size_t size,
137 std::string* message) {
138 std::vector<char> buffer(size);
139 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
140 callback.WaitForResult(
141 socket->RecvFrom(&buffer[0], static_cast<int32_t>(size),
142 callback.GetCallback()));
143 CHECK_CALLBACK_BEHAVIOR(callback);
144 ASSERT_FALSE(callback.result() < 0);
145 ASSERT_EQ(size, static_cast<size_t>(callback.result()));
146 message->assign(buffer.begin(), buffer.end());
147 PASS();
150 std::string TestUDPSocketPrivate::PassMessage(pp::UDPSocketPrivate* target,
151 pp::UDPSocketPrivate* source,
152 PP_NetAddress_Private* address,
153 const std::string& message) {
154 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
155 int32_t rv = source->SendTo(message.c_str(),
156 static_cast<int32_t>(message.size()), address,
157 callback.GetCallback());
158 std::string str;
159 ASSERT_SUBTEST_SUCCESS(ReadSocket(target, address, message.size(), &str));
161 callback.WaitForResult(rv);
162 CHECK_CALLBACK_BEHAVIOR(callback);
163 ASSERT_FALSE(callback.result() < 0);
164 ASSERT_EQ(message.size(), static_cast<size_t>(callback.result()));
165 ASSERT_EQ(message, str);
166 PASS();
169 std::string TestUDPSocketPrivate::TestConnect() {
170 pp::UDPSocketPrivate server_socket(instance_), client_socket(instance_);
171 PP_NetAddress_Private server_address, client_address;
173 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&server_socket,
174 &server_address));
175 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&client_socket,
176 &client_address));
177 const std::string message = "Simple message that will be sent via UDP";
178 ASSERT_SUBTEST_SUCCESS(PassMessage(&server_socket, &client_socket,
179 &server_address,
180 message));
181 PP_NetAddress_Private recv_from_address;
182 ASSERT_TRUE(server_socket.GetRecvFromAddress(&recv_from_address));
183 ASSERT_TRUE(pp::NetAddressPrivate::AreEqual(recv_from_address,
184 client_address));
186 server_socket.Close();
187 client_socket.Close();
189 if (server_socket.GetBoundAddress(&server_address))
190 return "PPB_UDPSocket_Private::GetBoundAddress: expected Failure";
191 PASS();
194 std::string TestUDPSocketPrivate::TestConnectFailure() {
195 pp::UDPSocketPrivate socket(instance_);
196 PP_NetAddress_Private invalid_address = {};
198 std::string error_message = BindUDPSocketFailure(&socket, &invalid_address);
199 if (!error_message.empty())
200 return error_message;
202 PASS();
205 std::string TestUDPSocketPrivate::TestBroadcast() {
206 const uint8_t broadcast_ip[4] = { 0xff, 0xff, 0xff, 0xff };
208 pp::UDPSocketPrivate server1(instance_), server2(instance_);
210 ASSERT_SUBTEST_SUCCESS(SetBroadcastOptions(&server1));
211 ASSERT_SUBTEST_SUCCESS(SetBroadcastOptions(&server2));
212 PP_NetAddress_Private server_address;
213 ASSERT_TRUE(pp::NetAddressPrivate::GetAnyAddress(false, &server_address));
214 ASSERT_SUBTEST_SUCCESS(BindUDPSocket(&server1, &server_address));
215 // Fill port field of |server_address|.
216 ASSERT_TRUE(server1.GetBoundAddress(&server_address));
217 ASSERT_SUBTEST_SUCCESS(BindUDPSocket(&server2, &server_address));
219 const uint16_t port = pp::NetAddressPrivate::GetPort(server_address);
220 PP_NetAddress_Private broadcast_address;
221 ASSERT_TRUE(pp::NetAddressPrivate::CreateFromIPv4Address(
222 broadcast_ip, port, &broadcast_address));
224 std::string message;
225 const std::string first_message = "first message";
226 const std::string second_message = "second_message";
228 ASSERT_SUBTEST_SUCCESS(PassMessage(&server1, &server2,
229 &broadcast_address,
230 first_message));
231 // |first_message| also arrived to |server2|.
232 ASSERT_SUBTEST_SUCCESS(ReadSocket(&server2, &broadcast_address,
233 first_message.size(), &message));
234 ASSERT_EQ(first_message, message);
236 ASSERT_SUBTEST_SUCCESS(PassMessage(&server2, &server1,
237 &broadcast_address,
238 second_message));
239 // |second_message| also arrived to |server1|.
240 ASSERT_SUBTEST_SUCCESS(ReadSocket(&server1, &broadcast_address,
241 second_message.size(), &message));
242 ASSERT_EQ(second_message, message);
244 server1.Close();
245 server2.Close();
246 PASS();
249 std::string TestUDPSocketPrivate::TestSetSocketFeatureErrors() {
250 pp::UDPSocketPrivate socket(instance_);
251 // Try to pass incorrect feature name.
252 int32_t rv = socket.SetSocketFeature(PP_UDPSOCKETFEATURE_PRIVATE_COUNT,
253 pp::Var(true));
254 ASSERT_EQ(PP_ERROR_BADARGUMENT, rv);
256 // Try to pass incorrect feature value's type.
257 rv = socket.SetSocketFeature(PP_UDPSOCKETFEATURE_PRIVATE_ADDRESS_REUSE,
258 pp::Var(1));
259 ASSERT_EQ(PP_ERROR_BADARGUMENT, rv);
260 PASS();