Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ipc / ipc_message_attachment_set_posix_unittest.cc
blob8df312f1d372aa163158f25ac10b9be7af45df3f
1 // Copyright (c) 2011 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.
5 // This test is POSIX only.
7 #include "ipc/ipc_message_attachment_set.h"
9 #include <fcntl.h>
10 #include <unistd.h>
12 #include "base/posix/eintr_wrapper.h"
13 #include "ipc/ipc_platform_file_attachment_posix.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace IPC {
17 namespace {
19 // Get a safe file descriptor for test purposes.
20 int GetSafeFd() {
21 return open("/dev/null", O_RDONLY);
24 // Returns true if fd was already closed. Closes fd if not closed.
25 bool VerifyClosed(int fd) {
26 const int duped = dup(fd);
27 if (duped != -1) {
28 EXPECT_NE(IGNORE_EINTR(close(duped)), -1);
29 EXPECT_NE(IGNORE_EINTR(close(fd)), -1);
30 return false;
32 return true;
35 // The MessageAttachmentSet will try and close some of the descriptor numbers
36 // which we given it. This is the base descriptor value. It's great enough such
37 // that no real descriptor will accidently be closed.
38 static const int kFDBase = 50000;
40 TEST(MessageAttachmentSet, BasicAdd) {
41 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
43 ASSERT_EQ(set->size(), 0u);
44 ASSERT_TRUE(set->empty());
45 ASSERT_TRUE(
46 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
47 ASSERT_EQ(set->size(), 1u);
48 ASSERT_TRUE(!set->empty());
50 // Empties the set and stops a warning about deleting a set with unconsumed
51 // descriptors
52 set->CommitAll();
55 TEST(MessageAttachmentSet, BasicAddAndClose) {
56 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
58 ASSERT_EQ(set->size(), 0u);
59 ASSERT_TRUE(set->empty());
60 const int fd = GetSafeFd();
61 ASSERT_TRUE(set->AddAttachment(
62 new internal::PlatformFileAttachment(base::ScopedFD(fd))));
63 ASSERT_EQ(set->size(), 1u);
64 ASSERT_TRUE(!set->empty());
66 set->CommitAll();
68 ASSERT_TRUE(VerifyClosed(fd));
70 TEST(MessageAttachmentSet, MaxSize) {
71 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
73 for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i)
74 ASSERT_TRUE(set->AddAttachment(
75 new internal::PlatformFileAttachment(kFDBase + 1 + i)));
77 ASSERT_TRUE(
78 !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
80 set->CommitAll();
83 #if defined(OS_ANDROID)
84 #define MAYBE_SetDescriptors DISABLED_SetDescriptors
85 #else
86 #define MAYBE_SetDescriptors SetDescriptors
87 #endif
88 TEST(MessageAttachmentSet, MAYBE_SetDescriptors) {
89 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
91 ASSERT_TRUE(set->empty());
92 set->AddDescriptorsToOwn(NULL, 0);
93 ASSERT_TRUE(set->empty());
95 const int fd = GetSafeFd();
96 static const int fds[] = {fd};
97 set->AddDescriptorsToOwn(fds, 1);
98 ASSERT_TRUE(!set->empty());
99 ASSERT_EQ(set->size(), 1u);
101 set->CommitAll();
103 ASSERT_TRUE(VerifyClosed(fd));
106 TEST(MessageAttachmentSet, PeekDescriptors) {
107 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
109 set->PeekDescriptors(NULL);
110 ASSERT_TRUE(
111 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
113 int fds[1];
114 fds[0] = 0;
115 set->PeekDescriptors(fds);
116 ASSERT_EQ(fds[0], kFDBase);
117 set->CommitAll();
118 ASSERT_TRUE(set->empty());
121 TEST(MessageAttachmentSet, WalkInOrder) {
122 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
124 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
125 // used to retrieve borrowed descriptors. That never happens in production.
126 ASSERT_TRUE(
127 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
128 ASSERT_TRUE(
129 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1)));
130 ASSERT_TRUE(
131 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2)));
133 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase);
134 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1);
135 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2);
137 set->CommitAll();
140 TEST(MessageAttachmentSet, WalkWrongOrder) {
141 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
143 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
144 // used to retrieve borrowed descriptors. That never happens in production.
145 ASSERT_TRUE(
146 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
147 ASSERT_TRUE(
148 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1)));
149 ASSERT_TRUE(
150 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2)));
152 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase);
153 ASSERT_EQ(set->GetAttachmentAt(2), nullptr);
155 set->CommitAll();
158 TEST(MessageAttachmentSet, WalkCycle) {
159 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
161 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
162 // used to retrieve borrowed descriptors. That never happens in production.
163 ASSERT_TRUE(
164 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
165 ASSERT_TRUE(
166 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 1)));
167 ASSERT_TRUE(
168 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase + 2)));
170 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase);
171 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1);
172 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2);
173 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase);
174 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1);
175 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2);
176 ASSERT_EQ(set->GetAttachmentAt(0)->TakePlatformFile(), kFDBase);
177 ASSERT_EQ(set->GetAttachmentAt(1)->TakePlatformFile(), kFDBase + 1);
178 ASSERT_EQ(set->GetAttachmentAt(2)->TakePlatformFile(), kFDBase + 2);
180 set->CommitAll();
183 #if defined(OS_ANDROID)
184 #define MAYBE_DontClose DISABLED_DontClose
185 #else
186 #define MAYBE_DontClose DontClose
187 #endif
188 TEST(MessageAttachmentSet, MAYBE_DontClose) {
189 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
191 const int fd = GetSafeFd();
192 ASSERT_TRUE(set->AddAttachment(new internal::PlatformFileAttachment(fd)));
193 set->CommitAll();
195 ASSERT_FALSE(VerifyClosed(fd));
198 TEST(MessageAttachmentSet, DoClose) {
199 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
201 const int fd = GetSafeFd();
202 ASSERT_TRUE(set->AddAttachment(
203 new internal::PlatformFileAttachment(base::ScopedFD(fd))));
204 set->CommitAll();
206 ASSERT_TRUE(VerifyClosed(fd));
209 } // namespace
210 } // namespace IPC