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.
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"
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
33 bool LoadManifestInternal(nacl_irt_resource_open
* nacl_irt_resource_open
,
34 const std::string
& key
,
35 const std::string
& content
) {
38 error
= nacl_irt_resource_open
->open_resource(key
.c_str(), &desc
);
40 g_last_error
= "Can't open file " + key
;
48 while ((len
= read(desc
, buffer
, sizeof(buffer
) - 1)) > 0) {
54 g_last_error
= "Close failed: file=" + key
;
59 g_last_error
= "Wrong file content: file=" + key
+ ", expected=" + content
+
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.
72 if (pthread_detach(pthread_self())) {
73 g_last_error
= "pthread_detach failed";
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";
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
))
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
))
100 void PostReply(void* user_data
, int32_t status
) {
101 if (!g_last_error
.empty())
102 g_instance
->PostMessage(g_last_error
.c_str());
104 g_instance
->PostMessage("PASS");
107 void* RunTestsOnBackgroundThread(void* thread_id
) {
109 pp::Module::Get()->core()->CallOnMainThread(
110 0, pp::CompletionCallback(&PostReply
, NULL
));
114 class MyInstance
: public pp::Instance
{
116 explicit MyInstance(PP_Instance instance
)
117 : pp::Instance(instance
), socket_(this), factory_(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
)
130 virtual bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) {
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";
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
));
146 pp::TCPSocket socket_
;
147 pp::CompletionCallbackFactory
<MyInstance
> factory_
;
150 class MyModule
: public pp::Module
{
152 MyModule() : pp::Module() { }
153 virtual ~MyModule() { }
155 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
156 return new MyInstance(instance
);
164 Module
* CreateModule() {
165 return new MyModule();