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
3 // found in the LICENSE file.
5 // This test is POSIX only.
7 #include "ipc/ipc_message_attachment_set.h"
12 #include "base/posix/eintr_wrapper.h"
13 #include "ipc/ipc_platform_file_attachment_posix.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 // Get a safe file descriptor for test purposes.
21 return open("/dev/null", O_RDONLY
);
24 // Returns true if fd was already closed. Closes fd if not closed.
25 bool VerifyClosed(int fd
) {
26 const int duped
= dup(fd
);
28 EXPECT_NE(IGNORE_EINTR(close(duped
)), -1);
29 EXPECT_NE(IGNORE_EINTR(close(fd
)), -1);
35 // 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 // that no real descriptor will accidently be closed.
38 static const int kFDBase
= 50000;
40 TEST(MessageAttachmentSet
, BasicAdd
) {
41 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
43 ASSERT_EQ(set
->size(), 0u);
44 ASSERT_TRUE(set
->empty());
46 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
)));
47 ASSERT_EQ(set
->size(), 1u);
48 ASSERT_TRUE(!set
->empty());
50 // Empties the set and stops a warning about deleting a set with unconsumed
55 TEST(MessageAttachmentSet
, BasicAddAndClose
) {
56 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
58 ASSERT_EQ(set
->size(), 0u);
59 ASSERT_TRUE(set
->empty());
60 const int fd
= GetSafeFd();
61 ASSERT_TRUE(set
->AddAttachment(
62 new internal::PlatformFileAttachment(base::ScopedFD(fd
))));
63 ASSERT_EQ(set
->size(), 1u);
64 ASSERT_TRUE(!set
->empty());
68 ASSERT_TRUE(VerifyClosed(fd
));
70 TEST(MessageAttachmentSet
, MaxSize
) {
71 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
73 for (size_t i
= 0; i
< MessageAttachmentSet::kMaxDescriptorsPerMessage
; ++i
)
74 ASSERT_TRUE(set
->AddAttachment(
75 new internal::PlatformFileAttachment(kFDBase
+ 1 + i
)));
78 !set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
)));
83 #if defined(OS_ANDROID)
84 #define MAYBE_SetDescriptors DISABLED_SetDescriptors
86 #define MAYBE_SetDescriptors SetDescriptors
88 TEST(MessageAttachmentSet
, MAYBE_SetDescriptors
) {
89 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
91 ASSERT_TRUE(set
->empty());
92 set
->AddDescriptorsToOwn(NULL
, 0);
93 ASSERT_TRUE(set
->empty());
95 const int fd
= GetSafeFd();
96 static const int fds
[] = {fd
};
97 set
->AddDescriptorsToOwn(fds
, 1);
98 ASSERT_TRUE(!set
->empty());
99 ASSERT_EQ(set
->size(), 1u);
103 ASSERT_TRUE(VerifyClosed(fd
));
106 TEST(MessageAttachmentSet
, PeekDescriptors
) {
107 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
109 set
->PeekDescriptors(NULL
);
111 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
)));
115 set
->PeekDescriptors(fds
);
116 ASSERT_EQ(fds
[0], kFDBase
);
118 ASSERT_TRUE(set
->empty());
121 TEST(MessageAttachmentSet
, WalkInOrder
) {
122 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
124 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
125 // used to retrieve borrowed descriptors. That never happens in production.
127 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
)));
129 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
+ 1)));
131 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
+ 2)));
133 ASSERT_EQ(set
->GetAttachmentAt(0)->TakePlatformFile(), kFDBase
);
134 ASSERT_EQ(set
->GetAttachmentAt(1)->TakePlatformFile(), kFDBase
+ 1);
135 ASSERT_EQ(set
->GetAttachmentAt(2)->TakePlatformFile(), kFDBase
+ 2);
140 TEST(MessageAttachmentSet
, WalkWrongOrder
) {
141 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
143 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
144 // used to retrieve borrowed descriptors. That never happens in production.
146 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
)));
148 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
+ 1)));
150 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
+ 2)));
152 ASSERT_EQ(set
->GetAttachmentAt(0)->TakePlatformFile(), kFDBase
);
153 ASSERT_EQ(set
->GetAttachmentAt(2), nullptr);
158 TEST(MessageAttachmentSet
, WalkCycle
) {
159 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
161 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
162 // used to retrieve borrowed descriptors. That never happens in production.
164 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
)));
166 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
+ 1)));
168 set
->AddAttachment(new internal::PlatformFileAttachment(kFDBase
+ 2)));
170 ASSERT_EQ(set
->GetAttachmentAt(0)->TakePlatformFile(), kFDBase
);
171 ASSERT_EQ(set
->GetAttachmentAt(1)->TakePlatformFile(), kFDBase
+ 1);
172 ASSERT_EQ(set
->GetAttachmentAt(2)->TakePlatformFile(), kFDBase
+ 2);
173 ASSERT_EQ(set
->GetAttachmentAt(0)->TakePlatformFile(), kFDBase
);
174 ASSERT_EQ(set
->GetAttachmentAt(1)->TakePlatformFile(), kFDBase
+ 1);
175 ASSERT_EQ(set
->GetAttachmentAt(2)->TakePlatformFile(), kFDBase
+ 2);
176 ASSERT_EQ(set
->GetAttachmentAt(0)->TakePlatformFile(), kFDBase
);
177 ASSERT_EQ(set
->GetAttachmentAt(1)->TakePlatformFile(), kFDBase
+ 1);
178 ASSERT_EQ(set
->GetAttachmentAt(2)->TakePlatformFile(), kFDBase
+ 2);
183 #if defined(OS_ANDROID)
184 #define MAYBE_DontClose DISABLED_DontClose
186 #define MAYBE_DontClose DontClose
188 TEST(MessageAttachmentSet
, MAYBE_DontClose
) {
189 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
191 const int fd
= GetSafeFd();
192 ASSERT_TRUE(set
->AddAttachment(new internal::PlatformFileAttachment(fd
)));
195 ASSERT_FALSE(VerifyClosed(fd
));
198 TEST(MessageAttachmentSet
, DoClose
) {
199 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
201 const int fd
= GetSafeFd();
202 ASSERT_TRUE(set
->AddAttachment(
203 new internal::PlatformFileAttachment(base::ScopedFD(fd
))));
206 ASSERT_TRUE(VerifyClosed(fd
));