Upstream version 9.38.198.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     // Null terminate.
53     buffer[len] = '\0';
54     str += buffer;
55   }
56
57   if (str != "Test File Content") {
58     printf("Wrong file content: \"%s\"\n", str.c_str());
59     return "Wrong file content: " + str;
60   }
61
62   return "Pass";
63 }
64
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) !=
69       (*query_func)(
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";
74   }
75
76   int desc;
77   int error = nacl_irt_resource_open.open_resource("non_existent_entry", &desc);
78
79   // We expect ENOENT here, as it does not exist.
80   if (error != ENOENT) {
81     printf("Unexpected error code: %d\n", error);
82     char buf[80];
83     snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
84     return std::string(buf);
85   }
86
87   return "Pass";
88 }
89
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) !=
94       (*query_func)(
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";
99   }
100
101   int desc;
102   int error = nacl_irt_resource_open.open_resource("dummy_test_file", &desc);
103
104   // We expect ENOENT here, as it does not exist.
105   if (error != ENOENT) {
106     printf("Unexpected error code: %d\n", error);
107     char buf[80];
108     snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
109     return std::string(buf);
110   }
111
112   return "Pass";
113 }
114
115 class TestInstance : public pp::Instance {
116  public:
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()) {
121       return;
122     }
123     if (var_message.AsString() != "hello") {
124       return;
125     }
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     }
130     PostMessage(reply);
131   }
132 };
133
134 class TestModule : public pp::Module {
135  public:
136   TestModule() : pp::Module() {}
137   virtual ~TestModule() {}
138
139   virtual pp::Instance* CreateInstance(PP_Instance instance) {
140     return new TestInstance(instance);
141   }
142 };
143
144 namespace pp {
145 Module* CreateModule() {
146   return new TestModule();
147 }
148 }
149
150 int main() {
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();
155 }