Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: tests/nameservice/srpc_nameservice_test.c

Issue 7108031: this patch adds the manifest proxy server to sel_ldr and the manifest (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: '' Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/nameservice/srpc_nameservice.nmf ('k') | tests/nameservice/srpc_nameservice_test.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2011 The Native Client 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 simple rpc based access to name services.
9 */
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <inttypes.h>
15 #include <sys/fcntl.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/nacl_syscalls.h>
19 #include <nacl/nacl_srpc.h>
20 #if 0 /* swap after SDK update */
21 #include <sys/nacl_name_service.h>
22 #else
23 #include "native_client/src/trusted/service_runtime/include/sys/nacl_name_servic e.h"
24 #endif
25 #include <nacl/nacl_srpc.h>
26
27 #define RNG_OUTPUT_BYTES 1024
28
29 #define BYTES_PER_LINE 32
30 #define BYTE_SPACING 4
31
32 struct StringBuffer {
33 size_t nbytes;
34 size_t insert;
35 char *buffer;
36 };
37
38 NaClSrpcChannel ns_channel;
39
40 void StringBufferCtor(struct StringBuffer *sb) {
41 sb->nbytes = 1024;
42 sb->insert = 0;
43 sb->buffer = malloc(sb->nbytes);
44 if (NULL == sb->buffer) {
45 perror("StringBufferInit::malloc");
46 abort();
47 }
48 sb->buffer[0] = '\0';
49 }
50
51 void StringBufferDiscardOutput(struct StringBuffer *sb) {
52 sb->insert = 0;
53 }
54
55 void StringBufferDtor(struct StringBuffer *sb) {
56 sb->nbytes = 0;
57 sb->insert = 0;
58 free(sb->buffer);
59 sb->buffer = NULL;
60 }
61
62 void StringBufferPrintf(struct StringBuffer *sb,
63 char const *fmt, ...)
64 __attribute__((format(printf, 2, 3)));
65
66 void StringBufferPrintf(struct StringBuffer *sb,
67 char const *fmt, ...) {
68 size_t space;
69 char *insert_pt;
70 va_list ap;
71 int written = 0;
72 char *new_buffer;
73
74 va_start(ap, fmt);
75 vfprintf(stderr, fmt, ap);
76 va_end(ap);
77
78 for (;;) {
79 space = sb->nbytes - sb->insert;
80 insert_pt = sb->buffer + sb->insert;
81 va_start(ap, fmt);
82 written = vsnprintf(insert_pt, space, fmt, ap);
83 va_end(ap);
84 if (written < space) {
85 sb->insert += written;
86 break;
87 }
88 /* insufficient space -- grow the buffer */
89 new_buffer = realloc(sb->buffer, 2 * sb->nbytes);
90 if (NULL == new_buffer) {
91 /* give up */
92 fprintf(stderr, "StringBufferPrintf: no memory\n");
93 break;
94 }
95 sb->nbytes *= 2;
96 sb->buffer = new_buffer;
97 }
98 }
99
100 void dump_output(struct StringBuffer *sb, int d, size_t nbytes) {
101 uint8_t *bytes;
102 int got;
103 int copied;
104 int ix;
105
106 bytes = malloc(nbytes);
107 if (!bytes) {
108 perror("dump_output");
109 fprintf(stderr, "No memory\n");
110 return;
111 }
112 /* read output */
113 for (got = 0; got < nbytes; got += copied) {
114 copied = read(d, bytes + got, nbytes - got);
115 if (-1 == copied) {
116 perror("dump_output:read");
117 fprintf(stderr, "read failure\n");
118 break;
119 }
120 printf("read(%d, ..., %zd) -> %d\n", d, nbytes - got, copied);
121 }
122 /* hex dump it */
123 for (ix = 0; ix < got; ++ix) {
124 if (0 == (ix & (BYTES_PER_LINE-1))) {
125 StringBufferPrintf(sb, "\n%04x:", ix);
126 } else if (0 == (ix & (BYTE_SPACING-1))) {
127 StringBufferPrintf(sb, " ");
128 }
129 StringBufferPrintf(sb, "%02x", bytes[ix]);
130 }
131 StringBufferPrintf(sb, "\n");
132
133 free(bytes);
134 }
135
136 int EnumerateNames(struct StringBuffer *sb, NaClSrpcChannel *nschan) {
137 char buffer[1024];
138 uint32_t nbytes = sizeof buffer;
139 char *p;
140 size_t name_len;
141
142 if (NACL_SRPC_RESULT_OK != NaClSrpcInvokeBySignature(nschan,
143 NACL_NAME_SERVICE_LIST,
144 &nbytes, buffer)) {
145 return 0;
146 }
147 StringBufferPrintf(sb, "nbytes = %zu\n", (size_t) nbytes);
148 if (nbytes == sizeof buffer) {
149 fprintf(stderr, "Insufficent space for namespace enumeration\n");
150 return 0;
151 }
152 for (p = buffer; p - buffer < nbytes; p += name_len) {
153 name_len = strlen(p) + 1;
154 StringBufferPrintf(sb, "%s\n", p);
155 }
156 return 1;
157 }
158
159 /*
160 * Return name service output
161 */
162 void NameServiceDump(NaClSrpcRpc *rpc,
163 NaClSrpcArg **in_args,
164 NaClSrpcArg **out_args,
165 NaClSrpcClosure *done) {
166 struct StringBuffer sb;
167 StringBufferCtor(&sb);
168
169 if (EnumerateNames(&sb, &ns_channel)) {
170 out_args[0]->arrays.str = strdup(sb.buffer);
171 rpc->result = NACL_SRPC_RESULT_OK;
172 } else {
173 rpc->result = NACL_SRPC_RESULT_APP_ERROR;
174 }
175 done->Run(done);
176 StringBufferDtor(&sb);
177 }
178
179 /*
180 * Dump RNG output into a string.
181 */
182 void RngDump(NaClSrpcRpc *rpc,
183 NaClSrpcArg **in_args,
184 NaClSrpcArg **out_args,
185 NaClSrpcClosure *done) {
186 struct StringBuffer sb;
187 NaClSrpcError rpc_result;
188 int status;
189 int rng;
190
191 StringBufferCtor(&sb);
192 rpc_result = NaClSrpcInvokeBySignature(&ns_channel, NACL_NAME_SERVICE_LOOKUP,
193 "SecureRandom", O_RDONLY,
194 &status, &rng);
195 assert(NACL_SRPC_RESULT_OK == rpc_result);
196 printf("rpc status %d\n", status);
197 assert(NACL_NAME_SERVICE_SUCCESS == status);
198 printf("rng descriptor %d\n", rng);
199
200 dump_output(&sb, rng, RNG_OUTPUT_BYTES);
201 out_args[0]->arrays.str = strdup(sb.buffer);
202 rpc->result = NACL_SRPC_RESULT_OK;
203 done->Run(done);
204 close(rng);
205 StringBufferDtor(&sb);
206 }
207
208 void ManifestTest(NaClSrpcRpc *rpc,
209 NaClSrpcArg **in_args,
210 NaClSrpcArg **out_args,
211 NaClSrpcClosure *done) {
212 struct StringBuffer sb;
213 int status;
214 int manifest;
215 char buffer[1024];
216 uint32_t nbytes = sizeof buffer;
217
218 /* just get the descriptor for now */
219 StringBufferCtor(&sb);
220 if (NACL_SRPC_RESULT_OK !=
221 NaClSrpcInvokeBySignature(&ns_channel, NACL_NAME_SERVICE_LOOKUP,
222 "manifest_proxy", O_RDWR, &status, &manifest)) {
223 fprintf(stderr, "nameservice lookup failed, status %d\n", status);
224 }
225 StringBufferPrintf(&sb, "Got manifest descriptor %d\n", manifest);
226 if (-1 != manifest) {
227 /* connect to manifest name server */
228 int manifest_conn;
229 struct NaClSrpcChannel manifest_channel;
230
231 manifest_conn = imc_connect(manifest);
232 StringBufferPrintf(&sb, "got manifest connection %d\n", manifest_conn);
233 if (-1 == manifest_conn) {
234 StringBufferPrintf(&sb, "could not connect\n");
235 goto done;
236 }
237 close(manifest);
238 if (!NaClSrpcClientCtor(&manifest_channel, manifest_conn)) {
239 StringBufferPrintf(&sb, "could not build srpc client\n");
240 goto done;
241 }
242 if (NACL_SRPC_RESULT_OK !=
243 NaClSrpcInvokeBySignature(&manifest_channel, NACL_NAME_SERVICE_LIST,
244 &nbytes, buffer)) {
245 StringBufferPrintf(&sb, "manifest list failed\n");
246 goto done;
247 }
248 StringBufferDiscardOutput(&sb);
249 StringBufferPrintf(&sb, "ManifestList: %.*s\n", (int) nbytes, buffer);
250 }
251 done:
252 out_args[0]->arrays.str = strdup(sb.buffer);
253 rpc->result = NACL_SRPC_RESULT_OK;
254 done->Run(done);
255 StringBufferDtor(&sb);
256 }
257
258 const struct NaClSrpcHandlerDesc srpc_methods[] = {
259 /* Export the methods as taking no arguments and returning a string. */
260 { "namedump::s", NameServiceDump },
261 { "rngdump::s", RngDump },
262 { "manifest_test::s", ManifestTest },
263 { NULL, NULL },
264 };
265
266 int main() {
267 int ns;
268 int connected_socket;
269
270 if (!NaClSrpcModuleInit()) {
271 return 1;
272 }
273 ns = -1;
274 nacl_nameservice(&ns);
275 printf("ns = %d\n", ns);
276 assert(-1 != ns);
277
278 connected_socket = imc_connect(ns);
279 assert(-1 != connected_socket);
280 if (!NaClSrpcClientCtor(&ns_channel, connected_socket)) {
281 fprintf(stderr, "Srpc client channel ctor failed\n");
282 return 1;
283 }
284 printf("NaClSrpcClientCtor succeeded\n");
285 close(ns);
286
287 if (!NaClSrpcAcceptClientConnection(srpc_methods)) {
288 return 1;
289 }
290 NaClSrpcModuleFini();
291 return 0;
292 }
OLDNEW
« no previous file with comments | « tests/nameservice/srpc_nameservice.nmf ('k') | tests/nameservice/srpc_nameservice_test.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698