blob: 0bd143df4dee9eeb497599412d17803dcd05edb9 [file] [log] [blame]
[email protected]05094a32011-09-01 00:50:131// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c792d812009-02-13 02:36:082// 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
morrita4b5c28e22015-01-14 21:17:067#include "ipc/ipc_message_attachment_set.h"
[email protected]22b42c52010-12-20 06:59:238
[email protected]56dacae2009-02-13 02:45:489#include <fcntl.h>
avi246998d82015-12-22 02:39:0410#include <stddef.h>
morrita4b5c28e22015-01-14 21:17:0611#include <unistd.h>
[email protected]89a104d2009-02-13 02:40:4612
[email protected]2025d002012-11-14 20:54:3513#include "base/posix/eintr_wrapper.h"
avi246998d82015-12-22 02:39:0414#include "build/build_config.h"
morrita1aa788c2015-01-31 05:45:4215#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]c792d812009-02-13 02:36:0816#include "testing/gtest/include/gtest/gtest.h"
17
morrita4b5c28e22015-01-14 21:17:0618namespace IPC {
[email protected]042070d2009-05-13 23:30:2019namespace {
20
21// Get a safe file descriptor for test purposes.
22int GetSafeFd() {
23 return open("/dev/null", O_RDONLY);
24}
25
26// Returns true if fd was already closed. Closes fd if not closed.
27bool VerifyClosed(int fd) {
28 const int duped = dup(fd);
29 if (duped != -1) {
[email protected]d89eec82013-12-03 14:10:5930 EXPECT_NE(IGNORE_EINTR(close(duped)), -1);
31 EXPECT_NE(IGNORE_EINTR(close(fd)), -1);
[email protected]042070d2009-05-13 23:30:2032 return false;
33 }
34 return true;
35}
36
sammc6ed3efb2016-11-23 03:17:3537int GetFdAt(MessageAttachmentSet* set, int id) {
38 return static_cast<internal::PlatformFileAttachment&>(
39 *set->GetAttachmentAt(id))
40 .TakePlatformFile();
41}
42
morrita4b5c28e22015-01-14 21:17:0643// The MessageAttachmentSet will try and close some of the descriptor numbers
[email protected]c792d812009-02-13 02:36:0844// which we given it. This is the base descriptor value. It's great enough such
45// that no real descriptor will accidently be closed.
46static const int kFDBase = 50000;
47
morrita4b5c28e22015-01-14 21:17:0648TEST(MessageAttachmentSet, BasicAdd) {
49 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:0850
51 ASSERT_EQ(set->size(), 0u);
52 ASSERT_TRUE(set->empty());
morrita1aa788c2015-01-31 05:45:4253 ASSERT_TRUE(
54 set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
[email protected]c792d812009-02-13 02:36:0855 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
erikchenae6d3212015-10-10 02:43:4960 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:0861}
62
morrita4b5c28e22015-01-14 21:17:0663TEST(MessageAttachmentSet, BasicAddAndClose) {
64 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:0865
66 ASSERT_EQ(set->size(), 0u);
67 ASSERT_TRUE(set->empty());
[email protected]042070d2009-05-13 23:30:2068 const int fd = GetSafeFd();
morrita1aa788c2015-01-31 05:45:4269 ASSERT_TRUE(set->AddAttachment(
70 new internal::PlatformFileAttachment(base::ScopedFD(fd))));
[email protected]c792d812009-02-13 02:36:0871 ASSERT_EQ(set->size(), 1u);
72 ASSERT_TRUE(!set->empty());
73
erikchenae6d3212015-10-10 02:43:4974 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:0875
[email protected]042070d2009-05-13 23:30:2076 ASSERT_TRUE(VerifyClosed(fd));
77}
morrita4b5c28e22015-01-14 21:17:0678TEST(MessageAttachmentSet, MaxSize) {
79 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:0880
morrita4b5c28e22015-01-14 21:17:0681 for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i)
morrita1aa788c2015-01-31 05:45:4282 ASSERT_TRUE(set->AddAttachment(
83 new internal::PlatformFileAttachment(kFDBase + 1 + i)));
[email protected]c792d812009-02-13 02:36:0884
morrita1aa788c2015-01-31 05:45:4285 ASSERT_TRUE(
86 !set->AddAttachment(new internal::PlatformFileAttachment(kFDBase)));
[email protected]c792d812009-02-13 02:36:0887
erikchenae6d3212015-10-10 02:43:4988 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:0889}
90
morrita4b5c28e22015-01-14 21:17:0691TEST(MessageAttachmentSet, WalkInOrder) {
92 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:0893
morrita96693852014-09-24 20:11:4594 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
95 // used to retrieve borrowed descriptors. That never happens in production.
morrita1aa788c2015-01-31 05:45:4296 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]c792d812009-02-13 02:36:08102
sammc6ed3efb2016-11-23 03:17:35103 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]c792d812009-02-13 02:36:08106
erikchenae6d3212015-10-10 02:43:49107 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:08108}
109
morrita4b5c28e22015-01-14 21:17:06110TEST(MessageAttachmentSet, WalkWrongOrder) {
111 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:08112
morrita96693852014-09-24 20:11:45113 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
114 // used to retrieve borrowed descriptors. That never happens in production.
morrita1aa788c2015-01-31 05:45:42115 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]c792d812009-02-13 02:36:08121
sammc6ed3efb2016-11-23 03:17:35122 ASSERT_EQ(GetFdAt(set.get(), 0), kFDBase);
123 ASSERT_FALSE(set->GetAttachmentAt(2));
[email protected]c792d812009-02-13 02:36:08124
erikchenae6d3212015-10-10 02:43:49125 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:08126}
127
morrita4b5c28e22015-01-14 21:17:06128TEST(MessageAttachmentSet, WalkCycle) {
129 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:08130
morrita96693852014-09-24 20:11:45131 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
132 // used to retrieve borrowed descriptors. That never happens in production.
morrita1aa788c2015-01-31 05:45:42133 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]c792d812009-02-13 02:36:08139
sammc6ed3efb2016-11-23 03:17:35140 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]c792d812009-02-13 02:36:08149
erikchenae6d3212015-10-10 02:43:49150 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:08151}
152
tfarina8514f0d2015-07-28 14:41:47153#if defined(OS_ANDROID)
154#define MAYBE_DontClose DISABLED_DontClose
155#else
156#define MAYBE_DontClose DontClose
157#endif
158TEST(MessageAttachmentSet, MAYBE_DontClose) {
morrita4b5c28e22015-01-14 21:17:06159 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:08160
[email protected]042070d2009-05-13 23:30:20161 const int fd = GetSafeFd();
morrita1aa788c2015-01-31 05:45:42162 ASSERT_TRUE(set->AddAttachment(new internal::PlatformFileAttachment(fd)));
erikchenae6d3212015-10-10 02:43:49163 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:08164
[email protected]042070d2009-05-13 23:30:20165 ASSERT_FALSE(VerifyClosed(fd));
[email protected]c792d812009-02-13 02:36:08166}
167
morrita4b5c28e22015-01-14 21:17:06168TEST(MessageAttachmentSet, DoClose) {
169 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
[email protected]c792d812009-02-13 02:36:08170
[email protected]042070d2009-05-13 23:30:20171 const int fd = GetSafeFd();
morrita1aa788c2015-01-31 05:45:42172 ASSERT_TRUE(set->AddAttachment(
173 new internal::PlatformFileAttachment(base::ScopedFD(fd))));
erikchenae6d3212015-10-10 02:43:49174 set->CommitAllDescriptors();
[email protected]c792d812009-02-13 02:36:08175
[email protected]042070d2009-05-13 23:30:20176 ASSERT_TRUE(VerifyClosed(fd));
[email protected]c792d812009-02-13 02:36:08177}
[email protected]042070d2009-05-13 23:30:20178
179} // namespace
morrita4b5c28e22015-01-14 21:17:06180} // namespace IPC