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/file_descriptor_set_posix.h" | 7 #include "ipc/file_descriptor_set_posix.h" |
8 | 8 |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
(...skipping 23 matching lines...) Expand all Loading... |
34 // The FileDescriptorSet will try and close some of the descriptor numbers | 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 | 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. | 36 // that no real descriptor will accidently be closed. |
37 static const int kFDBase = 50000; | 37 static const int kFDBase = 50000; |
38 | 38 |
39 TEST(FileDescriptorSet, BasicAdd) { | 39 TEST(FileDescriptorSet, BasicAdd) { |
40 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 40 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
41 | 41 |
42 ASSERT_EQ(set->size(), 0u); | 42 ASSERT_EQ(set->size(), 0u); |
43 ASSERT_TRUE(set->empty()); | 43 ASSERT_TRUE(set->empty()); |
44 ASSERT_TRUE(set->Add(kFDBase)); | 44 ASSERT_TRUE(set->AddToBorrow(kFDBase)); |
45 ASSERT_EQ(set->size(), 1u); | 45 ASSERT_EQ(set->size(), 1u); |
46 ASSERT_TRUE(!set->empty()); | 46 ASSERT_TRUE(!set->empty()); |
47 | 47 |
48 // Empties the set and stops a warning about deleting a set with unconsumed | 48 // Empties the set and stops a warning about deleting a set with unconsumed |
49 // descriptors | 49 // descriptors |
50 set->CommitAll(); | 50 set->CommitAll(); |
51 } | 51 } |
52 | 52 |
53 TEST(FileDescriptorSet, BasicAddAndClose) { | 53 TEST(FileDescriptorSet, BasicAddAndClose) { |
54 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 54 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
55 | 55 |
56 ASSERT_EQ(set->size(), 0u); | 56 ASSERT_EQ(set->size(), 0u); |
57 ASSERT_TRUE(set->empty()); | 57 ASSERT_TRUE(set->empty()); |
58 const int fd = GetSafeFd(); | 58 const int fd = GetSafeFd(); |
59 ASSERT_TRUE(set->AddAndAutoClose(fd)); | 59 ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd))); |
60 ASSERT_EQ(set->size(), 1u); | 60 ASSERT_EQ(set->size(), 1u); |
61 ASSERT_TRUE(!set->empty()); | 61 ASSERT_TRUE(!set->empty()); |
62 | 62 |
63 set->CommitAll(); | 63 set->CommitAll(); |
64 | 64 |
65 ASSERT_TRUE(VerifyClosed(fd)); | 65 ASSERT_TRUE(VerifyClosed(fd)); |
66 } | 66 } |
67 TEST(FileDescriptorSet, MaxSize) { | 67 TEST(FileDescriptorSet, MaxSize) { |
68 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 68 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
69 | 69 |
70 for (size_t i = 0; i < FileDescriptorSet::kMaxDescriptorsPerMessage; ++i) | 70 for (size_t i = 0; i < FileDescriptorSet::kMaxDescriptorsPerMessage; ++i) |
71 ASSERT_TRUE(set->Add(kFDBase + 1 + i)); | 71 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1 + i)); |
72 | 72 |
73 ASSERT_TRUE(!set->Add(kFDBase)); | 73 ASSERT_TRUE(!set->AddToBorrow(kFDBase)); |
74 | 74 |
75 set->CommitAll(); | 75 set->CommitAll(); |
76 } | 76 } |
77 | 77 |
78 TEST(FileDescriptorSet, SetDescriptors) { | 78 TEST(FileDescriptorSet, SetDescriptors) { |
79 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 79 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
80 | 80 |
81 ASSERT_TRUE(set->empty()); | 81 ASSERT_TRUE(set->empty()); |
82 set->SetDescriptors(NULL, 0); | 82 set->AddDescriptorsToOwn(NULL, 0); |
83 ASSERT_TRUE(set->empty()); | 83 ASSERT_TRUE(set->empty()); |
84 | 84 |
85 const int fd = GetSafeFd(); | 85 const int fd = GetSafeFd(); |
86 static const int fds[] = {fd}; | 86 static const int fds[] = {fd}; |
87 set->SetDescriptors(fds, 1); | 87 set->AddDescriptorsToOwn(fds, 1); |
88 ASSERT_TRUE(!set->empty()); | 88 ASSERT_TRUE(!set->empty()); |
89 ASSERT_EQ(set->size(), 1u); | 89 ASSERT_EQ(set->size(), 1u); |
90 | 90 |
91 set->CommitAll(); | 91 set->CommitAll(); |
92 | 92 |
93 ASSERT_TRUE(VerifyClosed(fd)); | 93 ASSERT_TRUE(VerifyClosed(fd)); |
94 } | 94 } |
95 | 95 |
96 TEST(FileDescriptorSet, GetDescriptors) { | 96 TEST(FileDescriptorSet, PeekDescriptors) { |
97 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 97 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
98 | 98 |
99 set->GetDescriptors(NULL); | 99 set->PeekDescriptors(NULL); |
100 ASSERT_TRUE(set->Add(kFDBase)); | 100 ASSERT_TRUE(set->AddToBorrow(kFDBase)); |
101 | 101 |
102 int fds[1]; | 102 int fds[1]; |
103 fds[0] = 0; | 103 fds[0] = 0; |
104 set->GetDescriptors(fds); | 104 set->PeekDescriptors(fds); |
105 ASSERT_EQ(fds[0], kFDBase); | 105 ASSERT_EQ(fds[0], kFDBase); |
106 set->CommitAll(); | 106 set->CommitAll(); |
107 ASSERT_TRUE(set->empty()); | 107 ASSERT_TRUE(set->empty()); |
108 } | 108 } |
109 | 109 |
110 TEST(FileDescriptorSet, WalkInOrder) { | 110 TEST(FileDescriptorSet, WalkInOrder) { |
111 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 111 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
112 | 112 |
113 ASSERT_TRUE(set->Add(kFDBase)); | 113 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
114 ASSERT_TRUE(set->Add(kFDBase + 1)); | 114 // used to retrieve borrowed descriptors. That never happens in production. |
115 ASSERT_TRUE(set->Add(kFDBase + 2)); | 115 ASSERT_TRUE(set->AddToBorrow(kFDBase)); |
| 116 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1)); |
| 117 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2)); |
116 | 118 |
117 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); | 119 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); |
118 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); | 120 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); |
119 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); | 121 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); |
120 | 122 |
121 set->CommitAll(); | 123 set->CommitAll(); |
122 } | 124 } |
123 | 125 |
124 TEST(FileDescriptorSet, WalkWrongOrder) { | 126 TEST(FileDescriptorSet, WalkWrongOrder) { |
125 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 127 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
126 | 128 |
127 ASSERT_TRUE(set->Add(kFDBase)); | 129 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
128 ASSERT_TRUE(set->Add(kFDBase + 1)); | 130 // used to retrieve borrowed descriptors. That never happens in production. |
129 ASSERT_TRUE(set->Add(kFDBase + 2)); | 131 ASSERT_TRUE(set->AddToBorrow(kFDBase)); |
| 132 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1)); |
| 133 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2)); |
130 | 134 |
131 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); | 135 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); |
132 ASSERT_EQ(set->GetDescriptorAt(2), -1); | 136 ASSERT_EQ(set->TakeDescriptorAt(2), -1); |
133 | 137 |
134 set->CommitAll(); | 138 set->CommitAll(); |
135 } | 139 } |
136 | 140 |
137 TEST(FileDescriptorSet, WalkCycle) { | 141 TEST(FileDescriptorSet, WalkCycle) { |
138 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 142 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
139 | 143 |
140 ASSERT_TRUE(set->Add(kFDBase)); | 144 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
141 ASSERT_TRUE(set->Add(kFDBase + 1)); | 145 // used to retrieve borrowed descriptors. That never happens in production. |
142 ASSERT_TRUE(set->Add(kFDBase + 2)); | 146 ASSERT_TRUE(set->AddToBorrow(kFDBase)); |
| 147 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1)); |
| 148 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2)); |
143 | 149 |
144 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); | 150 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); |
145 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); | 151 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); |
146 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); | 152 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); |
147 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); | 153 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); |
148 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); | 154 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); |
149 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); | 155 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); |
150 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); | 156 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); |
151 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); | 157 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); |
152 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); | 158 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); |
153 | 159 |
154 set->CommitAll(); | 160 set->CommitAll(); |
155 } | 161 } |
156 | 162 |
157 TEST(FileDescriptorSet, DontClose) { | 163 TEST(FileDescriptorSet, DontClose) { |
158 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 164 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
159 | 165 |
160 const int fd = GetSafeFd(); | 166 const int fd = GetSafeFd(); |
161 ASSERT_TRUE(set->Add(fd)); | 167 ASSERT_TRUE(set->AddToBorrow(fd)); |
162 set->CommitAll(); | 168 set->CommitAll(); |
163 | 169 |
164 ASSERT_FALSE(VerifyClosed(fd)); | 170 ASSERT_FALSE(VerifyClosed(fd)); |
165 } | 171 } |
166 | 172 |
167 TEST(FileDescriptorSet, DoClose) { | 173 TEST(FileDescriptorSet, DoClose) { |
168 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 174 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
169 | 175 |
170 const int fd = GetSafeFd(); | 176 const int fd = GetSafeFd(); |
171 ASSERT_TRUE(set->AddAndAutoClose(fd)); | 177 ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd))); |
172 set->CommitAll(); | 178 set->CommitAll(); |
173 | 179 |
174 ASSERT_TRUE(VerifyClosed(fd)); | 180 ASSERT_TRUE(VerifyClosed(fd)); |
175 } | 181 } |
176 | 182 |
177 } // namespace | 183 } // namespace |
OLD | NEW |