You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include <set>
  28. #include <sstream>
  29. #include "talk/base/gunit.h"
  30. #include "talk/base/linuxfdwalk.h"
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. static const int kArbitraryLargeFdNumber = 424;
  36. static void FdCheckVisitor(void *data, int fd) {
  37. std::set<int> *fds = static_cast<std::set<int> *>(data);
  38. EXPECT_EQ(1U, fds->erase(fd));
  39. }
  40. static void FdEnumVisitor(void *data, int fd) {
  41. std::set<int> *fds = static_cast<std::set<int> *>(data);
  42. EXPECT_TRUE(fds->insert(fd).second);
  43. }
  44. // Checks that the set of open fds is exactly the given list.
  45. static void CheckOpenFdList(std::set<int> fds) {
  46. EXPECT_EQ(0, fdwalk(&FdCheckVisitor, &fds));
  47. EXPECT_EQ(0U, fds.size());
  48. }
  49. static void GetOpenFdList(std::set<int> *fds) {
  50. fds->clear();
  51. EXPECT_EQ(0, fdwalk(&FdEnumVisitor, fds));
  52. }
  53. TEST(LinuxFdWalk, TestFdWalk) {
  54. std::set<int> fds;
  55. GetOpenFdList(&fds);
  56. std::ostringstream str;
  57. // I have observed that the open set when starting a test is [0, 6]. Leaked
  58. // fds would change that, but so can (e.g.) running under a debugger, so we
  59. // can't really do an EXPECT. :(
  60. str << "File descriptors open in test executable:";
  61. for (std::set<int>::const_iterator i = fds.begin(); i != fds.end(); ++i) {
  62. str << " " << *i;
  63. }
  64. LOG(LS_INFO) << str.str();
  65. // Open some files.
  66. int fd1 = open("/dev/null", O_RDONLY);
  67. EXPECT_LE(0, fd1);
  68. int fd2 = open("/dev/null", O_WRONLY);
  69. EXPECT_LE(0, fd2);
  70. int fd3 = open("/dev/null", O_RDWR);
  71. EXPECT_LE(0, fd3);
  72. int fd4 = dup2(fd3, kArbitraryLargeFdNumber);
  73. EXPECT_LE(0, fd4);
  74. EXPECT_TRUE(fds.insert(fd1).second);
  75. EXPECT_TRUE(fds.insert(fd2).second);
  76. EXPECT_TRUE(fds.insert(fd3).second);
  77. EXPECT_TRUE(fds.insert(fd4).second);
  78. CheckOpenFdList(fds);
  79. EXPECT_EQ(0, close(fd1));
  80. EXPECT_EQ(0, close(fd2));
  81. EXPECT_EQ(0, close(fd3));
  82. EXPECT_EQ(0, close(fd4));
  83. }