tylerliu | afb27a0 | 2020-10-06 17:20:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert 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 General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include <name-assignments/assignment-random.hpp> |
| 22 | #include <name-assignments/assignment-param.hpp> |
| 23 | #include <name-assignments/assignment-hash.hpp> |
tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame^] | 24 | #include <name-assignments/assignment-or.hpp> |
tylerliu | afb27a0 | 2020-10-06 17:20:06 -0700 | [diff] [blame] | 25 | #include "test-common.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestNameAssignment) |
| 32 | BOOST_AUTO_TEST_CASE(NameAssignmentRandom) |
| 33 | { |
| 34 | AssignmentRandom randomAssignment; |
| 35 | auto func = randomAssignment.getFunction(""); |
| 36 | BOOST_CHECK_EQUAL(func(std::vector<std::tuple<std::string, std::string>>()).size(), 1); |
| 37 | BOOST_CHECK_EQUAL(func(std::vector<std::tuple<std::string, std::string>>()).begin()->size(), 1); |
| 38 | } |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(NameAssignmentParam) |
| 41 | { |
| 42 | AssignmentParam paramAssignment; |
tylerliu | 3f63c02 | 2020-10-07 11:08:29 -0700 | [diff] [blame] | 43 | auto func = paramAssignment.getFunction("/abc/xyz/"); |
tylerliu | afb27a0 | 2020-10-06 17:20:06 -0700 | [diff] [blame] | 44 | std::vector<std::tuple<std::string, std::string>> requirements; |
| 45 | requirements.emplace_back("abc", "123"); |
| 46 | BOOST_CHECK_EQUAL(func(requirements).size(), 0); |
| 47 | requirements.emplace_back("xyz", "789"); |
| 48 | BOOST_CHECK_EQUAL(func(requirements).size(), 1); |
| 49 | BOOST_CHECK_EQUAL(*func(requirements).begin(), Name("/123/789")); |
| 50 | requirements.emplace_back("fake", "456"); |
| 51 | BOOST_CHECK_EQUAL(func(requirements).size(), 1); |
| 52 | BOOST_CHECK_EQUAL(*func(requirements).begin(), Name("/123/789")); |
| 53 | requirements[1] = std::tuple<std::string, std::string>("xyz", ""); |
| 54 | BOOST_CHECK_EQUAL(func(requirements).size(), 0); |
| 55 | } |
| 56 | |
| 57 | BOOST_AUTO_TEST_CASE(NameAssignmentHash) |
| 58 | { |
| 59 | AssignmentHash hashAssignment; |
tylerliu | 3f63c02 | 2020-10-07 11:08:29 -0700 | [diff] [blame] | 60 | auto func = hashAssignment.getFunction("/abc/xyz"); |
tylerliu | afb27a0 | 2020-10-06 17:20:06 -0700 | [diff] [blame] | 61 | std::vector<std::tuple<std::string, std::string>> requirements; |
| 62 | requirements.emplace_back("abc", "123"); |
| 63 | BOOST_CHECK_EQUAL(func(requirements).size(), 0); |
| 64 | requirements.emplace_back("xyz", "789"); |
| 65 | BOOST_CHECK_EQUAL(func(requirements).size(), 1); |
| 66 | BOOST_CHECK_EQUAL(func(requirements).begin()->size(), 1); |
| 67 | requirements.emplace_back("fake", "456"); |
| 68 | BOOST_CHECK_EQUAL(func(requirements).size(), 1); |
| 69 | BOOST_CHECK_EQUAL(func(requirements).begin()->size(), 1); |
| 70 | requirements[1] = std::tuple<std::string, std::string>("xyz", ""); |
| 71 | BOOST_CHECK_EQUAL(func(requirements).size(), 1); |
| 72 | BOOST_CHECK_EQUAL(func(requirements).begin()->size(), 1); |
| 73 | } |
| 74 | |
tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame^] | 75 | BOOST_AUTO_TEST_CASE(NameAssignmentOr) |
| 76 | { |
| 77 | AssignmentParam paramAssignment; |
| 78 | AssignmentOr orAssignment; |
| 79 | std::list<NameAssignmentFunc> func2Subfuncs; |
| 80 | auto func1 = orAssignment.getFunction(func2Subfuncs); |
| 81 | BOOST_CHECK_EQUAL(func1(std::vector<std::tuple<std::string, std::string>>()).size(), 0); |
| 82 | |
| 83 | auto funcUnit = paramAssignment.getFunction("/abc/xyz/"); |
| 84 | func2Subfuncs.push_back(funcUnit); |
| 85 | func2Subfuncs.push_back(funcUnit); |
| 86 | auto func2 = orAssignment.getFunction(func2Subfuncs); |
| 87 | std::list<NameAssignmentFunc> func3Subfuncs; |
| 88 | func3Subfuncs.push_back(func2); |
| 89 | func3Subfuncs.push_back(funcUnit); |
| 90 | auto func3 = orAssignment.getFunction(func3Subfuncs); |
| 91 | std::vector<std::tuple<std::string, std::string>> requirements; |
| 92 | requirements.emplace_back("abc", "123"); |
| 93 | BOOST_CHECK_EQUAL(func3(requirements).size(), 0); |
| 94 | requirements.emplace_back("xyz", "789"); |
| 95 | BOOST_CHECK_EQUAL(func3(requirements).size(), 3); |
| 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(NameAssignmentOrOperatorString) |
| 99 | { |
| 100 | AssignmentOr orAssignment; |
| 101 | auto func = orAssignment.getFunction(R"({"param": "/abc/xyz/", "param": "/abc/xyz"})"); |
| 102 | std::vector<std::tuple<std::string, std::string>> requirements; |
| 103 | requirements.emplace_back("abc", "123"); |
| 104 | BOOST_CHECK_EQUAL(func(requirements).size(), 0); |
| 105 | requirements.emplace_back("xyz", "789"); |
| 106 | BOOST_CHECK_EQUAL(func(requirements).size(), 2); |
| 107 | } |
| 108 | |
tylerliu | afb27a0 | 2020-10-06 17:20:06 -0700 | [diff] [blame] | 109 | BOOST_AUTO_TEST_SUITE_END() |
| 110 | |
| 111 | } // namespace tests |
| 112 | } // namespace ndncert |
| 113 | } // namespace ndn |
| 114 | |
| 115 | |