interprocess_sync_file_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. class InterprocessSyncFileTest : public ::testing::Test {
  20. protected:
  21. InterprocessSyncFileTest() {}
  22. };
  23. TEST_F(InterprocessSyncFileTest, TestLock) {
  24. InterprocessSync* sync = new InterprocessSyncFile("test");
  25. InterprocessSyncLocker* locker = sync->getLocker();
  26. EXPECT_TRUE(locker->lock());
  27. int fds[2];
  28. // Here, we check that a lock has been taken by forking and
  29. // checking from the child that a lock exists. This has to be
  30. // done from a separate process as we test by trying to lock the
  31. // range again on the lock file. The lock attempt would pass if
  32. // done from the same process for the granted range. The lock
  33. // attempt must fail to pass our check.
  34. bool was_locked(false);
  35. pipe(fds);
  36. if (fork() == 0) {
  37. unsigned char locked = 0;
  38. // Child writes to pipe
  39. close(fds[0]);
  40. InterprocessSync* sync2 = new InterprocessSyncFile("test");
  41. InterprocessSyncLocker* locker2 = sync2->getLocker();
  42. if (!locker2->tryLock()) {
  43. locked = 1;
  44. }
  45. delete locker2;
  46. delete sync2;
  47. write(fds[1], &locked, sizeof(locked));
  48. close(fds[1]);
  49. exit(0);
  50. } else {
  51. unsigned char locked = 0;
  52. // Parent reads from pipe
  53. close(fds[1]);
  54. // Read status and set flag
  55. read(fds[0], &locked, sizeof(locked));
  56. if (locked == 1) {
  57. was_locked = true;
  58. } else {
  59. was_locked = false;
  60. }
  61. close(fds[0]);
  62. }
  63. EXPECT_TRUE(was_locked);
  64. EXPECT_TRUE(locker->unlock());
  65. delete locker;
  66. delete sync;
  67. }
  68. TEST_F(InterprocessSyncFileTest, TestMultipleFilesDirect) {
  69. InterprocessSync* sync = new InterprocessSyncFile("test1");
  70. InterprocessSyncLocker* locker = sync->getLocker();
  71. EXPECT_TRUE(locker->lock());
  72. InterprocessSync* sync2 = new InterprocessSyncFile("test2");
  73. InterprocessSyncLocker* locker2 = sync2->getLocker();
  74. EXPECT_TRUE(locker2->lock());
  75. EXPECT_TRUE(locker2->unlock());
  76. delete sync2;
  77. delete locker2;
  78. EXPECT_TRUE(locker->unlock());
  79. delete locker;
  80. delete sync;
  81. }
  82. TEST_F(InterprocessSyncFileTest, TestMultipleFilesForked) {
  83. InterprocessSync* sync = new InterprocessSyncFile("test");
  84. InterprocessSyncLocker* locker = sync->getLocker();
  85. EXPECT_TRUE(locker->lock());
  86. int fds[2];
  87. bool was_not_locked(true);
  88. pipe(fds);
  89. if (fork() == 0) {
  90. unsigned char locked = 0xff;
  91. // Child writes to pipe
  92. close(fds[0]);
  93. InterprocessSync* sync2 = new InterprocessSyncFile("test2");
  94. InterprocessSyncLocker* locker2 = sync2->getLocker();
  95. if (locker2->tryLock()) {
  96. locked = 0;
  97. }
  98. delete locker2;
  99. delete sync2;
  100. write(fds[1], &locked, sizeof(locked));
  101. close(fds[1]);
  102. exit(0);
  103. } else {
  104. unsigned char locked = 0xff;
  105. // Parent reads from pipe
  106. close(fds[1]);
  107. // Read status and set flag
  108. read(fds[0], &locked, sizeof(locked));
  109. if (locked == 0) {
  110. was_not_locked = true;
  111. } else {
  112. was_not_locked = false;
  113. }
  114. close(fds[0]);
  115. }
  116. EXPECT_TRUE(was_not_locked);
  117. EXPECT_TRUE(locker->unlock());
  118. delete locker;
  119. delete sync;
  120. }
  121. } // namespace util
  122. } // namespace isc