blob: 868e7884ee36b435cd8055384a0a51c37236902d [file] [log] [blame]
tylerliuafb27a02020-10-06 17:20:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
tylerliuafb27a02020-10-06 17:20:06 -07004 *
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
Tianyuan Yu13aac732022-03-03 20:59:54 -080021#include "name-assignment/assignment-email.hpp"
Zhiyi Zhangc2c4ed22020-10-14 17:16:28 -070022#include "name-assignment/assignment-random.hpp"
23#include "name-assignment/assignment-param.hpp"
24#include "name-assignment/assignment-hash.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040025
Davide Pesavento829aff62022-05-15 20:30:34 -040026#include "tests/boost-test.hpp"
tylerliuafb27a02020-10-06 17:20:06 -070027
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040028namespace ndncert::tests {
tylerliuafb27a02020-10-06 17:20:06 -070029
30BOOST_AUTO_TEST_SUITE(TestNameAssignment)
Davide Pesavento0dc02012021-11-23 22:55:03 -050031
tylerliuafb27a02020-10-06 17:20:06 -070032BOOST_AUTO_TEST_CASE(NameAssignmentRandom)
33{
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070034 AssignmentRandom assignment;
tylerliu40226332020-11-11 15:37:16 -080035 BOOST_CHECK_EQUAL(assignment.assignName(std::multimap<std::string, std::string>()).size(), 1);
36 BOOST_CHECK_EQUAL(assignment.assignName(std::multimap<std::string, std::string>()).begin()->size(), 1);
tylerliuafb27a02020-10-06 17:20:06 -070037}
38
39BOOST_AUTO_TEST_CASE(NameAssignmentParam)
40{
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070041 AssignmentParam assignment("/abc/xyz");
tylerliu40226332020-11-11 15:37:16 -080042 std::multimap<std::string, std::string> params;
43 params.emplace("abc", "123");
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070044 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 0);
tylerliu40226332020-11-11 15:37:16 -080045 params.emplace("xyz", "789");
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070046 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 1);
47 BOOST_CHECK_EQUAL(*assignment.assignName(params).begin(), Name("/123/789"));
tylerliu40226332020-11-11 15:37:16 -080048 params.emplace("fake", "456");
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070049 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 1);
50 BOOST_CHECK_EQUAL(*assignment.assignName(params).begin(), Name("/123/789"));
tylerliu40226332020-11-11 15:37:16 -080051 params.find("xyz")->second = "";
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070052 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 0);
Tianyuan Yu13aac732022-03-03 20:59:54 -080053
54 AssignmentParam assignment2("/\"guest\"/email");
55 params.emplace("email", "1@1.com");
56 BOOST_CHECK_EQUAL(assignment2.assignName(params).size(), 1);
57 BOOST_CHECK_EQUAL(assignment2.assignName(params).begin()->toUri(), Name("/guest/1@1.com").toUri());
58
59 AssignmentParam assignment3("/\"/email");
60 BOOST_CHECK_EQUAL(assignment3.assignName(params).size(), 0);
tylerliuafb27a02020-10-06 17:20:06 -070061}
62
63BOOST_AUTO_TEST_CASE(NameAssignmentHash)
64{
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070065 AssignmentHash assignment("/abc/xyz");
tylerliu40226332020-11-11 15:37:16 -080066 std::multimap<std::string, std::string> params;
67 params.emplace("abc", "123");
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070068 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 0);
tylerliu40226332020-11-11 15:37:16 -080069 params.emplace("xyz", "789");
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070070 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 1);
71 BOOST_CHECK_EQUAL(assignment.assignName(params).begin()->size(), 2);
tylerliu40226332020-11-11 15:37:16 -080072 params.emplace("fake", "456");
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070073 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 1);
74 BOOST_CHECK_EQUAL(assignment.assignName(params).begin()->size(), 2);
tylerliu40226332020-11-11 15:37:16 -080075 params.find("xyz")->second = "";
Zhiyi Zhangcb8acf22020-10-07 19:35:16 -070076 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 1);
77 BOOST_CHECK_EQUAL(assignment.assignName(params).begin()->size(), 2);
tylerliubcd83482020-10-07 14:45:28 -070078}
79
Tianyuan Yu13aac732022-03-03 20:59:54 -080080BOOST_AUTO_TEST_CASE(NameAssignmentEmail)
81{
82 AssignmentEmail assignment("/edu/ucla");
83 std::multimap<std::string, std::string> params;
84 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 0);
85 params.emplace("email", "das@math.ucla.edu");
86 BOOST_CHECK_EQUAL(*assignment.assignName(params).begin(), Name("/math/das"));
87
88 params.clear();
89 params.emplace("email", "d/~.^as@6666=.9!");
90 BOOST_CHECK_EQUAL(assignment.assignName(params).size(), 1);
91 BOOST_CHECK_EQUAL(*assignment.assignName(params).begin(), Name("/9!/6666%3D/d%2F~.%5Eas"));
92}
93
Davide Pesavento0dc02012021-11-23 22:55:03 -050094BOOST_AUTO_TEST_SUITE_END() // TestNameAssignment
tylerliuafb27a02020-10-06 17:20:06 -070095
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040096} // namespace ndncert::tests