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

Side by Side Diff: ppapi/tests/test_broker.cc

Issue 8400024: Add TestConnectFailure, TestGetHandleFailure and TestConnectAndPipe to PPAPI Broker UI test. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Disable TestConnectAndPipe on Windows due to a bug on Win. Created 9 years, 1 month 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
« no previous file with comments | « ppapi/tests/test_broker.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/tests/test_broker.h" 5 #include "ppapi/tests/test_broker.h"
6 6
7 #include <cstdio>
8 #include <cstring>
9 #include <fstream>
10 #include <limits>
11
12 #include "base/memory/scoped_ptr.h"
7 #include "ppapi/c/pp_errors.h" 13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/c/trusted/ppp_broker.h"
8 #include "ppapi/c/trusted/ppb_broker_trusted.h" 15 #include "ppapi/c/trusted/ppb_broker_trusted.h"
9 #include "ppapi/cpp/module.h" 16 #include "ppapi/tests/test_utils.h"
10 #include "ppapi/tests/testing_instance.h" 17 #include "ppapi/tests/testing_instance.h"
11 18
19 #if defined(OS_WIN)
20 #include "Windows.h"
21 #elif !defined(OS_POSIX)
22 #error Not implemented. Don't remove as it's only checked here in this file.
23 #endif
24
12 REGISTER_TEST_CASE(Broker); 25 REGISTER_TEST_CASE(Broker);
13 26
27 namespace {
28
29 const char kHelloMessage[] = "Hello Plugin! This is Broker!";
30 const char kBrokerUnsandboxed[] = "Broker is Unsandboxed!";
31
32 #if defined(OS_WIN)
33 typedef HANDLE PlatformFile;
34 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
35 #elif defined(OS_POSIX)
36 typedef int PlatformFile;
37 const PlatformFile kInvalidPlatformFileValue = -1;
38 #endif
39
40 int32_t PlatformFileToInt(PlatformFile handle) {
41 #if defined(OS_WIN)
42 return static_cast<int32_t>(reinterpret_cast<intptr_t>(handle));
43 #elif defined(OS_POSIX)
44 return handle;
45 #endif
46 }
47
48 PlatformFile IntToPlatformFile(int32_t handle) {
49 #if defined(OS_WIN)
50 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle));
51 #elif defined(OS_POSIX)
52 return handle;
53 #endif
54 }
55
56 const int32_t kInvalidHandle = PlatformFileToInt(kInvalidPlatformFileValue);
ddorwin 2011/11/14 18:19:39 This would be a bad idea in non-test code. It's ac
xhwang 2011/11/14 22:24:30 Done.
57
58 bool ReadFromPlatformFile(PlatformFile file, char* buffer,
ddorwin 2011/11/14 18:19:39 parameter order should be input then outputs. Chan
xhwang 2011/11/14 22:24:30 Done.
59 size_t bytes_to_read, size_t& bytes_read) {
ddorwin 2011/11/14 18:19:39 output parameters must be pointers per coding styl
xhwang 2011/11/14 22:24:30 Done.
60 #if defined(OS_WIN)
61 assert(bytes_to_read < std::numeric_limits<DWORD>::max());
62 DWORD read = 0;
63 if (!::ReadFile(file, buffer, static_cast<DWORD>(bytes_to_read), &read, NULL))
64 return false;
65 bytes_read = static_cast<size_t>(read);
66 #elif defined(OS_POSIX)
67 assert(bytes_to_read <
68 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
69 ssize_t ret = ::read(file, buffer, bytes_to_read);
70 if (ret == -1)
71 return false;
72 bytes_read = static_cast<size_t>(ret);
73 #endif
74 return true;
ddorwin 2011/11/14 18:19:39 maybe a newline for readability and to separate fr
xhwang 2011/11/14 22:24:30 Done.
75 }
76
77 bool WriteToPlatformFile(PlatformFile file, const char* buffer,
78 size_t bytes_to_write, size_t& bytes_written) {
79 #if defined(OS_WIN)
80 assert(bytes_to_write < std::numeric_limits<DWORD>::max());
81 DWORD written = 0;
82 if (!::WriteFile(file, buffer, static_cast<DWORD>(bytes_to_write), &written,
83 NULL))
84 return false;
85 bytes_written = static_cast<size_t>(written);
86 #elif defined(OS_POSIX)
87 assert(bytes_to_write <
88 static_cast<size_t>(std::numeric_limits<ssize_t>::max()));
89 ssize_t ret = ::write(file, buffer, bytes_to_write);
90 if (ret == -1)
91 return false;
ddorwin 2011/11/14 18:19:39 indent
xhwang 2011/11/14 22:24:30 Done.
92 bytes_written = static_cast<size_t>(ret);
93 #endif
94 return true;
95 }
96
97 bool ClosePlatformFile(PlatformFile file) {
98 #if defined(OS_WIN)
99 return !!::CloseHandle(file);
100 #elif defined(OS_POSIX)
101 return !::close(file);
102 #endif
103 }
104
105 bool WriteMessage(PlatformFile file, const char* message, size_t message_len) {
106 size_t bytes_written = 0;
107 if (!WriteToPlatformFile(file, message, message_len, bytes_written) ||
ddorwin 2011/11/14 18:19:39 This might be easier to read if written positively
xhwang 2011/11/14 22:24:30 I like this :-)
xhwang 2011/11/14 22:24:30 Done.
108 bytes_written != message_len)
109 return false;
110
111 return true;
112 }
113
114 bool ReadMessage(PlatformFile file, char* message, size_t message_len) {
ddorwin 2011/11/14 18:19:39 parameter ordering.
xhwang 2011/11/14 22:24:30 Done.
115 size_t bytes_read = 0;
116 if (!ReadFromPlatformFile(file, message, message_len, bytes_read) ||
117 bytes_read != message_len)
118 return false;
119
120 return true;
121 }
122
123 bool VerifyMessage(PlatformFile file, const char* message_sent,
124 size_t length_sent) {
ddorwin 2011/11/14 18:19:39 length_sent doesn't make sense in the context of t
xhwang 2011/11/14 22:24:30 Done.
125 scoped_array<char> message_received(new char[length_sent]);
126
127 if (!ReadMessage(file, message_received.get(), length_sent))
128 return false;
129
130 return !::strcmp(message_received.get(), message_sent);
131 }
132
133 bool VerifyIsUnsandboxed() {
134 FILE* file = NULL;
135
136 #if defined(OS_WIN)
137 wchar_t temp_path[MAX_PATH] = {'\0'};
138 wchar_t file_name[MAX_PATH] = {'\0'};
139 if (!::GetTempPath(MAX_PATH, temp_path) ||
140 !::GetTempFileName(temp_path, L"test_pepper_broker", 0, file_name) ||
141 ::_wfopen_s(&file, file_name, L"w"))
142 return false;
143 #elif defined(OS_POSIX)
144 char file_name[] = "/tmp/test_pepper_broker_XXXXXX";
145 int fd = ::mkstemp(file_name);
146 if (-1 == fd)
147 return false;
148
149 file = ::fdopen(fd, "w");
150 if (!file) {
151 ::unlink(file_name);
ddorwin 2011/11/14 18:19:39 why unlink instead of remove? Maybe comment.
xhwang 2011/11/14 22:24:30 If path does not name a directory, remove(path) sh
xhwang 2011/11/14 22:24:30 Done.
152 ::close(fd);
153 return false;
154 }
155 #endif
156
157 const char text[] = "Verify that the broker is unsandboxed.";
158 const size_t text_len = sizeof(text);
159 if (text_len != ::fwrite(text, 1, text_len, file)) {
160 ::fclose(file);
ddorwin 2011/11/14 18:19:39 does it need to be deleted in case part of it was
xhwang 2011/11/14 22:24:30 Done.
161 return false;
162 }
163
164 if (::fclose(file))
165 return false;
166
ddorwin 2011/11/14 18:19:39 Clear file just to be safe.
xhwang 2011/11/14 22:24:30 Add comment at the top of the function to warn abo
ddorwin 2011/11/14 22:34:50 Sorry, I meant: file = NULL; since we're going to
xhwang 2011/11/14 22:50:19 Done.
167 #if defined(OS_WIN)
168 if (::_wfopen_s(&file, file_name, L"r"))
169 return false;
170 #elif defined(OS_POSIX)
171 file = ::fopen(file_name, "r");
172 if (!file)
173 return false;
174 #endif
175
176 scoped_array<char> buffer(new char[text_len]);
177 if (text_len != ::fread(buffer.get(), 1, text_len, file)) {
178 ::fclose(file);
179 return false;
180 }
181
182 if (::fclose(file))
183 return false;
184
185 #if defined(OS_WIN)
186 if (!::DeleteFile(file_name))
187 return false;
188 #elif defined(OS_POSIX)
189 if (::remove(file_name))
190 return false;
191 #endif
192
193 return true;
194 }
195
196 // Callback in the broker when a new broker connection occurs.
197 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) {
198 PlatformFile file = IntToPlatformFile(handle);
199 if (file == kInvalidPlatformFileValue)
200 return PP_ERROR_FAILED;
201
202 // Send hello message.
203 if (!WriteMessage(file, kHelloMessage, sizeof(kHelloMessage))) {
204 ClosePlatformFile(file);
205 return PP_ERROR_FAILED;
206 }
207
208 // Verify broker is not sandboxed and send result to plugin over the pipe.
209 if (VerifyIsUnsandboxed()) {
210 if (!WriteMessage(file, kBrokerUnsandboxed, sizeof(kBrokerUnsandboxed))) {
211 ClosePlatformFile(file);
212 return PP_ERROR_FAILED;
213 }
214 }
ddorwin 2011/11/14 18:19:39 comment that the plugin will handle the unsandboxe
xhwang 2011/11/14 22:24:30 Done.
215
216 if (!ClosePlatformFile(file))
217 return PP_ERROR_FAILED;
218
219 return PP_OK;
220 }
221
222 } // namespace
223
224 PP_EXPORT int32_t PPP_InitializeBroker(
225 PP_ConnectInstance_Func* connect_instance_func) {
226 *connect_instance_func = &OnInstanceConnected;
227 return PP_OK;
228 }
229
230 PP_EXPORT void PPP_ShutdownBroker() {}
231
14 TestBroker::TestBroker(TestingInstance* instance) 232 TestBroker::TestBroker(TestingInstance* instance)
15 : TestCase(instance), 233 : TestCase(instance),
16 broker_interface_(NULL) { 234 broker_interface_(NULL) {
17 } 235 }
18 236
19 bool TestBroker::Init() { 237 bool TestBroker::Init() {
20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( 238 broker_interface_ = static_cast<PPB_BrokerTrusted const*>(
21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); 239 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE));
22 return !!broker_interface_; 240 return !!broker_interface_;
23 } 241 }
24 242
25 void TestBroker::RunTest() { 243 void TestBroker::RunTest() {
26 RUN_TEST(Create); 244 RUN_TEST(Create);
245 RUN_TEST(GetHandleFailure);
246 RUN_TEST(ConnectFailure);
247 #if !defined(OS_WIN) // This is broken on Windows. See http://crbug.com/103975.
248 RUN_TEST(ConnectAndPipe);
249 #endif
27 } 250 }
28 251
29 std::string TestBroker::TestCreate() { 252 std::string TestBroker::TestCreate() {
30 // Very simplistic test to make sure we can create a broker interface. 253 // Very simplistic test to make sure we can create a broker interface.
31 PP_Resource broker = broker_interface_->CreateTrusted( 254 PP_Resource broker = broker_interface_->CreateTrusted(
32 instance_->pp_instance()); 255 instance_->pp_instance());
33 ASSERT_TRUE(broker); 256 ASSERT_TRUE(broker);
34 257
35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); 258 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0));
36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); 259 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker));
37 260
38 // Test getting the handle for an invalid resource. 261 PASS();
39 int32_t handle; 262 }
40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE);
41 263
42 // Connect hasn't been called so this should fail. 264 // Test connection on invalid resource.
43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); 265 std::string TestBroker::TestConnectFailure() {
266 // Callback NOT force asynced. Connect should fail. The callback will not be
267 // posted so there's no need to wait for the callback to complete.
268 TestCompletionCallback cb_1(instance_->pp_instance(), false);
269 ASSERT_EQ(PP_ERROR_BADRESOURCE,
270 broker_interface_->Connect(
271 0, pp::CompletionCallback(cb_1).pp_completion_callback()));
272
273 // Callback force aynced. Connect will return PP_OK_COMPLETIONPENDING and the
274 // callback will be posted. However, the callback should fail.
275 TestCompletionCallback cb_2(instance_->pp_instance(), true);
276 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
277 broker_interface_->Connect(
278 0, pp::CompletionCallback(cb_2).pp_completion_callback()));
279 ASSERT_EQ(PP_ERROR_BADRESOURCE, cb_2.WaitForResult());
44 280
45 PASS(); 281 PASS();
46 } 282 }
283
284 std::string TestBroker::TestGetHandleFailure() {
285 int32_t handle = kInvalidHandle;
286
287 // Test getting the handle for an invalid resource.
288 ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->GetHandle(0, &handle));
289
290 // Connect hasn't been called so this should fail.
291 PP_Resource broker = broker_interface_->CreateTrusted(
292 instance_->pp_instance());
293 ASSERT_TRUE(broker);
294 ASSERT_EQ(PP_ERROR_FAILED, broker_interface_->GetHandle(broker, &handle));
295
296 PASS();
297 }
298
299 std::string TestBroker::TestConnectAndPipe() {
300 PP_Resource broker = broker_interface_->CreateTrusted(
301 instance_->pp_instance());
302 ASSERT_TRUE(broker);
303
304 TestCompletionCallback cb_3(instance_->pp_instance());
305 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
306 broker_interface_->Connect(
307 broker, pp::CompletionCallback(cb_3).pp_completion_callback()));
308 ASSERT_EQ(PP_OK, cb_3.WaitForResult());
309
310 int32_t handle = kInvalidHandle;
311 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle));
312 ASSERT_NE(kInvalidHandle, handle);
313
314 PlatformFile file = IntToPlatformFile(handle);
315 ASSERT_TRUE(VerifyMessage(file, kHelloMessage, sizeof(kHelloMessage)));
316 ASSERT_TRUE(VerifyMessage(file, kBrokerUnsandboxed,
ddorwin 2011/11/14 18:19:39 Would this hang if the broker was sandboxed and ne
xhwang 2011/11/14 22:24:30 Both on POSIX and Win the socket pair/named pipes
317 sizeof(kBrokerUnsandboxed)));
318
319 ASSERT_TRUE(ClosePlatformFile(file));
320
321 PASS();
322 }
OLDNEW
« no previous file with comments | « ppapi/tests/test_broker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698