interprocess_sync_file_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. InterprocessSyncFile sync("test");
  25. InterprocessSyncLocker locker(sync);
  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. InterprocessSyncFile sync2("test");
  41. InterprocessSyncLocker locker2(sync2);
  42. if (!locker2.tryLock()) {
  43. locked = 1;
  44. }
  45. write(fds[1], &locked, sizeof(locked));
  46. close(fds[1]);
  47. exit(0);
  48. } else {
  49. unsigned char locked = 0;
  50. // Parent reads from pipe
  51. close(fds[1]);
  52. // Read status and set flag
  53. read(fds[0], &locked, sizeof(locked));
  54. if (locked == 1) {
  55. was_locked = true;
  56. } else {
  57. was_locked = false;
  58. }
  59. close(fds[0]);
  60. }
  61. EXPECT_TRUE(was_locked);
  62. EXPECT_TRUE(locker.unlock());
  63. }
  64. TEST_F(InterprocessSyncFileTest, TestMultipleFilesDirect) {
  65. InterprocessSyncFile sync("test1");
  66. InterprocessSyncLocker locker(sync);
  67. EXPECT_TRUE(locker.lock());
  68. InterprocessSyncFile sync2("test2");
  69. InterprocessSyncLocker locker2(sync2);
  70. EXPECT_TRUE(locker2.lock());
  71. EXPECT_TRUE(locker2.unlock());
  72. EXPECT_TRUE(locker.unlock());
  73. }
  74. TEST_F(InterprocessSyncFileTest, TestMultipleFilesForked) {
  75. InterprocessSyncFile sync("test");
  76. InterprocessSyncLocker locker(sync);
  77. EXPECT_TRUE(locker.lock());
  78. int fds[2];
  79. bool was_not_locked(true);
  80. pipe(fds);
  81. if (fork() == 0) {
  82. unsigned char locked = 0xff;
  83. // Child writes to pipe
  84. close(fds[0]);
  85. InterprocessSyncFile sync2("test2");
  86. InterprocessSyncLocker locker2(sync2);
  87. if (locker2.tryLock()) {
  88. locked = 0;
  89. }
  90. write(fds[1], &locked, sizeof(locked));
  91. close(fds[1]);
  92. exit(0);
  93. } else {
  94. unsigned char locked = 0xff;
  95. // Parent reads from pipe
  96. close(fds[1]);
  97. // Read status and set flag
  98. read(fds[0], &locked, sizeof(locked));
  99. if (locked == 0) {
  100. was_not_locked = true;
  101. } else {
  102. was_not_locked = false;
  103. }
  104. close(fds[0]);
  105. }
  106. EXPECT_TRUE(was_not_locked);
  107. EXPECT_TRUE(locker.unlock());
  108. }
  109. } // namespace util
  110. } // namespace isc