imcplugin demo: Extend to support stat() call
[nativeclient.git] / npapi_plugin / npnacl-unittest.cc
blob25d484ed0453706e0001bade333a21325489d4e8
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // Unit tests for npnacl_plugin.so
34 #include <dlfcn.h>
35 #include <stdio.h>
36 #include "bindings/npapi.h"
37 #include "bindings/npruntime.h"
38 #include "nphostapi.h"
40 void* dl_handle;
42 void* GetSymbolHandle(const char* name) {
43 void* sym_handle = dlsym(dl_handle, name);
44 char* error_string = dlerror();
45 if (!sym_handle || error_string) {
46 fprintf(stderr, "Couldn't get symbol %s: %s\n", name, error_string);
47 return NULL;
49 return sym_handle;
52 bool TestMIMEDescription() {
53 typedef char* (*FP)();
54 FP func = reinterpret_cast<FP>(GetSymbolHandle("NP_GetMIMEDescription"));
55 if (!func) {
56 return false;
58 char* mime_string = (*func)();
59 if (!mime_string) {
60 fprintf(stderr, "ERROR: NP_GetMIMEDescriptor returned no string\n");
61 return false;
63 return true;
66 bool TestInitialize() {
67 typedef NPError (*FP)(NPNetscapeFuncs*, NPPluginFuncs*);
68 FP func = reinterpret_cast<FP>(GetSymbolHandle("NP_Initialize"));
69 if (!func) {
70 return false;
72 NPNetscapeFuncs browser_funcs;
73 NPPluginFuncs plugin_funcs;
74 browser_funcs.version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
75 browser_funcs.size = sizeof(NPNetscapeFuncs);
76 if (NPERR_NO_ERROR != (*func)(&browser_funcs, &plugin_funcs)) {
77 fprintf(stderr, "ERROR: NP_Initialize returned error\n");
78 return false;
80 return true;
83 bool TestEntryPoints() {
84 typedef NPError (*FP)(NPPluginFuncs*);
85 FP func = reinterpret_cast<FP>(GetSymbolHandle("NP_GetEntryPoints"));
86 if (!func) {
87 return false;
89 NPPluginFuncs plugin_funcs;
90 if (NPERR_NO_ERROR != (*func)(&plugin_funcs)) {
91 fprintf(stderr, "ERROR: NP_GetEntryPoints returned error\n");
92 return false;
94 return true;
97 bool TestShutdown() {
98 typedef NPError (*FP)(void);
99 FP func = reinterpret_cast<FP>(GetSymbolHandle("NP_Shutdown"));
100 if (!func) {
101 return false;
103 if (NPERR_NO_ERROR != (*func)()) {
104 fprintf(stderr, "ERROR: NP_Shutdown returned error\n");
105 return false;
107 return true;
110 int main(int argc, char** argv) {
111 if (argc != 2) {
112 fprintf(stderr, "Usage: %s <soname>\n", argv[0]);
113 return 1;
115 // Test opening the .so
116 dl_handle = dlopen(argv[1], RTLD_NOW);
117 if (!dl_handle) {
118 fprintf(stderr, "Couldn't open: %s\n", dlerror());
119 return 1;
122 bool success =
123 (TestMIMEDescription() &&
124 TestInitialize() &&
125 TestEntryPoints() &&
126 TestShutdown());
128 // Test closing the .so
129 if (dlclose(dl_handle)) {
130 fprintf(stderr, "Couldn't close: %s\n", dlerror());
131 return 1;
134 if (success) {
135 printf("PASS\n");
136 return 0;
137 } else {
138 printf("FAIL\n");
139 return 1;