OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 // | 7 // |
8 // Test for resource open before PPAPI initialization. | 8 // Test for resource open before PPAPI initialization. |
9 // | 9 // |
10 | 10 |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 #include <string.h> | 12 #include <string.h> |
13 | 13 |
| 14 #include <sstream> |
14 #include <string> | 15 #include <string> |
15 #include <sstream> | 16 #include <vector> |
16 | 17 |
17 #include "native_client/src/untrusted/irt/irt.h" | 18 #include "native_client/src/untrusted/irt/irt.h" |
18 #include "native_client/src/untrusted/nacl/nacl_irt.h" | 19 #include "native_client/src/untrusted/nacl/nacl_irt.h" |
19 | 20 |
20 #include "ppapi/cpp/instance.h" | 21 #include "ppapi/cpp/instance.h" |
21 #include "ppapi/cpp/module.h" | 22 #include "ppapi/cpp/module.h" |
22 #include "ppapi/cpp/var.h" | 23 #include "ppapi/cpp/var.h" |
| 24 #include "ppapi/cpp/var_array.h" |
23 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" | 25 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" |
24 | 26 |
25 std::string str; | |
26 | 27 |
27 void load_manifest(TYPE_nacl_irt_query *query_func) { | 28 std::vector<std::string> result; |
| 29 |
| 30 std::string LoadManifestSuccess(TYPE_nacl_irt_query *query_func) { |
28 struct nacl_irt_resource_open nacl_irt_resource_open; | 31 struct nacl_irt_resource_open nacl_irt_resource_open; |
29 if (sizeof(nacl_irt_resource_open) != | 32 if (sizeof(nacl_irt_resource_open) != |
30 (*query_func)( | 33 (*query_func)( |
31 NACL_IRT_RESOURCE_OPEN_v0_1, | 34 NACL_IRT_RESOURCE_OPEN_v0_1, |
32 &nacl_irt_resource_open, | 35 &nacl_irt_resource_open, |
33 sizeof(nacl_irt_resource_open))) { | 36 sizeof(nacl_irt_resource_open))) { |
34 str = "irt manifest api not found"; | 37 return "irt manifest api not found"; |
35 return; | |
36 } | 38 } |
37 int desc; | 39 int desc; |
38 int error; | 40 int error; |
39 error = nacl_irt_resource_open.open_resource("test_file", &desc); | 41 error = nacl_irt_resource_open.open_resource("test_file", &desc); |
40 if (0 != error) { | 42 if (0 != error) { |
41 str = "Can't open file"; | |
42 printf("Can't open file, error=%d", error); | 43 printf("Can't open file, error=%d", error); |
43 return; | 44 return "Can't open file"; |
44 } | 45 } |
45 | 46 |
46 str = "File Contents:\n"; | 47 std::string str; |
47 | 48 |
48 char buffer[4096]; | 49 char buffer[4096]; |
49 int len; | 50 int len; |
50 while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) { | 51 while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) { |
51 // NB: fgets does not discard the newline nor any carriage return | 52 // NB: fgets does not discard the newline nor any carriage return |
52 // character before that. | 53 // character before that. |
53 // | 54 // |
54 // Note that CR LF is the default end-of-line style for Windows. | 55 // Note that CR LF is the default end-of-line style for Windows. |
55 // Furthermore, when the test_file (input data, which happens to | 56 // Furthermore, when the test_file (input data, which happens to |
56 // be the nmf file) is initially created in a change list, the | 57 // be the nmf file) is initially created in a change list, the |
(...skipping 16 matching lines...) Expand all Loading... |
73 // and just strip the CR if it is present. | 74 // and just strip the CR if it is present. |
74 int len = strlen(buffer); | 75 int len = strlen(buffer); |
75 if (len >= 2 && buffer[len-1] == '\n' && buffer[len-2] == '\r') { | 76 if (len >= 2 && buffer[len-1] == '\n' && buffer[len-2] == '\r') { |
76 buffer[len-2] = '\n'; | 77 buffer[len-2] = '\n'; |
77 buffer[len-1] = '\0'; | 78 buffer[len-1] = '\0'; |
78 } | 79 } |
79 // Null terminate. | 80 // Null terminate. |
80 buffer[len] = 0; | 81 buffer[len] = 0; |
81 str += buffer; | 82 str += buffer; |
82 } | 83 } |
83 printf("file loaded: %s\n", str.c_str()); | 84 |
84 return; | 85 if (str != "Test File Content") { |
| 86 printf("Wrong file content: \"%s\"\n", str.c_str()); |
| 87 return "Wrong file content: " + str; |
| 88 } |
| 89 |
| 90 return "Pass"; |
| 91 } |
| 92 |
| 93 std::string LoadManifestNonExistentEntry( |
| 94 TYPE_nacl_irt_query *query_func) { |
| 95 struct nacl_irt_resource_open nacl_irt_resource_open; |
| 96 if (sizeof(nacl_irt_resource_open) != |
| 97 (*query_func)( |
| 98 NACL_IRT_RESOURCE_OPEN_v0_1, |
| 99 &nacl_irt_resource_open, |
| 100 sizeof(nacl_irt_resource_open))) { |
| 101 return "irt manifest api not found"; |
| 102 } |
| 103 |
| 104 int desc; |
| 105 int error = nacl_irt_resource_open.open_resource("non_existent_entry", &desc); |
| 106 |
| 107 // We expect ENOENT here, as it does not exist. |
| 108 if (error != ENOENT) { |
| 109 printf("Unexpected error code: %d\n", error); |
| 110 char buf[80]; |
| 111 snprintf(buf, sizeof(buf), "open_resource() result: %d", error); |
| 112 return std::string(buf); |
| 113 } |
| 114 |
| 115 return "Pass"; |
| 116 } |
| 117 |
| 118 std::string LoadManifestNonExistentFile( |
| 119 TYPE_nacl_irt_query *query_func) { |
| 120 struct nacl_irt_resource_open nacl_irt_resource_open; |
| 121 if (sizeof(nacl_irt_resource_open) != |
| 122 (*query_func)( |
| 123 NACL_IRT_RESOURCE_OPEN_v0_1, |
| 124 &nacl_irt_resource_open, |
| 125 sizeof(nacl_irt_resource_open))) { |
| 126 return "irt manifest api not found"; |
| 127 } |
| 128 |
| 129 int desc; |
| 130 int error = nacl_irt_resource_open.open_resource("dummy_test_file", &desc); |
| 131 |
| 132 // We expect ENOENT here, as it does not exist. |
| 133 if (error != ENOENT) { |
| 134 printf("Unexpected error code: %d\n", error); |
| 135 char buf[80]; |
| 136 snprintf(buf, sizeof(buf), "open_resource() result: %d", error); |
| 137 return std::string(buf); |
| 138 } |
| 139 |
| 140 return "Pass"; |
85 } | 141 } |
86 | 142 |
87 class TestInstance : public pp::Instance { | 143 class TestInstance : public pp::Instance { |
88 public: | 144 public: |
89 explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {} | 145 explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {} |
90 virtual ~TestInstance() {} | 146 virtual ~TestInstance() {} |
91 virtual void HandleMessage(const pp::Var& var_message) { | 147 virtual void HandleMessage(const pp::Var& var_message) { |
92 if (!var_message.is_string()) { | 148 if (!var_message.is_string()) { |
93 return; | 149 return; |
94 } | 150 } |
95 if (var_message.AsString() != "hello") { | 151 if (var_message.AsString() != "hello") { |
96 return; | 152 return; |
97 } | 153 } |
98 pp::Var reply = pp::Var(str); | 154 pp::VarArray reply = pp::VarArray(); |
| 155 for (size_t i = 0; i < result.size(); ++i) { |
| 156 reply.Set(i, pp::Var(result[i])); |
| 157 } |
99 PostMessage(reply); | 158 PostMessage(reply); |
100 } | 159 } |
101 }; | 160 }; |
102 | 161 |
103 class TestModule : public pp::Module { | 162 class TestModule : public pp::Module { |
104 public: | 163 public: |
105 TestModule() : pp::Module() {} | 164 TestModule() : pp::Module() {} |
106 virtual ~TestModule() {} | 165 virtual ~TestModule() {} |
107 | 166 |
108 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 167 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
109 return new TestInstance(instance); | 168 return new TestInstance(instance); |
110 } | 169 } |
111 }; | 170 }; |
112 | 171 |
113 namespace pp { | 172 namespace pp { |
114 Module* CreateModule() { | 173 Module* CreateModule() { |
115 return new TestModule(); | 174 return new TestModule(); |
116 } | 175 } |
117 } | 176 } |
118 | 177 |
119 int main() { | 178 int main() { |
120 load_manifest(&__nacl_irt_query); | 179 result.push_back(LoadManifestSuccess(&__nacl_irt_query)); |
| 180 result.push_back(LoadManifestNonExistentEntry(&__nacl_irt_query)); |
| 181 result.push_back(LoadManifestNonExistentFile(&__nacl_irt_query)); |
121 return PpapiPluginMain(); | 182 return PpapiPluginMain(); |
122 } | 183 } |
123 | |
OLD | NEW |