Increase MessageAttachmentSet::kMaxDescriptorsPerMessage to 128
[chromium-blink-merge.git] / ppapi / tests / extensions / packaged_app / test_packaged_app.cc
blobbdbc68386a0f27c43bc9a27ec6c9b18fea7d5a57
1 // Copyright (c) 2014 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 <pthread.h>
6 #include <unistd.h>
8 #include <sstream>
9 #include <string>
11 #include "native_client/src/untrusted/irt/irt.h"
13 #include "ppapi/cpp/completion_callback.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/module.h"
16 #include "ppapi/cpp/tcp_socket.h"
17 #include "ppapi/cpp/var.h"
18 #include "ppapi/utility/completion_callback_factory.h"
20 namespace {
22 std::string g_last_error;
23 pp::Instance* g_instance = NULL;
25 // This should be the same as MessageAttachmentSet::kMaxDescriptorsPerMessage in
26 // ipc/ipc_message_attachment_set.h.
27 // TODO(yusukes): Once all the NaCl toolchains support C++11, Add
28 // #include "ipc/file_descriptor_set_posix.h" and remove the constant.
29 const size_t kMaxDescriptorsPerMessage = 128;
31 // Returns true if the resource file whose name is |key| exists and its content
32 // matches |content|.
33 bool LoadManifestInternal(nacl_irt_resource_open* nacl_irt_resource_open,
34 const std::string& key,
35 const std::string& content) {
36 int desc;
37 int error;
38 error = nacl_irt_resource_open->open_resource(key.c_str(), &desc);
39 if (0 != error) {
40 g_last_error = "Can't open file " + key;
41 return false;
44 std::string str;
46 char buffer[4096];
47 int len;
48 while ((len = read(desc, buffer, sizeof(buffer) - 1)) > 0) {
49 // Null terminate.
50 buffer[len] = '\0';
51 str += buffer;
53 if (close(desc)) {
54 g_last_error = "Close failed: file=" + key;
55 return false;
58 if (str != content) {
59 g_last_error = "Wrong file content: file=" + key + ", expected=" + content +
60 ", actual=" + str;
61 return false;
64 return true;
67 // Tests if open_resource works in a packaged app. This test is similar to
68 // NaClBrowserTest*.IrtManifestFile, but unlike the NaCl test, this one tests
69 // the "fast path" in DownloadNexe() in ppb_nacl_private_impl.cc which opens
70 // resource files without using URLLoader.
71 void LoadManifest() {
72 if (pthread_detach(pthread_self())) {
73 g_last_error = "pthread_detach failed";
74 return;
77 struct nacl_irt_resource_open nacl_irt_resource_open;
78 if (sizeof(nacl_irt_resource_open) !=
79 nacl_interface_query(NACL_IRT_RESOURCE_OPEN_v0_1,
80 &nacl_irt_resource_open,
81 sizeof(nacl_irt_resource_open))) {
82 g_last_error = "NACL_IRT_RESOURCE_OPEN_v0_1 not found";
83 return;
86 for (size_t i = 0; i <= kMaxDescriptorsPerMessage; ++i) {
87 std::stringstream key;
88 key << "test_file" << i;
89 std::string content = "Example contents for open_resource test" +
90 std::string(i % 2 ? "2" : "");
91 if (!LoadManifestInternal(&nacl_irt_resource_open, key.str(), content))
92 break;
93 // Open the same resource file again to make sure each file descriptor
94 // returned from open_resource has its own file offset.
95 if (!LoadManifestInternal(&nacl_irt_resource_open, key.str(), content))
96 break;
100 void PostReply(void* user_data, int32_t status) {
101 if (!g_last_error.empty())
102 g_instance->PostMessage(g_last_error.c_str());
103 else
104 g_instance->PostMessage("PASS");
107 void* RunTestsOnBackgroundThread(void* thread_id) {
108 LoadManifest();
109 pp::Module::Get()->core()->CallOnMainThread(
110 0, pp::CompletionCallback(&PostReply, NULL));
111 return NULL;
114 class MyInstance : public pp::Instance {
115 public:
116 explicit MyInstance(PP_Instance instance)
117 : pp::Instance(instance), socket_(this), factory_(this) {
118 g_instance = this;
120 virtual ~MyInstance() { }
122 void DidBindSocket(int32_t result) {
123 // We didn't ask for socket permission in our manifest, so it should fail.
124 if (result == PP_ERROR_NOACCESS)
125 PostMessage("PASS");
126 else
127 PostMessage(result);
130 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
131 pthread_t thread;
132 // irt_open_resource() isn't allowed to be called on the main thread once
133 // Pepper starts, so the test must happen on a background thread.
134 if (pthread_create(&thread, NULL, &RunTestsOnBackgroundThread, NULL)) {
135 g_last_error = "pthread_create failed";
136 PostReply(NULL, 0);
138 // Attempt to bind a socket. We don't have permissions, so it should fail.
139 PP_NetAddress_IPv4 ipv4_address = {80, {127, 0, 0, 1} };
140 pp::NetAddress address(this, ipv4_address);
141 socket_.Bind(address, factory_.NewCallback(&MyInstance::DidBindSocket));
142 return true;
145 private:
146 pp::TCPSocket socket_;
147 pp::CompletionCallbackFactory<MyInstance> factory_;
150 class MyModule : public pp::Module {
151 public:
152 MyModule() : pp::Module() { }
153 virtual ~MyModule() { }
155 virtual pp::Instance* CreateInstance(PP_Instance instance) {
156 return new MyInstance(instance);
160 } // namespace
162 namespace pp {
164 Module* CreateModule() {
165 return new MyModule();
168 } // namespace pp