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/basictypes.h"
13 #include "base/posix/eintr_wrapper.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());
45 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
46 ASSERT_EQ(set
->size(), 1u);
47 ASSERT_TRUE(!set
->empty());
49 // Empties the set and stops a warning about deleting a set with unconsumed
54 TEST(MessageAttachmentSet
, BasicAddAndClose
) {
55 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
57 ASSERT_EQ(set
->size(), 0u);
58 ASSERT_TRUE(set
->empty());
59 const int fd
= GetSafeFd();
60 ASSERT_TRUE(set
->AddToOwn(base::ScopedFD(fd
)));
61 ASSERT_EQ(set
->size(), 1u);
62 ASSERT_TRUE(!set
->empty());
66 ASSERT_TRUE(VerifyClosed(fd
));
68 TEST(MessageAttachmentSet
, MaxSize
) {
69 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
71 for (size_t i
= 0; i
< MessageAttachmentSet::kMaxDescriptorsPerMessage
; ++i
)
72 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1 + i
));
74 ASSERT_TRUE(!set
->AddToBorrow(kFDBase
));
79 TEST(MessageAttachmentSet
, SetDescriptors
) {
80 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
82 ASSERT_TRUE(set
->empty());
83 set
->AddDescriptorsToOwn(NULL
, 0);
84 ASSERT_TRUE(set
->empty());
86 const int fd
= GetSafeFd();
87 static const int fds
[] = {fd
};
88 set
->AddDescriptorsToOwn(fds
, 1);
89 ASSERT_TRUE(!set
->empty());
90 ASSERT_EQ(set
->size(), 1u);
94 ASSERT_TRUE(VerifyClosed(fd
));
97 TEST(MessageAttachmentSet
, PeekDescriptors
) {
98 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
100 set
->PeekDescriptors(NULL
);
101 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
105 set
->PeekDescriptors(fds
);
106 ASSERT_EQ(fds
[0], kFDBase
);
108 ASSERT_TRUE(set
->empty());
111 TEST(MessageAttachmentSet
, WalkInOrder
) {
112 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
114 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
115 // used to retrieve borrowed descriptors. That never happens in production.
116 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
117 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1));
118 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 2));
120 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
121 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
122 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
127 TEST(MessageAttachmentSet
, WalkWrongOrder
) {
128 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
130 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
131 // used to retrieve borrowed descriptors. That never happens in production.
132 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
133 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1));
134 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 2));
136 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
137 ASSERT_EQ(set
->TakeDescriptorAt(2), -1);
142 TEST(MessageAttachmentSet
, WalkCycle
) {
143 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
145 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
146 // used to retrieve borrowed descriptors. That never happens in production.
147 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
148 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1));
149 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 2));
151 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
152 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
153 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
154 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
155 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
156 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
157 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
158 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
159 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
164 TEST(MessageAttachmentSet
, DontClose
) {
165 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
167 const int fd
= GetSafeFd();
168 ASSERT_TRUE(set
->AddToBorrow(fd
));
171 ASSERT_FALSE(VerifyClosed(fd
));
174 TEST(MessageAttachmentSet
, DoClose
) {
175 scoped_refptr
<MessageAttachmentSet
> set(new MessageAttachmentSet
);
177 const int fd
= GetSafeFd();
178 ASSERT_TRUE(set
->AddToOwn(base::ScopedFD(fd
)));
181 ASSERT_TRUE(VerifyClosed(fd
));