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

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: Modified as commented. 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 <cstring>
8 #include <fstream>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/platform_file.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/sync_socket.h"
7 #include "ppapi/c/pp_errors.h" 14 #include "ppapi/c/pp_errors.h"
15 #include "ppapi/c/trusted/ppp_broker.h"
8 #include "ppapi/c/trusted/ppb_broker_trusted.h" 16 #include "ppapi/c/trusted/ppb_broker_trusted.h"
9 #include "ppapi/cpp/module.h" 17 #include "ppapi/tests/test_utils.h"
10 #include "ppapi/tests/testing_instance.h" 18 #include "ppapi/tests/testing_instance.h"
11 19
12 REGISTER_TEST_CASE(Broker); 20 REGISTER_TEST_CASE(Broker);
13 21
22 namespace {
23
24 const char kHelloMessage[] = "Hello Plugin! This is Broker!";
25 const char kBrokerIsUnsandboxed[] = "Broker is Unsandboxed!";
26
27 // TODO(xhwang): merge PlatformFileToInt definitions and move to a common place.
28 int32_t PlatformFileToInt(base::PlatformFile handle) {
29 #if defined(OS_WIN)
30 return static_cast<int32_t>(reinterpret_cast<intptr_t>(handle));
31 #elif defined(OS_POSIX)
32 return handle;
33 #else
34 #error Not implemented.
35 #endif
36 }
37
38 base::PlatformFile IntToPlatformFile(int32_t handle) {
39 #if defined(OS_WIN)
40 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle));
41 #elif defined(OS_POSIX)
42 return handle;
43 #else
44 #error Not implemented.
45 #endif
46 }
47
48 const int32_t kInvalidHandle =
49 PlatformFileToInt(base::kInvalidPlatformFileValue);
50
51 bool VerifyIsUnsandboxed() {
52 std::fstream fs;
53
54 // Temporary directory will be cleaned when this object goes out of scope.
55 ScopedTempDir temp_dir_;
56 if (!temp_dir_.CreateUniqueTempDir())
57 return false;
58
59 FilePath test_file_name =
60 temp_dir_.path().Append(FILE_PATH_LITERAL("test_pepper_broker.txt"));
61
62 fs.open(test_file_name.value().c_str(), std::fstream::out);
63 if (!fs)
64 return false;
65 fs << "Verify that the broker is unsandboxed.";
66 if (!fs)
67 return false;
68 fs.close();
69
70 fs.open(test_file_name.value().c_str(), std::fstream::in);
71 if (!fs)
72 return false;
73 char buffer[16] = {0};
74 fs.read(buffer, 8);
75 if (!fs)
76 return false;
77 fs.close();
78
79 return true;
80 }
81
82 bool VerifyMessage(base::SyncSocket& sync_socket, const char* message_sent,
83 size_t length) {
84 scoped_array<char> message_received(new char[length]);
85 sync_socket.Receive(message_received.get(), length);
ddorwin 2011/10/28 19:05:59 Should we check that the return value is length?
xhwang 2011/10/28 19:39:10 Done.
86 return !::strcmp(message_received.get(), message_sent);
87 }
88
89 // Callback in the broker when a new broker connection occurs.
90 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) {
91 base::PlatformFile platform_file = IntToPlatformFile(handle);
92 if (platform_file == base::kInvalidPlatformFileValue)
93 return PP_ERROR_FAILED;
94
95 base::SyncSocket sync_socket(platform_file);
96
97 // Send hello message.
98 size_t ret = sync_socket.Send(kHelloMessage, sizeof(kHelloMessage));
99 if (ret != sizeof(kHelloMessage))
100 return PP_ERROR_FAILED;
101
102 // Verify broker is not sandboxed and send result to plugin over the pipe.
103 if (VerifyIsUnsandboxed()) {
104 ret = sync_socket.Send(kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed));
105 if (ret != sizeof(kBrokerIsUnsandboxed))
106 return PP_ERROR_FAILED;
107 }
108
109 return PP_OK;
110 }
111
112 } // namespace
113
114 PP_EXPORT int32_t PPP_InitializeBroker(
115 PP_ConnectInstance_Func* connect_instance_func) {
116 *connect_instance_func = &OnInstanceConnected;
117 return PP_OK;
118 }
119
120 PP_EXPORT void PPP_ShutdownBroker() {}
121
14 TestBroker::TestBroker(TestingInstance* instance) 122 TestBroker::TestBroker(TestingInstance* instance)
15 : TestCase(instance), 123 : TestCase(instance),
16 broker_interface_(NULL) { 124 broker_interface_(NULL) {
17 } 125 }
18 126
19 bool TestBroker::Init() { 127 bool TestBroker::Init() {
20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( 128 broker_interface_ = static_cast<PPB_BrokerTrusted const*>(
21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); 129 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE));
22 return !!broker_interface_; 130 return !!broker_interface_;
23 } 131 }
24 132
25 void TestBroker::RunTest() { 133 void TestBroker::RunTest() {
26 RUN_TEST(Create); 134 RUN_TEST(Create);
135 RUN_TEST(GetHandleFailure);
136 RUN_TEST(ConnectFailure);
137 RUN_TEST(ConnectAndPipe);
27 } 138 }
28 139
29 std::string TestBroker::TestCreate() { 140 std::string TestBroker::TestCreate() {
30 // Very simplistic test to make sure we can create a broker interface. 141 // Very simplistic test to make sure we can create a broker interface.
31 PP_Resource broker = broker_interface_->CreateTrusted( 142 PP_Resource broker = broker_interface_->CreateTrusted(
32 instance_->pp_instance()); 143 instance_->pp_instance());
33 ASSERT_TRUE(broker); 144 ASSERT_TRUE(broker);
34 145
35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); 146 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0));
36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); 147 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker));
37 148
38 // Test getting the handle for an invalid resource. 149 PASS();
39 int32_t handle; 150 }
40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE);
41 151
42 // Connect hasn't been called so this should fail. 152 // Test connection on invalid resource.
43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); 153 std::string TestBroker::TestConnectFailure() {
154 PP_Resource broker = broker_interface_->CreateTrusted(
155 instance_->pp_instance());
156 ASSERT_TRUE(broker);
157
158 // Callback NOT force asynced. Connect should fail. The callback will not be
159 // posted so there's no need to wait for the callback to complete.
160 TestCompletionCallback cb_1(instance_->pp_instance(), false);
161 ASSERT_EQ(PP_ERROR_BADRESOURCE,
162 broker_interface_->Connect(
163 0, pp::CompletionCallback(cb_1).pp_completion_callback()));
164
165 // Callback force aynced. Connect will return PP_OK_COMPLETIONPENDING and the
166 // callback will be posted. However, the callback should fail.
167 TestCompletionCallback cb_2(instance_->pp_instance(), true);
168 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
169 broker_interface_->Connect(
170 0, pp::CompletionCallback(cb_2).pp_completion_callback()));
171 ASSERT_EQ(PP_ERROR_BADRESOURCE, cb_2.WaitForResult());
44 172
45 PASS(); 173 PASS();
46 } 174 }
175
176 std::string TestBroker::TestGetHandleFailure() {
177 PP_Resource broker = broker_interface_->CreateTrusted(
ddorwin 2011/10/28 19:05:59 This _could_ be moved down to 185.
xhwang 2011/10/28 19:39:10 Done.
178 instance_->pp_instance());
179 ASSERT_TRUE(broker);
180
181 int32_t handle = kInvalidHandle;
182 // Test getting the handle for an invalid resource.
183 ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->GetHandle(0, &handle));
184 // Connect hasn't been called so this should fail.
185 ASSERT_EQ(PP_ERROR_FAILED, broker_interface_->GetHandle(broker, &handle));
186
187 PASS();
188 }
189
190 std::string TestBroker::TestConnectAndPipe() {
191 PP_Resource broker = broker_interface_->CreateTrusted(
192 instance_->pp_instance());
193 ASSERT_TRUE(broker);
194
195 TestCompletionCallback cb_3(instance_->pp_instance());
196 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
197 broker_interface_->Connect(
198 broker, pp::CompletionCallback(cb_3).pp_completion_callback()));
199 ASSERT_EQ(PP_OK, cb_3.WaitForResult());
200
201 int32_t handle = kInvalidHandle;
202 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle));
203 ASSERT_NE(handle, kInvalidHandle);
ddorwin 2011/10/28 19:05:59 swap. Expected on the left. (That's the GTest conv
204
205 base::PlatformFile platform_file = IntToPlatformFile(handle);
206 base::SyncSocket sync_socket(platform_file);
207
208 ASSERT_TRUE(VerifyMessage(sync_socket, kHelloMessage, sizeof(kHelloMessage)));
209 ASSERT_TRUE(VerifyMessage(sync_socket, kBrokerIsUnsandboxed,
210 sizeof(kBrokerIsUnsandboxed)));
211
212 PASS();
213 }
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