pkasting@chromium.org | 9687a8f | 2011-09-01 09:50:13 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [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 | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 7 | #include "ipc/ipc_message_attachment_set.h" |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 8 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 9 | #include <fcntl.h> |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 10 | #include <stddef.h> |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 11 | #include <unistd.h> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 12 | |
brettw@chromium.org | b1788fb | 2012-11-15 05:54:35 +0900 | [diff] [blame] | 13 | #include "base/posix/eintr_wrapper.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 14 | #include "build/build_config.h" |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 15 | #include "ipc/ipc_platform_file_attachment_posix.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 18 | namespace IPC { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [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) { |
Dale Curtis | 56733a0 | 2017-08-30 06:53:23 +0900 | [diff] [blame] | 28 | const int duped = HANDLE_EINTR(dup(fd)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 29 | if (duped != -1) { |
mark@chromium.org | fa5a0f9 | 2013-12-03 23:10:59 +0900 | [diff] [blame] | 30 | EXPECT_NE(IGNORE_EINTR(close(duped)), -1); |
| 31 | EXPECT_NE(IGNORE_EINTR(close(fd)), -1); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 37 | int GetFdAt(MessageAttachmentSet* set, int id) { |
| 38 | return static_cast<internal::PlatformFileAttachment&>( |
| 39 | *set->GetAttachmentAt(id)) |
| 40 | .TakePlatformFile(); |
| 41 | } |
| 42 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 43 | // The MessageAttachmentSet will try and close some of the descriptor numbers |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [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 | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 48 | TEST(MessageAttachmentSet, BasicAdd) { |
| 49 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 50 | |
| 51 | ASSERT_EQ(set->size(), 0u); |
| 52 | ASSERT_TRUE(set->empty()); |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 53 | ASSERT_TRUE( |
| 54 | set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [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 | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 60 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 61 | } |
| 62 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 63 | TEST(MessageAttachmentSet, BasicAddAndClose) { |
| 64 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 65 | |
| 66 | ASSERT_EQ(set->size(), 0u); |
| 67 | ASSERT_TRUE(set->empty()); |
| 68 | const int fd = GetSafeFd(); |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 69 | ASSERT_TRUE(set->AddAttachment( |
| 70 | new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 71 | ASSERT_EQ(set->size(), 1u); |
| 72 | ASSERT_TRUE(!set->empty()); |
| 73 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 74 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 75 | |
| 76 | ASSERT_TRUE(VerifyClosed(fd)); |
| 77 | } |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 78 | TEST(MessageAttachmentSet, MaxSize) { |
| 79 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 80 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 81 | for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i) |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 82 | ASSERT_TRUE(set->AddAttachment( |
| 83 | new internal::PlatformFileAttachment(kFDBase + 1 + i))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 84 | |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 85 | ASSERT_TRUE( |
| 86 | !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 87 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 88 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 89 | } |
| 90 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 91 | TEST(MessageAttachmentSet, WalkInOrder) { |
| 92 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 93 | |
morrita | ab20725 | 2014-09-25 05:11:45 +0900 | [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 | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [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))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 102 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [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); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 106 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 107 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 108 | } |
| 109 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 110 | TEST(MessageAttachmentSet, WalkWrongOrder) { |
| 111 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 112 | |
morrita | ab20725 | 2014-09-25 05:11:45 +0900 | [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 | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [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))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 121 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 122 | ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase); |
| 123 | ASSERT_FALSE(set->GetAttachmentAt(2)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 124 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 125 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 126 | } |
| 127 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 128 | TEST(MessageAttachmentSet, WalkCycle) { |
| 129 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 130 | |
morrita | ab20725 | 2014-09-25 05:11:45 +0900 | [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 | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [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))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 139 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [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); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 149 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 150 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 151 | } |
| 152 | |
tfarina | d66bf11 | 2015-07-28 23:41:47 +0900 | [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 | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 159 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 160 | |
| 161 | const int fd = GetSafeFd(); |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 162 | ASSERT_TRUE(set->AddAttachment(new internal::PlatformFileAttachment(fd))); |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 163 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 164 | |
| 165 | ASSERT_FALSE(VerifyClosed(fd)); |
| 166 | } |
| 167 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 168 | TEST(MessageAttachmentSet, DoClose) { |
| 169 | scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 170 | |
| 171 | const int fd = GetSafeFd(); |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 172 | ASSERT_TRUE(set->AddAttachment( |
| 173 | new internal::PlatformFileAttachment(base::ScopedFD(fd)))); |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 174 | set->CommitAllDescriptors(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 175 | |
| 176 | ASSERT_TRUE(VerifyClosed(fd)); |
| 177 | } |
| 178 | |
| 179 | } // namespace |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 180 | } // namespace IPC |