summaryrefslogtreecommitdiff
path: root/test/sbuild/lock.cc
blob: 9e2644d208c4d30f51ed990312ffa9ef4ac76278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Copyright © 2006-2013  Roger Leigh <rleigh@debian.org>
 *
 * schroot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * schroot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *********************************************************************/

#include <gtest/gtest.h>

#include <sbuild/lock.h>

#include <iostream>

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <config.h>

class FileLockParameters
{
public:
  sbuild::lock::type initial;
  sbuild::lock::type establish;
  bool willthrow;

  FileLockParameters(sbuild::lock::type initial,
                     sbuild::lock::type establish,
                     bool               willthrow):
    initial(initial),
    establish(establish),
    willthrow(willthrow)
  {}
};

class FileLock : public ::testing::TestWithParam<FileLockParameters>
{
public:
  int fd;
  sbuild::file_lock *lck;

  FileLock():
    fd(-1),
    lck(0)
  {
    // Remove test file if it exists.
    unlink(TESTDATADIR "/filelock.ex1");
  }

  virtual ~FileLock()
  {}

  void SetUp()
  {
    fd = open(TESTDATADIR "/filelock.ex1", O_RDWR|O_EXCL|O_CREAT, 0600);
    ASSERT_GE(fd, 0);

    ssize_t wsize = write(fd,
                          "This file exists in order to test "
                          "sbuild::file_lock locking.\n", 61);
    ASSERT_EQ(wsize, 61);

    lck = new sbuild::file_lock(fd);
    ASSERT_NE(lck, nullptr);
  }

  void TearDown()
  {
    ASSERT_NE(lck, nullptr);
    lck->unset_lock();
    delete lck;

    ASSERT_EQ(close(fd), 0);
    ASSERT_EQ(unlink(TESTDATADIR "/filelock.ex1"), 0);
  }
};

TEST_P(FileLock, Locking)
{
  const FileLockParameters& params = GetParam();

  lck->unset_lock();
  int pid = fork();
  ASSERT_GE(pid, 0);
  if (pid == 0)
    {
      try
        {
          lck->set_lock(params.initial, 1);
          // Note: can cause unexpected success if < 4.  Set to 8 to
          // allow for slow or heavily-loaded machines.
          sleep(4);
          lck->unset_lock();
        }
      catch (const std::exception& e)
        {
          try
            {
              lck->unset_lock();
            }
          catch (const std::exception& ignore)
            {
            }
          std::cerr << "Child fail: " << e.what() << std::endl;
            _exit(EXIT_FAILURE);
        }
      _exit(EXIT_SUCCESS);
    }
  else
    {
      try
        {
          sleep(2);
          lck->set_lock(params.establish, 1);

          int status;
          ASSERT_GE(waitpid(pid, &status, 0), 0);
          ASSERT_EQ(WIFEXITED(status) && WEXITSTATUS(status), 0);
          ASSERT_FALSE(params.willthrow);
        }
      catch (const std::exception& e)
        {
          int status;
          waitpid(pid, &status, 0);
          ASSERT_TRUE(params.willthrow);
        }
    }
}

FileLockParameters params[] =
  {
    FileLockParameters(sbuild::lock::LOCK_NONE,      sbuild::lock::LOCK_NONE,      false),
    FileLockParameters(sbuild::lock::LOCK_NONE,      sbuild::lock::LOCK_SHARED,    false),
    FileLockParameters(sbuild::lock::LOCK_NONE,      sbuild::lock::LOCK_EXCLUSIVE, false),
    FileLockParameters(sbuild::lock::LOCK_SHARED,    sbuild::lock::LOCK_NONE,      false),
    FileLockParameters(sbuild::lock::LOCK_SHARED,    sbuild::lock::LOCK_SHARED,    false),
    FileLockParameters(sbuild::lock::LOCK_SHARED,    sbuild::lock::LOCK_EXCLUSIVE, true),
    FileLockParameters(sbuild::lock::LOCK_EXCLUSIVE, sbuild::lock::LOCK_NONE,      false),
    FileLockParameters(sbuild::lock::LOCK_EXCLUSIVE, sbuild::lock::LOCK_SHARED,    true),
    FileLockParameters(sbuild::lock::LOCK_EXCLUSIVE, sbuild::lock::LOCK_EXCLUSIVE, true)
  };

INSTANTIATE_TEST_CASE_P(LockVariants, FileLock, ::testing::ValuesIn(params));