Upstream version 10.39.225.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 <pthread.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include <sstream>
16 #include <string>
17 #include <vector>
18
19 #include "native_client/src/untrusted/irt/irt.h"
20 #include "native_client/src/untrusted/nacl/nacl_irt.h"
21
22 #include "ppapi/cpp/completion_callback.h"
23 #include "ppapi/cpp/instance.h"
24 #include "ppapi/cpp/module.h"
25 #include "ppapi/cpp/var.h"
26 #include "ppapi/cpp/var_array.h"
27 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
28
29
30 std::vector<std::string> result;
31
32 pp::Instance* g_instance = NULL;
33
34 std::string LoadManifestSuccess(TYPE_nacl_irt_query *query_func) {
35   struct nacl_irt_resource_open nacl_irt_resource_open;
36   if (sizeof(nacl_irt_resource_open) !=
37       (*query_func)(
38           NACL_IRT_RESOURCE_OPEN_v0_1,
39           &nacl_irt_resource_open,
40           sizeof(nacl_irt_resource_open))) {
41     return "irt manifest api not found";
42   }
43   int desc;
44   int error;
45   error = nacl_irt_resource_open.open_resource("test_file", &desc);
46   if (0 != error) {
47     printf("Can't open file, error=%d", error);
48     return "Can't open file";
49   }
50
51   std::string str;
52
53   char buffer[4096];
54   int len;
55   while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) {
56     // Null terminate.
57     buffer[len] = '\0';
58     str += buffer;
59   }
60
61   if (str != "Test File Content") {
62     printf("Wrong file content: \"%s\"\n", str.c_str());
63     return "Wrong file content: " + str;
64   }
65
66   return "Pass";
67 }
68
69 std::string LoadManifestNonExistentEntry(
70     TYPE_nacl_irt_query *query_func) {
71   struct nacl_irt_resource_open nacl_irt_resource_open;
72   if (sizeof(nacl_irt_resource_open) !=
73       (*query_func)(
74           NACL_IRT_RESOURCE_OPEN_v0_1,
75           &nacl_irt_resource_open,
76           sizeof(nacl_irt_resource_open))) {
77     return "irt manifest api not found";
78   }
79
80   int desc;
81   int error = nacl_irt_resource_open.open_resource("non_existent_entry", &desc);
82
83   // We expect ENOENT here, as it does not exist.
84   if (error != ENOENT) {
85     printf("Unexpected error code: %d\n", error);
86     char buf[80];
87     snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
88     return std::string(buf);
89   }
90
91   return "Pass";
92 }
93
94 std::string LoadManifestNonExistentFile(
95     TYPE_nacl_irt_query *query_func) {
96   struct nacl_irt_resource_open nacl_irt_resource_open;
97   if (sizeof(nacl_irt_resource_open) !=
98       (*query_func)(
99           NACL_IRT_RESOURCE_OPEN_v0_1,
100           &nacl_irt_resource_open,
101           sizeof(nacl_irt_resource_open))) {
102     return "irt manifest api not found";
103   }
104
105   int desc;
106   int error = nacl_irt_resource_open.open_resource("dummy_test_file", &desc);
107
108   // We expect ENOENT here, as it does not exist.
109   if (error != ENOENT) {
110     printf("Unexpected error code: %d\n", error);
111     char buf[80];
112     snprintf(buf, sizeof(buf), "open_resource() result: %d", error);
113     return std::string(buf);
114   }
115
116   return "Pass";
117 }
118
119 void RunTests() {
120   result.push_back(LoadManifestSuccess(&__nacl_irt_query));
121   result.push_back(LoadManifestNonExistentEntry(&__nacl_irt_query));
122   result.push_back(LoadManifestNonExistentFile(&__nacl_irt_query));
123 }
124
125 void PostReply(void* user_data, int32_t status) {
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   g_instance->PostMessage(reply);
130 }
131
132 void* RunTestsOnBackgroundThread(void *thread_id) {
133   RunTests();
134   pp::Module::Get()->core()->CallOnMainThread(
135       0, pp::CompletionCallback(&PostReply, NULL));
136   return NULL;
137 }
138
139 class TestInstance : public pp::Instance {
140  public:
141   explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {
142     g_instance = this;
143   }
144
145   virtual ~TestInstance() {}
146   virtual void HandleMessage(const pp::Var& var_message) {
147     if (!var_message.is_string()) {
148       return;
149     }
150     if (var_message.AsString() != "hello") {
151       return;
152     }
153
154     pthread_t thread;
155     // We test the manifest routines again after PPAPI has initialized to
156     // ensure that they still work.
157     //
158     // irt_open_resource() isn't allowed to be called on the main thread once
159     // pepper starts, so these tests must happen on a background thread.
160     //
161     // TODO(teravest): Check the return value here.
162     pthread_create(&thread, NULL, &RunTestsOnBackgroundThread, NULL);
163   }
164 };
165
166 class TestModule : public pp::Module {
167  public:
168   TestModule() : pp::Module() {}
169   virtual ~TestModule() {}
170
171   virtual pp::Instance* CreateInstance(PP_Instance instance) {
172     return new TestInstance(instance);
173   }
174 };
175
176 namespace pp {
177 Module* CreateModule() {
178   return new TestModule();
179 }
180 }
181
182 int main() {
183   RunTests();
184   return PpapiPluginMain();
185 }