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/file_descriptor_set_posix.h"
12 #include "base/basictypes.h"
13 #include "base/posix/eintr_wrapper.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 // Get a safe file descriptor for test purposes.
20 return open("/dev/null", O_RDONLY
);
23 // Returns true if fd was already closed. Closes fd if not closed.
24 bool VerifyClosed(int fd
) {
25 const int duped
= dup(fd
);
27 EXPECT_NE(IGNORE_EINTR(close(duped
)), -1);
28 EXPECT_NE(IGNORE_EINTR(close(fd
)), -1);
34 // The FileDescriptorSet will try and close some of the descriptor numbers
35 // which we given it. This is the base descriptor value. It's great enough such
36 // that no real descriptor will accidently be closed.
37 static const int kFDBase
= 50000;
39 TEST(FileDescriptorSet
, BasicAdd
) {
40 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
42 ASSERT_EQ(set
->size(), 0u);
43 ASSERT_TRUE(set
->empty());
44 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
45 ASSERT_EQ(set
->size(), 1u);
46 ASSERT_TRUE(!set
->empty());
48 // Empties the set and stops a warning about deleting a set with unconsumed
53 TEST(FileDescriptorSet
, BasicAddAndClose
) {
54 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
56 ASSERT_EQ(set
->size(), 0u);
57 ASSERT_TRUE(set
->empty());
58 const int fd
= GetSafeFd();
59 ASSERT_TRUE(set
->AddToOwn(base::ScopedFD(fd
)));
60 ASSERT_EQ(set
->size(), 1u);
61 ASSERT_TRUE(!set
->empty());
65 ASSERT_TRUE(VerifyClosed(fd
));
67 TEST(FileDescriptorSet
, MaxSize
) {
68 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
70 for (size_t i
= 0; i
< FileDescriptorSet::kMaxDescriptorsPerMessage
; ++i
)
71 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1 + i
));
73 ASSERT_TRUE(!set
->AddToBorrow(kFDBase
));
78 TEST(FileDescriptorSet
, SetDescriptors
) {
79 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
81 ASSERT_TRUE(set
->empty());
82 set
->AddDescriptorsToOwn(NULL
, 0);
83 ASSERT_TRUE(set
->empty());
85 const int fd
= GetSafeFd();
86 static const int fds
[] = {fd
};
87 set
->AddDescriptorsToOwn(fds
, 1);
88 ASSERT_TRUE(!set
->empty());
89 ASSERT_EQ(set
->size(), 1u);
93 ASSERT_TRUE(VerifyClosed(fd
));
96 TEST(FileDescriptorSet
, PeekDescriptors
) {
97 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
99 set
->PeekDescriptors(NULL
);
100 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
104 set
->PeekDescriptors(fds
);
105 ASSERT_EQ(fds
[0], kFDBase
);
107 ASSERT_TRUE(set
->empty());
110 TEST(FileDescriptorSet
, WalkInOrder
) {
111 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
113 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
114 // used to retrieve borrowed descriptors. That never happens in production.
115 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
116 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1));
117 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 2));
119 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
120 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
121 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
126 TEST(FileDescriptorSet
, WalkWrongOrder
) {
127 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
129 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
130 // used to retrieve borrowed descriptors. That never happens in production.
131 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
132 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1));
133 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 2));
135 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
136 ASSERT_EQ(set
->TakeDescriptorAt(2), -1);
141 TEST(FileDescriptorSet
, WalkCycle
) {
142 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
144 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
145 // used to retrieve borrowed descriptors. That never happens in production.
146 ASSERT_TRUE(set
->AddToBorrow(kFDBase
));
147 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 1));
148 ASSERT_TRUE(set
->AddToBorrow(kFDBase
+ 2));
150 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
151 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
152 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
153 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
154 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
155 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
156 ASSERT_EQ(set
->TakeDescriptorAt(0), kFDBase
);
157 ASSERT_EQ(set
->TakeDescriptorAt(1), kFDBase
+ 1);
158 ASSERT_EQ(set
->TakeDescriptorAt(2), kFDBase
+ 2);
163 TEST(FileDescriptorSet
, DontClose
) {
164 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
166 const int fd
= GetSafeFd();
167 ASSERT_TRUE(set
->AddToBorrow(fd
));
170 ASSERT_FALSE(VerifyClosed(fd
));
173 TEST(FileDescriptorSet
, DoClose
) {
174 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
176 const int fd
= GetSafeFd();
177 ASSERT_TRUE(set
->AddToOwn(base::ScopedFD(fd
)));
180 ASSERT_TRUE(VerifyClosed(fd
));