27 #include <boost/algorithm/string/predicate.hpp>
32 namespace validator_config {
36 const shared_ptr<ValidationState>& state)
53 , m_relation(relation)
59 const shared_ptr<ValidationState>& state)
65 std::ostringstream os;
66 os <<
"KeyLocator check failed: name relation " << m_name <<
" " << m_relation
67 <<
" for packet " << pktName <<
" is invalid"
68 <<
" (KeyLocator=" << klName <<
", identity=" << identity <<
")";
82 bool result = m_regex.
match(klName);
84 std::ostringstream os;
85 os <<
"KeyLocator check failed: regex " << m_regex <<
" for packet " << pktName <<
" is invalid"
86 <<
" (KeyLocator=" << klName <<
")";
93 const std::string& klNameExpr,
const std::string klNameExpand,
95 : m_hyperPRegex(pktNameExpr, pktNameExpand)
96 , m_hyperKRegex(klNameExpr, klNameExpand)
97 , m_hyperRelation(hyperRelation)
103 const shared_ptr<ValidationState>& state)
105 if (!m_hyperPRegex.
match(pktName) || !m_hyperKRegex.
match(klName)) {
106 std::ostringstream os;
107 os <<
"Packet " << pktName <<
" (" <<
"KeyLocator=" << klName <<
") does not match "
108 <<
"the hyper relation rule pkt=" << m_hyperPRegex <<
", key=" << m_hyperKRegex;
115 std::ostringstream os;
116 os <<
"KeyLocator check failed: hyper relation " << m_hyperRelation
117 <<
" pkt=" << m_hyperPRegex <<
", key=" << m_hyperKRegex
118 <<
" of packet " << pktName <<
" (KeyLocator=" << klName <<
") is invalid";
127 auto propertyIt = configSection.begin();
130 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type")) {
134 std::string type = propertyIt->second.data();
135 if (boost::iequals(type,
"customized")) {
136 return createCustomizedChecker(configSection, configFilename);
138 else if (boost::iequals(type,
"hierarchical")) {
139 return createHierarchicalChecker(configSection, configFilename);
147 Checker::createCustomizedChecker(
const ConfigSection& configSection,
148 const std::string& configFilename)
150 auto propertyIt = configSection.begin();
155 if (propertyIt != configSection.end() && boost::iequals(propertyIt->first,
"sig-type")) {
161 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"key-locator")) {
162 NDN_THROW(Error(
"Expecting <checker.key-locator>"));
165 auto checker = createKeyLocatorChecker(propertyIt->second, configFilename);
168 if (propertyIt != configSection.end()) {
169 NDN_THROW(Error(
"Expecting end of <checker>"));
175 Checker::createHierarchicalChecker(
const ConfigSection& configSection,
176 const std::string& configFilename)
178 auto propertyIt = configSection.begin();
183 if (propertyIt != configSection.end() && boost::iequals(propertyIt->first,
"sig-type")) {
188 if (propertyIt != configSection.end()) {
189 NDN_THROW(Error(
"Expecting end of <checker>"));
191 return make_unique<HyperRelationChecker>(
"^(<>*)$",
"\\1",
192 "^(<>*)<KEY><>$",
"\\1",
197 Checker::createKeyLocatorChecker(
const ConfigSection& configSection,
198 const std::string& configFilename)
200 auto propertyIt = configSection.begin();
203 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
204 NDN_THROW(Error(
"Expecting <checker.key-locator.type>"));
206 std::string type = propertyIt->second.data();
207 if (boost::iequals(type,
"name"))
208 return createKeyLocatorNameChecker(configSection, configFilename);
210 NDN_THROW(Error(
"Unrecognized <checker.key-locator.type>: " + type));
214 Checker::createKeyLocatorNameChecker(
const ConfigSection& configSection,
215 const std::string& configFilename)
217 auto propertyIt = configSection.begin();
220 if (propertyIt == configSection.end())
221 NDN_THROW(Error(
"Unexpected end of <checker.key-locator>"));
223 if (boost::iequals(propertyIt->first,
"name")) {
226 name =
Name(propertyIt->second.data());
229 NDN_THROW_NESTED(Error(
"Invalid <checker.key-locator.name>: " + propertyIt->second.data()));
233 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"relation")) {
234 NDN_THROW(Error(
"Expecting <checker.key-locator.relation>"));
237 std::string relationString = propertyIt->second.data();
242 if (propertyIt != configSection.end()) {
243 NDN_THROW(Error(
"Expecting end of <checker.key-locator>"));
245 return make_unique<NameRelationChecker>(
name, relation);
247 else if (boost::iequals(propertyIt->first,
"regex")) {
248 std::string regexString = propertyIt->second.data();
251 if (propertyIt != configSection.end()) {
252 NDN_THROW(Error(
"Expecting end of <checker.key-locator>"));
256 return make_unique<RegexChecker>(
Regex(regexString));
258 catch (
const Regex::Error&) {
259 NDN_THROW_NESTED(Error(
"Invalid <checker.key-locator.regex>: " + regexString));
262 else if (boost::iequals(propertyIt->first,
"hyper-relation")) {
264 auto hPropertyIt = hSection.begin();
267 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-regex")) {
268 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.k-regex>"));
271 std::string kRegex = hPropertyIt->second.data();
275 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-expand")) {
276 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.k-expand>"));
279 std::string kExpand = hPropertyIt->second.data();
283 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"h-relation")) {
284 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.h-relation>"));
287 std::string hRelation = hPropertyIt->second.data();
291 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-regex")) {
292 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.p-regex>"));
295 std::string pRegex = hPropertyIt->second.data();
299 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-expand")) {
300 NDN_THROW(Error(
"Expecting <checker.key-locator.hyper-relation.p-expand>"));
303 std::string pExpand = hPropertyIt->second.data();
306 if (hPropertyIt != hSection.end()) {
307 NDN_THROW(Error(
"Expecting end of <checker.key-locator.hyper-relation>"));
312 return make_unique<HyperRelationChecker>(pRegex, pExpand, kRegex, kExpand, relation);
314 catch (
const Regex::Error&) {
319 NDN_THROW(Error(
"Unrecognized <checker.key-locator>: " + propertyIt->first));