Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Unified Diff: native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.cc

Issue 148223005: [NaCl SDK] Add fake for ppb_host_resolver and ppb_net_address (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.cc
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 0000000000000000000000000000000000000000..6d72933edfbed10e3d46a94a5e3fe9bda0e88d44
--- /dev/null
+++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_host_resolver_interface.cc
@@ -0,0 +1,103 @@
+// 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"
+
+// This fake resolver only know how to resolve this one
+// host to this one IP address.
+#define FAKE_HOSTNAME "testhost.com"
binji 2014/01/28 18:13:22 example.com is more traditional, right?
Sam Clegg 2014/01/28 19:55:22 Done.
+#define FAKE_IP 0x01020304
binji 2014/01/28 18:13:22 In the other fakes, I allowed the user of the fake
Sam Clegg 2014/01/28 19:55:22 I moved these into the .h file and use them in the
+
+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;
binji 2014/01/28 18:13:22 is it worth testing the cases where this can retur
Sam Clegg 2014/01/28 19:55:22 Yes, CL is a preliminary CL to the one where I add
+}
+
+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);
+}

Powered by Google App Engine
This is Rietveld 408576698