blob: ff14b669f03a7069ae4b9e9362213f13647bc408 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2009, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#include <set>
29#include <sstream>
30
31#include "talk/base/gunit.h"
32#include "talk/base/linuxfdwalk.h"
33
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37#include <unistd.h>
38
39static const int kArbitraryLargeFdNumber = 424;
40
41static void FdCheckVisitor(void *data, int fd) {
42 std::set<int> *fds = static_cast<std::set<int> *>(data);
43 EXPECT_EQ(1U, fds->erase(fd));
44}
45
46static void FdEnumVisitor(void *data, int fd) {
47 std::set<int> *fds = static_cast<std::set<int> *>(data);
48 EXPECT_TRUE(fds->insert(fd).second);
49}
50
51// Checks that the set of open fds is exactly the given list.
52static void CheckOpenFdList(std::set<int> fds) {
53 EXPECT_EQ(0, fdwalk(&FdCheckVisitor, &fds));
54 EXPECT_EQ(0U, fds.size());
55}
56
57static void GetOpenFdList(std::set<int> *fds) {
58 fds->clear();
59 EXPECT_EQ(0, fdwalk(&FdEnumVisitor, fds));
60}
61
62TEST(LinuxFdWalk, TestFdWalk) {
63 std::set<int> fds;
64 GetOpenFdList(&fds);
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) {
71 str << " " << *i;
72 }
73 LOG(LS_INFO) << str.str();
74 // Open some files.
75 int fd1 = open("/dev/null", O_RDONLY);
76 EXPECT_LE(0, fd1);
77 int fd2 = open("/dev/null", O_WRONLY);
78 EXPECT_LE(0, fd2);
79 int fd3 = open("/dev/null", O_RDWR);
80 EXPECT_LE(0, fd3);
81 int fd4 = dup2(fd3, kArbitraryLargeFdNumber);
82 EXPECT_LE(0, fd4);
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);
87 CheckOpenFdList(fds);
88 EXPECT_EQ(0, close(fd1));
89 EXPECT_EQ(0, close(fd2));
90 EXPECT_EQ(0, close(fd3));
91 EXPECT_EQ(0, close(fd4));
92}