Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 2 | /* |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 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 | |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 21 | #include <protocol-detail/error.hpp> |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 22 | #include "client-module.hpp" |
| 23 | #include "challenge-module.hpp" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 24 | #include "ca-module.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 25 | #include "test-common.hpp" |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | namespace tests { |
| 30 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 31 | BOOST_FIXTURE_TEST_SUITE(TestClientModule, IdentityManagementTimeFixture) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 32 | |
| 33 | BOOST_AUTO_TEST_CASE(ClientModuleInitialize) |
| 34 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 35 | ClientModule client(m_keyChain); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 36 | client.getClientConf().load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 37 | BOOST_CHECK_EQUAL(client.getClientConf().m_caItems.size(), 2); |
| 38 | } |
| 39 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(Probe) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 41 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | ClientModule client(m_keyChain); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 43 | client.getClientConf().load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 44 | |
Zhiyi Zhang | 1013078 | 2018-02-01 18:28:49 -0800 | [diff] [blame] | 45 | auto identity = addIdentity(Name("/site")); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 46 | auto key = identity.getDefaultKey(); |
| 47 | auto cert = key.getDefaultCertificate(); |
| 48 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 49 | CaConfigItem item; |
| 50 | item.m_probeParameterKeys.push_back("email"); |
| 51 | item.m_probeParameterKeys.push_back("uid"); |
| 52 | item.m_probeParameterKeys.push_back("name"); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 53 | item.m_caPrefix = Name("/site"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 54 | item.m_cert = std::make_shared<security::v2::Certificate>(cert); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 55 | client.getClientConf().m_caItems.push_back(item); |
| 56 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 57 | std::vector<std::tuple<std::string, std::string>> probeParams; |
| 58 | probeParams.push_back(std::make_tuple("email", "zhiyi@cs.ucla.edu")); |
| 59 | probeParams.push_back(std::make_tuple("uid", "987654321")); |
| 60 | probeParams.push_back(std::make_tuple("name", "Zhiyi Zhang")); |
| 61 | auto firstInterest = client.generateProbeInterest(item, std::move(probeParams)); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 62 | BOOST_CHECK(firstInterest->getName().at(-1).isParametersSha256Digest()); |
| 63 | // ignore the last name component (ParametersSha256Digest) |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(firstInterest->getName().getPrefix(-1), "/site/CA/PROBE"); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(readString(firstInterest->getApplicationParameters().get(tlv_parameter_value)), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 66 | "zhiyi@cs.ucla.edu"); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 67 | } |
| 68 | |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 69 | BOOST_AUTO_TEST_CASE(ErrorHandling) |
| 70 | { |
| 71 | auto identity = addIdentity(Name("/site")); |
| 72 | auto key = identity.getDefaultKey(); |
| 73 | auto cert = key.getDefaultCertificate(); |
| 74 | |
| 75 | ClientModule client(m_keyChain); |
| 76 | CaConfigItem item; |
| 77 | item.m_caPrefix = Name("/site"); |
| 78 | item.m_cert = std::make_shared<security::v2::Certificate>(cert); |
| 79 | client.getClientConf().m_caItems.push_back(item); |
| 80 | |
| 81 | client.generateProbeInterest(item,std::vector<std::tuple<std::string, std::string>>()); |
| 82 | |
| 83 | Data errorPacket; |
| 84 | errorPacket.setName(Name("/site/pretend/this/is/error/packet")); |
| 85 | errorPacket.setFreshnessPeriod(time::seconds(100)); |
| 86 | errorPacket.setContent(ErrorTLV::encodeDataContent(ErrorCode::NO_ERROR, "This is a test.")); |
| 87 | m_keyChain.sign(errorPacket, signingByIdentity(identity)); |
| 88 | |
| 89 | BOOST_CHECK_THROW(client.onProbeResponse(errorPacket), std::exception); |
| 90 | BOOST_CHECK_THROW(client.onNewRenewRevokeResponse(errorPacket), std::exception); |
| 91 | BOOST_CHECK_THROW(client.onChallengeResponse(errorPacket), std::exception); |
| 92 | } |
| 93 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 94 | // BOOST_AUTO_TEST_CASE(GenProbeRequestJson) |
| 95 | // { |
| 96 | // ClientModule client(m_keyChain); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 97 | // client.getClientConf().load("tests/unit-tests/config-files/config-client-1"); |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 98 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 99 | // auto identity = addIdentity(Name("/site")); |
| 100 | // auto key = identity.getDefaultKey(); |
| 101 | // auto cert = key.getDefaultCertificate(); |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 102 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 103 | // CaConfigItem item; |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 104 | // item.m_probe = "email:uid:name"; |
| 105 | // item.m_caPrefix = Name("/site"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 106 | // item.m_cert = std::make_shared<security::v2::Certificate>(cert); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 107 | // client.getClientConf().m_caItems.push_back(item); |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 108 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 109 | // auto interestPacket = client.genProbeRequestJson(item, "yufeng@ucla.edu:123456789:Yufeng Zhang"); |
| 110 | // BOOST_CHECK_EQUAL(interestPacket.get("email", ""), "yufeng@ucla.edu"); |
| 111 | // BOOST_CHECK_EQUAL(interestPacket.get("uid", ""), "123456789"); |
| 112 | // BOOST_CHECK_EQUAL(interestPacket.get("name", ""), "Yufeng Zhang"); |
| 113 | // } |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 114 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 115 | BOOST_AUTO_TEST_SUITE_END() // TestClientModule |
| 116 | |
| 117 | } // namespace tests |
| 118 | } // namespace ndncert |
| 119 | } // namespace ndn |