[email protected] | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This test is POSIX only. |
| 6 | |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 7 | #include "ipc/file_descriptor_set_posix.h" |
| 8 | |
[email protected] | 89a104d | 2009-02-13 02:40:46 | [diff] [blame] | 9 | #include <unistd.h> |
[email protected] | 56dacae | 2009-02-13 02:45:48 | [diff] [blame] | 10 | #include <fcntl.h> |
[email protected] | 89a104d | 2009-02-13 02:40:46 | [diff] [blame] | 11 | |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 12 | #include "base/basictypes.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 13 | #include "base/posix/eintr_wrapper.h" |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // Get a safe file descriptor for test purposes. |
| 19 | int GetSafeFd() { |
| 20 | return open("/dev/null", O_RDONLY); |
| 21 | } |
| 22 | |
| 23 | // Returns true if fd was already closed. Closes fd if not closed. |
| 24 | bool VerifyClosed(int fd) { |
| 25 | const int duped = dup(fd); |
| 26 | if (duped != -1) { |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 27 | EXPECT_NE(IGNORE_EINTR(close(duped)), -1); |
| 28 | EXPECT_NE(IGNORE_EINTR(close(fd)), -1); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 29 | return false; |
| 30 | } |
| 31 | return true; |
| 32 | } |
| 33 | |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 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; |
| 38 | |
| 39 | TEST(FileDescriptorSet, BasicAdd) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 40 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 41 | |
| 42 | ASSERT_EQ(set->size(), 0u); |
| 43 | ASSERT_TRUE(set->empty()); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 44 | ASSERT_TRUE(set->AddToBorrow(kFDBase)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 45 | ASSERT_EQ(set->size(), 1u); |
| 46 | ASSERT_TRUE(!set->empty()); |
| 47 | |
| 48 | // Empties the set and stops a warning about deleting a set with unconsumed |
| 49 | // descriptors |
| 50 | set->CommitAll(); |
| 51 | } |
| 52 | |
| 53 | TEST(FileDescriptorSet, BasicAddAndClose) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 54 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 55 | |
| 56 | ASSERT_EQ(set->size(), 0u); |
| 57 | ASSERT_TRUE(set->empty()); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 58 | const int fd = GetSafeFd(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 59 | ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 60 | ASSERT_EQ(set->size(), 1u); |
| 61 | ASSERT_TRUE(!set->empty()); |
| 62 | |
| 63 | set->CommitAll(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 64 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 65 | ASSERT_TRUE(VerifyClosed(fd)); |
| 66 | } |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 67 | TEST(FileDescriptorSet, MaxSize) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 68 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 69 | |
[email protected] | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 70 | for (size_t i = 0; i < FileDescriptorSet::kMaxDescriptorsPerMessage; ++i) |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 71 | ASSERT_TRUE(set->AddToBorrow(kFDBase + 1 + i)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 72 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 73 | ASSERT_TRUE(!set->AddToBorrow(kFDBase)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 74 | |
| 75 | set->CommitAll(); |
| 76 | } |
| 77 | |
| 78 | TEST(FileDescriptorSet, SetDescriptors) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 79 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 80 | |
| 81 | ASSERT_TRUE(set->empty()); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 82 | set->AddDescriptorsToOwn(NULL, 0); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 83 | ASSERT_TRUE(set->empty()); |
| 84 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 85 | const int fd = GetSafeFd(); |
| 86 | static const int fds[] = {fd}; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 87 | set->AddDescriptorsToOwn(fds, 1); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 88 | ASSERT_TRUE(!set->empty()); |
| 89 | ASSERT_EQ(set->size(), 1u); |
| 90 | |
| 91 | set->CommitAll(); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 92 | |
| 93 | ASSERT_TRUE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 94 | } |
| 95 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 96 | TEST(FileDescriptorSet, PeekDescriptors) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 97 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 98 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 99 | set->PeekDescriptors(NULL); |
| 100 | ASSERT_TRUE(set->AddToBorrow(kFDBase)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 101 | |
| 102 | int fds[1]; |
| 103 | fds[0] = 0; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 104 | set->PeekDescriptors(fds); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 105 | ASSERT_EQ(fds[0], kFDBase); |
| 106 | set->CommitAll(); |
| 107 | ASSERT_TRUE(set->empty()); |
| 108 | } |
| 109 | |
| 110 | TEST(FileDescriptorSet, WalkInOrder) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 111 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 112 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 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)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 118 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 119 | ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); |
| 120 | ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1); |
| 121 | ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 122 | |
| 123 | set->CommitAll(); |
| 124 | } |
| 125 | |
| 126 | TEST(FileDescriptorSet, WalkWrongOrder) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 127 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 128 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 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)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 134 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 135 | ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase); |
| 136 | ASSERT_EQ(set->TakeDescriptorAt(2), -1); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 137 | |
| 138 | set->CommitAll(); |
| 139 | } |
| 140 | |
| 141 | TEST(FileDescriptorSet, WalkCycle) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 142 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 143 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 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)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 149 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 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); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 159 | |
| 160 | set->CommitAll(); |
| 161 | } |
| 162 | |
| 163 | TEST(FileDescriptorSet, DontClose) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 164 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 165 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 166 | const int fd = GetSafeFd(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 167 | ASSERT_TRUE(set->AddToBorrow(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 168 | set->CommitAll(); |
| 169 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 170 | ASSERT_FALSE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | TEST(FileDescriptorSet, DoClose) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 174 | scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 175 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 176 | const int fd = GetSafeFd(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame^] | 177 | ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 178 | set->CommitAll(); |
| 179 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 180 | ASSERT_TRUE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 181 | } |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 182 | |
| 183 | } // namespace |