Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/common/file_descriptor_set_unittest.cc

Issue 155905: Separates ipc code from common (http://crbug.com/16829) (Closed)
Patch Set: Fixes reference to 'common_message_traits' it's actually 'common_param_traits' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/common/file_descriptor_set_posix.cc ('k') | chrome/common/ipc_channel.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2009 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.
4
5 // This test is POSIX only.
6
7 #include <unistd.h>
8 #include <fcntl.h>
9
10 #include "base/basictypes.h"
11 #include "base/eintr_wrapper.h"
12 #include "chrome/common/file_descriptor_set_posix.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 // Get a safe file descriptor for test purposes.
18 int GetSafeFd() {
19 return open("/dev/null", O_RDONLY);
20 }
21
22 // Returns true if fd was already closed. Closes fd if not closed.
23 bool VerifyClosed(int fd) {
24 const int duped = dup(fd);
25 if (duped != -1) {
26 HANDLE_EINTR(close(duped));
27 HANDLE_EINTR(close(fd));
28 return false;
29 }
30 return true;
31 }
32
33 // The FileDescriptorSet will try and close some of the descriptor numbers
34 // which we given it. This is the base descriptor value. It's great enough such
35 // that no real descriptor will accidently be closed.
36 static const int kFDBase = 50000;
37
38 TEST(FileDescriptorSet, BasicAdd) {
39 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
40
41 ASSERT_EQ(set->size(), 0u);
42 ASSERT_TRUE(set->empty());
43 ASSERT_TRUE(set->Add(kFDBase));
44 ASSERT_EQ(set->size(), 1u);
45 ASSERT_TRUE(!set->empty());
46
47 // Empties the set and stops a warning about deleting a set with unconsumed
48 // descriptors
49 set->CommitAll();
50 }
51
52 TEST(FileDescriptorSet, BasicAddAndClose) {
53 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
54
55 ASSERT_EQ(set->size(), 0u);
56 ASSERT_TRUE(set->empty());
57 const int fd = GetSafeFd();
58 ASSERT_TRUE(set->AddAndAutoClose(fd));
59 ASSERT_EQ(set->size(), 1u);
60 ASSERT_TRUE(!set->empty());
61
62 set->CommitAll();
63
64 ASSERT_TRUE(VerifyClosed(fd));
65 }
66 TEST(FileDescriptorSet, MaxSize) {
67 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
68
69 for (unsigned i = 0;
70 i < FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE; ++i) {
71 ASSERT_TRUE(set->Add(kFDBase + 1 + i));
72 }
73
74 ASSERT_TRUE(!set->Add(kFDBase));
75
76 set->CommitAll();
77 }
78
79 TEST(FileDescriptorSet, SetDescriptors) {
80 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
81
82 ASSERT_TRUE(set->empty());
83 set->SetDescriptors(NULL, 0);
84 ASSERT_TRUE(set->empty());
85
86 const int fd = GetSafeFd();
87 static const int fds[] = {fd};
88 set->SetDescriptors(fds, 1);
89 ASSERT_TRUE(!set->empty());
90 ASSERT_EQ(set->size(), 1u);
91
92 set->CommitAll();
93
94 ASSERT_TRUE(VerifyClosed(fd));
95 }
96
97 TEST(FileDescriptorSet, GetDescriptors) {
98 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
99
100 set->GetDescriptors(NULL);
101 ASSERT_TRUE(set->Add(kFDBase));
102
103 int fds[1];
104 fds[0] = 0;
105 set->GetDescriptors(fds);
106 ASSERT_EQ(fds[0], kFDBase);
107 set->CommitAll();
108 ASSERT_TRUE(set->empty());
109 }
110
111 TEST(FileDescriptorSet, WalkInOrder) {
112 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
113
114 ASSERT_TRUE(set->Add(kFDBase));
115 ASSERT_TRUE(set->Add(kFDBase + 1));
116 ASSERT_TRUE(set->Add(kFDBase + 2));
117
118 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
119 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
120 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
121
122 set->CommitAll();
123 }
124
125 TEST(FileDescriptorSet, WalkWrongOrder) {
126 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
127
128 ASSERT_TRUE(set->Add(kFDBase));
129 ASSERT_TRUE(set->Add(kFDBase + 1));
130 ASSERT_TRUE(set->Add(kFDBase + 2));
131
132 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
133 ASSERT_EQ(set->GetDescriptorAt(2), -1);
134
135 set->CommitAll();
136 }
137
138 TEST(FileDescriptorSet, WalkCycle) {
139 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
140
141 ASSERT_TRUE(set->Add(kFDBase));
142 ASSERT_TRUE(set->Add(kFDBase + 1));
143 ASSERT_TRUE(set->Add(kFDBase + 2));
144
145 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
146 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
147 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
148 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
149 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
150 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
151 ASSERT_EQ(set->GetDescriptorAt(0), kFDBase);
152 ASSERT_EQ(set->GetDescriptorAt(1), kFDBase + 1);
153 ASSERT_EQ(set->GetDescriptorAt(2), kFDBase + 2);
154
155 set->CommitAll();
156 }
157
158 TEST(FileDescriptorSet, DontClose) {
159 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
160
161 const int fd = GetSafeFd();
162 ASSERT_TRUE(set->Add(fd));
163 set->CommitAll();
164
165 ASSERT_FALSE(VerifyClosed(fd));
166 }
167
168 TEST(FileDescriptorSet, DoClose) {
169 scoped_refptr<FileDescriptorSet> set = new FileDescriptorSet;
170
171 const int fd = GetSafeFd();
172 ASSERT_TRUE(set->AddAndAutoClose(fd));
173 set->CommitAll();
174
175 ASSERT_TRUE(VerifyClosed(fd));
176 }
177
178 } // namespace
OLDNEW
« no previous file with comments | « chrome/common/file_descriptor_set_posix.cc ('k') | chrome/common/ipc_channel.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698