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 <stddef.h> | 10 #include <stddef.h> |
(...skipping 16 matching lines...) Expand all Loading... |
27 bool VerifyClosed(int fd) { | 27 bool VerifyClosed(int fd) { |
28 const int duped = dup(fd); | 28 const int duped = dup(fd); |
29 if (duped != -1) { | 29 if (duped != -1) { |
30 EXPECT_NE(IGNORE_EINTR(close(duped)), -1); | 30 EXPECT_NE(IGNORE_EINTR(close(duped)), -1); |
31 EXPECT_NE(IGNORE_EINTR(close(fd)), -1); | 31 EXPECT_NE(IGNORE_EINTR(close(fd)), -1); |
32 return false; | 32 return false; |
33 } | 33 } |
34 return true; | 34 return true; |
35 } | 35 } |
36 | 36 |
| 37 int GetFdAt(MessageAttachmentSet* set, int id) { |
| 38 return static_cast<internal::PlatformFileAttachment&>( |
| 39 *set->GetAttachmentAt(id)) |
| 40 .TakePlatformFile(); |
| 41 } |
| 42 |
37 // The MessageAttachmentSet will try and close some of the descriptor numbers | 43 // The MessageAttachmentSet will try and close some of the descriptor numbers |
38 // which we given it. This is the base descriptor value. It's great enough such | 44 // which we given it. This is the base descriptor value. It's great enough such |
39 // that no real descriptor will accidently be closed. | 45 // that no real descriptor will accidently be closed. |
40 static const int kFDBase = 50000; | 46 static const int kFDBase = 50000; |
41 | 47 |
42 TEST(MessageAttachmentSet, BasicAdd) { | 48 TEST(MessageAttachmentSet, BasicAdd) { |
43 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 49 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
44 | 50 |
45 ASSERT_EQ(set->size(), 0u); | 51 ASSERT_EQ(set->size(), 0u); |
46 ASSERT_TRUE(set->empty()); | 52 ASSERT_TRUE(set->empty()); |
(...skipping 28 matching lines...) Expand all Loading... |
75 for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i) | 81 for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i) |
76 ASSERT_TRUE(set->AddAttachment( | 82 ASSERT_TRUE(set->AddAttachment( |
77 new internal::PlatformFileAttachment(kFDBase + 1 + i))); | 83 new internal::PlatformFileAttachment(kFDBase + 1 + i))); |
78 | 84 |
79 ASSERT_TRUE( | 85 ASSERT_TRUE( |
80 !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); | 86 !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
81 | 87 |
82 set->CommitAllDescriptors(); | 88 set->CommitAllDescriptors(); |
83 } | 89 } |
84 | 90 |
85 #if defined(OS_ANDROID) | |
86 #define MAYBE_SetDescriptors DISABLED_SetDescriptors | |
87 #else | |
88 #define MAYBE_SetDescriptors SetDescriptors | |
89 #endif | |
90 TEST(MessageAttachmentSet, MAYBE_SetDescriptors) { | |
91 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | |
92 | |
93 ASSERT_TRUE(set->empty()); | |
94 set->AddDescriptorsToOwn(NULL, 0); | |
95 ASSERT_TRUE(set->empty()); | |
96 | |
97 const int fd = GetSafeFd(); | |
98 static const int fds[] = {fd}; | |
99 set->AddDescriptorsToOwn(fds, 1); | |
100 ASSERT_TRUE(!set->empty()); | |
101 ASSERT_EQ(set->size(), 1u); | |
102 | |
103 set->CommitAllDescriptors(); | |
104 | |
105 ASSERT_TRUE(VerifyClosed(fd)); | |
106 } | |
107 | |
108 TEST(MessageAttachmentSet, PeekDescriptors) { | |
109 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | |
110 | |
111 set->PeekDescriptors(NULL); | |
112 ASSERT_TRUE( | |
113 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); | |
114 | |
115 int fds[1]; | |
116 fds[0] = 0; | |
117 set->PeekDescriptors(fds); | |
118 ASSERT_EQ(fds[0], kFDBase); | |
119 set->CommitAllDescriptors(); | |
120 ASSERT_TRUE(set->empty()); | |
121 } | |
122 | |
123 TEST(MessageAttachmentSet, WalkInOrder) { | 91 TEST(MessageAttachmentSet, WalkInOrder) { |
124 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 92 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
125 | 93 |
126 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be | 94 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
127 // used to retrieve borrowed descriptors. That never happens in production. | 95 // used to retrieve borrowed descriptors. That never happens in production. |
128 ASSERT_TRUE( | 96 ASSERT_TRUE( |
129 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); | 97 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
130 ASSERT_TRUE( | 98 ASSERT_TRUE( |
131 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); | 99 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
132 ASSERT_TRUE( | 100 ASSERT_TRUE( |
133 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); | 101 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
134 | 102 |
135 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); | 103 ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
136 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), | 104 ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
137 kFDBase + 1); | 105 ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
138 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), | |
139 kFDBase + 2); | |
140 | 106 |
141 set->CommitAllDescriptors(); | 107 set->CommitAllDescriptors(); |
142 } | 108 } |
143 | 109 |
144 TEST(MessageAttachmentSet, WalkWrongOrder) { | 110 TEST(MessageAttachmentSet, WalkWrongOrder) { |
145 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 111 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
146 | 112 |
147 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be | 113 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
148 // used to retrieve borrowed descriptors. That never happens in production. | 114 // used to retrieve borrowed descriptors. That never happens in production. |
149 ASSERT_TRUE( | 115 ASSERT_TRUE( |
150 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); | 116 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
151 ASSERT_TRUE( | 117 ASSERT_TRUE( |
152 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); | 118 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
153 ASSERT_TRUE( | 119 ASSERT_TRUE( |
154 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); | 120 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
155 | 121 |
156 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); | 122 ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
157 ASSERT_FALSE(set->GetNonBrokerableAttachmentAt(2)); | 123 ASSERT_FALSE(set->GetAttachmentAt(2)); |
158 | 124 |
159 set->CommitAllDescriptors(); | 125 set->CommitAllDescriptors(); |
160 } | 126 } |
161 | 127 |
162 TEST(MessageAttachmentSet, WalkCycle) { | 128 TEST(MessageAttachmentSet, WalkCycle) { |
163 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); | 129 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
164 | 130 |
165 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be | 131 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
166 // used to retrieve borrowed descriptors. That never happens in production. | 132 // used to retrieve borrowed descriptors. That never happens in production. |
167 ASSERT_TRUE( | 133 ASSERT_TRUE( |
168 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); | 134 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
169 ASSERT_TRUE( | 135 ASSERT_TRUE( |
170 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); | 136 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
171 ASSERT_TRUE( | 137 ASSERT_TRUE( |
172 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); | 138 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
173 | 139 |
174 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); | 140 ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
175 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), | 141 ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
176 kFDBase + 1); | 142 ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
177 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), | 143 ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
178 kFDBase + 2); | 144 ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
179 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); | 145 ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
180 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), | 146 ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
181 kFDBase + 1); | 147 ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
182 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), | 148 ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
183 kFDBase + 2); | |
184 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); | |
185 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), | |
186 kFDBase + 1); | |
187 ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), | |
188 kFDBase + 2); | |
189 | 149 |
190 set->CommitAllDescriptors(); | 150 set->CommitAllDescriptors(); |
191 } | 151 } |
192 | 152 |
193 #if defined(OS_ANDROID) | 153 #if defined(OS_ANDROID) |
194 #define MAYBE_DontClose DISABLED_DontClose | 154 #define MAYBE_DontClose DISABLED_DontClose |
195 #else | 155 #else |
196 #define MAYBE_DontClose DontClose | 156 #define MAYBE_DontClose DontClose |
197 #endif | 157 #endif |
198 TEST(MessageAttachmentSet, MAYBE_DontClose) { | 158 TEST(MessageAttachmentSet, MAYBE_DontClose) { |
(...skipping 12 matching lines...) Expand all Loading... |
211 const int fd = GetSafeFd(); | 171 const int fd = GetSafeFd(); |
212 ASSERT_TRUE(set->AddAttachment( | 172 ASSERT_TRUE(set->AddAttachment( |
213 new internal::PlatformFileAttachment(base::ScopedFD(fd)))); | 173 new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
214 set->CommitAllDescriptors(); | 174 set->CommitAllDescriptors(); |
215 | 175 |
216 ASSERT_TRUE(VerifyClosed(fd)); | 176 ASSERT_TRUE(VerifyClosed(fd)); |
217 } | 177 } |
218 | 178 |
219 } // namespace | 179 } // namespace |
220 } // namespace IPC | 180 } // namespace IPC |
OLD | NEW |