Fix for Hyperlinks do not open in new tab by Print preview window.
[chromium-blink-merge.git] / ipc / ipc_message_attachment_set_posix_unittest.cc
blob0a358fb16b9cea2f99b2adbc8872e0c468517720
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/basictypes.h"
13 #include "base/posix/eintr_wrapper.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(set->AddToBorrow(kFDBase));
46 ASSERT_EQ(set->size(), 1u);
47 ASSERT_TRUE(!set->empty());
49 // Empties the set and stops a warning about deleting a set with unconsumed
50 // descriptors
51 set->CommitAll();
54 TEST(MessageAttachmentSet, BasicAddAndClose) {
55 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
57 ASSERT_EQ(set->size(), 0u);
58 ASSERT_TRUE(set->empty());
59 const int fd = GetSafeFd();
60 ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd)));
61 ASSERT_EQ(set->size(), 1u);
62 ASSERT_TRUE(!set->empty());
64 set->CommitAll();
66 ASSERT_TRUE(VerifyClosed(fd));
68 TEST(MessageAttachmentSet, MaxSize) {
69 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
71 for (size_t i = 0; i < MessageAttachmentSet::kMaxDescriptorsPerMessage; ++i)
72 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1 + i));
74 ASSERT_TRUE(!set->AddToBorrow(kFDBase));
76 set->CommitAll();
79 TEST(MessageAttachmentSet, SetDescriptors) {
80 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
82 ASSERT_TRUE(set->empty());
83 set->AddDescriptorsToOwn(NULL, 0);
84 ASSERT_TRUE(set->empty());
86 const int fd = GetSafeFd();
87 static const int fds[] = {fd};
88 set->AddDescriptorsToOwn(fds, 1);
89 ASSERT_TRUE(!set->empty());
90 ASSERT_EQ(set->size(), 1u);
92 set->CommitAll();
94 ASSERT_TRUE(VerifyClosed(fd));
97 TEST(MessageAttachmentSet, PeekDescriptors) {
98 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
100 set->PeekDescriptors(NULL);
101 ASSERT_TRUE(set->AddToBorrow(kFDBase));
103 int fds[1];
104 fds[0] = 0;
105 set->PeekDescriptors(fds);
106 ASSERT_EQ(fds[0], kFDBase);
107 set->CommitAll();
108 ASSERT_TRUE(set->empty());
111 TEST(MessageAttachmentSet, WalkInOrder) {
112 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
114 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
115 // used to retrieve borrowed descriptors. That never happens in production.
116 ASSERT_TRUE(set->AddToBorrow(kFDBase));
117 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1));
118 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2));
120 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
121 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
122 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);
124 set->CommitAll();
127 TEST(MessageAttachmentSet, WalkWrongOrder) {
128 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
130 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
131 // used to retrieve borrowed descriptors. That never happens in production.
132 ASSERT_TRUE(set->AddToBorrow(kFDBase));
133 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1));
134 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2));
136 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
137 ASSERT_EQ(set->TakeDescriptorAt(2), -1);
139 set->CommitAll();
142 TEST(MessageAttachmentSet, WalkCycle) {
143 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
145 // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
146 // used to retrieve borrowed descriptors. That never happens in production.
147 ASSERT_TRUE(set->AddToBorrow(kFDBase));
148 ASSERT_TRUE(set->AddToBorrow(kFDBase + 1));
149 ASSERT_TRUE(set->AddToBorrow(kFDBase + 2));
151 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
152 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
153 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);
154 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
155 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
156 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);
157 ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
158 ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
159 ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);
161 set->CommitAll();
164 TEST(MessageAttachmentSet, DontClose) {
165 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
167 const int fd = GetSafeFd();
168 ASSERT_TRUE(set->AddToBorrow(fd));
169 set->CommitAll();
171 ASSERT_FALSE(VerifyClosed(fd));
174 TEST(MessageAttachmentSet, DoClose) {
175 scoped_refptr<MessageAttachmentSet> set(new MessageAttachmentSet);
177 const int fd = GetSafeFd();
178 ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd)));
179 set->CommitAll();
181 ASSERT_TRUE(VerifyClosed(fd));
184 } // namespace
185 } // namespace IPC