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(HANDLE_EINTR(close(duped
)), -1);
28 EXPECT_NE(HANDLE_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
->Add(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
->AddAndAutoClose(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
->Add(kFDBase
+ 1 + i
));
73 ASSERT_TRUE(!set
->Add(kFDBase
));
78 TEST(FileDescriptorSet
, SetDescriptors
) {
79 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
81 ASSERT_TRUE(set
->empty());
82 set
->SetDescriptors(NULL
, 0);
83 ASSERT_TRUE(set
->empty());
85 const int fd
= GetSafeFd();
86 static const int fds
[] = {fd
};
87 set
->SetDescriptors(fds
, 1);
88 ASSERT_TRUE(!set
->empty());
89 ASSERT_EQ(set
->size(), 1u);
93 ASSERT_TRUE(VerifyClosed(fd
));
96 TEST(FileDescriptorSet
, GetDescriptors
) {
97 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
99 set
->GetDescriptors(NULL
);
100 ASSERT_TRUE(set
->Add(kFDBase
));
104 set
->GetDescriptors(fds
);
105 ASSERT_EQ(fds
[0], kFDBase
);
107 ASSERT_TRUE(set
->empty());
110 TEST(FileDescriptorSet
, WalkInOrder
) {
111 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
113 ASSERT_TRUE(set
->Add(kFDBase
));
114 ASSERT_TRUE(set
->Add(kFDBase
+ 1));
115 ASSERT_TRUE(set
->Add(kFDBase
+ 2));
117 ASSERT_EQ(set
->GetDescriptorAt(0), kFDBase
);
118 ASSERT_EQ(set
->GetDescriptorAt(1), kFDBase
+ 1);
119 ASSERT_EQ(set
->GetDescriptorAt(2), kFDBase
+ 2);
124 TEST(FileDescriptorSet
, WalkWrongOrder
) {
125 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
127 ASSERT_TRUE(set
->Add(kFDBase
));
128 ASSERT_TRUE(set
->Add(kFDBase
+ 1));
129 ASSERT_TRUE(set
->Add(kFDBase
+ 2));
131 ASSERT_EQ(set
->GetDescriptorAt(0), kFDBase
);
132 ASSERT_EQ(set
->GetDescriptorAt(2), -1);
137 TEST(FileDescriptorSet
, WalkCycle
) {
138 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
140 ASSERT_TRUE(set
->Add(kFDBase
));
141 ASSERT_TRUE(set
->Add(kFDBase
+ 1));
142 ASSERT_TRUE(set
->Add(kFDBase
+ 2));
144 ASSERT_EQ(set
->GetDescriptorAt(0), kFDBase
);
145 ASSERT_EQ(set
->GetDescriptorAt(1), kFDBase
+ 1);
146 ASSERT_EQ(set
->GetDescriptorAt(2), kFDBase
+ 2);
147 ASSERT_EQ(set
->GetDescriptorAt(0), kFDBase
);
148 ASSERT_EQ(set
->GetDescriptorAt(1), kFDBase
+ 1);
149 ASSERT_EQ(set
->GetDescriptorAt(2), kFDBase
+ 2);
150 ASSERT_EQ(set
->GetDescriptorAt(0), kFDBase
);
151 ASSERT_EQ(set
->GetDescriptorAt(1), kFDBase
+ 1);
152 ASSERT_EQ(set
->GetDescriptorAt(2), kFDBase
+ 2);
157 TEST(FileDescriptorSet
, DontClose
) {
158 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
160 const int fd
= GetSafeFd();
161 ASSERT_TRUE(set
->Add(fd
));
164 ASSERT_FALSE(VerifyClosed(fd
));
167 TEST(FileDescriptorSet
, DoClose
) {
168 scoped_refptr
<FileDescriptorSet
> set(new FileDescriptorSet
);
170 const int fd
= GetSafeFd();
171 ASSERT_TRUE(set
->AddAndAutoClose(fd
));
174 ASSERT_TRUE(VerifyClosed(fd
));