Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / nacl / manifest_file / pm_pre_init_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 // Post-message based test for simple rpc based access to name services.
9 //
10
11 #include <string>
12
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <inttypes.h>
17 #include <sys/fcntl.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <pthread.h>
21
22 #include "native_client/src/include/nacl_base.h"
23 #include "native_client/src/public/imc_syscalls.h"
24 #include "native_client/src/public/name_service.h"
25 #include "native_client/src/shared/platform/nacl_sync.h"
26 #include "native_client/src/shared/platform/nacl_sync_checked.h"
27 #include "native_client/src/shared/platform/nacl_sync_raii.h"
28 #include "native_client/src/shared/srpc/nacl_srpc.h"
29
30 // TODO(bsy): move weak_ref module to the shared directory
31 #include "native_client/src/trusted/weak_ref/weak_ref.h"
32
33 #include "ppapi/cpp/instance.h"
34 #include "ppapi/cpp/module.h"
35 #include "ppapi/cpp/var.h"
36
37 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
38 #include "ppapi/native_client/src/trusted/weak_ref/call_on_main_thread.h"
39 #include "ppapi/native_client/src/untrusted/nacl_ppapi_util/nacl_ppapi_util.h"
40 #include "ppapi/native_client/src/untrusted/nacl_ppapi_util/string_buffer.h"
41
42 std::string *manifest_contents = NULL;
43
44 void TestManifestContents() {
45   nacl::StringBuffer      sb;
46   int                     status = -1;
47   int                     manifest;
48   struct NaClSrpcChannel  manifest_channel;
49   struct NaClSrpcChannel  ns_channel;
50
51   int ns = -1;
52   nacl_nameservice(&ns);
53   printf("ns = %d\n", ns);
54   assert(-1 != ns);
55   int connected_socket = imc_connect(ns);
56   assert(-1 != connected_socket);
57   if (!NaClSrpcClientCtor(&ns_channel, connected_socket)) {
58     close(ns);
59     sb.Printf("Srpc client channel ctor failed\n");
60     manifest_contents = new std::string(sb.ToString());
61     return;
62   }
63   sb.Printf("NaClSrpcClientCtor succeeded\n");
64   close(ns);
65   // name service lookup for the manifest service descriptor
66   if (NACL_SRPC_RESULT_OK !=
67       NaClSrpcInvokeBySignature(&ns_channel, NACL_NAME_SERVICE_LOOKUP,
68                                 "ManifestNameService", O_RDWR,
69                                 &status, &manifest) ||
70       NACL_NAME_SERVICE_SUCCESS != status) {
71     sb.Printf("nameservice lookup failed, status %d\n", status);
72     manifest_contents = new std::string(sb.ToString());
73     return;
74   }
75   sb.Printf("Got manifest descriptor %d\n", manifest);
76   if (-1 == manifest) {
77     manifest_contents = new std::string(sb.ToString());
78     return;
79   }
80
81   // connect to manifest name server
82   int manifest_conn = imc_connect(manifest);
83   close(manifest);
84   sb.Printf("got manifest connection %d\n", manifest_conn);
85   if (-1 == manifest_conn) {
86     sb.Printf("could not connect\n");
87     manifest_contents = new std::string(sb.ToString());
88     return;
89   }
90
91   // build the SRPC connection (do service discovery)
92   if (!NaClSrpcClientCtor(&manifest_channel, manifest_conn)) {
93     sb.Printf("could not build srpc client\n");
94     manifest_contents = new std::string(sb.ToString());
95     return;
96   }
97
98   int desc;
99
100   sb.Printf("Invoking name service lookup\n");
101   if (NACL_SRPC_RESULT_OK !=
102       NaClSrpcInvokeBySignature(&manifest_channel,
103                                 NACL_NAME_SERVICE_LOOKUP,
104                                 "files/test_file", O_RDONLY,
105                                 &status, &desc)) {
106     sb.Printf("manifest lookup RPC failed\n");
107     NaClSrpcDtor(&manifest_channel);
108     manifest_contents = new std::string(sb.ToString());
109     return;
110   }
111
112   sb.DiscardOutput();
113   sb.Printf("File Contents:\n");
114
115   char buffer[4096];
116   int len;
117   while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) {
118     // Null terminate.
119     buffer[len] = '\0';
120     sb.Printf("%s", buffer);
121   }
122
123   sb.Printf("\n");
124   sb.Printf("Opening non-existent file:\n");
125   if (NACL_SRPC_RESULT_OK !=
126       NaClSrpcInvokeBySignature(&manifest_channel,
127                                 NACL_NAME_SERVICE_LOOKUP,
128                                 "foobar/baz", O_RDONLY,
129                                 &status, &desc)) {
130     sb.Printf("bogus manifest lookup RPC failed\n");
131     NaClSrpcDtor(&manifest_channel);
132     manifest_contents = new std::string(sb.ToString());
133     return;
134   }
135   sb.Printf("Got descriptor %d, status %d\n", desc, status);
136   if (-1 != desc) {
137     (void) close(desc);
138   }
139
140   NaClSrpcDtor(&manifest_channel);
141   manifest_contents = new std::string(sb.ToString());
142 }
143
144 class PostStringMessageWrapper
145     : public nacl_ppapi::EventThreadWorkStateWrapper<nacl_ppapi::VoidResult> {
146  public:
147   PostStringMessageWrapper(nacl_ppapi::EventThreadWorkState<
148                              nacl_ppapi::VoidResult>
149                            *state,
150                            const std::string &msg)
151       : nacl_ppapi::EventThreadWorkStateWrapper<nacl_ppapi::VoidResult>(
152           state),
153         msg_(msg) {}
154   ~PostStringMessageWrapper();
155   const std::string &msg() const { return msg_; }
156  private:
157   std::string msg_;
158
159   DISALLOW_COPY_AND_ASSIGN(PostStringMessageWrapper);
160 };
161
162 // ---------------------------------------------------------------------------
163
164 class MyInstance;
165
166 // This object represents one time the page says <embed>.
167 class MyInstance : public nacl_ppapi::NaClPpapiPluginInstance {
168  public:
169   explicit MyInstance(PP_Instance instance);
170   virtual ~MyInstance();
171   virtual void HandleMessage(const pp::Var& message_data);
172
173  private:
174   DISALLOW_COPY_AND_ASSIGN(MyInstance);
175 };
176
177 // ---------------------------------------------------------------------------
178
179 MyInstance::MyInstance(PP_Instance instance)
180     : nacl_ppapi::NaClPpapiPluginInstance(instance) {
181 }
182
183 MyInstance::~MyInstance() {}
184
185 // ---------------------------------------------------------------------------
186
187 // HandleMessage gets invoked when postMessage is called on the DOM
188 // element associated with this plugin instance.  In this case, if we
189 // are given a string, we'll post a message back to JavaScript with a
190 // reply -- essentially treating this as a string-based RPC.
191 void MyInstance::HandleMessage(const pp::Var& message) {
192   if (message.is_string()) {
193     if (message.AsString() == "manifest_data") {
194       PostMessage(*manifest_contents);
195     } else {
196       fprintf(stderr, "HandleMessage: Unrecognized request \"%s\".\n",
197               message.AsString().c_str());
198     }
199   } else {
200     fprintf(stderr, "HandleMessage: message is not a string\n");
201     fflush(NULL);
202   }
203 }
204
205 // This object is the global object representing this plugin library as long
206 // as it is loaded.
207 class MyModule : public pp::Module {
208  public:
209   MyModule() : pp::Module() {}
210   virtual ~MyModule() {}
211
212   // Override CreateInstance to create your customized Instance object.
213   virtual pp::Instance *CreateInstance(PP_Instance instance);
214
215   DISALLOW_COPY_AND_ASSIGN(MyModule);
216 };
217
218 pp::Instance *MyModule::CreateInstance(PP_Instance pp_instance) {
219   MyInstance *instance = new MyInstance(pp_instance);
220   fprintf(stderr, "CreateInstance: returning instance %p\n",
221           reinterpret_cast<void *>(instance));
222
223   return instance;
224 }
225
226 namespace pp {
227
228 // Factory function for your specialization of the Module object.
229 Module* CreateModule() {
230   fprintf(stderr, "CreateModule invoked\n"); fflush(NULL);
231   return new MyModule();
232 }
233
234 }  // namespace pp
235
236 int main() {
237   NaClSrpcModuleInit();
238   TestManifestContents();
239   return PpapiPluginMain();
240 }