1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/browser_io_surface_manager_mac.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 class BrowserIOSurfaceManagerTest
: public testing::Test
{
13 void EnsureRunning() { io_surface_manager_
.EnsureRunning(); }
15 IOSurfaceManagerToken
GenerateGpuProcessToken() {
16 return io_surface_manager_
.GenerateGpuProcessToken();
19 void InvalidateGpuProcessToken() {
20 io_surface_manager_
.InvalidateGpuProcessToken();
23 IOSurfaceManagerToken
GenerateChildProcessToken(int child_process_id
) {
24 return io_surface_manager_
.GenerateChildProcessToken(child_process_id
);
27 void InvalidateChildProcessToken(const IOSurfaceManagerToken
& token
) {
28 io_surface_manager_
.InvalidateChildProcessToken(token
);
31 bool HandleRegisterIOSurfaceRequest(
32 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
33 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
) {
34 return io_surface_manager_
.HandleRegisterIOSurfaceRequest(request
, reply
);
37 bool HandleUnregisterIOSurfaceRequest(
38 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
) {
39 return io_surface_manager_
.HandleUnregisterIOSurfaceRequest(request
);
42 bool HandleAcquireIOSurfaceRequest(
43 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
44 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
) {
45 return io_surface_manager_
.HandleAcquireIOSurfaceRequest(request
, reply
);
48 // Helper function used to register an IOSurface for testing.
49 void RegisterIOSurface(const IOSurfaceManagerToken
& gpu_process_token
,
52 mach_port_t io_surface_port
) {
53 IOSurfaceManagerHostMsg_RegisterIOSurface request
= {{0}};
54 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply
= {{0}};
55 request
.io_surface_id
= io_surface_id
;
56 request
.client_id
= client_id
;
57 request
.io_surface_port
.name
= io_surface_port
;
58 memcpy(request
.token_name
, gpu_process_token
.name
,
59 sizeof(gpu_process_token
.name
));
60 ASSERT_TRUE(HandleRegisterIOSurfaceRequest(request
, &reply
));
61 ASSERT_TRUE(reply
.result
);
65 BrowserIOSurfaceManager io_surface_manager_
;
68 TEST_F(BrowserIOSurfaceManagerTest
, EnsureRunning
) {
70 base::mac::ScopedMachSendRight service_port
=
71 BrowserIOSurfaceManager::LookupServicePort(getpid());
72 EXPECT_TRUE(service_port
.is_valid());
75 TEST_F(BrowserIOSurfaceManagerTest
, RegisterIOSurfaceSucceeds
) {
76 const int kIOSurfaceId
= 1;
77 const int kClientId
= 1;
79 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
81 IOSurfaceManagerHostMsg_RegisterIOSurface request
= {{0}};
82 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply
= {{0}};
84 request
.io_surface_id
= kIOSurfaceId
;
85 request
.client_id
= kClientId
;
86 memcpy(request
.token_name
, gpu_process_token
.name
,
87 sizeof(gpu_process_token
.name
));
88 EXPECT_TRUE(HandleRegisterIOSurfaceRequest(request
, &reply
));
89 EXPECT_TRUE(reply
.result
);
92 TEST_F(BrowserIOSurfaceManagerTest
, RegisterIOSurfaceFailsWithInvalidToken
) {
93 const int kIOSurfaceId
= 1;
94 const int kClientId
= 1;
96 IOSurfaceManagerHostMsg_RegisterIOSurface request
= {{0}};
97 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply
= {{0}};
98 request
.io_surface_id
= kIOSurfaceId
;
99 request
.client_id
= kClientId
;
100 // Fails as request doesn't contain a valid token.
101 EXPECT_FALSE(HandleRegisterIOSurfaceRequest(request
, &reply
));
104 TEST_F(BrowserIOSurfaceManagerTest
,
105 RegisterIOSurfaceFailsWithChildProcessToken
) {
106 const int kIOSurfaceId
= 1;
107 const int kClientId
= 1;
109 IOSurfaceManagerToken child_process_token
=
110 GenerateChildProcessToken(kClientId
);
112 IOSurfaceManagerHostMsg_RegisterIOSurface request
= {{0}};
113 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply
= {{0}};
114 request
.io_surface_id
= kIOSurfaceId
;
115 request
.client_id
= kClientId
;
116 memcpy(request
.token_name
, child_process_token
.name
,
117 sizeof(child_process_token
.name
));
118 // Fails as child process is not allowed to register IOSurfaces.
119 EXPECT_FALSE(HandleRegisterIOSurfaceRequest(request
, &reply
));
122 TEST_F(BrowserIOSurfaceManagerTest
, UnregisterIOSurfaceSucceeds
) {
123 const int kIOSurfaceId
= 1;
124 const int kClientId
= 1;
126 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
128 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId
, 0);
130 IOSurfaceManagerHostMsg_UnregisterIOSurface request
= {{0}};
131 request
.io_surface_id
= kIOSurfaceId
;
132 request
.client_id
= kClientId
;
133 memcpy(request
.token_name
, gpu_process_token
.name
,
134 sizeof(gpu_process_token
.name
));
135 EXPECT_TRUE(HandleUnregisterIOSurfaceRequest(request
));
138 TEST_F(BrowserIOSurfaceManagerTest
, UnregisterIOSurfaceFailsWithInvalidToken
) {
139 const int kIOSurfaceId
= 1;
140 const int kClientId
= 1;
142 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
144 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId
, 0);
146 IOSurfaceManagerHostMsg_UnregisterIOSurface request
= {{0}};
147 request
.io_surface_id
= kIOSurfaceId
;
148 request
.client_id
= kClientId
;
149 // Fails as request doesn't contain valid GPU token.
150 EXPECT_FALSE(HandleUnregisterIOSurfaceRequest(request
));
153 TEST_F(BrowserIOSurfaceManagerTest
,
154 UnregisterIOSurfaceFailsWithChildProcessToken
) {
155 const int kIOSurfaceId
= 1;
156 const int kClientId
= 1;
158 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
159 IOSurfaceManagerToken child_process_token
=
160 GenerateChildProcessToken(kClientId
);
162 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId
, 0);
164 IOSurfaceManagerHostMsg_UnregisterIOSurface request
= {{0}};
165 request
.io_surface_id
= kIOSurfaceId
;
166 request
.client_id
= kClientId
;
167 memcpy(request
.token_name
, child_process_token
.name
,
168 sizeof(child_process_token
.name
));
169 // Fails as child process is not allowed to unregister IOSurfaces.
170 EXPECT_FALSE(HandleUnregisterIOSurfaceRequest(request
));
173 TEST_F(BrowserIOSurfaceManagerTest
, AcquireIOSurfaceSucceeds
) {
174 const int kIOSurfaceId
= 10;
175 const int kClientId
= 1;
176 const mach_port_t kIOSurfacePortId
= 100u;
178 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
179 IOSurfaceManagerToken child_process_token
=
180 GenerateChildProcessToken(kClientId
);
182 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId
,
185 IOSurfaceManagerHostMsg_AcquireIOSurface request
= {{0}};
186 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply
= {{0}};
187 request
.io_surface_id
= kIOSurfaceId
;
188 memcpy(request
.token_name
, child_process_token
.name
,
189 sizeof(child_process_token
.name
));
190 EXPECT_TRUE(HandleAcquireIOSurfaceRequest(request
, &reply
));
191 EXPECT_EQ(kIOSurfacePortId
, reply
.io_surface_port
.name
);
194 TEST_F(BrowserIOSurfaceManagerTest
, AcquireIOSurfaceFailsWithInvalidToken
) {
195 const int kIOSurfaceId
= 10;
196 const int kClientId
= 1;
197 const mach_port_t kIOSurfacePortId
= 100u;
199 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
201 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId
,
204 IOSurfaceManagerHostMsg_AcquireIOSurface request
= {{0}};
205 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply
= {{0}};
206 request
.io_surface_id
= kIOSurfaceId
;
207 // Fails as request doesn't contain valid token.
208 EXPECT_FALSE(HandleAcquireIOSurfaceRequest(request
, &reply
));
211 TEST_F(BrowserIOSurfaceManagerTest
,
212 AcquireIOSurfaceFailsWithWrongChildProcessToken
) {
213 const int kIOSurfaceId
= 10;
214 const int kClientId1
= 1;
215 const int kClientId2
= 2;
216 const mach_port_t kIOSurfacePortId
= 100u;
218 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
219 IOSurfaceManagerToken child_process_token2
=
220 GenerateChildProcessToken(kClientId2
);
222 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId1
,
225 IOSurfaceManagerHostMsg_AcquireIOSurface request
= {{0}};
226 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply
= {{0}};
227 request
.io_surface_id
= kIOSurfaceId
;
228 memcpy(request
.token_name
, child_process_token2
.name
,
229 sizeof(child_process_token2
.name
));
230 EXPECT_TRUE(HandleAcquireIOSurfaceRequest(request
, &reply
));
231 // Fails as child process 2 is not allowed to acquire this IOSurface.
232 EXPECT_EQ(static_cast<mach_port_t
>(MACH_PORT_NULL
),
233 reply
.io_surface_port
.name
);
236 TEST_F(BrowserIOSurfaceManagerTest
,
237 AcquireIOSurfaceFailsAfterInvalidatingChildProcessToken
) {
238 const int kIOSurfaceId
= 10;
239 const int kClientId
= 1;
240 const mach_port_t kIOSurfacePortId
= 100u;
242 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
243 IOSurfaceManagerToken child_process_token
=
244 GenerateChildProcessToken(kClientId
);
246 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId
,
249 InvalidateChildProcessToken(child_process_token
);
251 IOSurfaceManagerHostMsg_AcquireIOSurface request
= {{0}};
252 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply
= {{0}};
253 request
.io_surface_id
= kIOSurfaceId
;
254 memcpy(request
.token_name
, child_process_token
.name
,
255 sizeof(child_process_token
.name
));
256 // Fails as token is no longer valid.
257 EXPECT_FALSE(HandleAcquireIOSurfaceRequest(request
, &reply
));
260 TEST_F(BrowserIOSurfaceManagerTest
,
261 AcquireIOSurfaceAfterInvalidatingGpuProcessToken
) {
262 const int kIOSurfaceId
= 10;
263 const int kClientId
= 1;
264 const mach_port_t kIOSurfacePortId
= 100u;
266 IOSurfaceManagerToken gpu_process_token
= GenerateGpuProcessToken();
267 IOSurfaceManagerToken child_process_token
=
268 GenerateChildProcessToken(kClientId
);
270 RegisterIOSurface(gpu_process_token
, kIOSurfaceId
, kClientId
,
273 InvalidateGpuProcessToken();
275 IOSurfaceManagerHostMsg_AcquireIOSurface request
= {{0}};
276 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply
= {{0}};
277 request
.io_surface_id
= kIOSurfaceId
;
278 memcpy(request
.token_name
, child_process_token
.name
,
279 sizeof(child_process_token
.name
));
280 EXPECT_TRUE(HandleAcquireIOSurfaceRequest(request
, &reply
));
281 // Should return invalid port.
282 EXPECT_EQ(static_cast<mach_port_t
>(MACH_PORT_NULL
),
283 reply
.io_surface_port
.name
);
286 } // namespace content