[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 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 7 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | 22b42c59 | 2010-12-20 06:59:23 | [diff] [blame] | 8 | |
[email protected] | 56dacae | 2009-02-13 02:45:48 | [diff] [blame] | 9 | #include <fcntl.h> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 10 | #include <stddef.h> |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 11 | #include <unistd.h> |
[email protected] | 89a104d | 2009-02-13 02:40:46 | [diff] [blame] | 12 | |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 13 | #include "base/posix/eintr_wrapper.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 14 | #include "build/build_config.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 15 | #include "ipc/ipc_platform_file_attachment_posix.h" |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 18 | namespace IPC { |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | // Get a safe file descriptor for test purposes. |
| 22 | int GetSafeFd() { |
| 23 | return open("/dev/null", O_RDONLY); |
| 24 | } |
| 25 | |
| 26 | // Returns true if fd was already closed. Closes fd if not closed. |
| 27 | bool VerifyClosed(int fd) { |
| 28 | const int duped = dup(fd); |
| 29 | if (duped != -1) { |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 30 | EXPECT_NE(IGNORE_EINTR(close(duped)), -1); |
| 31 | EXPECT_NE(IGNORE_EINTR(close(fd)), -1); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 37 | // The MessageAttachmentSet will try and close some of the descriptor numbers |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 38 | // which we given it. This is the base descriptor value. It's great enough such |
| 39 | // that no real descriptor will accidently be closed. |
| 40 | static const int kFDBase = 50000; |
| 41 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 42 | TEST(MessageAttachmentSet, BasicAdd) { |
| 43 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 44 | |
| 45 | ASSERT_EQ(set->size(), 0u); |
| 46 | ASSERT_TRUE(set->empty()); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 47 | ASSERT_TRUE( |
| 48 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 49 | ASSERT_EQ(set->size(), 1u); |
| 50 | ASSERT_TRUE(!set->empty()); |
| 51 | |
| 52 | // Empties the set and stops a warning about deleting a set with unconsumed |
| 53 | // descriptors |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 54 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 55 | } |
| 56 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 57 | TEST(MessageAttachmentSet, BasicAddAndClose) { |
| 58 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 59 | |
| 60 | ASSERT_EQ(set->size(), 0u); |
| 61 | ASSERT_TRUE(set->empty()); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 62 | const int fd = GetSafeFd(); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 63 | ASSERT_TRUE(set->AddAttachment( |
| 64 | new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 65 | ASSERT_EQ(set->size(), 1u); |
| 66 | ASSERT_TRUE(!set->empty()); |
| 67 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 68 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 69 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 70 | ASSERT_TRUE(VerifyClosed(fd)); |
| 71 | } |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 72 | TEST(MessageAttachmentSet, MaxSize) { |
| 73 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 74 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 75 | for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i) |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 76 | ASSERT_TRUE(set->AddAttachment( |
| 77 | new internal::PlatformFileAttachment(kFDBase + 1 + i))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 78 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 79 | ASSERT_TRUE( |
| 80 | !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 81 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 82 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 83 | } |
| 84 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame] | 85 | #if defined(OS_ANDROID) |
| 86 | #define MAYBE_SetDescriptors DISABLED_SetDescriptors |
| 87 | #else |
| 88 | #define MAYBE_SetDescriptors SetDescriptors |
| 89 | #endif |
| 90 | TEST(MessageAttachmentSet, MAYBE_SetDescriptors) { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 91 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 92 | |
| 93 | ASSERT_TRUE(set->empty()); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 94 | set->AddDescriptorsToOwn(NULL, 0); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 95 | ASSERT_TRUE(set->empty()); |
| 96 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 97 | const int fd = GetSafeFd(); |
| 98 | static const int fds[] = {fd}; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 99 | set->AddDescriptorsToOwn(fds, 1); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 100 | ASSERT_TRUE(!set->empty()); |
| 101 | ASSERT_EQ(set->size(), 1u); |
| 102 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 103 | set->CommitAllDescriptors(); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 104 | |
| 105 | ASSERT_TRUE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 106 | } |
| 107 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 108 | TEST(MessageAttachmentSet, PeekDescriptors) { |
| 109 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 110 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 111 | set->PeekDescriptors(NULL); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 112 | ASSERT_TRUE( |
| 113 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 114 | |
| 115 | int fds[1]; |
| 116 | fds[0] = 0; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 117 | set->PeekDescriptors(fds); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 118 | ASSERT_EQ(fds[0], kFDBase); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 119 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 120 | ASSERT_TRUE(set->empty()); |
| 121 | } |
| 122 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 123 | TEST(MessageAttachmentSet, WalkInOrder) { |
| 124 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 125 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 126 | // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
| 127 | // used to retrieve borrowed descriptors. That never happens in production. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 128 | ASSERT_TRUE( |
| 129 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
| 130 | ASSERT_TRUE( |
| 131 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 132 | ASSERT_TRUE( |
| 133 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 134 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 135 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); |
| 136 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), |
| 137 | kFDBase + 1); |
| 138 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), |
| 139 | kFDBase + 2); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 140 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 141 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 142 | } |
| 143 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 144 | TEST(MessageAttachmentSet, WalkWrongOrder) { |
| 145 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 146 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 147 | // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
| 148 | // used to retrieve borrowed descriptors. That never happens in production. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 149 | ASSERT_TRUE( |
| 150 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
| 151 | ASSERT_TRUE( |
| 152 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 153 | ASSERT_TRUE( |
| 154 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 155 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 156 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); |
scheib | 4dac7f0 | 2016-05-12 00:55:03 | [diff] [blame] | 157 | ASSERT_FALSE(set->GetNonBrokerableAttachmentAt(2)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 158 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 159 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 160 | } |
| 161 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 162 | TEST(MessageAttachmentSet, WalkCycle) { |
| 163 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 164 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 165 | // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
| 166 | // used to retrieve borrowed descriptors. That never happens in production. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 167 | ASSERT_TRUE( |
| 168 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
| 169 | ASSERT_TRUE( |
| 170 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 171 | ASSERT_TRUE( |
| 172 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 173 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 174 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); |
| 175 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), |
| 176 | kFDBase + 1); |
| 177 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), |
| 178 | kFDBase + 2); |
| 179 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); |
| 180 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), |
| 181 | kFDBase + 1); |
| 182 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), |
| 183 | kFDBase + 2); |
| 184 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(0)->TakePlatformFile(), kFDBase); |
| 185 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(1)->TakePlatformFile(), |
| 186 | kFDBase + 1); |
| 187 | ASSERT_EQ(set->GetNonBrokerableAttachmentAt(2)->TakePlatformFile(), |
| 188 | kFDBase + 2); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 189 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 190 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 191 | } |
| 192 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame] | 193 | #if defined(OS_ANDROID) |
| 194 | #define MAYBE_DontClose DISABLED_DontClose |
| 195 | #else |
| 196 | #define MAYBE_DontClose DontClose |
| 197 | #endif |
| 198 | TEST(MessageAttachmentSet, MAYBE_DontClose) { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 199 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 200 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 201 | const int fd = GetSafeFd(); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 202 | ASSERT_TRUE(set->AddAttachment(new internal::PlatformFileAttachment(fd))); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 203 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 204 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 205 | ASSERT_FALSE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 206 | } |
| 207 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 208 | TEST(MessageAttachmentSet, DoClose) { |
| 209 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 210 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 211 | const int fd = GetSafeFd(); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 212 | ASSERT_TRUE(set->AddAttachment( |
| 213 | new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 214 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 215 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 216 | ASSERT_TRUE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 217 | } |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 218 | |
| 219 | } // namespace |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 220 | } // namespace IPC |