[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] | 22b42c5 | 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 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 37 | int GetFdAt(MessageAttachmentSet* set, int id) { |
| 38 | return static_cast<internal::PlatformFileAttachment&>( |
| 39 | *set->GetAttachmentAt(id)) |
| 40 | .TakePlatformFile(); |
| 41 | } |
| 42 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 43 | // The MessageAttachmentSet will try and close some of the descriptor numbers |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 44 | // which we given it. This is the base descriptor value. It's great enough such |
| 45 | // that no real descriptor will accidently be closed. |
| 46 | static const int kFDBase = 50000; |
| 47 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 48 | TEST(MessageAttachmentSet, BasicAdd) { |
| 49 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 50 | |
| 51 | ASSERT_EQ(set->size(), 0u); |
| 52 | ASSERT_TRUE(set->empty()); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 53 | ASSERT_TRUE( |
| 54 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 55 | ASSERT_EQ(set->size(), 1u); |
| 56 | ASSERT_TRUE(!set->empty()); |
| 57 | |
| 58 | // Empties the set and stops a warning about deleting a set with unconsumed |
| 59 | // descriptors |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 60 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 61 | } |
| 62 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 63 | TEST(MessageAttachmentSet, BasicAddAndClose) { |
| 64 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 65 | |
| 66 | ASSERT_EQ(set->size(), 0u); |
| 67 | ASSERT_TRUE(set->empty()); |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 68 | const int fd = GetSafeFd(); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 69 | ASSERT_TRUE(set->AddAttachment( |
| 70 | new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 71 | ASSERT_EQ(set->size(), 1u); |
| 72 | ASSERT_TRUE(!set->empty()); |
| 73 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 74 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 75 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 76 | ASSERT_TRUE(VerifyClosed(fd)); |
| 77 | } |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 78 | TEST(MessageAttachmentSet, MaxSize) { |
| 79 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 80 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 81 | for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i) |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 82 | ASSERT_TRUE(set->AddAttachment( |
| 83 | new internal::PlatformFileAttachment(kFDBase + 1 + i))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 84 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 85 | ASSERT_TRUE( |
| 86 | !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 87 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 88 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 89 | } |
| 90 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 91 | TEST(MessageAttachmentSet, WalkInOrder) { |
| 92 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 93 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 94 | // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
| 95 | // used to retrieve borrowed descriptors. That never happens in production. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 96 | ASSERT_TRUE( |
| 97 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
| 98 | ASSERT_TRUE( |
| 99 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 100 | ASSERT_TRUE( |
| 101 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 102 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 103 | ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
| 104 | ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
| 105 | ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 106 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 107 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 108 | } |
| 109 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 110 | TEST(MessageAttachmentSet, WalkWrongOrder) { |
| 111 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[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. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 115 | ASSERT_TRUE( |
| 116 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
| 117 | ASSERT_TRUE( |
| 118 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 119 | ASSERT_TRUE( |
| 120 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 121 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 122 | ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
| 123 | ASSERT_FALSE(set->GetAttachmentAt(2)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 124 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 125 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 126 | } |
| 127 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 128 | TEST(MessageAttachmentSet, WalkCycle) { |
| 129 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 130 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 131 | // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be |
| 132 | // used to retrieve borrowed descriptors. That never happens in production. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 133 | ASSERT_TRUE( |
| 134 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
| 135 | ASSERT_TRUE( |
| 136 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1))); |
| 137 | ASSERT_TRUE( |
| 138 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2))); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 139 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 140 | ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
| 141 | ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
| 142 | ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
| 143 | ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
| 144 | ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
| 145 | ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
| 146 | ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
| 147 | ASSERT_EQ(GetFdAt(set.get(), 1), kFDBase + 1); |
| 148 | ASSERT_EQ(GetFdAt(set.get(), 2), kFDBase + 2); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 149 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 150 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 151 | } |
| 152 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame] | 153 | #if defined(OS_ANDROID) |
| 154 | #define MAYBE_DontClose DISABLED_DontClose |
| 155 | #else |
| 156 | #define MAYBE_DontClose DontClose |
| 157 | #endif |
| 158 | TEST(MessageAttachmentSet, MAYBE_DontClose) { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 159 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 160 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 161 | const int fd = GetSafeFd(); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 162 | ASSERT_TRUE(set->AddAttachment(new internal::PlatformFileAttachment(fd))); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 163 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 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 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 168 | TEST(MessageAttachmentSet, DoClose) { |
| 169 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 170 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 171 | const int fd = GetSafeFd(); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 172 | ASSERT_TRUE(set->AddAttachment( |
| 173 | new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 174 | set->CommitAllDescriptors(); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 175 | |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 176 | ASSERT_TRUE(VerifyClosed(fd)); |
[email protected] | c792d81 | 2009-02-13 02:36:08 | [diff] [blame] | 177 | } |
[email protected] | 042070d | 2009-05-13 23:30:20 | [diff] [blame] | 178 | |
| 179 | } // namespace |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 180 | } // namespace IPC |