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
// Copyright (C) 2012-2015,2017 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <log/interprocess/interprocess_sync_file.h>

#include <util/unittests/check_valgrind.h>
#include <util/unittests/interprocess_util.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unistd.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc::log::interprocess;
using isc::util::unittests::parentReadState;

namespace {
TEST(InterprocessSyncFileTest, BadFile) {
    InterprocessSyncFile sync("/no-such--dir/or--file");
    InterprocessSyncLocker locker(sync);

    EXPECT_FALSE(locker.isLocked());
    ASSERT_THROW(locker.lock(), InterprocessSyncFileError);
}

TEST(InterprocessSyncFileTest, TestLock) {<--- syntax error
    InterprocessSyncFile sync("test");
    InterprocessSyncLocker locker(sync);

    EXPECT_FALSE(locker.isLocked());
    EXPECT_TRUE(locker.lock());
    EXPECT_TRUE(locker.isLocked());

    if (!isc::util::unittests::runningOnValgrind()) {

        int fds[2];

        // Here, we check that a lock has been taken by forking and
        // checking from the child that a lock exists. This has to be
        // done from a separate process as we test by trying to lock the
        // range again on the lock file. The lock attempt would pass if
        // done from the same process for the granted range. The lock
        // attempt must fail to pass our check.

        EXPECT_EQ(0, pipe(fds));

        if (fork() == 0) {
            unsigned char locked = 0;
            // Child writes to pipe
            close(fds[0]);

            InterprocessSyncFile sync2("test");
            InterprocessSyncLocker locker2(sync2);

            if (!locker2.tryLock()) {
                EXPECT_FALSE(locker2.isLocked());
                locked = 1;
            } else {
                EXPECT_TRUE(locker2.isLocked());
            }

            ssize_t bytes_written = write(fds[1], &locked, sizeof(locked));
            EXPECT_EQ(sizeof(locked), bytes_written);

            close(fds[1]);
            exit(0);
        } else {
            // Parent reads from pipe
            close(fds[1]);

            const unsigned char locked = parentReadState(fds[0]);

            close(fds[0]);

            EXPECT_EQ(1, locked);
        }
    }

    EXPECT_TRUE(locker.unlock());
    EXPECT_FALSE(locker.isLocked());

    EXPECT_EQ (0, remove(TEST_DATA_TOPBUILDDIR "/test_lockfile"));
}

TEST(InterprocessSyncFileTest, TestMultipleFilesDirect) {
  InterprocessSyncFile sync("test1");
  InterprocessSyncLocker locker(sync);

  EXPECT_TRUE(locker.lock());

  InterprocessSyncFile sync2("test2");
  InterprocessSyncLocker locker2(sync2);
  EXPECT_TRUE(locker2.lock());
  EXPECT_TRUE(locker2.unlock());

  EXPECT_TRUE(locker.unlock());

  EXPECT_EQ (0, remove(TEST_DATA_TOPBUILDDIR "/test1_lockfile"));
  EXPECT_EQ (0, remove(TEST_DATA_TOPBUILDDIR "/test2_lockfile"));
}

TEST(InterprocessSyncFileTest, TestMultipleFilesForked) {
    InterprocessSyncFile sync("test1");
    InterprocessSyncLocker locker(sync);

    EXPECT_TRUE(locker.lock());

    if (!isc::util::unittests::runningOnValgrind()) {

        int fds[2];

        EXPECT_EQ(0, pipe(fds));

        if (fork() == 0) {
            unsigned char locked = 0xff;
            // Child writes to pipe
            close(fds[0]);

            InterprocessSyncFile sync2("test2");
            InterprocessSyncLocker locker2(sync2);

            if (locker2.tryLock()) {
                locked = 0;
            }

            ssize_t bytes_written = write(fds[1], &locked, sizeof(locked));
            EXPECT_EQ(sizeof(locked), bytes_written);

            close(fds[1]);
            exit(0);
        } else {
            // Parent reads from pipe
            close(fds[1]);

            const unsigned char locked = parentReadState(fds[0]);

            close(fds[0]);

            EXPECT_EQ(0, locked);
        }

        EXPECT_EQ (0, remove(TEST_DATA_TOPBUILDDIR "/test2_lockfile"));
    }

    EXPECT_TRUE(locker.unlock());

    EXPECT_EQ (0, remove(TEST_DATA_TOPBUILDDIR "/test1_lockfile"));
}

} // unnamed namespace