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
// Copyright (C) 2015-2024 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 <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <testutils/sandbox.h>
#include <asiolink/io_service.h>
#include <config/unix_command_mgr.h>
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace isc::config;
using namespace isc::data;
using namespace std;

// Test class for Unix Command Manager
class UnixCommandMgrTest : public ::testing::Test {
public:
    isc::test::Sandbox sandbox;

    /// Default constructor
    UnixCommandMgrTest() : io_service_(new IOService()) {
        UnixCommandMgr::instance().setIOService(io_service_);
        UnixCommandMgr::instance().closeCommandSocket();
    }

    /// Default destructor
    virtual ~UnixCommandMgrTest() {
        UnixCommandMgr::instance().closeCommandSocket();
    }

    /// @brief Returns socket path (using either hardcoded path or env variable)
    /// @return path to the unix socket
    std::string getSocketPath() {
        std::string socket_path;
        const char* env = getenv("KEA_SOCKET_TEST_DIR");
        if (env) {
            socket_path = std::string(env) + "/test-socket";
        } else {
            socket_path = sandbox.join("test-socket");
        }
        return (socket_path);
    }

    /// @brief IO service used by these tests.
    IOServicePtr io_service_;
};

// This test verifies that a Unix socket can be opened properly and that input
// parameters (socket-type and socket-name) are verified.
TEST_F(UnixCommandMgrTest, unixCreate) {
    // Null pointer is obviously a bad idea.
    EXPECT_THROW(UnixCommandMgr::instance().openCommandSocket(ConstElementPtr()),
                 isc::config::BadSocketInfo);

    // So is passing no parameters.
    ElementPtr socket_info = Element::createMap();
    EXPECT_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info),
                 isc::config::BadSocketInfo);

    // We don't support ipx sockets
    socket_info->set("socket-type", Element::create("ipx"));
    EXPECT_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info),
                 isc::config::BadSocketInfo);

    socket_info->set("socket-type", Element::create("unix"));
    EXPECT_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info),
                 isc::config::BadSocketInfo);

    socket_info->set("socket-name", Element::create(getSocketPath()));
    EXPECT_NO_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info));
    EXPECT_GE(UnixCommandMgr::instance().getControlSocketFD(), 0);

    // It should be possible to close the socket.
    EXPECT_NO_THROW(UnixCommandMgr::instance().closeCommandSocket());
}

// This test checks that when unix path is too long, the socket cannot be opened.
TEST_F(UnixCommandMgrTest, unixCreateTooLong) {
    ElementPtr socket_info = Element::fromJSON("{ \"socket-type\": \"unix\","
        "\"socket-name\": \"/tmp/toolongtoolongtoolongtoolongtoolongtoolong"
        "toolongtoolongtoolongtoolongtoolongtoolongtoolongtoolongtoolong"
        "\" }");

    EXPECT_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info),
                 SocketError);
}

// Verifies that a socket cannot be concurrently opened more than once.
TEST_F(UnixCommandMgrTest, exclusiveOpen) {
    // Pass in valid parameters.
    ElementPtr socket_info = Element::createMap();
    socket_info->set("socket-type", Element::create("unix"));
    socket_info->set("socket-name", Element::create(getSocketPath()));

    EXPECT_NO_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info));
    EXPECT_GE(UnixCommandMgr::instance().getControlSocketFD(), 0);

    // Should not be able to open it twice.
    EXPECT_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info),
                 isc::config::SocketError);

    // Now let's close it.
    EXPECT_NO_THROW(UnixCommandMgr::instance().closeCommandSocket());

    // Should be able to re-open it now.
    EXPECT_NO_THROW(UnixCommandMgr::instance().openCommandSocket(socket_info));
    EXPECT_GE(UnixCommandMgr::instance().getControlSocketFD(), 0);

    // Now let's close it.
    EXPECT_NO_THROW(UnixCommandMgr::instance().closeCommandSocket());
}