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. 4 5 #include "ppapi/tests/test_net_address_private_untrusted.h" 6 7 #include <limits> 8 #include <sstream> 9 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" 14 15 REGISTER_TEST_CASE(NetAddressPrivateUntrusted); 16 17 using pp::NetAddressPrivate; 18 using pp::TCPSocketPrivate; 19 20 TestNetAddressPrivateUntrusted::TestNetAddressPrivateUntrusted( 21 TestingInstance* instance) : TestCase(instance), port_(0) { 22 } 23 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"); 28 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"); 32 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"); 37 38 return net_address_private_is_available && 39 tcp_socket_private_is_available && 40 init_host_port && 41 CheckTestingInterface(); 42 } 43 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); 53 } 54 55 int32_t TestNetAddressPrivateUntrusted::Connect(TCPSocketPrivate* socket, 56 const std::string& host, 57 uint16_t port) { 58 TestCompletionCallback callback(instance_->pp_instance(), false); 59 60 callback.WaitForResult( 61 socket->Connect(host.c_str(), port, callback.GetCallback())); 62 return callback.result(); 63 } 64 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); 70 71 PP_NetAddress_Private local_address, remote_address; 72 ASSERT_TRUE(socket.GetLocalAddress(&local_address)); 73 ASSERT_TRUE(socket.GetRemoteAddress(&remote_address)); 74 75 ASSERT_TRUE(NetAddressPrivate::AreEqual(local_address, local_address)); 76 ASSERT_FALSE(NetAddressPrivate::AreEqual(local_address, remote_address)); 77 78 socket.Disconnect(); 79 PASS(); 80 } 81 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); 87 88 PP_NetAddress_Private local_address, remote_address; 89 ASSERT_TRUE(socket.GetLocalAddress(&local_address)); 90 ASSERT_TRUE(socket.GetRemoteAddress(&remote_address)); 91 92 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(local_address, local_address)); 93 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(local_address, remote_address)); 94 95 socket.Disconnect(); 96 PASS(); 97 } 98 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); 104 105 PP_NetAddress_Private remote_address; 106 ASSERT_TRUE(socket.GetRemoteAddress(&remote_address)); 107 108 std::ostringstream os; 109 110 os << host_; 111 ASSERT_EQ(os.str(), NetAddressPrivate::Describe(remote_address, false)); 112 113 os << ':' << port_; 114 ASSERT_EQ(os.str(), NetAddressPrivate::Describe(remote_address, true)); 115 116 socket.Disconnect(); 117 PASS(); 118 } 119 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); 125 126 PP_NetAddress_Private src_addr, dst_addr; 127 ASSERT_TRUE(socket.GetRemoteAddress(&src_addr)); 128 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)); 135 136 std::ostringstream os; 137 os << host_ << ':' << nport; 138 139 ASSERT_EQ(os.str(), NetAddressPrivate::Describe(dst_addr, true)); 140 141 socket.Disconnect(); 142 PASS(); 143 } 144 145 std::string TestNetAddressPrivateUntrusted::TestGetAnyAddress() { 146 PP_NetAddress_Private address; 147 148 NetAddressPrivate::GetAnyAddress(false, &address); 149 ASSERT_TRUE(NetAddressPrivate::AreEqual(address, address)); 150 151 NetAddressPrivate::GetAnyAddress(true, &address); 152 ASSERT_TRUE(NetAddressPrivate::AreEqual(address, address)); 153 154 PASS(); 155 } 156 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); 162 163 PP_NetAddress_Private remote_address; 164 ASSERT_TRUE(socket.GetRemoteAddress(&remote_address)); 165 166 ASSERT_EQ(NetAddressPrivate::GetFamily(remote_address), 167 NetAddressPrivate::GetFamily(remote_address)); 168 169 socket.Disconnect(); 170 PASS(); 171 } 172 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); 178 179 PP_NetAddress_Private remote_address; 180 ASSERT_TRUE(socket.GetRemoteAddress(&remote_address)); 181 182 ASSERT_EQ(NetAddressPrivate::GetPort(remote_address), port_); 183 184 socket.Disconnect(); 185 PASS(); 186 } 187 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); 193 194 PP_NetAddress_Private remote_address; 195 ASSERT_TRUE(socket.GetRemoteAddress(&remote_address)); 196 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)); 201 202 socket.Disconnect(); 203 PASS(); 204 } 205