blob: 9f6029e62c157ddb530d610b83092dca80eebb9a [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Alexander Afanasyev09236c22020-06-03 13:42:38 -04003 * Copyright (c) 2013-2020 Regents of the University of California.
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Alexander Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/validator-config/filter.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25#include "tests/identity-management-fixture.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040026#include "tests/unit/security/validator-config/common.hpp"
Eric Newberry17d7c472020-06-18 21:29:22 -070027#include "tests/unit/security/validator-fixture.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080028
29namespace ndn {
30namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040031inline namespace v2 {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080032namespace validator_config {
33namespace tests {
34
35using namespace ndn::tests;
Eric Newberry17d7c472020-06-18 21:29:22 -070036using namespace ndn::security::v2::tests;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080037
38BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080039BOOST_AUTO_TEST_SUITE(ValidatorConfig)
40
Eric Newberry17d7c472020-06-18 21:29:22 -070041BOOST_FIXTURE_TEST_SUITE(TestFilter, IdentityManagementFixture)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080042
Eric Newberry17d7c472020-06-18 21:29:22 -070043#define CHECK_FOR_MATCHES(filter, same, longer, shorter, different) \
44 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/foo/bar", m_keyChain), \
45 InterestV02Pkt::makeState()), same); \
46 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/foo/bar", m_keyChain), \
47 InterestV03Pkt::makeState()), same); \
48 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/foo/bar", m_keyChain), \
49 DataPkt::makeState()), same); \
50 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/foo/bar/bar", m_keyChain), \
51 InterestV02Pkt::makeState()), longer); \
52 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/foo/bar/bar", m_keyChain), \
53 InterestV03Pkt::makeState()), longer); \
54 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/foo/bar/bar", m_keyChain), \
55 DataPkt::makeState()), longer); \
56 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/foo", m_keyChain), \
57 InterestV02Pkt::makeState()), shorter); \
58 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/foo", m_keyChain), \
59 InterestV03Pkt::makeState()), shorter); \
60 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/foo", m_keyChain), \
61 DataPkt::makeState()), shorter); \
62 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/other/prefix", m_keyChain), \
63 InterestV02Pkt::makeState()), different); \
64 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/other/prefix", m_keyChain), \
65 InterestV03Pkt::makeState()), different); \
66 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/other/prefix", m_keyChain), \
67 DataPkt::makeState()), different);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080068
69BOOST_AUTO_TEST_CASE(RelationName)
70{
71 RelationNameFilter f1("/foo/bar", NameRelation::EQUAL);
72 CHECK_FOR_MATCHES(f1, true, false, false, false);
73
74 RelationNameFilter f2("/foo/bar", NameRelation::IS_PREFIX_OF);
75 CHECK_FOR_MATCHES(f2, true, true, false, false);
76
77 RelationNameFilter f3("/foo/bar", NameRelation::IS_STRICT_PREFIX_OF);
78 CHECK_FOR_MATCHES(f3, false, true, false, false);
79}
80
81BOOST_AUTO_TEST_CASE(RegexName)
82{
83 RegexNameFilter f1(Regex("^<foo><bar>$"));
84 CHECK_FOR_MATCHES(f1, true, false, false, false);
85
86 RegexNameFilter f2(Regex("^<foo><bar><>*$"));
87 CHECK_FOR_MATCHES(f2, true, true, false, false);
88
89 RegexNameFilter f3(Regex("^<foo><bar><>+$"));
90 CHECK_FOR_MATCHES(f3, false, true, false, false);
91}
92
Eric Newberry17d7c472020-06-18 21:29:22 -070093BOOST_FIXTURE_TEST_SUITE(Create, IdentityManagementFixture)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080094
95BOOST_AUTO_TEST_CASE(Errors)
96{
97 BOOST_CHECK_THROW(Filter::create(makeSection(""), "test-config"), Error);
98 BOOST_CHECK_THROW(Filter::create(makeSection("type unknown"), "test-config"), Error);
99 BOOST_CHECK_THROW(Filter::create(makeSection("type name"), "test-config"), Error);
100
101 std::string config = R"CONF(
102 type name
103 not-name-or-regex stuff
104 )CONF";
105 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
106
107 config = R"CONF(
108 type name
109 name /foo/bar
110 )CONF";
111 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
112
113 config = R"CONF(
114 type name
115 name /foo/bar
116 not-relation stuff
117 )CONF";
118 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
119
120 config = R"CONF(
121 type name
122 name /foo/bar
123 relation equal
124 not-end stuff
125 )CONF";
126 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
127
128 config = R"CONF(
129 type name
130 regex ^<foo><bar>$
131 not-end stuff
132 )CONF";
133 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
134}
135
136BOOST_AUTO_TEST_CASE(NameFilter)
137{
138 std::string config = R"CONF(
139 type name
140 name /foo/bar
141 relation equal
142 )CONF";
143 auto f1 = Filter::create(makeSection(config), "test-config");
144 CHECK_FOR_MATCHES((*f1), true, false, false, false);
145
146 config = R"CONF(
147 type name
148 name /foo/bar
149 relation is-prefix-of
150 )CONF";
151 auto f2 = Filter::create(makeSection(config), "test-config");
152 CHECK_FOR_MATCHES((*f2), true, true, false, false);
153
154 config = R"CONF(
155 type name
156 name /foo/bar
157 relation is-strict-prefix-of
158 )CONF";
159 auto f3 = Filter::create(makeSection(config), "test-config");
160 CHECK_FOR_MATCHES((*f3), false, true, false, false);
161}
162
163BOOST_AUTO_TEST_CASE(RegexFilter)
164{
165 std::string config = R"CONF(
166 type name
167 regex ^<foo><bar>$
168 )CONF";
169 auto f1 = Filter::create(makeSection(config), "test-config");
170 CHECK_FOR_MATCHES((*f1), true, false, false, false);
171
172 config = R"CONF(
173 type name
174 regex ^<foo><bar><>*$
175 )CONF";
176 auto f2 = Filter::create(makeSection(config), "test-config");
177 CHECK_FOR_MATCHES((*f2), true, true, false, false);
178
179 config = R"CONF(
180 type name
181 regex ^<foo><bar><>+$
182 )CONF";
183 auto f3 = Filter::create(makeSection(config), "test-config");
184 CHECK_FOR_MATCHES((*f3), false, true, false, false);
185
186 config = R"CONF(
187 type name
188 regex ^<>*$
189 )CONF";
190 auto f4 = Filter::create(makeSection(config), "test-config");
191 CHECK_FOR_MATCHES((*f4), true, true, true, true);
192}
193
194BOOST_AUTO_TEST_SUITE_END() // Create
195
196BOOST_AUTO_TEST_SUITE_END() // TestFilter
197BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800198BOOST_AUTO_TEST_SUITE_END() // Security
199
200} // namespace tests
201} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400202} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800203} // namespace security
204} // namespace ndn