blob: 5b77a12470b76e81cd8228352089826e631cabdb [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"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040025#include "tests/unit/security/validator-config/common.hpp"
Eric Newberry17d7c472020-06-18 21:29:22 -070026#include "tests/unit/security/validator-fixture.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080027
28namespace ndn {
29namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040030inline namespace v2 {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080031namespace validator_config {
32namespace tests {
33
34using namespace ndn::tests;
Eric Newberry17d7c472020-06-18 21:29:22 -070035using namespace ndn::security::v2::tests;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080036
37BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080038BOOST_AUTO_TEST_SUITE(ValidatorConfig)
39
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050040BOOST_FIXTURE_TEST_SUITE(TestFilter, KeyChainFixture)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080041
Eric Newberry17d7c472020-06-18 21:29:22 -070042#define CHECK_FOR_MATCHES(filter, same, longer, shorter, different) \
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050043 do { \
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); \
68 } while (false)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080069
70BOOST_AUTO_TEST_CASE(RelationName)
71{
72 RelationNameFilter f1("/foo/bar", NameRelation::EQUAL);
73 CHECK_FOR_MATCHES(f1, true, false, false, false);
74
75 RelationNameFilter f2("/foo/bar", NameRelation::IS_PREFIX_OF);
76 CHECK_FOR_MATCHES(f2, true, true, false, false);
77
78 RelationNameFilter f3("/foo/bar", NameRelation::IS_STRICT_PREFIX_OF);
79 CHECK_FOR_MATCHES(f3, false, true, false, false);
80}
81
82BOOST_AUTO_TEST_CASE(RegexName)
83{
84 RegexNameFilter f1(Regex("^<foo><bar>$"));
85 CHECK_FOR_MATCHES(f1, true, false, false, false);
86
87 RegexNameFilter f2(Regex("^<foo><bar><>*$"));
88 CHECK_FOR_MATCHES(f2, true, true, false, false);
89
90 RegexNameFilter f3(Regex("^<foo><bar><>+$"));
91 CHECK_FOR_MATCHES(f3, false, true, false, false);
92}
93
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050094BOOST_FIXTURE_TEST_SUITE(Create, KeyChainFixture)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080095
96BOOST_AUTO_TEST_CASE(Errors)
97{
98 BOOST_CHECK_THROW(Filter::create(makeSection(""), "test-config"), Error);
99 BOOST_CHECK_THROW(Filter::create(makeSection("type unknown"), "test-config"), Error);
100 BOOST_CHECK_THROW(Filter::create(makeSection("type name"), "test-config"), Error);
101
102 std::string config = R"CONF(
103 type name
104 not-name-or-regex stuff
105 )CONF";
106 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
107
108 config = R"CONF(
109 type name
110 name /foo/bar
111 )CONF";
112 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
113
114 config = R"CONF(
115 type name
116 name /foo/bar
117 not-relation stuff
118 )CONF";
119 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
120
121 config = R"CONF(
122 type name
123 name /foo/bar
124 relation equal
125 not-end stuff
126 )CONF";
127 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
128
129 config = R"CONF(
130 type name
131 regex ^<foo><bar>$
132 not-end stuff
133 )CONF";
134 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
135}
136
137BOOST_AUTO_TEST_CASE(NameFilter)
138{
139 std::string config = R"CONF(
140 type name
141 name /foo/bar
142 relation equal
143 )CONF";
144 auto f1 = Filter::create(makeSection(config), "test-config");
145 CHECK_FOR_MATCHES((*f1), true, false, false, false);
146
147 config = R"CONF(
148 type name
149 name /foo/bar
150 relation is-prefix-of
151 )CONF";
152 auto f2 = Filter::create(makeSection(config), "test-config");
153 CHECK_FOR_MATCHES((*f2), true, true, false, false);
154
155 config = R"CONF(
156 type name
157 name /foo/bar
158 relation is-strict-prefix-of
159 )CONF";
160 auto f3 = Filter::create(makeSection(config), "test-config");
161 CHECK_FOR_MATCHES((*f3), false, true, false, false);
162}
163
164BOOST_AUTO_TEST_CASE(RegexFilter)
165{
166 std::string config = R"CONF(
167 type name
168 regex ^<foo><bar>$
169 )CONF";
170 auto f1 = Filter::create(makeSection(config), "test-config");
171 CHECK_FOR_MATCHES((*f1), true, false, false, false);
172
173 config = R"CONF(
174 type name
175 regex ^<foo><bar><>*$
176 )CONF";
177 auto f2 = Filter::create(makeSection(config), "test-config");
178 CHECK_FOR_MATCHES((*f2), true, true, false, false);
179
180 config = R"CONF(
181 type name
182 regex ^<foo><bar><>+$
183 )CONF";
184 auto f3 = Filter::create(makeSection(config), "test-config");
185 CHECK_FOR_MATCHES((*f3), false, true, false, false);
186
187 config = R"CONF(
188 type name
189 regex ^<>*$
190 )CONF";
191 auto f4 = Filter::create(makeSection(config), "test-config");
192 CHECK_FOR_MATCHES((*f4), true, true, true, true);
193}
194
195BOOST_AUTO_TEST_SUITE_END() // Create
196
197BOOST_AUTO_TEST_SUITE_END() // TestFilter
198BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800199BOOST_AUTO_TEST_SUITE_END() // Security
200
201} // namespace tests
202} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400203} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800204} // namespace security
205} // namespace ndn