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> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Get a safe file descriptor for test purposes. | 18 // Get a safe file descriptor for test purposes. |
19 int GetSafeFd() { | 19 int GetSafeFd() { |
20 return open("/dev/null", O_RDONLY); | 20 return open("/dev/null", O_RDONLY); |
21 } | 21 } |
22 | 22 |
23 // Returns true if fd was already closed. Closes fd if not closed. | 23 // Returns true if fd was already closed. Closes fd if not closed. |
24 bool VerifyClosed(int fd) { | 24 bool VerifyClosed(int fd) { |
25 const int duped = dup(fd); | 25 const int duped = dup(fd); |
26 if (duped != -1) { | 26 if (duped != -1) { |
27 EXPECT_NE(HANDLE_EINTR(close(duped)), -1); | 27 EXPECT_NE(IGNORE_EINTR(close(duped)), -1); |
28 EXPECT_NE(HANDLE_EINTR(close(fd)), -1); | 28 EXPECT_NE(IGNORE_EINTR(close(fd)), -1); |
29 return false; | 29 return false; |
30 } | 30 } |
31 return true; | 31 return true; |
32 } | 32 } |
33 | 33 |
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 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); | 168 scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
169 | 169 |
170 const int fd = GetSafeFd(); | 170 const int fd = GetSafeFd(); |
171 ASSERT_TRUE(set->AddAndAutoClose(fd)); | 171 ASSERT_TRUE(set->AddAndAutoClose(fd)); |
172 set->CommitAll(); | 172 set->CommitAll(); |
173 | 173 |
174 ASSERT_TRUE(VerifyClosed(fd)); | 174 ASSERT_TRUE(VerifyClosed(fd)); |
175 } | 175 } |
176 | 176 |
177 } // namespace | 177 } // namespace |
OLD | NEW |