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) {
52 // NB: fgets does not discard the newline nor any carriage return
53 // character before that.
55 // Note that CR LF is the default end-of-line style for Windows.
56 // Furthermore, when the test_file (input data, which happens to
57 // be the nmf file) is initially created in a change list, the
58 // patch is sent to our try bots as text. This means that when
59 // the file arrives, it has CR LF endings instead of the original
60 // LF line endings. Since the expected or golden data is
61 // (manually) encoded in the HTML file's JavaScript, there will be
62 // a mismatch. After submission, the svn property svn:eol-style
63 // will be set to LF, so a clean check out should have LF and not
64 // CR LF endings, and the tests will pass without CR removal.
65 // However -- and there's always a however in long discourses --
66 // if the nmf file is edited, say, because the test is being
67 // modified, and the modification is being done on a Windows
68 // machine, then it is likely that the editor used by the
69 // programmer will convert the file to CR LF endings. Which,
70 // unfortunatly, implies that the test will mysteriously fail
73 // To defend against such nonsense, we weaken the test slighty,
74 // and just strip the CR if it is present.
75 int len = strlen(buffer);
76 if (len >= 2 && buffer[len-1] == '\n' && buffer[len-2] == '\r') {
85 if (str != "Test File Content") {
86 printf("Wrong file content: \"%s\"\n", str.c_str());
87 return "Wrong file content: " + str;
93 std::string LoadManifestNonExistentEntry(
94 TYPE_nacl_irt_query *query_func) {
95 struct nacl_irt_resource_open nacl_irt_resource_open;
96 if (sizeof(nacl_irt_resource_open) !=
98 NACL_IRT_RESOURCE_OPEN_v0_1,
99 &nacl_irt_resource_open,
100 sizeof(nacl_irt_resource_open))) {
101 return "irt manifest api not found";
105 int error = nacl_irt_resource_open.open_resource("non_existent_entry", &desc);
107 // We expect ENOENT here, as it does not exist.
108 if (error != ENOENT) {
109 printf("Unexpected error code: %d\n", error);
111 snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
112 return std::string(buf);
118 std::string LoadManifestNonExistentFile(
119 TYPE_nacl_irt_query *query_func) {
120 struct nacl_irt_resource_open nacl_irt_resource_open;
121 if (sizeof(nacl_irt_resource_open) !=
123 NACL_IRT_RESOURCE_OPEN_v0_1,
124 &nacl_irt_resource_open,
125 sizeof(nacl_irt_resource_open))) {
126 return "irt manifest api not found";
130 int error = nacl_irt_resource_open.open_resource("dummy_test_file", &desc);
132 // We expect ENOENT here, as it does not exist.
133 if (error != ENOENT) {
134 printf("Unexpected error code: %d\n", error);
136 snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
137 return std::string(buf);
143 class TestInstance : public pp::Instance {
145 explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {}
146 virtual ~TestInstance() {}
147 virtual void HandleMessage(const pp::Var& var_message) {
148 if (!var_message.is_string()) {
151 if (var_message.AsString() != "hello") {
154 pp::VarArray reply = pp::VarArray();
155 for (size_t i = 0; i < result.size(); ++i) {
156 reply.Set(i, pp::Var(result[i]));
162 class TestModule : public pp::Module {
164 TestModule() : pp::Module() {}
165 virtual ~TestModule() {}
167 virtual pp::Instance* CreateInstance(PP_Instance instance) {
168 return new TestInstance(instance);
173 Module* CreateModule() {
174 return new TestModule();
179 result.push_back(LoadManifestSuccess(&__nacl_irt_query));
180 result.push_back(LoadManifestNonExistentEntry(&__nacl_irt_query));
181 result.push_back(LoadManifestNonExistentFile(&__nacl_irt_query));
182 return PpapiPluginMain();