blob: a83217bcb3f880ac1fd14101ab63c10da18e6cee [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030using namespace ndn::security::validator_config;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080031
32BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080033BOOST_AUTO_TEST_SUITE(ValidatorConfig)
34
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035BOOST_FIXTURE_TEST_SUITE(TestFilter, KeyChainFixture)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080036
Eric Newberry17d7c472020-06-18 21:29:22 -070037#define CHECK_FOR_MATCHES(filter, same, longer, shorter, different) \
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050038 do { \
39 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/foo/bar", m_keyChain), \
40 InterestV02Pkt::makeState()), same); \
41 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/foo/bar", m_keyChain), \
42 InterestV03Pkt::makeState()), same); \
43 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/foo/bar", m_keyChain), \
44 DataPkt::makeState()), same); \
45 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/foo/bar/bar", m_keyChain), \
46 InterestV02Pkt::makeState()), longer); \
47 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/foo/bar/bar", m_keyChain), \
48 InterestV03Pkt::makeState()), longer); \
49 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/foo/bar/bar", m_keyChain), \
50 DataPkt::makeState()), longer); \
51 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/foo", m_keyChain), \
52 InterestV02Pkt::makeState()), shorter); \
53 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/foo", m_keyChain), \
54 InterestV03Pkt::makeState()), shorter); \
55 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/foo", m_keyChain), \
56 DataPkt::makeState()), shorter); \
57 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV02Pkt::makeName("/other/prefix", m_keyChain), \
58 InterestV02Pkt::makeState()), different); \
59 BOOST_CHECK_EQUAL(filter.match(tlv::Interest, InterestV03Pkt::makeName("/other/prefix", m_keyChain), \
60 InterestV03Pkt::makeState()), different); \
61 BOOST_CHECK_EQUAL(filter.match(tlv::Data, DataPkt::makeName("/other/prefix", m_keyChain), \
62 DataPkt::makeState()), different); \
63 } while (false)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080064
65BOOST_AUTO_TEST_CASE(RelationName)
66{
67 RelationNameFilter f1("/foo/bar", NameRelation::EQUAL);
68 CHECK_FOR_MATCHES(f1, true, false, false, false);
69
70 RelationNameFilter f2("/foo/bar", NameRelation::IS_PREFIX_OF);
71 CHECK_FOR_MATCHES(f2, true, true, false, false);
72
73 RelationNameFilter f3("/foo/bar", NameRelation::IS_STRICT_PREFIX_OF);
74 CHECK_FOR_MATCHES(f3, false, true, false, false);
75}
76
77BOOST_AUTO_TEST_CASE(RegexName)
78{
79 RegexNameFilter f1(Regex("^<foo><bar>$"));
80 CHECK_FOR_MATCHES(f1, true, false, false, false);
81
82 RegexNameFilter f2(Regex("^<foo><bar><>*$"));
83 CHECK_FOR_MATCHES(f2, true, true, false, false);
84
85 RegexNameFilter f3(Regex("^<foo><bar><>+$"));
86 CHECK_FOR_MATCHES(f3, false, true, false, false);
87}
88
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050089BOOST_FIXTURE_TEST_SUITE(Create, KeyChainFixture)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080090
91BOOST_AUTO_TEST_CASE(Errors)
92{
93 BOOST_CHECK_THROW(Filter::create(makeSection(""), "test-config"), Error);
94 BOOST_CHECK_THROW(Filter::create(makeSection("type unknown"), "test-config"), Error);
95 BOOST_CHECK_THROW(Filter::create(makeSection("type name"), "test-config"), Error);
96
97 std::string config = R"CONF(
98 type name
99 not-name-or-regex stuff
100 )CONF";
101 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
102
103 config = R"CONF(
104 type name
105 name /foo/bar
106 )CONF";
107 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
108
109 config = R"CONF(
110 type name
111 name /foo/bar
112 not-relation stuff
113 )CONF";
114 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
115
116 config = R"CONF(
117 type name
118 name /foo/bar
119 relation equal
120 not-end stuff
121 )CONF";
122 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
123
124 config = R"CONF(
125 type name
126 regex ^<foo><bar>$
127 not-end stuff
128 )CONF";
129 BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error);
130}
131
132BOOST_AUTO_TEST_CASE(NameFilter)
133{
134 std::string config = R"CONF(
135 type name
136 name /foo/bar
137 relation equal
138 )CONF";
139 auto f1 = Filter::create(makeSection(config), "test-config");
140 CHECK_FOR_MATCHES((*f1), true, false, false, false);
141
142 config = R"CONF(
143 type name
144 name /foo/bar
145 relation is-prefix-of
146 )CONF";
147 auto f2 = Filter::create(makeSection(config), "test-config");
148 CHECK_FOR_MATCHES((*f2), true, true, false, false);
149
150 config = R"CONF(
151 type name
152 name /foo/bar
153 relation is-strict-prefix-of
154 )CONF";
155 auto f3 = Filter::create(makeSection(config), "test-config");
156 CHECK_FOR_MATCHES((*f3), false, true, false, false);
157}
158
159BOOST_AUTO_TEST_CASE(RegexFilter)
160{
161 std::string config = R"CONF(
162 type name
163 regex ^<foo><bar>$
164 )CONF";
165 auto f1 = Filter::create(makeSection(config), "test-config");
166 CHECK_FOR_MATCHES((*f1), true, false, false, false);
167
168 config = R"CONF(
169 type name
170 regex ^<foo><bar><>*$
171 )CONF";
172 auto f2 = Filter::create(makeSection(config), "test-config");
173 CHECK_FOR_MATCHES((*f2), true, true, false, false);
174
175 config = R"CONF(
176 type name
177 regex ^<foo><bar><>+$
178 )CONF";
179 auto f3 = Filter::create(makeSection(config), "test-config");
180 CHECK_FOR_MATCHES((*f3), false, true, false, false);
181
182 config = R"CONF(
183 type name
184 regex ^<>*$
185 )CONF";
186 auto f4 = Filter::create(makeSection(config), "test-config");
187 CHECK_FOR_MATCHES((*f4), true, true, true, true);
188}
189
190BOOST_AUTO_TEST_SUITE_END() // Create
191
192BOOST_AUTO_TEST_SUITE_END() // TestFilter
193BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800194BOOST_AUTO_TEST_SUITE_END() // Security
195
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400196} // namespace ndn::tests