summaryrefslogtreecommitdiffstats
path: root/ipc/file_descriptor_set_posix_unittest.cc
blob: 34ef465edc05cb5e519ef414b3b76e6243e8c418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This test is POSIX only.

#include "ipc/file_descriptor_set_posix.h"

#include <unistd.h>
#include <fcntl.h>

#include "base/basictypes.h"
#include "base/posix/eintr_wrapper.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Get a safe file descriptor for test purposes.
int GetSafeFd() {
  return open("/dev/null", O_RDONLY);
}

// Returns true if fd was already closed.  Closes fd if not closed.
bool VerifyClosed(int fd) {
  const int duped = dup(fd);
  if (duped != -1) {
    EXPECT_NE(IGNORE_EINTR(close(duped)), -1);
    EXPECT_NE(IGNORE_EINTR(close(fd)), -1);
    return false;
  }
  return true;
}

// The FileDescriptorSet will try and close some of the descriptor numbers
// which we given it. This is the base descriptor value. It's great enough such
// that no real descriptor will accidently be closed.
static const int kFDBase = 50000;

TEST(FileDescriptorSet, BasicAdd) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  ASSERT_EQ(set->size(), 0u);
  ASSERT_TRUE(set->empty());
  ASSERT_TRUE(set->AddToBorrow(kFDBase));
  ASSERT_EQ(set->size(), 1u);
  ASSERT_TRUE(!set->empty());

  // Empties the set and stops a warning about deleting a set with unconsumed
  // descriptors
  set->CommitAll();
}

TEST(FileDescriptorSet, BasicAddAndClose) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  ASSERT_EQ(set->size(), 0u);
  ASSERT_TRUE(set->empty());
  const int fd = GetSafeFd();
  ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd)));
  ASSERT_EQ(set->size(), 1u);
  ASSERT_TRUE(!set->empty());

  set->CommitAll();

  ASSERT_TRUE(VerifyClosed(fd));
}
TEST(FileDescriptorSet, MaxSize) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  for (size_t i = 0; i < FileDescriptorSet::kMaxDescriptorsPerMessage; ++i)
    ASSERT_TRUE(set->AddToBorrow(kFDBase + 1 + i));

  ASSERT_TRUE(!set->AddToBorrow(kFDBase));

  set->CommitAll();
}

TEST(FileDescriptorSet, SetDescriptors) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  ASSERT_TRUE(set->empty());
  set->AddDescriptorsToOwn(NULL, 0);
  ASSERT_TRUE(set->empty());

  const int fd = GetSafeFd();
  static const int fds[] = {fd};
  set->AddDescriptorsToOwn(fds, 1);
  ASSERT_TRUE(!set->empty());
  ASSERT_EQ(set->size(), 1u);

  set->CommitAll();

  ASSERT_TRUE(VerifyClosed(fd));
}

TEST(FileDescriptorSet, PeekDescriptors) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  set->PeekDescriptors(NULL);
  ASSERT_TRUE(set->AddToBorrow(kFDBase));

  int fds[1];
  fds[0] = 0;
  set->PeekDescriptors(fds);
  ASSERT_EQ(fds[0], kFDBase);
  set->CommitAll();
  ASSERT_TRUE(set->empty());
}

TEST(FileDescriptorSet, WalkInOrder) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
  // used to retrieve borrowed descriptors. That never happens in production.
  ASSERT_TRUE(set->AddToBorrow(kFDBase));
  ASSERT_TRUE(set->AddToBorrow(kFDBase + 1));
  ASSERT_TRUE(set->AddToBorrow(kFDBase + 2));

  ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
  ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
  ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);

  set->CommitAll();
}

TEST(FileDescriptorSet, WalkWrongOrder) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
  // used to retrieve borrowed descriptors. That never happens in production.
  ASSERT_TRUE(set->AddToBorrow(kFDBase));
  ASSERT_TRUE(set->AddToBorrow(kFDBase + 1));
  ASSERT_TRUE(set->AddToBorrow(kFDBase + 2));

  ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
  ASSERT_EQ(set->TakeDescriptorAt(2), -1);

  set->CommitAll();
}

TEST(FileDescriptorSet, WalkCycle) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  // TODO(morrita): This test is wrong. TakeDescriptorAt() shouldn't be
  // used to retrieve borrowed descriptors. That never happens in production.
  ASSERT_TRUE(set->AddToBorrow(kFDBase));
  ASSERT_TRUE(set->AddToBorrow(kFDBase + 1));
  ASSERT_TRUE(set->AddToBorrow(kFDBase + 2));

  ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
  ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
  ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);
  ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
  ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
  ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);
  ASSERT_EQ(set->TakeDescriptorAt(0), kFDBase);
  ASSERT_EQ(set->TakeDescriptorAt(1), kFDBase + 1);
  ASSERT_EQ(set->TakeDescriptorAt(2), kFDBase + 2);

  set->CommitAll();
}

TEST(FileDescriptorSet, DontClose) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  const int fd = GetSafeFd();
  ASSERT_TRUE(set->AddToBorrow(fd));
  set->CommitAll();

  ASSERT_FALSE(VerifyClosed(fd));
}

TEST(FileDescriptorSet, DoClose) {
  scoped_refptr<FileDescriptorSet> set(new FileDescriptorSet);

  const int fd = GetSafeFd();
  ASSERT_TRUE(set->AddToOwn(base::ScopedFD(fd)));
  set->CommitAll();

  ASSERT_TRUE(VerifyClosed(fd));
}

}  // namespace