3
* Copyright 2009, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
#include "talk/base/gunit.h"
32
#include "talk/base/linuxfdwalk.h"
34
#include <sys/types.h>
39
static const int kArbitraryLargeFdNumber = 424;
41
static void FdCheckVisitor(void *data, int fd) {
42
std::set<int> *fds = static_cast<std::set<int> *>(data);
43
EXPECT_EQ(1U, fds->erase(fd));
46
static void FdEnumVisitor(void *data, int fd) {
47
std::set<int> *fds = static_cast<std::set<int> *>(data);
48
EXPECT_TRUE(fds->insert(fd).second);
51
// Checks that the set of open fds is exactly the given list.
52
static void CheckOpenFdList(std::set<int> fds) {
53
EXPECT_EQ(0, fdwalk(&FdCheckVisitor, &fds));
54
EXPECT_EQ(0U, fds.size());
57
static void GetOpenFdList(std::set<int> *fds) {
59
EXPECT_EQ(0, fdwalk(&FdEnumVisitor, fds));
62
TEST(LinuxFdWalk, TestFdWalk) {
65
std::ostringstream str;
66
// I have observed that the open set when starting a test is [0, 6]. Leaked
67
// fds would change that, but so can (e.g.) running under a debugger, so we
68
// can't really do an EXPECT. :(
69
str << "File descriptors open in test executable:";
70
for (std::set<int>::const_iterator i = fds.begin(); i != fds.end(); ++i) {
73
LOG(LS_INFO) << str.str();
75
int fd1 = open("/dev/null", O_RDONLY);
77
int fd2 = open("/dev/null", O_WRONLY);
79
int fd3 = open("/dev/null", O_RDWR);
81
int fd4 = dup2(fd3, kArbitraryLargeFdNumber);
83
EXPECT_TRUE(fds.insert(fd1).second);
84
EXPECT_TRUE(fds.insert(fd2).second);
85
EXPECT_TRUE(fds.insert(fd3).second);
86
EXPECT_TRUE(fds.insert(fd4).second);
88
EXPECT_EQ(0, close(fd1));
89
EXPECT_EQ(0, close(fd2));
90
EXPECT_EQ(0, close(fd3));
91
EXPECT_EQ(0, close(fd4));