diff options
author | sbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-28 19:56:39 +0000 |
---|---|---|
committer | sbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-28 19:56:39 +0000 |
commit | 1c798492c0b4df43712220e0995e2065bed9595a (patch) | |
tree | ba5727242d465b4030d23c2d385561c0830de674 | |
parent | 8a56a0cbcd98f591f1fff88737df5864b7d205e6 (diff) | |
download | chromium_src-1c798492c0b4df43712220e0995e2065bed9595a.zip chromium_src-1c798492c0b4df43712220e0995e2065bed9595a.tar.gz chromium_src-1c798492c0b4df43712220e0995e2065bed9595a.tar.bz2 |
[NaCl SDK] Add fake for ppb_host_resolver and ppb_net_address
Also add tests for gethostbyname that usese these fakes
and fix the resulting leaks found in host_resolver.cc.
Also add a third variant of ki_init() which takes a
PepperInterface instance. This can be used by the test
code to inject a fake pepper interface.
R=binji@chromium.org
Review URL: https://codereview.chromium.org/148223005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247487 0039d316-1c4b-4281-b951-d872f2087c98
17 files changed, 441 insertions, 40 deletions
diff --git a/native_client_sdk/src/libraries/nacl_io/host_resolver.cc b/native_client_sdk/src/libraries/nacl_io/host_resolver.cc index beae726..4fbf3c2 100644 --- a/native_client_sdk/src/libraries/nacl_io/host_resolver.cc +++ b/native_client_sdk/src/libraries/nacl_io/host_resolver.cc @@ -33,7 +33,15 @@ struct hostent* HostResolver::gethostbyname(const char* name) { return NULL; HostResolverInterface* resolver_interface = - ppapi_->GetHostResolverInterface(); + ppapi_->GetHostResolverInterface(); + VarInterface* var_interface = ppapi_->GetVarInterface(); + NetAddressInterface* netaddr_iface = ppapi_->GetNetAddressInterface(); + + if (NULL == resolver_interface || + NULL == netaddr_iface || + NULL == var_interface) + return NULL; + ScopedResource resolver(ppapi_, resolver_interface->Create(ppapi_->GetInstance())); @@ -72,7 +80,7 @@ struct hostent* HostResolver::gethostbyname(const char* name) { return NULL; uint32_t len; - const char* name_ptr = ppapi_->GetVarInterface()->VarToUtf8(name_var, &len); + const char* name_ptr = var_interface->VarToUtf8(name_var, &len); if (NULL == name_ptr) return NULL; if (0 == len) { @@ -88,19 +96,20 @@ struct hostent* HostResolver::gethostbyname(const char* name) { memcpy(hostent_.h_name, name_ptr, len); hostent_.h_name[len] = '\0'; + var_interface->Release(name_var); + // Aliases aren't supported at the moment, so we just make an empty list. hostent_.h_aliases = static_cast<char**>(malloc(sizeof(char*))); if (NULL == hostent_.h_aliases) return NULL; hostent_.h_aliases[0] = NULL; - NetAddressInterface* netaddr_interface = ppapi_->GetNetAddressInterface(); - PP_Resource addr = - resolver_interface->GetNetAddress(resolver.pp_resource(), 0); - if (!PP_ToBool(netaddr_interface->IsNetAddress(addr))) + ScopedResource addr(ppapi_); + addr.Reset(resolver_interface->GetNetAddress(resolver.pp_resource(), 0)); + if (!PP_ToBool(netaddr_iface->IsNetAddress(addr.pp_resource()))) return NULL; - switch (netaddr_interface->GetFamily(addr)) { + switch (netaddr_iface->GetFamily(addr.pp_resource())) { case PP_NETADDRESS_FAMILY_IPV4: hostent_.h_addrtype = AF_INET; hostent_.h_length = 4; @@ -123,22 +132,22 @@ struct hostent* HostResolver::gethostbyname(const char* name) { return NULL; for (uint32_t i = 0; i < num_addresses; i++) { - PP_Resource addr = - resolver_interface->GetNetAddress(resolver.pp_resource(), i); - if (!PP_ToBool(netaddr_interface->IsNetAddress(addr))) + addr.Reset(resolver_interface->GetNetAddress(resolver.pp_resource(), i)); + if (!PP_ToBool(netaddr_iface->IsNetAddress(addr.pp_resource()))) return NULL; if (AF_INET == hostent_.h_addrtype) { struct PP_NetAddress_IPv4 addr_struct; - if (!netaddr_interface->DescribeAsIPv4Address(addr, &addr_struct)) { + if (!netaddr_iface->DescribeAsIPv4Address(addr.pp_resource(), + &addr_struct)) return NULL; - } hostent_.h_addr_list[i] = static_cast<char*>(malloc(hostent_.h_length)); if (NULL == hostent_.h_addr_list[i]) return NULL; memcpy(hostent_.h_addr_list[i], addr_struct.addr, hostent_.h_length); } else { // IPv6 struct PP_NetAddress_IPv6 addr_struct; - if (!netaddr_interface->DescribeAsIPv6Address(addr, &addr_struct)) + if (!netaddr_iface->DescribeAsIPv6Address(addr.pp_resource(), + &addr_struct)) return NULL; hostent_.h_addr_list[i] = static_cast<char*>(malloc(hostent_.h_length)); if (NULL == hostent_.h_addr_list[i]) @@ -146,6 +155,7 @@ struct hostent* HostResolver::gethostbyname(const char* name) { memcpy(hostent_.h_addr_list[i], addr_struct.addr, hostent_.h_length); } } + return &hostent_; } diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc index edf1eb8..d8d3d98 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc @@ -33,6 +33,14 @@ void ki_init(void* kp) { void ki_init_ppapi(void* kp, PP_Instance instance, PPB_GetInterface get_browser_interface) { + PepperInterface* ppapi = NULL; + if (instance && get_browser_interface) + ppapi = new RealPepperInterface(instance, get_browser_interface); + ki_init_interface(kp, ppapi); +} + +void ki_init_interface(void* kp, void* pepper_interface) { + PepperInterface* ppapi = static_cast<PepperInterface*>(pepper_interface); kernel_wrap_init(); if (kp == NULL) { @@ -43,10 +51,6 @@ void ki_init_ppapi(void* kp, s_kp_owned = false; } - PepperInterface* ppapi = NULL; - if (instance && get_browser_interface) - ppapi = new RealPepperInterface(instance, get_browser_interface); - s_kp->Init(ppapi); } diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h index 6192b8b..df92b67 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h +++ b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h @@ -1,6 +1,6 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. +/* Copyright (c) 2012 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. */ #ifndef LIBRARIES_NACL_IO_KERNEL_INTERCEPT_H_ #define LIBRARIES_NACL_IO_KERNEL_INTERCEPT_H_ @@ -23,16 +23,28 @@ EXTERN_C_BEGIN struct fuse_operations; -// The kernel intercept module provides a C->C++ thunk between the libc -// kernel calls and the KernelProxy singleton. +/* + * The kernel intercept module provides a C->C++ thunk between the libc + * kernel calls and the KernelProxy singleton. + */ -// ki_init must be called with an uninitialized KernelProxy object. Calling -// with NULL will instantiate a default kernel proxy object. ki_init must -// be called before any other ki_XXX function can be used. +/* + * ki_init must be called with an uninitialized KernelProxy object. Calling + * with NULL will instantiate a default kernel proxy object. ki_init must + * be called before any other ki_XXX function can be used. + */ void ki_init(void* kernel_proxy); void ki_init_ppapi(void* kernel_proxy, PP_Instance instance, PPB_GetInterface get_browser_interface); + +/* + * ki_init_interface() is a variant of ki_init() that can be called with + * a PepperInterface object. The ownership of this object then passes + * to nacl_io and it will be deleted on ki_uninit(). + */ +void ki_init_interface(void* kp, void* pepper_interface); + int ki_register_fs_type(const char* fs_type, struct fuse_operations* fuse_ops); int ki_unregister_fs_type(const char* fs_type); int ki_is_initialized(); @@ -104,7 +116,7 @@ sighandler_t ki_signal(int signum, sighandler_t handler); sighandler_t ki_sigset(int signum, sighandler_t handler); #ifdef PROVIDES_SOCKET_API -// Socket Functions +/* Socket Functions */ int ki_accept(int fd, struct sockaddr* addr, socklen_t* len); int ki_bind(int fd, const struct sockaddr* addr, socklen_t len); int ki_connect(int fd, const struct sockaddr* addr, socklen_t len); @@ -126,8 +138,8 @@ int ki_setsockopt(int fd, int lvl, int optname, const void* optval, int ki_shutdown(int fd, int how); int ki_socket(int domain, int type, int protocol); int ki_socketpair(int domain, int type, int protocl, int* sv); -#endif // PROVIDES_SOCKET_API +#endif /* PROVIDES_SOCKET_API */ EXTERN_C_END -#endif // LIBRARIES_NACL_IO_KERNEL_INTERCEPT_H_ +#endif /* LIBRARIES_NACL_IO_KERNEL_INTERCEPT_H_ */ diff --git a/native_client_sdk/src/tests/nacl_io_test/example.dsc b/native_client_sdk/src/tests/nacl_io_test/example.dsc index daed336..8ec7dbc 100644 --- a/native_client_sdk/src/tests/nacl_io_test/example.dsc +++ b/native_client_sdk/src/tests/nacl_io_test/example.dsc @@ -11,8 +11,12 @@ 'event_test.cc', 'fake_ppapi/fake_core_interface.cc', 'fake_ppapi/fake_core_interface.h', + 'fake_ppapi/fake_host_resolver_interface.cc', + 'fake_ppapi/fake_host_resolver_interface.h', 'fake_ppapi/fake_messaging_interface.cc', 'fake_ppapi/fake_messaging_interface.h', + 'fake_ppapi/fake_net_address_interface.cc', + 'fake_ppapi/fake_net_address_interface.h', 'fake_ppapi/fake_pepper_interface.cc', 'fake_ppapi/fake_pepper_interface.h', 'fake_ppapi/fake_pepper_interface_html5_fs.cc', @@ -32,6 +36,7 @@ 'fifo_test.cc', 'filesystem_test.cc', 'fuse_fs_test.cc', + 'host_resolver_test.cc', 'html5_fs_test.cc', 'http_fs_test.cc', 'kernel_object_test.cc', diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.cc b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.cc index 4aa4d7e..ae3c59b 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.cc +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.cc @@ -4,12 +4,13 @@ #include "fake_ppapi/fake_core_interface.h" -FakeCoreInterface::FakeCoreInterface() {} +FakeCoreInterface::FakeCoreInterface(FakeResourceManager* manager) + : resource_manager_(manager) {} void FakeCoreInterface::AddRefResource(PP_Resource handle) { - return resource_manager_.AddRef(handle); + return resource_manager_->AddRef(handle); } void FakeCoreInterface::ReleaseResource(PP_Resource handle) { - return resource_manager_.Release(handle); + return resource_manager_->Release(handle); } diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.h b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.h index fa6fe23..acab5b9 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.h +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_core_interface.h @@ -11,16 +11,16 @@ class FakeCoreInterface : public nacl_io::CoreInterface { public: - FakeCoreInterface(); + explicit FakeCoreInterface(FakeResourceManager* manager); virtual void AddRefResource(PP_Resource handle); virtual void ReleaseResource(PP_Resource handle); virtual PP_Bool IsMainThread() { return PP_FALSE; } - FakeResourceManager* resource_manager() { return &resource_manager_; } + FakeResourceManager* resource_manager() { return resource_manager_; } private: - FakeResourceManager resource_manager_; + FakeResourceManager* resource_manager_; // Weak reference DISALLOW_COPY_AND_ASSIGN(FakeCoreInterface); }; diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.cc b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.cc new file mode 100644 index 0000000..ef7f7baf --- /dev/null +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.cc @@ -0,0 +1,98 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "fake_ppapi/fake_host_resolver_interface.h" + +#include <netinet/in.h> + +#include "fake_ppapi/fake_pepper_interface.h" +#include "fake_ppapi/fake_resource_manager.h" +#include "fake_ppapi/fake_var_manager.h" +#include "gtest/gtest.h" + +namespace { + +class FakeHostResolverResource : public FakeResource { + public: + FakeHostResolverResource() : resolved(false), name(NULL) {} + static const char* classname() { return "FakeHostResolverResource"; } + + bool resolved; + in_addr_t address; + const char* name; +}; + +int32_t RunCompletionCallback(PP_CompletionCallback* callback, int32_t result) { + if (callback->func) { + PP_RunCompletionCallback(callback, result); + return PP_OK_COMPLETIONPENDING; + } + return result; +} + +} + +FakeHostResolverInterface::FakeHostResolverInterface(FakePepperInterface* ppapi) + : ppapi_(ppapi) {} + +PP_Resource FakeHostResolverInterface::Create(PP_Instance instance) { + if (instance != ppapi_->GetInstance()) + return PP_ERROR_BADRESOURCE; + + FakeHostResolverResource* resolver_resource = new FakeHostResolverResource; + + return CREATE_RESOURCE(ppapi_->resource_manager(), + FakeHostResolverResource, + resolver_resource); +} + +int32_t FakeHostResolverInterface::Resolve(PP_Resource resource, + const char* hostname, + uint16_t, + const PP_HostResolver_Hint*, + PP_CompletionCallback callback) { + FakeHostResolverResource* resolver = + ppapi_->resource_manager()->Get<FakeHostResolverResource>(resource); + resolver->resolved = false; + if (!strcmp(hostname, FAKE_HOSTNAME)) { + resolver->resolved = true; + resolver->name = FAKE_HOSTNAME; + resolver->address = htonl(FAKE_IP); + return RunCompletionCallback(&callback, PP_OK); + } + return RunCompletionCallback(&callback, PP_ERROR_NAME_NOT_RESOLVED); +} + +PP_Var FakeHostResolverInterface::GetCanonicalName(PP_Resource resource) { + FakeHostResolverResource* res = + ppapi_->resource_manager()->Get<FakeHostResolverResource>(resource); + if (!res->resolved) + return PP_Var(); + return ppapi_->GetVarInterface()->VarFromUtf8(res->name, strlen(res->name)); +} + +uint32_t FakeHostResolverInterface::GetNetAddressCount(PP_Resource resolver) { + FakeHostResolverResource* res = + ppapi_->resource_manager()->Get<FakeHostResolverResource>(resolver); + if (!res->resolved) + return 0; + return 1; +} + +PP_Resource FakeHostResolverInterface::GetNetAddress(PP_Resource resource, + uint32_t index) { + FakeHostResolverResource* res = + ppapi_->resource_manager()->Get<FakeHostResolverResource>(resource); + if (!res->resolved) + return 0; + + if (index != 0) + return 0; + + // Create a new NetAddress resource and return it. + PP_NetAddress_IPv4 addr; + memcpy(addr.addr, &res->address, sizeof(res->address)); + nacl_io::NetAddressInterface* iface = ppapi_->GetNetAddressInterface(); + return iface->CreateFromIPv4Address(ppapi_->GetInstance(), &addr); +} diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.h b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.h new file mode 100644 index 0000000..058ae4d --- /dev/null +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.h @@ -0,0 +1,43 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef TESTS_NACL_IO_TEST_FAKE_HOST_RESOLVER_INTERFACE_H_ +#define TESTS_NACL_IO_TEST_FAKE_HOST_RESOLVER_INTERFACE_H_ + +#include <ppapi/c/ppb_host_resolver.h> + +#include "nacl_io/pepper_interface.h" +#include "sdk_util/macros.h" + +// This fake resolver only know how to resolve this one +// host to this one IP address. +#define FAKE_HOSTNAME "example.com" +#define FAKE_IP 0x01020304 + +class FakePepperInterface; +class FakeVarManager; + +class FakeHostResolverInterface : public nacl_io::HostResolverInterface { + public: + explicit FakeHostResolverInterface(FakePepperInterface* ppapi); + + virtual PP_Resource Create(PP_Instance); + + virtual int32_t Resolve(PP_Resource, + const char*, + uint16_t, + const PP_HostResolver_Hint*, + PP_CompletionCallback); + + virtual PP_Var GetCanonicalName(PP_Resource); + virtual uint32_t GetNetAddressCount(PP_Resource); + virtual PP_Resource GetNetAddress(PP_Resource, uint32_t); + + private: + FakePepperInterface* ppapi_; + + DISALLOW_COPY_AND_ASSIGN(FakeHostResolverInterface); +}; + +#endif // TESTS_NACL_IO_TEST_FAKE_HOST_RESOLVER_INTERFACE_H_ diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_net_address_interface.cc b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_net_address_interface.cc new file mode 100644 index 0000000..78e6f23 --- /dev/null +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_net_address_interface.cc @@ -0,0 +1,88 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "fake_ppapi/fake_net_address_interface.h" + +#include <netinet/in.h> + +#include "fake_ppapi/fake_pepper_interface.h" +#include "fake_ppapi/fake_resource_manager.h" +#include "fake_ppapi/fake_var_manager.h" +#include "gtest/gtest.h" + +namespace { + +class FakeNetAddressResource : public FakeResource { + public: + explicit FakeNetAddressResource(PP_NetAddress_IPv4 addr) : address(addr) {} + static const char* classname() { return "FakeNetAddressResource"; } + + PP_NetAddress_IPv4 address; +}; + +} // namespace + +FakeNetAddressInterface::FakeNetAddressInterface(FakePepperInterface* ppapi) + : ppapi_(ppapi) {} + +PP_Resource FakeNetAddressInterface::CreateFromIPv4Address( + PP_Instance instance, + PP_NetAddress_IPv4* address) { + if (instance != ppapi_->GetInstance()) + return 0; + + FakeNetAddressResource* addr_resource = new FakeNetAddressResource(*address); + + PP_Resource rtn = CREATE_RESOURCE(ppapi_->resource_manager(), + FakeNetAddressResource, + addr_resource); + return rtn; +} + +PP_Resource FakeNetAddressInterface::CreateFromIPv6Address( + PP_Instance instance, + PP_NetAddress_IPv6* address) { + if (instance != ppapi_->GetInstance()) + return 0; + + // TODO(sbc): implement + assert(false); + return 0; +} + +PP_Bool FakeNetAddressInterface::IsNetAddress(PP_Resource address) { + FakeNetAddressResource* address_resource = + ppapi_->resource_manager()->Get<FakeNetAddressResource>(address); + if (address_resource == NULL) + return PP_FALSE; + return PP_TRUE; +} + +PP_NetAddress_Family FakeNetAddressInterface::GetFamily(PP_Resource) { + return PP_NETADDRESS_FAMILY_IPV4; +} + +PP_Bool FakeNetAddressInterface::DescribeAsIPv4Address( + PP_Resource address, PP_NetAddress_IPv4* target) { + FakeNetAddressResource* address_resource = + ppapi_->resource_manager()->Get<FakeNetAddressResource>(address); + if (address_resource == NULL) + return PP_FALSE; + + *target = address_resource->address; + return PP_TRUE; +} + +PP_Bool FakeNetAddressInterface::DescribeAsIPv6Address( + PP_Resource adddress, PP_NetAddress_IPv6* taret) { + // TODO(sbc): implement + assert(false); + return PP_FALSE; +} + +PP_Var FakeNetAddressInterface::DescribeAsString(PP_Resource, PP_Bool) { + // TODO(sbc): implement + assert(false); + return PP_Var(); +} diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_net_address_interface.h b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_net_address_interface.h new file mode 100644 index 0000000..ae2a8ae --- /dev/null +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_net_address_interface.h @@ -0,0 +1,34 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef TESTS_NACL_IO_TEST_FAKE_NET_ADDRESS_INTERFACE_H_ +#define TESTS_NACL_IO_TEST_FAKE_NET_ADDRESS_INTERFACE_H_ + +#include <ppapi/c/ppb_host_resolver.h> + +#include "nacl_io/pepper_interface.h" +#include "sdk_util/macros.h" + +class FakeVarManager; +class FakePepperInterface; + +class FakeNetAddressInterface : public nacl_io::NetAddressInterface { + public: + explicit FakeNetAddressInterface(FakePepperInterface* ppapi); + + virtual PP_Resource CreateFromIPv4Address(PP_Instance, PP_NetAddress_IPv4*); + virtual PP_Resource CreateFromIPv6Address(PP_Instance, PP_NetAddress_IPv6*); + virtual PP_Bool IsNetAddress(PP_Resource); + virtual PP_NetAddress_Family GetFamily(PP_Resource); + virtual PP_Bool DescribeAsIPv4Address(PP_Resource, PP_NetAddress_IPv4*); + virtual PP_Bool DescribeAsIPv6Address(PP_Resource, PP_NetAddress_IPv6*); + virtual PP_Var DescribeAsString(PP_Resource, PP_Bool); + + private: + FakePepperInterface* ppapi_; + + DISALLOW_COPY_AND_ASSIGN(FakeNetAddressInterface); +}; + +#endif // TESTS_NACL_IO_TEST_FAKE_NET_ADDRESS_INTERFACE_H_ diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.cc b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.cc index 7b60a4b..f070c03 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.cc +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.cc @@ -4,13 +4,37 @@ #include "fake_ppapi/fake_pepper_interface.h" +#include "fake_ppapi/fake_resource_manager.h" + using namespace nacl_io; +namespace { + +class FakeInstanceResource : public FakeResource { + public: + FakeInstanceResource() {} + static const char* classname() { return "FakeInstanceResource"; } +}; + +} + FakePepperInterface::FakePepperInterface() - : messaging_interface_(&var_manager_, &var_interface_), + : core_interface_(&resource_manager_), + messaging_interface_(&var_manager_, &var_interface_), var_array_interface_(&var_manager_), var_array_buffer_interface_(&var_manager_), - var_interface_(&var_manager_) {} + var_interface_(&var_manager_), + resolver_interface_(this), + net_address_interface_(this) { + FakeInstanceResource* instance_resource = new FakeInstanceResource; + instance_ = CREATE_RESOURCE(&resource_manager_, + FakeInstanceResource, + instance_resource); +} + +FakePepperInterface::~FakePepperInterface() { + core_interface_.ReleaseResource(instance_); +} CoreInterface* FakePepperInterface::GetCoreInterface() { return &core_interface_; @@ -31,3 +55,11 @@ VarInterface* FakePepperInterface::GetVarInterface() { MessagingInterface* FakePepperInterface::GetMessagingInterface() { return &messaging_interface_; } + +HostResolverInterface* FakePepperInterface::GetHostResolverInterface() { + return &resolver_interface_; +} + +NetAddressInterface* FakePepperInterface::GetNetAddressInterface() { + return &net_address_interface_; +} diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.h b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.h index 68f7864..a1a5ebc 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.h +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface.h @@ -6,7 +6,10 @@ #define TESTS_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_H_ #include "fake_ppapi/fake_core_interface.h" +#include "fake_ppapi/fake_host_resolver_interface.h" #include "fake_ppapi/fake_messaging_interface.h" +#include "fake_ppapi/fake_net_address_interface.h" +#include "fake_ppapi/fake_resource_manager.h" #include "fake_ppapi/fake_var_array_buffer_interface.h" #include "fake_ppapi/fake_var_array_interface.h" #include "fake_ppapi/fake_var_interface.h" @@ -16,21 +19,32 @@ class FakePepperInterface : public nacl_io::PepperInterfaceDummy { public: FakePepperInterface(); - virtual ~FakePepperInterface() {} + virtual ~FakePepperInterface(); virtual nacl_io::CoreInterface* GetCoreInterface(); virtual nacl_io::MessagingInterface* GetMessagingInterface(); virtual nacl_io::VarArrayInterface* GetVarArrayInterface(); virtual nacl_io::VarArrayBufferInterface* GetVarArrayBufferInterface(); virtual nacl_io::VarInterface* GetVarInterface(); + virtual nacl_io::HostResolverInterface* GetHostResolverInterface(); + virtual nacl_io::NetAddressInterface* GetNetAddressInterface(); + virtual PP_Instance GetInstance() { return instance_; } + + FakeResourceManager* resource_manager() { return &resource_manager_; } + FakeVarManager* var_manager() { return &var_manager_; } private: + PP_Instance instance_; FakeVarManager var_manager_; + FakeResourceManager resource_manager_; + FakeCoreInterface core_interface_; FakeMessagingInterface messaging_interface_; FakeVarArrayInterface var_array_interface_; FakeVarArrayBufferInterface var_array_buffer_interface_; FakeVarInterface var_interface_; + FakeHostResolverInterface resolver_interface_; + FakeNetAddressInterface net_address_interface_; DISALLOW_COPY_AND_ASSIGN(FakePepperInterface); }; diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.cc b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.cc index 18776b9..73549658 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.cc +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.cc @@ -656,7 +656,8 @@ int32_t FakeFileSystemInterface::Open(PP_Resource file_system, } FakePepperInterfaceHtml5Fs::FakePepperInterfaceHtml5Fs() - : var_interface_(&var_manager_), + : core_interface_(&resource_manager_), + var_interface_(&var_manager_), file_system_interface_(&core_interface_), file_ref_interface_(&core_interface_, &var_interface_), file_io_interface_(&core_interface_) { @@ -665,7 +666,8 @@ FakePepperInterfaceHtml5Fs::FakePepperInterfaceHtml5Fs() FakePepperInterfaceHtml5Fs::FakePepperInterfaceHtml5Fs( const FakeHtml5FsFilesystem& filesystem) - : var_interface_(&var_manager_), + : core_interface_(&resource_manager_), + var_interface_(&var_manager_), filesystem_template_(filesystem), file_system_interface_(&core_interface_), file_ref_interface_(&core_interface_, &var_interface_), diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.h b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.h index 000894e..2effa6c 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.h +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_html5_fs.h @@ -188,6 +188,7 @@ class FakePepperInterfaceHtml5Fs : public nacl_io::PepperInterfaceDummy { private: void Init(); + FakeResourceManager resource_manager_; FakeCoreInterface core_interface_; FakeVarInterface var_interface_; FakeVarManager var_manager_; diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.cc b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.cc index 988aac4..7e5e382 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.cc +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.cc @@ -497,7 +497,8 @@ PP_Var FakeURLResponseInfoInterface::GetProperty( } FakePepperInterfaceURLLoader::FakePepperInterfaceURLLoader() - : var_interface_(&var_manager_), + : core_interface_(&resource_manager_), + var_interface_(&var_manager_), url_loader_interface_(&core_interface_), url_request_info_interface_(&core_interface_, &var_interface_), url_response_info_interface_(&core_interface_, &var_interface_) { diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.h b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.h index 9a2b633..5068f2d 100644 --- a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.h +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_pepper_interface_url_loader.h @@ -138,6 +138,7 @@ class FakePepperInterfaceURLLoader : public nacl_io::PepperInterfaceDummy { private: void Init(); + FakeResourceManager resource_manager_; FakeCoreInterface core_interface_; FakeVarInterface var_interface_; FakeVarManager var_manager_; diff --git a/native_client_sdk/src/tests/nacl_io_test/host_resolver_test.cc b/native_client_sdk/src/tests/nacl_io_test/host_resolver_test.cc new file mode 100644 index 0000000..9d79e1c --- /dev/null +++ b/native_client_sdk/src/tests/nacl_io_test/host_resolver_test.cc @@ -0,0 +1,55 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/types.h> +#include <sys/socket.h> + +#include "fake_ppapi/fake_pepper_interface.h" +#include "gtest/gtest.h" +#include "nacl_io/kernel_intercept.h" + +using namespace nacl_io; +using namespace sdk_util; + +namespace { + +class HostResolverTest : public ::testing::Test { + public: + HostResolverTest() : pepper_(NULL) {} + + void SetUp() { + pepper_ = new FakePepperInterface(); + ki_init_interface(NULL, pepper_); + } + + void TearDown() { + ki_uninit(); + pepper_ = NULL; + } + + protected: + FakePepperInterface* pepper_; +}; + +} // namespace + +#define NULL_HOST (static_cast<hostent*>(NULL)) + +TEST_F(HostResolverTest, GethostbynameNumeric) { + hostent* host = ki_gethostbyname(FAKE_HOSTNAME); + + // Verify the returned hostent structure + ASSERT_NE(NULL_HOST, host); + ASSERT_EQ(AF_INET, host->h_addrtype); + ASSERT_EQ(sizeof(in_addr_t), host->h_length); + ASSERT_STREQ(FAKE_HOSTNAME, host->h_name); + + in_addr_t** addr_list = reinterpret_cast<in_addr_t**>(host->h_addr_list); + ASSERT_NE(reinterpret_cast<in_addr_t**>(NULL), addr_list); + ASSERT_EQ(NULL, addr_list[1]); + in_addr_t exptected_addr = htonl(FAKE_IP); + ASSERT_EQ(exptected_addr, *addr_list[0]); +} |