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 (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <rbac.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/command_interpreter.h>
#include <testutils/gtest_utils.h>

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

namespace {

/// @brief Test fixture for checking RBAC response filters.
class ResponseFilterTest : public ::testing::Test {
public:

    /// @brief Constructor.
    ResponseFilterTest() {
        responseFilterTable.clear();
        ResponseFilter::initResponseFilterTable();
    }

    /// @brief Destructor.
    virtual ~ResponseFilterTest() {
        responseFilterTable.clear();
    }
};

/// @brief This test verifies that the table works as expected.
TEST_F(ResponseFilterTest, table) {<--- syntax error
    EXPECT_EQ(2, responseFilterTable.size());
}

/// @brief This test verifies that noop response filter works as expected.
TEST_F(ResponseFilterTest, noop) {
    RoleConfigPtr rc(new RoleConfig("my-role", AclPtr(), AclPtr(),
                                    true, true, ResponseFilterList()));
    ASSERT_TRUE(rc);
    ResponseFilterPtr rf(new NoopResponseFilter());
    ASSERT_TRUE(rf);
    EXPECT_EQ("noop", rf->getName());
    EXPECT_FALSE(rf->filter("foo", *rc, ConstElementPtr()));

    // Get the noop response filter from the table.
    rf = responseFilterTable["noop"];
    ASSERT_TRUE(rf);
    EXPECT_EQ("noop", rf->getName());
    EXPECT_FALSE(rf->filter("bar", *rc, ConstElementPtr()));
}

/// @brief This test verifies that list-commands response filter works
/// as expected.
TEST_F(ResponseFilterTest, listCommands) {
    // Response filter
    ResponseFilterPtr rf(new ListCommandsResponseFilter());
    ASSERT_TRUE(rf);
    EXPECT_EQ("list-commands", rf->getName());
    ResponseFilterList rfl;
    rfl.push_back(rf);

    // Get it from the table too.
    ResponseFilterPtr got = responseFilterTable["list-commands"];
    ASSERT_TRUE(got);
    EXPECT_EQ("list-commands", got->getName());

    // Role config.
    set<string> commands = { "foo", "bar", "xyz" };
    AclPtr accept(new CommandsAcl(commands));
    RoleConfigPtr rc(new RoleConfig("my-role", accept, AclPtr(),
                                    false, true, rfl));
    ASSERT_TRUE(rc);

    // Build a list-commands answer.
    ElementPtr answer = Element::createMap();
    ElementPtr result = Element::create(CONTROL_RESULT_SUCCESS);
    answer->set(CONTROL_RESULT, result);
    ElementPtr args = Element::createList();
    answer->set(CONTROL_ARGUMENTS, args);
    args->add(Element::create(string("list-commands")));
    args->add(Element::create(string("foo")));
    args->add(Element::create(string("bar")));
    string expected = answer->str();
    args->add(Element::create(string("abc")));
    args->add(Element::create(string("def")));
    ConstElementPtr answer1 = data::copy(answer);

    // Tests.
    EXPECT_FALSE(rf->filter("foo", *rc, ConstElementPtr()));
    EXPECT_FALSE(rf->filter("list-commands", *rc, ConstElementPtr()));
    EXPECT_FALSE(rf->filter("list-commands", *rc, Element::createList()));
    string arguments = "{ \"foo\": \"bar\" }";
    ConstElementPtr json = Element::fromJSON(arguments);
    EXPECT_FALSE(rf->filter("list-commands", *rc, json));
    arguments = "{ \"arguments\": { } }";
    json = Element::fromJSON(arguments);
    EXPECT_FALSE(rf->filter("list-commands", *rc, json));
    arguments = "{ \"arguments\": [ ] }";
    json = Element::fromJSON(arguments);
    EXPECT_FALSE(rf->filter("list-commands", *rc, json));

    EXPECT_TRUE(rf->filter("list-commands", *rc, answer));
    EXPECT_EQ(expected, answer->str());
    EXPECT_TRUE(rc->filter("list-commands", answer1));
    EXPECT_EQ(expected, answer1->str());
}

/// @brief This test verifies that response filter parsing works as expected.
TEST_F(ResponseFilterTest, parse) {
    ElementPtr cfg;
    string expected = "parse null response filter list";
    EXPECT_THROW_MSG(ResponseFilter::parse(cfg), BadValue, expected);

    cfg = Element::createMap();
    expected = "response filter list is not a list";
    EXPECT_THROW_MSG(ResponseFilter::parse(cfg), BadValue, expected);

    cfg = Element::createList();
    cfg->add(Element::create(1));
    expected = "response filter name is not a string";
    EXPECT_THROW_MSG(ResponseFilter::parse(cfg), BadValue, expected);

    cfg = Element::createList();
    cfg->add(Element::create(string()));
    expected = "response filter name is empty";
    EXPECT_THROW_MSG(ResponseFilter::parse(cfg), BadValue, expected);

    cfg = Element::createList();
    cfg->add(Element::create(string("foo")));
    expected = "unknown response filter 'foo'";
    EXPECT_THROW_MSG(ResponseFilter::parse(cfg), BadValue, expected);

    cfg = Element::createList();
    cfg->add(Element::create(string("noop")));
    cfg->add(Element::create(string("list-commands")));
    ResponseFilterList rfl;
    EXPECT_NO_THROW(rfl = ResponseFilter::parse(cfg));
    ASSERT_EQ(2, rfl.size());
    ResponseFilterPtr rf = rfl.front();
    ASSERT_TRUE(rf);
    EXPECT_EQ("noop", rf->getName());
    rf = rfl.back();
    ASSERT_TRUE(rf);
    EXPECT_EQ("list-commands", rf->getName());
}

} // end of anonymous namespace