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

Side by Side Diff: mojo/public/c/system/tests/handle_unittest.cc

Issue 2056513002: Plumb MojoReplaceHandleWithReducedRights() out. (Closed) Base URL: https://github.com/domokit/mojo.git@work790_replace_reduce_1
Patch Set: Created 4 years, 6 months 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 // This file tests the C handle API (the functions declared in 5 // This file tests the C handle API (the functions declared in
6 // mojo/public/c/system/handle.h). Note: The functionality of these APIs for 6 // mojo/public/c/system/handle.h). Note: The functionality of these APIs for
7 // specific types of handles are tested with the APIs for those types of 7 // specific types of handles are tested with the APIs for those types of
8 // handles. 8 // handles.
9 9
10 #include "mojo/public/c/system/handle.h" 10 #include "mojo/public/c/system/handle.h"
11 #include "mojo/public/c/system/message_pipe.h"
11 #include "mojo/public/c/system/result.h" 12 #include "mojo/public/c/system/result.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace { 15 namespace {
15 16
17 const MojoHandleRights kDefaultMessagePipeHandleRights =
18 MOJO_HANDLE_RIGHT_TRANSFER | MOJO_HANDLE_RIGHT_READ |
19 MOJO_HANDLE_RIGHT_WRITE | MOJO_HANDLE_RIGHT_GET_OPTIONS |
20 MOJO_HANDLE_RIGHT_SET_OPTIONS;
21
16 TEST(HandleTest, InvalidHandle) { 22 TEST(HandleTest, InvalidHandle) {
17 // Close: 23 // MojoClose:
18 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(MOJO_HANDLE_INVALID)); 24 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(MOJO_HANDLE_INVALID));
19 25
20 // GetRights: 26 // MojoGetRights:
21 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE; 27 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE;
22 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 28 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
23 MojoGetRights(MOJO_HANDLE_INVALID, &rights)); 29 MojoGetRights(MOJO_HANDLE_INVALID, &rights));
24 30
25 // DuplicateHandleWithReducedRights: 31 // MojoReplaceHandleWithReducedRights:
32 MojoHandle replacement_handle = MOJO_HANDLE_INVALID;
33 EXPECT_EQ(
34 MOJO_RESULT_INVALID_ARGUMENT,
35 MojoReplaceHandleWithReducedRights(
36 MOJO_HANDLE_INVALID, MOJO_HANDLE_RIGHT_NONE, &replacement_handle));
37
38 // MojoDuplicateHandleWithReducedRights:
26 MojoHandle new_handle = MOJO_HANDLE_INVALID; 39 MojoHandle new_handle = MOJO_HANDLE_INVALID;
27 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 40 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
28 MojoDuplicateHandleWithReducedRights( 41 MojoDuplicateHandleWithReducedRights(
29 MOJO_HANDLE_INVALID, MOJO_HANDLE_RIGHT_DUPLICATE, &new_handle)); 42 MOJO_HANDLE_INVALID, MOJO_HANDLE_RIGHT_DUPLICATE, &new_handle));
30 EXPECT_EQ(MOJO_HANDLE_INVALID, new_handle); 43 EXPECT_EQ(MOJO_HANDLE_INVALID, new_handle);
31 44
32 // DuplicateHandle: 45 // MojoDuplicateHandle:
33 new_handle = MOJO_HANDLE_INVALID; 46 new_handle = MOJO_HANDLE_INVALID;
34 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 47 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
35 MojoDuplicateHandle(MOJO_HANDLE_INVALID, &new_handle)); 48 MojoDuplicateHandle(MOJO_HANDLE_INVALID, &new_handle));
36 EXPECT_EQ(MOJO_HANDLE_INVALID, new_handle); 49 EXPECT_EQ(MOJO_HANDLE_INVALID, new_handle);
37 } 50 }
38 51
52 // |MojoReplaceHandleWithReducedRights()| is not handle-type specific, so we'll
53 // test it here, even though it requires actually creating/using a specific
54 // handle type.
55 TEST(HandleTest, ReplaceHandleWithReducedRights) {
56 MojoHandle h0 = MOJO_HANDLE_INVALID;
57 MojoHandle h1 = MOJO_HANDLE_INVALID;
58 // That |MojoCreateMessagePipe()| works correctly is checked in
59 // |MessagePipeTest|.
60 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1));
61
62 // Still check the rights on one of the handles, just to make sure that
63 // |kDefaultMessagePipeHandleRights| stays in sync with reality.
64 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE;
65 EXPECT_EQ(MOJO_RESULT_OK, MojoGetRights(h0, &rights));
66 EXPECT_EQ(kDefaultMessagePipeHandleRights, rights);
67
68 // First try replacing without reducing rights.
69 MojoHandle h0r0 = MOJO_HANDLE_INVALID;
70 EXPECT_EQ(MOJO_RESULT_OK, MojoReplaceHandleWithReducedRights(
71 h0, MOJO_HANDLE_RIGHT_NONE, &h0r0));
72 EXPECT_NE(h0r0, MOJO_HANDLE_INVALID);
73 // Not guaranteed, but we depend on handle values not being reused eagerly.
74 EXPECT_NE(h0r0, h0);
75 EXPECT_NE(h0r0, h1); // |h0r0| should definitely not be the same as |h1|.
76 // |h0| should be dead, so this should fail.
77 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(h0));
78
79 // Check that the rights remain the same.
80 rights = MOJO_HANDLE_RIGHT_NONE;
81 EXPECT_EQ(MOJO_RESULT_OK, MojoGetRights(h0r0, &rights));
82 EXPECT_EQ(kDefaultMessagePipeHandleRights, rights);
83
84 // Make sure the replacement handle is still usable.
85 char x = 'x';
86 EXPECT_EQ(MOJO_RESULT_OK, MojoWriteMessage(h0r0, &x, 1u, nullptr, 0,
87 MOJO_WRITE_MESSAGE_FLAG_NONE));
88
89 // Try replacing, but removing a couple of rights.
90 MojoHandle h0r1 = MOJO_HANDLE_INVALID;
91 constexpr MojoHandleRights kRightsToRemove =
92 MOJO_HANDLE_RIGHT_TRANSFER | MOJO_HANDLE_RIGHT_WRITE;
93 EXPECT_EQ(MOJO_RESULT_OK,
94 MojoReplaceHandleWithReducedRights(h0r0, kRightsToRemove, &h0r1));
95 EXPECT_NE(h0r1, MOJO_HANDLE_INVALID);
96 // Not guaranteed, but we depend on handle values not being reused eagerly.
97 EXPECT_NE(h0r1, h0r0);
98 EXPECT_NE(h0r1, h1); // |h0r1| should definitely not be the same as |h1|.
99 // |h0r0| should be dead, so this should fail.
100 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(h0r0));
101
102 // Check that |h0r1| has the expected rights.
103 rights = MOJO_HANDLE_RIGHT_NONE;
104 EXPECT_EQ(MOJO_RESULT_OK, MojoGetRights(h0r1, &rights));
105 EXPECT_EQ(kDefaultMessagePipeHandleRights & ~kRightsToRemove, rights);
106
107 // Make sure that the rights are actually correctly enforced.
108 EXPECT_EQ(
109 MOJO_RESULT_PERMISSION_DENIED,
110 MojoWriteMessage(h0r1, &x, 1u, nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
111
112 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0r1));
113 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
114 }
115
39 } // namespace 116 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698