[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 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. |
| 4 | |
| 5 | // This test is POSIX only. |
| 6 | |
[email protected] | 89a104d | 2009-02-13 02:40:46 | [diff] [blame] | 7 | #include <unistd.h> |
[email protected] | 56dacae | 2009-02-13 02:45:48 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | 89a104d | 2009-02-13 02:40:46 | [diff] [blame] | 9 | |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 11 | #include "base/eintr_wrapper.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 12 | #include "ipc/file_descriptor_set_posix.h" |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // Get a safe file descriptor for test purposes. |
| 18 | int GetSafeFd() { |
| 19 | return open("/dev/null", O_RDONLY); |
| 20 | } |
| 21 | |
| 22 | // Returns true if fd was already closed. Closes fd if not closed. |
| 23 | bool VerifyClosed(int fd) { |
| 24 | const int duped = dup(fd); |
| 25 | if (duped != -1) { |
[email protected] | a9c836f | 2010-09-17 14:22:00 | [diff] [blame] | 26 | EXPECT_NE(HANDLE_EINTR(close(duped)), -1); |
| 27 | EXPECT_NE(HANDLE_EINTR(close(fd)), -1); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 28 | return false; |
| 29 | } |
| 30 | return true; |
| 31 | } |
| 32 | |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 33 | // The FileDescriptorSet will try and close some of the descriptor numbers |
| 34 | // which we given it. This is the base descriptor value. It's great enough such |
| 35 | // that no real descriptor will accidently be closed. |
| 36 | static const int kFDBase = 50000; |
| 37 | |
| 38 | TEST(FileDescriptorSet, BasicAdd) { |
| 39 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 40 | |
| 41 | ASSERT_EQ(set->size(), 0u); |
| 42 | ASSERT_TRUE(set->empty()); |
| 43 | ASSERT_TRUE(set->Add(kFDBase)); |
| 44 | ASSERT_EQ(set->size(), 1u); |
| 45 | ASSERT_TRUE(!set->empty()); |
| 46 | |
| 47 | // Empties the set and stops a warning about deleting a set with unconsumed |
| 48 | // descriptors |
| 49 | set->CommitAll(); |
| 50 | } |
| 51 | |
| 52 | TEST(FileDescriptorSet, BasicAddAndClose) { |
| 53 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 54 | |
| 55 | ASSERT_EQ(set->size(), 0u); |
| 56 | ASSERT_TRUE(set->empty()); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 57 | const int fd = GetSafeFd(); |
| 58 | ASSERT_TRUE(set->AddAndAutoClose(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 59 | ASSERT_EQ(set->size(), 1u); |
| 60 | ASSERT_TRUE(!set->empty()); |
| 61 | |
| 62 | set->CommitAll(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 63 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 64 | ASSERT_TRUE(VerifyClosed(fd)); |
| 65 | } |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 66 | TEST(FileDescriptorSet, MaxSize) { |
| 67 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 68 | |
| 69 | for (unsigned i = 0; |
| 70 | i < FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE; ++i) { |
| 71 | ASSERT_TRUE(set->Add(kFDBase + 1 + i)); |
| 72 | } |
| 73 | |
| 74 | ASSERT_TRUE(!set->Add(kFDBase)); |
| 75 | |
| 76 | set->CommitAll(); |
| 77 | } |
| 78 | |
| 79 | TEST(FileDescriptorSet, SetDescriptors) { |
| 80 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 81 | |
| 82 | ASSERT_TRUE(set->empty()); |
| 83 | set->SetDescriptors(NULL, 0); |
| 84 | ASSERT_TRUE(set->empty()); |
| 85 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 86 | const int fd = GetSafeFd(); |
| 87 | static const int fds[] = {fd}; |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 88 | set->SetDescriptors(fds, 1); |
| 89 | ASSERT_TRUE(!set->empty()); |
| 90 | ASSERT_EQ(set->size(), 1u); |
| 91 | |
| 92 | set->CommitAll(); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 93 | |
| 94 | ASSERT_TRUE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST(FileDescriptorSet, GetDescriptors) { |
| 98 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 99 | |
| 100 | set->GetDescriptors(NULL); |
| 101 | ASSERT_TRUE(set->Add(kFDBase)); |
| 102 | |
| 103 | int fds[1]; |
| 104 | fds[0] = 0; |
| 105 | set->GetDescriptors(fds); |
| 106 | ASSERT_EQ(fds[0], kFDBase); |
| 107 | set->CommitAll(); |
| 108 | ASSERT_TRUE(set->empty()); |
| 109 | } |
| 110 | |
| 111 | TEST(FileDescriptorSet, WalkInOrder) { |
| 112 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 113 | |
| 114 | ASSERT_TRUE(set->Add(kFDBase)); |
| 115 | ASSERT_TRUE(set->Add(kFDBase + 1)); |
| 116 | ASSERT_TRUE(set->Add(kFDBase + 2)); |
| 117 | |
| 118 | ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); |
| 119 | ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); |
| 120 | ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); |
| 121 | |
| 122 | set->CommitAll(); |
| 123 | } |
| 124 | |
| 125 | TEST(FileDescriptorSet, WalkWrongOrder) { |
| 126 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 127 | |
| 128 | ASSERT_TRUE(set->Add(kFDBase)); |
| 129 | ASSERT_TRUE(set->Add(kFDBase + 1)); |
| 130 | ASSERT_TRUE(set->Add(kFDBase + 2)); |
| 131 | |
| 132 | ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); |
| 133 | ASSERT_EQ(set->GetDescriptorAt(2), -1); |
| 134 | |
| 135 | set->CommitAll(); |
| 136 | } |
| 137 | |
| 138 | TEST(FileDescriptorSet, WalkCycle) { |
| 139 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 140 | |
| 141 | ASSERT_TRUE(set->Add(kFDBase)); |
| 142 | ASSERT_TRUE(set->Add(kFDBase + 1)); |
| 143 | ASSERT_TRUE(set->Add(kFDBase + 2)); |
| 144 | |
| 145 | ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); |
| 146 | ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); |
| 147 | ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); |
| 148 | ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); |
| 149 | ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); |
| 150 | ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); |
| 151 | ASSERT_EQ(set->GetDescriptorAt(0), kFDBase); |
| 152 | ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1); |
| 153 | ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2); |
| 154 | |
| 155 | set->CommitAll(); |
| 156 | } |
| 157 | |
| 158 | TEST(FileDescriptorSet, DontClose) { |
| 159 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 160 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 161 | const int fd = GetSafeFd(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 162 | ASSERT_TRUE(set->Add(fd)); |
| 163 | set->CommitAll(); |
| 164 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 165 | ASSERT_FALSE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST(FileDescriptorSet, DoClose) { |
| 169 | scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet; |
| 170 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 171 | const int fd = GetSafeFd(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 172 | ASSERT_TRUE(set->AddAndAutoClose(fd)); |
| 173 | set->CommitAll(); |
| 174 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 175 | ASSERT_TRUE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 176 | } |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 177 | |
| 178 | } // namespace |