interprocess_sync_file_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "util/interprocess_sync_file.h"
  15. #include <gtest/gtest.h>
  16. using namespace std;
  17. namespace isc {
  18. namespace util {
  19. namespace {
  20. unsigned char
  21. parentReadLockedState (int fd) {
  22. unsigned char locked = 0xff;
  23. fd_set rfds;
  24. FD_ZERO(&rfds);
  25. FD_SET(fd, &rfds);
  26. // We use select() here to wait for new data on the input end of
  27. // the pipe. We wait for 5 seconds (an arbitrary value) for input
  28. // data, and continue if no data is available. This is done so
  29. // that read() is not blocked due to some issue in the child
  30. // process (and the tests continue running).
  31. struct timeval tv;
  32. tv.tv_sec = 5;
  33. tv.tv_usec = 0;
  34. const int nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
  35. EXPECT_EQ(1, nfds);
  36. if (nfds == 1) {
  37. // Read status
  38. read(fd, &locked, sizeof(locked));
  39. }
  40. return (locked);
  41. }
  42. TEST(InterprocessSyncFileTest, TestLock) {
  43. InterprocessSyncFile sync("test");
  44. InterprocessSyncLocker locker(sync);
  45. EXPECT_TRUE(locker.lock());
  46. int fds[2];
  47. // Here, we check that a lock has been taken by forking and
  48. // checking from the child that a lock exists. This has to be
  49. // done from a separate process as we test by trying to lock the
  50. // range again on the lock file. The lock attempt would pass if
  51. // done from the same process for the granted range. The lock
  52. // attempt must fail to pass our check.
  53. pipe(fds);
  54. if (fork() == 0) {
  55. unsigned char locked = 0;
  56. // Child writes to pipe
  57. close(fds[0]);
  58. InterprocessSyncFile sync2("test");
  59. InterprocessSyncLocker locker2(sync2);
  60. if (!locker2.tryLock()) {
  61. locked = 1;
  62. }
  63. write(fds[1], &locked, sizeof(locked));
  64. close(fds[1]);
  65. exit(0);
  66. } else {
  67. // Parent reads from pipe
  68. close(fds[1]);
  69. const unsigned char locked = parentReadLockedState(fds[0]);
  70. close(fds[0]);
  71. EXPECT_EQ(1, locked);
  72. }
  73. EXPECT_TRUE(locker.unlock());
  74. }
  75. TEST(InterprocessSyncFileTest, TestMultipleFilesDirect) {
  76. InterprocessSyncFile sync("test1");
  77. InterprocessSyncLocker locker(sync);
  78. EXPECT_TRUE(locker.lock());
  79. InterprocessSyncFile sync2("test2");
  80. InterprocessSyncLocker locker2(sync2);
  81. EXPECT_TRUE(locker2.lock());
  82. EXPECT_TRUE(locker2.unlock());
  83. EXPECT_TRUE(locker.unlock());
  84. }
  85. TEST(InterprocessSyncFileTest, TestMultipleFilesForked) {
  86. InterprocessSyncFile sync("test");
  87. InterprocessSyncLocker locker(sync);
  88. EXPECT_TRUE(locker.lock());
  89. int fds[2];
  90. pipe(fds);
  91. if (fork() == 0) {
  92. unsigned char locked = 0xff;
  93. // Child writes to pipe
  94. close(fds[0]);
  95. InterprocessSyncFile sync2("test2");
  96. InterprocessSyncLocker locker2(sync2);
  97. if (locker2.tryLock()) {
  98. locked = 0;
  99. }
  100. write(fds[1], &locked, sizeof(locked));
  101. close(fds[1]);
  102. exit(0);
  103. } else {
  104. // Parent reads from pipe
  105. close(fds[1]);
  106. const unsigned char locked = parentReadLockedState(fds[0]);
  107. close(fds[0]);
  108. EXPECT_EQ(0, locked);
  109. }
  110. EXPECT_TRUE(locker.unlock());
  111. }
  112. }
  113. } // namespace util
  114. } // namespace isc