2 * Copyright (c) 2011 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.
17 #include "native_client/src/untrusted/irt/irt.h"
18 #include "native_client/src/untrusted/nacl/nacl_irt.h"
20 #include "ppapi/cpp/instance.h"
21 #include "ppapi/cpp/module.h"
22 #include "ppapi/cpp/var.h"
23 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
27 void load_manifest(TYPE_nacl_irt_query *query_func) {
28 struct nacl_irt_resource_open nacl_irt_resource_open;
29 if (sizeof(nacl_irt_resource_open) !=
31 NACL_IRT_RESOURCE_OPEN_v0_1,
32 &nacl_irt_resource_open,
33 sizeof(nacl_irt_resource_open))) {
34 str = "irt manifest api not found";
39 error = nacl_irt_resource_open.open_resource("test_file", &desc);
41 str = "Can't open file";
42 printf("Can't open file, error=%d", error);
46 str = "File Contents:\n";
48 FILE *iob = fdopen(desc, "r");
50 while (fgets(buffer, sizeof buffer, iob) != NULL) {
51 // NB: fgets does not discard the newline nor any carriage return
52 // character before that.
54 // Note that CR LF is the default end-of-line style for Windows.
55 // Furthermore, when the test_file (input data, which happens to
56 // be the nmf file) is initially created in a change list, the
57 // patch is sent to our try bots as text. This means that when
58 // the file arrives, it has CR LF endings instead of the original
59 // LF line endings. Since the expected or golden data is
60 // (manually) encoded in the HTML file's JavaScript, there will be
61 // a mismatch. After submission, the svn property svn:eol-style
62 // will be set to LF, so a clean check out should have LF and not
63 // CR LF endings, and the tests will pass without CR removal.
64 // However -- and there's always a however in long discourses --
65 // if the nmf file is edited, say, because the test is being
66 // modified, and the modification is being done on a Windows
67 // machine, then it is likely that the editor used by the
68 // programmer will convert the file to CR LF endings. Which,
69 // unfortunatly, implies that the test will mysteriously fail
72 // To defend against such nonsense, we weaken the test slighty,
73 // and just strip the CR if it is present.
74 int len = strlen(buffer);
75 if (len >= 2 && buffer[len-1] == '\n' && buffer[len-2] == '\r') {
81 printf("file loaded: %s\n", str.c_str());
82 fclose(iob); // closed desc
86 class TestInstance : public pp::Instance {
88 explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {}
89 virtual ~TestInstance() {}
90 virtual void HandleMessage(const pp::Var& var_message) {
91 if (!var_message.is_string()) {
94 if (var_message.AsString() != "hello") {
97 pp::Var reply = pp::Var(str);
102 class TestModule : public pp::Module {
104 TestModule() : pp::Module() {}
105 virtual ~TestModule() {}
107 virtual pp::Instance* CreateInstance(PP_Instance instance) {
108 return new TestInstance(instance);
113 Module* CreateModule() {
114 return new TestModule();
119 load_manifest(&__nacl_irt_query);
120 return PpapiPluginMain();