| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_utils.h" | 5 #include "ppapi/tests/test_utils.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #if defined(_MSC_VER) | 10 #if defined(_MSC_VER) |
| 11 #include <windows.h> | 11 #include <windows.h> |
| 12 #else | 12 #else |
| 13 #include <unistd.h> | 13 #include <unistd.h> |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 #include "ppapi/c/pp_errors.h" | 16 #include "ppapi/c/pp_errors.h" |
| 17 #include "ppapi/cpp/dev/net_address_dev.h" | |
| 18 #include "ppapi/cpp/instance_handle.h" | 17 #include "ppapi/cpp/instance_handle.h" |
| 19 #include "ppapi/cpp/module.h" | 18 #include "ppapi/cpp/module.h" |
| 19 #include "ppapi/cpp/net_address.h" |
| 20 #include "ppapi/cpp/private/host_resolver_private.h" | 20 #include "ppapi/cpp/private/host_resolver_private.h" |
| 21 #include "ppapi/cpp/private/net_address_private.h" | 21 #include "ppapi/cpp/private/net_address_private.h" |
| 22 #include "ppapi/cpp/var.h" | 22 #include "ppapi/cpp/var.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 bool IsBigEndian() { | 26 bool IsBigEndian() { |
| 27 union { | 27 union { |
| 28 uint32_t integer32; | 28 uint32_t integer32; |
| 29 uint8_t integer8[4]; | 29 uint8_t integer8[4]; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 return (x << 8) | (x >> 8); | 96 return (x << 8) | (x >> 8); |
| 97 } | 97 } |
| 98 | 98 |
| 99 uint16_t ConvertToNetEndian16(uint16_t x) { | 99 uint16_t ConvertToNetEndian16(uint16_t x) { |
| 100 if (IsBigEndian()) | 100 if (IsBigEndian()) |
| 101 return x; | 101 return x; |
| 102 else | 102 else |
| 103 return (x << 8) | (x >> 8); | 103 return (x << 8) | (x >> 8); |
| 104 } | 104 } |
| 105 | 105 |
| 106 bool EqualNetAddress(const pp::NetAddress_Dev& addr1, | 106 bool EqualNetAddress(const pp::NetAddress& addr1, const pp::NetAddress& addr2) { |
| 107 const pp::NetAddress_Dev& addr2) { | |
| 108 if (addr1.GetFamily() == PP_NETADDRESS_FAMILY_UNSPECIFIED || | 107 if (addr1.GetFamily() == PP_NETADDRESS_FAMILY_UNSPECIFIED || |
| 109 addr2.GetFamily() == PP_NETADDRESS_FAMILY_UNSPECIFIED) { | 108 addr2.GetFamily() == PP_NETADDRESS_FAMILY_UNSPECIFIED) { |
| 110 return false; | 109 return false; |
| 111 } | 110 } |
| 112 | 111 |
| 113 if (addr1.GetFamily() == PP_NETADDRESS_FAMILY_IPV4) { | 112 if (addr1.GetFamily() == PP_NETADDRESS_FAMILY_IPV4) { |
| 114 PP_NetAddress_IPv4_Dev ipv4_addr1, ipv4_addr2; | 113 PP_NetAddress_IPv4 ipv4_addr1, ipv4_addr2; |
| 115 if (!addr1.DescribeAsIPv4Address(&ipv4_addr1) || | 114 if (!addr1.DescribeAsIPv4Address(&ipv4_addr1) || |
| 116 !addr2.DescribeAsIPv4Address(&ipv4_addr2)) { | 115 !addr2.DescribeAsIPv4Address(&ipv4_addr2)) { |
| 117 return false; | 116 return false; |
| 118 } | 117 } |
| 119 | 118 |
| 120 return ipv4_addr1.port == ipv4_addr2.port && | 119 return ipv4_addr1.port == ipv4_addr2.port && |
| 121 !memcmp(ipv4_addr1.addr, ipv4_addr2.addr, sizeof(ipv4_addr1.addr)); | 120 !memcmp(ipv4_addr1.addr, ipv4_addr2.addr, sizeof(ipv4_addr1.addr)); |
| 122 } else { | 121 } else { |
| 123 PP_NetAddress_IPv6_Dev ipv6_addr1, ipv6_addr2; | 122 PP_NetAddress_IPv6 ipv6_addr1, ipv6_addr2; |
| 124 if (!addr1.DescribeAsIPv6Address(&ipv6_addr1) || | 123 if (!addr1.DescribeAsIPv6Address(&ipv6_addr1) || |
| 125 !addr2.DescribeAsIPv6Address(&ipv6_addr2)) { | 124 !addr2.DescribeAsIPv6Address(&ipv6_addr2)) { |
| 126 return false; | 125 return false; |
| 127 } | 126 } |
| 128 | 127 |
| 129 return ipv6_addr1.port == ipv6_addr2.port && | 128 return ipv6_addr1.port == ipv6_addr2.port && |
| 130 !memcmp(ipv6_addr1.addr, ipv6_addr2.addr, sizeof(ipv6_addr1.addr)); | 129 !memcmp(ipv6_addr1.addr, ipv6_addr2.addr, sizeof(ipv6_addr1.addr)); |
| 131 } | 130 } |
| 132 } | 131 } |
| 133 | 132 |
| 134 bool ResolveHost(PP_Instance instance, | 133 bool ResolveHost(PP_Instance instance, |
| 135 const std::string& host, | 134 const std::string& host, |
| 136 uint16_t port, | 135 uint16_t port, |
| 137 pp::NetAddress_Dev* addr) { | 136 pp::NetAddress* addr) { |
| 138 // TODO(yzshen): Change to use the public host resolver once it is supported. | 137 // TODO(yzshen): Change to use the public host resolver once it is supported. |
| 139 pp::InstanceHandle instance_handle(instance); | 138 pp::InstanceHandle instance_handle(instance); |
| 140 pp::HostResolverPrivate host_resolver(instance_handle); | 139 pp::HostResolverPrivate host_resolver(instance_handle); |
| 141 PP_HostResolver_Private_Hint hint = { PP_NETADDRESSFAMILY_UNSPECIFIED, 0 }; | 140 PP_HostResolver_Private_Hint hint = { PP_NETADDRESSFAMILY_UNSPECIFIED, 0 }; |
| 142 | 141 |
| 143 TestCompletionCallback callback(instance); | 142 TestCompletionCallback callback(instance); |
| 144 callback.WaitForResult( | 143 callback.WaitForResult( |
| 145 host_resolver.Resolve(host, port, hint, callback.GetCallback())); | 144 host_resolver.Resolve(host, port, hint, callback.GetCallback())); |
| 146 | 145 |
| 147 PP_NetAddress_Private addr_private; | 146 PP_NetAddress_Private addr_private; |
| 148 if (callback.result() != PP_OK || host_resolver.GetSize() == 0 || | 147 if (callback.result() != PP_OK || host_resolver.GetSize() == 0 || |
| 149 !host_resolver.GetNetAddress(0, &addr_private)) { | 148 !host_resolver.GetNetAddress(0, &addr_private)) { |
| 150 return false; | 149 return false; |
| 151 } | 150 } |
| 152 | 151 |
| 153 switch (pp::NetAddressPrivate::GetFamily(addr_private)) { | 152 switch (pp::NetAddressPrivate::GetFamily(addr_private)) { |
| 154 case PP_NETADDRESSFAMILY_IPV4: { | 153 case PP_NETADDRESSFAMILY_IPV4: { |
| 155 PP_NetAddress_IPv4_Dev ipv4_addr; | 154 PP_NetAddress_IPv4 ipv4_addr; |
| 156 ipv4_addr.port = ConvertToNetEndian16( | 155 ipv4_addr.port = ConvertToNetEndian16( |
| 157 pp::NetAddressPrivate::GetPort(addr_private)); | 156 pp::NetAddressPrivate::GetPort(addr_private)); |
| 158 if (!pp::NetAddressPrivate::GetAddress(addr_private, ipv4_addr.addr, | 157 if (!pp::NetAddressPrivate::GetAddress(addr_private, ipv4_addr.addr, |
| 159 sizeof(ipv4_addr.addr))) { | 158 sizeof(ipv4_addr.addr))) { |
| 160 return false; | 159 return false; |
| 161 } | 160 } |
| 162 *addr = pp::NetAddress_Dev(instance_handle, ipv4_addr); | 161 *addr = pp::NetAddress(instance_handle, ipv4_addr); |
| 163 return true; | 162 return true; |
| 164 } | 163 } |
| 165 case PP_NETADDRESSFAMILY_IPV6: { | 164 case PP_NETADDRESSFAMILY_IPV6: { |
| 166 PP_NetAddress_IPv6_Dev ipv6_addr; | 165 PP_NetAddress_IPv6 ipv6_addr; |
| 167 ipv6_addr.port = ConvertToNetEndian16( | 166 ipv6_addr.port = ConvertToNetEndian16( |
| 168 pp::NetAddressPrivate::GetPort(addr_private)); | 167 pp::NetAddressPrivate::GetPort(addr_private)); |
| 169 if (!pp::NetAddressPrivate::GetAddress(addr_private, ipv6_addr.addr, | 168 if (!pp::NetAddressPrivate::GetAddress(addr_private, ipv6_addr.addr, |
| 170 sizeof(ipv6_addr.addr))) { | 169 sizeof(ipv6_addr.addr))) { |
| 171 return false; | 170 return false; |
| 172 } | 171 } |
| 173 *addr = pp::NetAddress_Dev(instance_handle, ipv6_addr); | 172 *addr = pp::NetAddress(instance_handle, ipv6_addr); |
| 174 return true; | 173 return true; |
| 175 } | 174 } |
| 176 default: { | 175 default: { |
| 177 return false; | 176 return false; |
| 178 } | 177 } |
| 179 } | 178 } |
| 180 } | 179 } |
| 181 | 180 |
| 182 void NestedEvent::Wait() { | 181 void NestedEvent::Wait() { |
| 183 PP_DCHECK(pp::Module::Get()->core()->IsMainThread()); | 182 PP_DCHECK(pp::Module::Get()->core()->IsMainThread()); |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 pp::MessageLoop loop(pp::MessageLoop::GetCurrent()); | 368 pp::MessageLoop loop(pp::MessageLoop::GetCurrent()); |
| 370 // If we don't have a message loop, we're probably running in process, where | 369 // If we don't have a message loop, we're probably running in process, where |
| 371 // PPB_MessageLoop is not supported. Just use the Testing message loop. | 370 // PPB_MessageLoop is not supported. Just use the Testing message loop. |
| 372 if (loop.is_null() || loop == pp::MessageLoop::GetForMainThread()) { | 371 if (loop.is_null() || loop == pp::MessageLoop::GetForMainThread()) { |
| 373 GetTestingInterface()->QuitMessageLoop(instance_); | 372 GetTestingInterface()->QuitMessageLoop(instance_); |
| 374 } else { | 373 } else { |
| 375 const bool should_quit = false; | 374 const bool should_quit = false; |
| 376 loop.PostQuit(should_quit); | 375 loop.PostQuit(should_quit); |
| 377 } | 376 } |
| 378 } | 377 } |
| OLD | NEW |