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