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

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: Added SyncSocket for platform compatibility. Also made changes based on comments. 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 bool VerifyIsUnsandboxed() {
49 std::fstream fs;
50
51 // Temporary directory will be cleaned when this object goes out of scope.
52 ScopedTempDir temp_dir_;
53 if (!temp_dir_.CreateUniqueTempDir())
54 return false;
55
56 FilePath test_file_name =
57 temp_dir_.path().Append(FILE_PATH_LITERAL("test_pepper_broker.txt"));
58
59 fs.open(test_file_name.value().c_str(), std::fstream::out);
60 if (!fs)
61 return false;
62 fs << "Verify that the broker is unsandboxed.";
63 if (!fs)
64 return false;
65 fs.close();
66
67 fs.open(test_file_name.value().c_str(), std::fstream::in);
68 if (!fs)
69 return false;
70 char buffer[16] = {0};
71 fs.read(buffer, 8);
72 if (!fs)
73 return false;
74 fs.close();
75
76 return true;
77 }
78
79 bool VerifyMessage(base::SyncSocket& sync_socket, const char* message_sent,
80 size_t length) {
81 scoped_array<char> message_received(new char[length]);
82 sync_socket.Receive(message_received.get(), length);
83 return !::strcmp(message_received.get(), message_sent);
84 }
85
86 // Callback in the broker when a new broker connection occurs.
87 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) {
88 base::PlatformFile platform_file = IntToPlatformFile(handle);
89 if (platform_file == base::kInvalidPlatformFileValue)
90 return PP_ERROR_FAILED;
91
92 base::SyncSocket sync_socket(platform_file);
93
94 // Send hello message.
95 size_t ret = sync_socket.Send(kHelloMessage, sizeof(kHelloMessage));
96 if (ret != sizeof(kHelloMessage))
97 return PP_ERROR_FAILED;
98
99 // Verify broker is not sandboxed and send result to plugin over the pipe.
100 if (VerifyIsUnsandboxed()) {
101 ret = sync_socket.Send(kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed));
102 if (ret != sizeof(kBrokerIsUnsandboxed))
103 return PP_ERROR_FAILED;
104 }
105
106 return PP_OK;
107 }
108
109 } // namespace
110
111 PP_EXPORT int32_t PPP_InitializeBroker(
112 PP_ConnectInstance_Func* connect_instance_func) {
113 *connect_instance_func = &OnInstanceConnected;
114 return PP_OK;
115 }
116
117 PP_EXPORT void PPP_ShutdownBroker() {}
118
14 TestBroker::TestBroker(TestingInstance* instance) 119 TestBroker::TestBroker(TestingInstance* instance)
15 : TestCase(instance), 120 : TestCase(instance),
16 broker_interface_(NULL) { 121 broker_interface_(NULL) {
17 } 122 }
18 123
19 bool TestBroker::Init() { 124 bool TestBroker::Init() {
20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( 125 broker_interface_ = static_cast<PPB_BrokerTrusted const*>(
21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); 126 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE));
22 return !!broker_interface_; 127 return !!broker_interface_;
23 } 128 }
24 129
25 void TestBroker::RunTest() { 130 void TestBroker::RunTest() {
26 RUN_TEST(Create); 131 RUN_TEST(Create);
132 RUN_TEST(ConnectAndGetHandle);
27 } 133 }
28 134
29 std::string TestBroker::TestCreate() { 135 std::string TestBroker::TestCreate() {
30 // Very simplistic test to make sure we can create a broker interface. 136 // Very simplistic test to make sure we can create a broker interface.
31 PP_Resource broker = broker_interface_->CreateTrusted( 137 PP_Resource broker = broker_interface_->CreateTrusted(
32 instance_->pp_instance()); 138 instance_->pp_instance());
33 ASSERT_TRUE(broker); 139 ASSERT_TRUE(broker);
34 140
35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); 141 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0));
36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); 142 ASSERT_TRUE (broker_interface_->IsBrokerTrusted(broker));
ddorwin 2011/10/28 17:11:29 Remove space.
xhwang 2011/10/28 18:18:28 Done.
37
38 // Test getting the handle for an invalid resource.
39 int32_t handle;
40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE);
41
42 // Connect hasn't been called so this should fail.
43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED);
44 143
45 PASS(); 144 PASS();
46 } 145 }
146
147 std::string TestBroker::TestConnectAndGetHandle() {
148 PP_Resource broker = broker_interface_->CreateTrusted(
149 instance_->pp_instance());
150 ASSERT_TRUE(broker);
151
152 const int32_t kInvalidHandle =
153 PlatformFileToInt(base::kInvalidPlatformFileValue);
154 int32_t handle = kInvalidHandle;
155
156 // Test getting the handle for an invalid resource.
157 ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->GetHandle(0, &handle));
158 // Connect hasn't been called so this should fail.
159 ASSERT_EQ(PP_ERROR_FAILED, broker_interface_->GetHandle(broker, &handle));
160
161 // Connect should fail on invalid resource and since not force asynced, the
162 // callback will not be posted.
163 TestCompletionCallback cb_1(instance_->pp_instance());
ddorwin 2011/10/28 17:11:29 I'd be explicit about the parameter since you refe
xhwang 2011/10/28 18:18:28 Done.
164 ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->Connect(
ddorwin 2011/10/28 17:11:29 This is a bit hard to read. Maybe this: ASSERT_EQ(
xhwang 2011/10/28 18:18:28 Done.
165 0, pp::CompletionCallback(cb_1).pp_completion_callback()));
166
167 // Since force asynced, Connect will return PP_OK_COMPLETIONPENDING and the
168 // callback will be posted. However, the callback should fail.
169 const bool kForceAsync = true;
170 TestCompletionCallback cb_2(instance_->pp_instance(), kForceAsync);
171 ASSERT_EQ(PP_OK_COMPLETIONPENDING, broker_interface_->Connect(
172 0, pp::CompletionCallback(cb_2).pp_completion_callback()));
173 ASSERT_EQ(PP_ERROR_BADRESOURCE, cb_2.WaitForResult());
174
175 // Test normal case.
ddorwin 2011/10/28 17:11:29 It's usually best to do one thing in a test, and i
xhwang 2011/10/28 18:18:28 Done.
176 TestCompletionCallback cb_3(instance_->pp_instance());
177 ASSERT_EQ(PP_OK_COMPLETIONPENDING, broker_interface_->Connect(
178 broker, pp::CompletionCallback(cb_3).pp_completion_callback()));
179 ASSERT_EQ(PP_OK, cb_3.WaitForResult());
180
181 handle = kInvalidHandle;
182 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle));
183 base::PlatformFile platform_file = IntToPlatformFile(handle);
184 ASSERT_NE(platform_file, base::kInvalidPlatformFileValue);
185
186 base::SyncSocket sync_socket(platform_file);
187
188 ASSERT_TRUE(VerifyMessage(sync_socket, kHelloMessage, sizeof(kHelloMessage)));
189 ASSERT_TRUE(VerifyMessage(
190 sync_socket, kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed)));
ddorwin 2011/10/28 17:11:29 Maybe: ASSERT_TRUE(VerifyMessage(sync_socket, kBro
xhwang 2011/10/28 18:18:28 Done.
191
192 PASS();
193 }
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