2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 // Test for resource open before PPAPI initialization.
19 #include "native_client/src/untrusted/irt/irt.h"
20 #include "native_client/src/untrusted/nacl/nacl_irt.h"
22 #include "ppapi/cpp/completion_callback.h"
23 #include "ppapi/cpp/instance.h"
24 #include "ppapi/cpp/module.h"
25 #include "ppapi/cpp/var.h"
26 #include "ppapi/cpp/var_array.h"
27 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
30 std::vector<std::string> result;
32 pp::Instance* g_instance = NULL;
34 std::string LoadManifestSuccess(TYPE_nacl_irt_query *query_func) {
35 struct nacl_irt_resource_open nacl_irt_resource_open;
36 if (sizeof(nacl_irt_resource_open) !=
38 NACL_IRT_RESOURCE_OPEN_v0_1,
39 &nacl_irt_resource_open,
40 sizeof(nacl_irt_resource_open))) {
41 return "irt manifest api not found";
45 error = nacl_irt_resource_open.open_resource("test_file", &desc);
47 printf("Can't open file, error=%d", error);
48 return "Can't open file";
55 while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) {
61 if (str != "Test File Content") {
62 printf("Wrong file content: \"%s\"\n", str.c_str());
63 return "Wrong file content: " + str;
69 std::string LoadManifestNonExistentEntry(
70 TYPE_nacl_irt_query *query_func) {
71 struct nacl_irt_resource_open nacl_irt_resource_open;
72 if (sizeof(nacl_irt_resource_open) !=
74 NACL_IRT_RESOURCE_OPEN_v0_1,
75 &nacl_irt_resource_open,
76 sizeof(nacl_irt_resource_open))) {
77 return "irt manifest api not found";
81 int error = nacl_irt_resource_open.open_resource("non_existent_entry", &desc);
83 // We expect ENOENT here, as it does not exist.
84 if (error != ENOENT) {
85 printf("Unexpected error code: %d\n", error);
87 snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
88 return std::string(buf);
94 std::string LoadManifestNonExistentFile(
95 TYPE_nacl_irt_query *query_func) {
96 struct nacl_irt_resource_open nacl_irt_resource_open;
97 if (sizeof(nacl_irt_resource_open) !=
99 NACL_IRT_RESOURCE_OPEN_v0_1,
100 &nacl_irt_resource_open,
101 sizeof(nacl_irt_resource_open))) {
102 return "irt manifest api not found";
106 int error = nacl_irt_resource_open.open_resource("dummy_test_file", &desc);
108 // We expect ENOENT here, as it does not exist.
109 if (error != ENOENT) {
110 printf("Unexpected error code: %d\n", error);
112 snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
113 return std::string(buf);
120 result.push_back(LoadManifestSuccess(&__nacl_irt_query));
121 result.push_back(LoadManifestNonExistentEntry(&__nacl_irt_query));
122 result.push_back(LoadManifestNonExistentFile(&__nacl_irt_query));
125 void PostReply(void* user_data, int32_t status) {
126 pp::VarArray reply = pp::VarArray();
127 for (size_t i = 0; i < result.size(); ++i)
128 reply.Set(i, pp::Var(result[i]));
129 g_instance->PostMessage(reply);
132 void* RunTestsOnBackgroundThread(void *thread_id) {
134 pp::Module::Get()->core()->CallOnMainThread(
135 0, pp::CompletionCallback(&PostReply, NULL));
139 class TestInstance : public pp::Instance {
141 explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {
145 virtual ~TestInstance() {}
146 virtual void HandleMessage(const pp::Var& var_message) {
147 if (!var_message.is_string()) {
150 if (var_message.AsString() != "hello") {
155 // We test the manifest routines again after PPAPI has initialized to
156 // ensure that they still work.
158 // irt_open_resource() isn't allowed to be called on the main thread once
159 // pepper starts, so these tests must happen on a background thread.
161 // TODO(teravest): Check the return value here.
162 pthread_create(&thread, NULL, &RunTestsOnBackgroundThread, NULL);
166 class TestModule : public pp::Module {
168 TestModule() : pp::Module() {}
169 virtual ~TestModule() {}
171 virtual pp::Instance* CreateInstance(PP_Instance instance) {
172 return new TestInstance(instance);
177 Module* CreateModule() {
178 return new TestModule();
184 return PpapiPluginMain();