OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <set> | |
12 #include <sstream> | |
13 | |
14 #include "webrtc/base/gunit.h" | |
15 #include "webrtc/base/linuxfdwalk.h" | |
16 | |
17 #include <sys/types.h> | |
18 #include <sys/stat.h> | |
19 #include <fcntl.h> | |
20 #include <unistd.h> | |
21 | |
22 static const int kArbitraryLargeFdNumber = 424; | |
23 | |
24 static void FdCheckVisitor(void *data, int fd) { | |
25 std::set<int> *fds = static_cast<std::set<int> *>(data); | |
26 EXPECT_EQ(1U, fds->erase(fd)); | |
27 } | |
28 | |
29 static void FdEnumVisitor(void *data, int fd) { | |
30 std::set<int> *fds = static_cast<std::set<int> *>(data); | |
31 EXPECT_TRUE(fds->insert(fd).second); | |
32 } | |
33 | |
34 // Checks that the set of open fds is exactly the given list. | |
35 static void CheckOpenFdList(std::set<int> fds) { | |
36 EXPECT_EQ(0, fdwalk(&FdCheckVisitor, &fds)); | |
37 EXPECT_EQ(0U, fds.size()); | |
38 } | |
39 | |
40 static void GetOpenFdList(std::set<int> *fds) { | |
41 fds->clear(); | |
42 EXPECT_EQ(0, fdwalk(&FdEnumVisitor, fds)); | |
43 } | |
44 | |
45 TEST(LinuxFdWalk, TestFdWalk) { | |
46 std::set<int> fds; | |
47 GetOpenFdList(&fds); | |
48 std::ostringstream str; | |
49 // I have observed that the open set when starting a test is [0, 6]. Leaked | |
50 // fds would change that, but so can (e.g.) running under a debugger, so we | |
51 // can't really do an EXPECT. :( | |
52 str << "File descriptors open in test executable:"; | |
53 for (std::set<int>::const_iterator i = fds.begin(); i != fds.end(); ++i) { | |
54 str << " " << *i; | |
55 } | |
56 LOG(LS_INFO) << str.str(); | |
57 // Open some files. | |
58 int fd1 = open("/dev/null", O_RDONLY); | |
59 EXPECT_LE(0, fd1); | |
60 int fd2 = open("/dev/null", O_WRONLY); | |
61 EXPECT_LE(0, fd2); | |
62 int fd3 = open("/dev/null", O_RDWR); | |
63 EXPECT_LE(0, fd3); | |
64 int fd4 = dup2(fd3, kArbitraryLargeFdNumber); | |
65 EXPECT_LE(0, fd4); | |
66 EXPECT_TRUE(fds.insert(fd1).second); | |
67 EXPECT_TRUE(fds.insert(fd2).second); | |
68 EXPECT_TRUE(fds.insert(fd3).second); | |
69 EXPECT_TRUE(fds.insert(fd4).second); | |
70 CheckOpenFdList(fds); | |
71 EXPECT_EQ(0, close(fd1)); | |
72 EXPECT_EQ(0, close(fd2)); | |
73 EXPECT_EQ(0, close(fd3)); | |
74 EXPECT_EQ(0, close(fd4)); | |
75 } | |
OLD | NEW |