[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / browser_io_surface_manager_mac_unittest.cc
blobb8863e4ad9dcdf1ff2a297f696280071434db192
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"
9 namespace content {
11 class BrowserIOSurfaceManagerTest : public testing::Test {
12 public:
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 io_surface_manager_.HandleRegisterIOSurfaceRequest(request, reply);
35 return reply->result;
38 bool HandleUnregisterIOSurfaceRequest(
39 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request) {
40 return io_surface_manager_.HandleUnregisterIOSurfaceRequest(request);
43 bool HandleAcquireIOSurfaceRequest(
44 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
45 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply) {
46 io_surface_manager_.HandleAcquireIOSurfaceRequest(request, reply);
47 return reply->result;
50 // Helper function used to register an IOSurface for testing.
51 void RegisterIOSurface(const IOSurfaceManagerToken& gpu_process_token,
52 int io_surface_id,
53 int client_id,
54 mach_port_t io_surface_port) {
55 IOSurfaceManagerHostMsg_RegisterIOSurface request = {{0}};
56 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply = {{0}};
57 request.io_surface_id = io_surface_id;
58 request.client_id = client_id;
59 request.io_surface_port.name = io_surface_port;
60 memcpy(request.token_name, gpu_process_token.name,
61 sizeof(gpu_process_token.name));
62 ASSERT_TRUE(HandleRegisterIOSurfaceRequest(request, &reply));
63 ASSERT_TRUE(reply.result);
66 private:
67 BrowserIOSurfaceManager io_surface_manager_;
70 TEST_F(BrowserIOSurfaceManagerTest, EnsureRunning) {
71 EnsureRunning();
72 base::mac::ScopedMachSendRight service_port =
73 BrowserIOSurfaceManager::LookupServicePort(getpid());
74 EXPECT_TRUE(service_port.is_valid());
77 TEST_F(BrowserIOSurfaceManagerTest, RegisterIOSurfaceSucceeds) {
78 const int kIOSurfaceId = 1;
79 const int kClientId = 1;
81 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
83 IOSurfaceManagerHostMsg_RegisterIOSurface request = {{0}};
84 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply = {{0}};
86 request.io_surface_id = kIOSurfaceId;
87 request.client_id = kClientId;
88 memcpy(request.token_name, gpu_process_token.name,
89 sizeof(gpu_process_token.name));
90 EXPECT_TRUE(HandleRegisterIOSurfaceRequest(request, &reply));
91 EXPECT_TRUE(reply.result);
94 TEST_F(BrowserIOSurfaceManagerTest, RegisterIOSurfaceFailsWithInvalidToken) {
95 const int kIOSurfaceId = 1;
96 const int kClientId = 1;
98 IOSurfaceManagerHostMsg_RegisterIOSurface request = {{0}};
99 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply = {{0}};
100 request.io_surface_id = kIOSurfaceId;
101 request.client_id = kClientId;
102 // Fails as request doesn't contain a valid token.
103 EXPECT_FALSE(HandleRegisterIOSurfaceRequest(request, &reply));
106 TEST_F(BrowserIOSurfaceManagerTest,
107 RegisterIOSurfaceFailsWithChildProcessToken) {
108 const int kIOSurfaceId = 1;
109 const int kClientId = 1;
111 IOSurfaceManagerToken child_process_token =
112 GenerateChildProcessToken(kClientId);
114 IOSurfaceManagerHostMsg_RegisterIOSurface request = {{0}};
115 IOSurfaceManagerMsg_RegisterIOSurfaceReply reply = {{0}};
116 request.io_surface_id = kIOSurfaceId;
117 request.client_id = kClientId;
118 memcpy(request.token_name, child_process_token.name,
119 sizeof(child_process_token.name));
120 // Fails as child process is not allowed to register IOSurfaces.
121 EXPECT_FALSE(HandleRegisterIOSurfaceRequest(request, &reply));
124 TEST_F(BrowserIOSurfaceManagerTest, UnregisterIOSurfaceSucceeds) {
125 const int kIOSurfaceId = 1;
126 const int kClientId = 1;
128 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
130 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId, 0);
132 IOSurfaceManagerHostMsg_UnregisterIOSurface request = {{0}};
133 request.io_surface_id = kIOSurfaceId;
134 request.client_id = kClientId;
135 memcpy(request.token_name, gpu_process_token.name,
136 sizeof(gpu_process_token.name));
137 EXPECT_TRUE(HandleUnregisterIOSurfaceRequest(request));
140 TEST_F(BrowserIOSurfaceManagerTest, UnregisterIOSurfaceFailsWithInvalidToken) {
141 const int kIOSurfaceId = 1;
142 const int kClientId = 1;
144 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
146 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId, 0);
148 IOSurfaceManagerHostMsg_UnregisterIOSurface request = {{0}};
149 request.io_surface_id = kIOSurfaceId;
150 request.client_id = kClientId;
151 // Fails as request doesn't contain valid GPU token.
152 EXPECT_FALSE(HandleUnregisterIOSurfaceRequest(request));
155 TEST_F(BrowserIOSurfaceManagerTest,
156 UnregisterIOSurfaceFailsWithChildProcessToken) {
157 const int kIOSurfaceId = 1;
158 const int kClientId = 1;
160 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
161 IOSurfaceManagerToken child_process_token =
162 GenerateChildProcessToken(kClientId);
164 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId, 0);
166 IOSurfaceManagerHostMsg_UnregisterIOSurface request = {{0}};
167 request.io_surface_id = kIOSurfaceId;
168 request.client_id = kClientId;
169 memcpy(request.token_name, child_process_token.name,
170 sizeof(child_process_token.name));
171 // Fails as child process is not allowed to unregister IOSurfaces.
172 EXPECT_FALSE(HandleUnregisterIOSurfaceRequest(request));
175 TEST_F(BrowserIOSurfaceManagerTest, AcquireIOSurfaceSucceeds) {
176 const int kIOSurfaceId = 10;
177 const int kClientId = 1;
178 const mach_port_t kIOSurfacePortId = 100u;
180 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
181 IOSurfaceManagerToken child_process_token =
182 GenerateChildProcessToken(kClientId);
184 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId,
185 kIOSurfacePortId);
187 IOSurfaceManagerHostMsg_AcquireIOSurface request = {{0}};
188 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply = {{0}};
189 request.io_surface_id = kIOSurfaceId;
190 memcpy(request.token_name, child_process_token.name,
191 sizeof(child_process_token.name));
192 EXPECT_TRUE(HandleAcquireIOSurfaceRequest(request, &reply));
193 EXPECT_EQ(kIOSurfacePortId, reply.io_surface_port.name);
196 TEST_F(BrowserIOSurfaceManagerTest, AcquireIOSurfaceFailsWithInvalidToken) {
197 const int kIOSurfaceId = 10;
198 const int kClientId = 1;
199 const mach_port_t kIOSurfacePortId = 100u;
201 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
203 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId,
204 kIOSurfacePortId);
206 IOSurfaceManagerHostMsg_AcquireIOSurface request = {{0}};
207 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply = {{0}};
208 request.io_surface_id = kIOSurfaceId;
209 // Fails as request doesn't contain valid token.
210 EXPECT_FALSE(HandleAcquireIOSurfaceRequest(request, &reply));
213 TEST_F(BrowserIOSurfaceManagerTest,
214 AcquireIOSurfaceFailsWithWrongChildProcessToken) {
215 const int kIOSurfaceId = 10;
216 const int kClientId1 = 1;
217 const int kClientId2 = 2;
218 const mach_port_t kIOSurfacePortId = 100u;
220 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
221 IOSurfaceManagerToken child_process_token2 =
222 GenerateChildProcessToken(kClientId2);
224 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId1,
225 kIOSurfacePortId);
227 IOSurfaceManagerHostMsg_AcquireIOSurface request = {{0}};
228 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply = {{0}};
229 request.io_surface_id = kIOSurfaceId;
230 memcpy(request.token_name, child_process_token2.name,
231 sizeof(child_process_token2.name));
232 EXPECT_TRUE(HandleAcquireIOSurfaceRequest(request, &reply));
233 // Fails as child process 2 is not allowed to acquire this IOSurface.
234 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
235 reply.io_surface_port.name);
238 TEST_F(BrowserIOSurfaceManagerTest,
239 AcquireIOSurfaceFailsAfterInvalidatingChildProcessToken) {
240 const int kIOSurfaceId = 10;
241 const int kClientId = 1;
242 const mach_port_t kIOSurfacePortId = 100u;
244 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
245 IOSurfaceManagerToken child_process_token =
246 GenerateChildProcessToken(kClientId);
248 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId,
249 kIOSurfacePortId);
251 InvalidateChildProcessToken(child_process_token);
253 IOSurfaceManagerHostMsg_AcquireIOSurface request = {{0}};
254 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply = {{0}};
255 request.io_surface_id = kIOSurfaceId;
256 memcpy(request.token_name, child_process_token.name,
257 sizeof(child_process_token.name));
258 // Fails as token is no longer valid.
259 EXPECT_FALSE(HandleAcquireIOSurfaceRequest(request, &reply));
262 TEST_F(BrowserIOSurfaceManagerTest,
263 AcquireIOSurfaceAfterInvalidatingGpuProcessToken) {
264 const int kIOSurfaceId = 10;
265 const int kClientId = 1;
266 const mach_port_t kIOSurfacePortId = 100u;
268 IOSurfaceManagerToken gpu_process_token = GenerateGpuProcessToken();
269 IOSurfaceManagerToken child_process_token =
270 GenerateChildProcessToken(kClientId);
272 RegisterIOSurface(gpu_process_token, kIOSurfaceId, kClientId,
273 kIOSurfacePortId);
275 InvalidateGpuProcessToken();
277 IOSurfaceManagerHostMsg_AcquireIOSurface request = {{0}};
278 IOSurfaceManagerMsg_AcquireIOSurfaceReply reply = {{0}};
279 request.io_surface_id = kIOSurfaceId;
280 memcpy(request.token_name, child_process_token.name,
281 sizeof(child_process_token.name));
282 EXPECT_TRUE(HandleAcquireIOSurfaceRequest(request, &reply));
283 // Should return invalid port.
284 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
285 reply.io_surface_port.name);
288 } // namespace content