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.
18 #include "native_client/src/untrusted/irt/irt.h"
19 #include "native_client/src/untrusted/nacl/nacl_irt.h"
21 #include "ppapi/cpp/instance.h"
22 #include "ppapi/cpp/module.h"
23 #include "ppapi/cpp/var.h"
24 #include "ppapi/cpp/var_array.h"
25 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
28 std::vector<std::string> result;
30 std::string LoadManifestSuccess(TYPE_nacl_irt_query *query_func) {
31 struct nacl_irt_resource_open nacl_irt_resource_open;
32 if (sizeof(nacl_irt_resource_open) !=
34 NACL_IRT_RESOURCE_OPEN_v0_1,
35 &nacl_irt_resource_open,
36 sizeof(nacl_irt_resource_open))) {
37 return "irt manifest api not found";
41 error = nacl_irt_resource_open.open_resource("test_file", &desc);
43 printf("Can't open file, error=%d", error);
44 return "Can't open file";
51 while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) {
57 if (str != "Test File Content") {
58 printf("Wrong file content: \"%s\"\n", str.c_str());
59 return "Wrong file content: " + str;
65 std::string LoadManifestNonExistentEntry(
66 TYPE_nacl_irt_query *query_func) {
67 struct nacl_irt_resource_open nacl_irt_resource_open;
68 if (sizeof(nacl_irt_resource_open) !=
70 NACL_IRT_RESOURCE_OPEN_v0_1,
71 &nacl_irt_resource_open,
72 sizeof(nacl_irt_resource_open))) {
73 return "irt manifest api not found";
77 int error = nacl_irt_resource_open.open_resource("non_existent_entry", &desc);
79 // We expect ENOENT here, as it does not exist.
80 if (error != ENOENT) {
81 printf("Unexpected error code: %d\n", error);
83 snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
84 return std::string(buf);
90 std::string LoadManifestNonExistentFile(
91 TYPE_nacl_irt_query *query_func) {
92 struct nacl_irt_resource_open nacl_irt_resource_open;
93 if (sizeof(nacl_irt_resource_open) !=
95 NACL_IRT_RESOURCE_OPEN_v0_1,
96 &nacl_irt_resource_open,
97 sizeof(nacl_irt_resource_open))) {
98 return "irt manifest api not found";
102 int error = nacl_irt_resource_open.open_resource("dummy_test_file", &desc);
104 // We expect ENOENT here, as it does not exist.
105 if (error != ENOENT) {
106 printf("Unexpected error code: %d\n", error);
108 snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
109 return std::string(buf);
115 class TestInstance : public pp::Instance {
117 explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {}
118 virtual ~TestInstance() {}
119 virtual void HandleMessage(const pp::Var& var_message) {
120 if (!var_message.is_string()) {
123 if (var_message.AsString() != "hello") {
126 pp::VarArray reply = pp::VarArray();
127 for (size_t i = 0; i < result.size(); ++i) {
128 reply.Set(i, pp::Var(result[i]));
134 class TestModule : public pp::Module {
136 TestModule() : pp::Module() {}
137 virtual ~TestModule() {}
139 virtual pp::Instance* CreateInstance(PP_Instance instance) {
140 return new TestInstance(instance);
145 Module* CreateModule() {
146 return new TestModule();
151 result.push_back(LoadManifestSuccess(&__nacl_irt_query));
152 result.push_back(LoadManifestNonExistentEntry(&__nacl_irt_query));
153 result.push_back(LoadManifestNonExistentFile(&__nacl_irt_query));
154 return PpapiPluginMain();