Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / nacl / manifest_file / irt_manifest_file_test.cc
1 /*
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.
5  */
6
7 //
8 // Test for resource open before PPAPI initialization.
9 //
10
11 #include <stdio.h>
12 #include <string.h>
13
14 #include <sstream>
15 #include <string>
16 #include <vector>
17
18 #include "native_client/src/untrusted/irt/irt.h"
19 #include "native_client/src/untrusted/nacl/nacl_irt.h"
20
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"
26
27
28 std::vector<std::string> result;
29
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) !=
33       (*query_func)(
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";
38   }
39   int desc;
40   int error;
41   error = nacl_irt_resource_open.open_resource("test_file", &desc);
42   if (0 != error) {
43     printf("Can't open file, error=%d", error);
44     return "Can't open file";
45   }
46
47   std::string str;
48
49   char buffer[4096];
50   int len;
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.
54     //
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
71     // again.
72     //
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') {
77       buffer[len-2] = '\n';
78       buffer[len-1] = '\0';
79     }
80     // Null terminate.
81     buffer[len] = 0;
82     str += buffer;
83   }
84
85   if (str != "Test File Content") {
86     printf("Wrong file content: \"%s\"\n", str.c_str());
87     return "Wrong file content: " + str;
88   }
89
90   return "Pass";
91 }
92
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) !=
97       (*query_func)(
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";
102   }
103
104   int desc;
105   int error = nacl_irt_resource_open.open_resource("non_existent_entry", &desc);
106
107   // We expect ENOENT here, as it does not exist.
108   if (error != ENOENT) {
109     printf("Unexpected error code: %d\n", error);
110     char buf[80];
111     snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
112     return std::string(buf);
113   }
114
115   return "Pass";
116 }
117
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) !=
122       (*query_func)(
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";
127   }
128
129   int desc;
130   int error = nacl_irt_resource_open.open_resource("dummy_test_file", &desc);
131
132   // We expect ENOENT here, as it does not exist.
133   if (error != ENOENT) {
134     printf("Unexpected error code: %d\n", error);
135     char buf[80];
136     snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
137     return std::string(buf);
138   }
139
140   return "Pass";
141 }
142
143 class TestInstance : public pp::Instance {
144  public:
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()) {
149       return;
150     }
151     if (var_message.AsString() != "hello") {
152       return;
153     }
154     pp::VarArray reply = pp::VarArray();
155     for (size_t i = 0; i < result.size(); ++i) {
156       reply.Set(i, pp::Var(result[i]));
157     }
158     PostMessage(reply);
159   }
160 };
161
162 class TestModule : public pp::Module {
163  public:
164   TestModule() : pp::Module() {}
165   virtual ~TestModule() {}
166
167   virtual pp::Instance* CreateInstance(PP_Instance instance) {
168     return new TestInstance(instance);
169   }
170 };
171
172 namespace pp {
173 Module* CreateModule() {
174   return new TestModule();
175 }
176 }
177
178 int main() {
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();
183 }