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

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: 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"
7 #include "ppapi/c/pp_errors.h" 11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/c/trusted/ppp_broker.h"
8 #include "ppapi/c/trusted/ppb_broker_trusted.h" 13 #include "ppapi/c/trusted/ppb_broker_trusted.h"
9 #include "ppapi/cpp/module.h" 14 #include "ppapi/tests/test_utils.h"
10 #include "ppapi/tests/testing_instance.h" 15 #include "ppapi/tests/testing_instance.h"
11 16
12 REGISTER_TEST_CASE(Broker); 17 REGISTER_TEST_CASE(Broker);
13 18
19 namespace {
20
21 const size_t kTestMessageLength = 30;
ddorwin 2011/10/27 18:19:46 use arraysize() instead?
xhwang 2011/10/27 22:31:57 Done.
22 const char kTestMessage[] = "Hello Plugin! This is Broker!";
23 const int32_t kTestSandboxSuccess = 0;
ddorwin 2011/10/27 18:19:46 kBrokerIsUnsandboxed I would write a string since
xhwang 2011/10/27 22:31:57 Done.
24 const int32_t kTestSandboxFailure = -1;
25
26 // Test whether the process is sandboxed or not as expected.
27 bool TestSandbox(bool is_sandbox_expected) {
ddorwin 2011/10/27 18:19:46 VerifyIsUnsandboxed Since we're only testing it i
xhwang 2011/10/27 22:31:57 Done.
xhwang 2011/10/27 22:31:57 Done.
28 bool success = true;
29 std::fstream fs;
30
31 // FIXME: Add sandbox test for other platforms.
ddorwin 2011/10/27 18:19:46 Move to an #else below and change to "Windows". Ac
xhwang 2011/10/27 22:31:57 Done.
32 #if defined(OS_POSIX)
33 const char* const kWriteFile = "/tmp/broker_sandbox_test.txt";
ddorwin 2011/10/27 18:19:46 /tmp/ppapi_... Paths should be the only differenc
xhwang 2011/10/27 22:31:57 Done.
34 fs.open(kWriteFile, std::fstream::out);
35 success &= (fs.fail() == is_sandbox_expected);
36 fs << "ochoococohoho";
37 success &= (fs.fail() == is_sandbox_expected);
38 fs.close();
39
40 const char* const kReadFile = "/var/log/messages";
ddorwin 2011/10/27 18:19:46 messages does not exist on my Mac. kernel.log migh
xhwang 2011/10/27 22:31:57 Done.
41 fs.open(kReadFile, std::fstream::in);
42 success &= (fs.fail() == is_sandbox_expected);
43 char buffer[16] = {0};
44 fs.read(buffer, 8);
45 success &= (fs.fail() == is_sandbox_expected);
46 fs << "this will not work";
ddorwin 2011/10/27 18:19:46 Probably not necessary.
xhwang 2011/10/27 22:31:57 Done.
47 success &= fs.fail();
48 fs.close();
49 #endif
50
51 return success;
52 }
53
54 // Callback in the broker when a new broker connection occurs.
55 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) {
56 // Send test message
57 ::write(handle, kTestMessage, kTestMessageLength);
58
59 // Do sandbox test and send result to plugin over the pipe.
60 const bool kIsChromeSandboxEnabled = false;
61 int32_t test_sandbox_result = TestSandbox(kIsChromeSandboxEnabled) ?
62 kTestSandboxSuccess : kTestSandboxFailure;
63 ::write(handle, &test_sandbox_result, sizeof(test_sandbox_result));
64
65 ::close(handle);
66 return PP_OK;
67 }
68
69 } // namespace
70
71 PP_EXPORT int32_t PPP_InitializeBroker(
72 PP_ConnectInstance_Func* connect_instance_func) {
73 *connect_instance_func = &OnInstanceConnected;
74 return PP_OK;
75 }
76
77 PP_EXPORT void PPP_ShutdownBroker() {}
78
14 TestBroker::TestBroker(TestingInstance* instance) 79 TestBroker::TestBroker(TestingInstance* instance)
15 : TestCase(instance), 80 : TestCase(instance),
16 broker_interface_(NULL) { 81 broker_interface_(NULL) {
17 } 82 }
18 83
19 bool TestBroker::Init() { 84 bool TestBroker::Init() {
20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( 85 broker_interface_ = static_cast<PPB_BrokerTrusted const*>(
21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); 86 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE));
22 return !!broker_interface_; 87 return !!broker_interface_;
23 } 88 }
24 89
25 void TestBroker::RunTest() { 90 void TestBroker::RunTest() {
26 RUN_TEST(Create); 91 RUN_TEST(Create);
92 RUN_TEST(Connect);
27 } 93 }
28 94
29 std::string TestBroker::TestCreate() { 95 std::string TestBroker::TestCreate() {
30 // Very simplistic test to make sure we can create a broker interface. 96 // Very simplistic test to make sure we can create a broker interface.
31 PP_Resource broker = broker_interface_->CreateTrusted( 97 PP_Resource broker = broker_interface_->CreateTrusted(
32 instance_->pp_instance()); 98 instance_->pp_instance());
33 ASSERT_TRUE(broker); 99 ASSERT_TRUE(broker);
34 100
35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); 101 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0));
36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); 102 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker));
37 103
38 // Test getting the handle for an invalid resource. 104 // Test getting the handle for an invalid resource.
39 int32_t handle; 105 int32_t handle;
40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE); 106 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE);
41 107
42 // Connect hasn't been called so this should fail. 108 // Connect hasn't been called so this should fail.
43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); 109 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED);
44 110
ddorwin 2011/10/27 18:19:46 There is more testing of Connect and the handle th
xhwang 2011/10/27 22:31:57 ?? Don't get it...
ddorwin 2011/10/27 23:21:23 As discussed.
45 PASS(); 111 PASS();
46 } 112 }
113
114 std::string TestBroker::TestConnect() {
ddorwin 2011/10/27 18:19:46 ...AndGetHandle
xhwang 2011/10/27 22:31:57 Done.
115 ASSERT_TRUE(broker_interface_);
ddorwin 2011/10/27 18:19:46 I think this is an assumption in all tests, so we
xhwang 2011/10/27 22:31:57 Done.
116 PP_Resource broker = broker_interface_->CreateTrusted(
117 instance_->pp_instance());
118 ASSERT_TRUE(broker);
119
120 TestCompletionCallback cb(instance_->pp_instance());
121
122 ASSERT_EQ(PP_OK_COMPLETIONPENDING, broker_interface_->Connect(
ddorwin 2011/10/27 18:19:46 I'm not sure why the existing test uses ASSERT, bu
xhwang 2011/10/27 22:31:57 EXPECT is no available :-(
123 broker, pp::CompletionCallback(cb).pp_completion_callback()));
124 ASSERT_EQ(PP_OK, cb.WaitForResult());
125
126 int32_t handle = -1;
ddorwin 2011/10/27 18:19:46 Can we use PlatformFileToInt(base::kInvalidPlatfor
xhwang 2011/10/27 22:31:57 Done.
127 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle));
128
129 scoped_array<char> received_msg(new char[kTestMessageLength]);
130 ::read(handle, received_msg.get(), kTestMessageLength);
131 ASSERT_EQ(0, ::strcmp(received_msg.get(), kTestMessage));
ddorwin 2011/10/27 18:19:46 EXPECT_STREQ
132
133 int32_t test_sandbox_result = kTestSandboxFailure;
134 ::read(handle, &test_sandbox_result, sizeof(test_sandbox_result));
135 ASSERT_EQ(kTestSandboxSuccess, test_sandbox_result);
136
137 ASSERT_EQ(0, ::close(handle));
138
139 PASS();
140 }
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