OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 test is POSIX only. | 5 // This test is POSIX only. |
6 | 6 |
7 #include "ipc/ipc_message_attachment_set.h" | 7 #include "ipc/ipc_message_attachment_set.h" |
8 | 8 |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
| 14 #include "ipc/ipc_platform_file_attachment_posix.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace IPC { | 17 namespace IPC { |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Get a safe file descriptor for test purposes. | 20 // Get a safe file descriptor for test purposes. |
20 int GetSafeFd() { | 21 int GetSafeFd() { |
21 return open("/dev/null", O_RDONLY); | 22 return open("/dev/null", O_RDONLY); |
22 } | 23 } |
23 | 24 |
(...skipping 11 matching lines...) Expand all Loading... |
35 // The MessageAttachmentSet will try and close some of the descriptor numbers | 36 // The MessageAttachmentSet will try and close some of the descriptor numbers |
36 // which we given it. This is the base descriptor value. It's great enough such | 37 // which we given it. This is the base descriptor value. It's great enough such |
37 // that no real descriptor will accidently be closed. | 38 // that no real descriptor will accidently be closed. |
38 static const int kFDBase = 50000; | 39 static const int kFDBase = 50000; |
39 | 40 |
40 TEST(MessageAttachmentSet, BasicAdd) { | 41 TEST(MessageAttachmentSet, BasicAdd) { |
41 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 42 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
42 | 43 |
43 ASSERT_EQ(set->size(), 0u); | 44 ASSERT_EQ(set->size(), 0u); |
44 ASSERT_TRUE(set->empty()); | 45 ASSERT_TRUE(set->empty()); |
45 ASSERT_TRUE(set->AddToBorrow(kFDBase)); | 46 ASSERT_TRUE( |
| 47 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
46 ASSERT_EQ(set->size(), 1u); | 48 ASSERT_EQ(set->size(), 1u); |
47 ASSERT_TRUE(!set->empty()); | 49 ASSERT_TRUE(!set->empty()); |
48 | 50 |
49 // Empties the set and stops a warning about deleting a set with unconsumed | 51 // Empties the set and stops a warning about deleting a set with unconsumed |
50 // descriptors | 52 // descriptors |
51 set->CommitAll(); | 53 set->CommitAll(); |
52 } | 54 } |
53 | 55 |
54 TEST(MessageAttachmentSet, BasicAddAndClose) { | 56 TEST(MessageAttachmentSet, BasicAddAndClose) { |
55 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 57 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
56 | 58 |
57 ASSERT_EQ(set->size(), 0u); | 59 ASSERT_EQ(set->size(), 0u); |
58 ASSERT_TRUE(set->empty()); | 60 ASSERT_TRUE(set->empty()); |
59 const int fd = GetSafeFd(); | 61 const int fd = GetSafeFd(); |
60 ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd))); | 62 ASSERT_TRUE(set->AddAttachment( |
| 63 new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
61 ASSERT_EQ(set->size(), 1u); | 64 ASSERT_EQ(set->size(), 1u); |
62 ASSERT_TRUE(!set->empty()); | 65 ASSERT_TRUE(!set->empty()); |
63 | 66 |
64 set->CommitAll(); | 67 set->CommitAll(); |
65 | 68 |
66 ASSERT_TRUE(VerifyClosed(fd)); | 69 ASSERT_TRUE(VerifyClosed(fd)); |
67 } | 70 } |
68 TEST(MessageAttachmentSet, MaxSize) { | 71 TEST(MessageAttachmentSet, MaxSize) { |
69 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 72 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
70 | 73 |
71 for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i) | 74 for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i) |
72 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1 + i)); | 75 ASSERT_TRUE(set->AddAttachment( |
| 76 new internal::PlatformFileAttachment(kFDBase + 1 + i))); |
73 | 77 |
74 ASSERT_TRUE(!set->AddToBorrow(kFDBase)); | 78 ASSERT_TRUE( |
| 79 !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
75 | 80 |
76 set->CommitAll(); | 81 set->CommitAll(); |
77 } | 82 } |
78 | 83 |
79 TEST(MessageAttachmentSet, SetDescriptors) { | 84 TEST(MessageAttachmentSet, SetDescriptors) { |
80 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 85 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
81 | 86 |
82 ASSERT_TRUE(set->empty()); | 87 ASSERT_TRUE(set->empty()); |
83 set->AddDescriptorsToOwn(NULL, 0); | 88 set->AddDescriptorsToOwn(NULL, 0); |
84 ASSERT_TRUE(set->empty()); | 89 ASSERT_TRUE(set->empty()); |
85 | 90 |
86 const int fd = GetSafeFd(); | 91 const int fd = GetSafeFd(); |
87 static const int fds[] = {fd}; | 92 static const int fds[] = {fd}; |
88 set->AddDescriptorsToOwn(fds, 1); | 93 set->AddDescriptorsToOwn(fds, 1); |
89 ASSERT_TRUE(!set->empty()); | 94 ASSERT_TRUE(!set->empty()); |
90 ASSERT_EQ(set->size(), 1u); | 95 ASSERT_EQ(set->size(), 1u); |
91 | 96 |
92 set->CommitAll(); | 97 set->CommitAll(); |
93 | 98 |
94 ASSERT_TRUE(VerifyClosed(fd)); | 99 ASSERT_TRUE(VerifyClosed(fd)); |
95 } | 100 } |
96 | 101 |
97 TEST(MessageAttachmentSet, PeekDescriptors) { | 102 TEST(MessageAttachmentSet, PeekDescriptors) { |
98 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 103 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
99 | 104 |
100 set->PeekDescriptors(NULL); | 105 set->PeekDescriptors(NULL); |
101 ASSERT_TRUE(set->AddToBorrow(kFDBase)); | 106 ASSERT_TRUE( |
| 107 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
102 | 108 |
103 int fds[1]; | 109 int fds[1]; |
104 fds[0] = 0; | 110 fds[0] = 0; |
105 set->PeekDescriptors(fds); | 111 set->PeekDescriptors(fds); |
106 ASSERT_EQ(fds[0], kFDBase); | 112 ASSERT_EQ(fds[0], kFDBase); |
107 set->CommitAll(); | 113 set->CommitAll(); |
108 ASSERT_TRUE(set->empty()); | 114 ASSERT_TRUE(set->empty()); |
109 } | 115 } |
110 | 116 |
111 TEST(MessageAttachmentSet, WalkInOrder) { | 117 TEST(MessageAttachmentSet, WalkInOrder) { |
112 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 118 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
113 | 119 |
114 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be | 120 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
115 // used to retrieve borrowed descriptors. That never happens in production. | 121 // used to retrieve borrowed descriptors. That never happens in production. |
116 ASSERT_TRUE(set->AddToBorrow(kFDBase)); | 122 ASSERT_TRUE( |
117 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1)); | 123 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
118 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2)); | 124 ASSERT_TRUE( |
| 125 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 126 ASSERT_TRUE( |
| 127 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
119 | 128 |
120 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); | 129 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase); |
121 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); | 130 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1); |
122 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); | 131 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2); |
123 | 132 |
124 set->CommitAll(); | 133 set->CommitAll(); |
125 } | 134 } |
126 | 135 |
127 TEST(MessageAttachmentSet, WalkWrongOrder) { | 136 TEST(MessageAttachmentSet, WalkWrongOrder) { |
128 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 137 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
129 | 138 |
130 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be | 139 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
131 // used to retrieve borrowed descriptors. That never happens in production. | 140 // used to retrieve borrowed descriptors. That never happens in production. |
132 ASSERT_TRUE(set->AddToBorrow(kFDBase)); | 141 ASSERT_TRUE( |
133 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1)); | 142 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
134 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2)); | 143 ASSERT_TRUE( |
| 144 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 145 ASSERT_TRUE( |
| 146 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
135 | 147 |
136 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); | 148 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase); |
137 ASSERT_EQ(set->TakeDescriptorAt(2), -1); | 149 ASSERT_EQ(set->GetAttachmentAt(2), nullptr); |
138 | 150 |
139 set->CommitAll(); | 151 set->CommitAll(); |
140 } | 152 } |
141 | 153 |
142 TEST(MessageAttachmentSet, WalkCycle) { | 154 TEST(MessageAttachmentSet, WalkCycle) { |
143 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 155 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
144 | 156 |
145 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be | 157 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
146 // used to retrieve borrowed descriptors. That never happens in production. | 158 // used to retrieve borrowed descriptors. That never happens in production. |
147 ASSERT_TRUE(set->AddToBorrow(kFDBase)); | 159 ASSERT_TRUE( |
148 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1)); | 160 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
149 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2)); | 161 ASSERT_TRUE( |
| 162 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 163 ASSERT_TRUE( |
| 164 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
150 | 165 |
151 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); | 166 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase); |
152 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); | 167 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1); |
153 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); | 168 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2); |
154 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); | 169 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase); |
155 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); | 170 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1); |
156 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); | 171 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2); |
157 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); | 172 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase); |
158 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); | 173 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1); |
159 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); | 174 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2); |
160 | 175 |
161 set->CommitAll(); | 176 set->CommitAll(); |
162 } | 177 } |
163 | 178 |
164 TEST(MessageAttachmentSet, DontClose) { | 179 TEST(MessageAttachmentSet, DontClose) { |
165 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 180 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
166 | 181 |
167 const int fd = GetSafeFd(); | 182 const int fd = GetSafeFd(); |
168 ASSERT_TRUE(set->AddToBorrow(fd)); | 183 ASSERT_TRUE(set->AddAttachment(new internal::PlatformFileAttachment(fd))); |
169 set->CommitAll(); | 184 set->CommitAll(); |
170 | 185 |
171 ASSERT_FALSE(VerifyClosed(fd)); | 186 ASSERT_FALSE(VerifyClosed(fd)); |
172 } | 187 } |
173 | 188 |
174 TEST(MessageAttachmentSet, DoClose) { | 189 TEST(MessageAttachmentSet, DoClose) { |
175 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 190 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
176 | 191 |
177 const int fd = GetSafeFd(); | 192 const int fd = GetSafeFd(); |
178 ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd))); | 193 ASSERT_TRUE(set->AddAttachment( |
| 194 new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
179 set->CommitAll(); | 195 set->CommitAll(); |
180 | 196 |
181 ASSERT_TRUE(VerifyClosed(fd)); | 197 ASSERT_TRUE(VerifyClosed(fd)); |
182 } | 198 } |
183 | 199 |
184 } // namespace | 200 } // namespace |
185 } // namespace IPC | 201 } // namespace IPC |
OLD | NEW |