Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, 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 "identity-management-fixture.hpp" |
| 22 | #include "json-helper.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | namespace tests { |
| 27 | |
| 28 | BOOST_FIXTURE_TEST_SUITE(TestJsonHelper, IdentityManagementV2Fixture) |
| 29 | |
| 30 | BOOST_AUTO_TEST_CASE(GenerateProbeJson) |
| 31 | { |
| 32 | auto result = genResponseProbeJson(Name("/ndn/edu/ucla/cs/zhiyi/macbook"), |
| 33 | Name("/ndn/edu/ucla/cs/zhiyi/ca-info")); |
| 34 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_IDNENTIFIER), "/ndn/edu/ucla/cs/zhiyi/macbook"); |
| 35 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CA_INFO), "/ndn/edu/ucla/cs/zhiyi/ca-info"); |
| 36 | } |
| 37 | |
| 38 | BOOST_AUTO_TEST_CASE(GenerateNewResponseJson) |
| 39 | { |
| 40 | auto identity = addIdentity(Name("/ndn/site1")); |
| 41 | auto key = identity.getDefaultKey(); |
| 42 | auto cert = key.getDefaultCertificate(); |
| 43 | |
| 44 | CertificateRequest request(Name("/ndn/site1"), "598234759", cert); |
| 45 | std::list<std::tuple<std::string, std::string>> challenges; |
| 46 | challenges.push_back(std::make_tuple("PIN", "Please ask ca officer")); |
| 47 | challenges.push_back(std::make_tuple("EMAIL", "Please provide your email")); |
| 48 | auto result = genResponseNewJson(request, challenges); |
| 49 | |
| 50 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "pending"); |
| 51 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759"); |
| 52 | auto child = result.get_child(JSON_CHALLENGES); |
| 53 | auto it = child.begin(); |
| 54 | BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_TYPE), "PIN"); |
| 55 | BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_INSTRUCTION), |
| 56 | "Please ask ca officer"); |
| 57 | it++; |
| 58 | BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL"); |
| 59 | BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_INSTRUCTION), |
| 60 | "Please provide your email"); |
| 61 | } |
| 62 | |
| 63 | BOOST_AUTO_TEST_CASE(GeneratePollResponseJson) |
| 64 | { |
| 65 | auto identity = addIdentity(Name("/ndn/site1")); |
| 66 | auto key = identity.getDefaultKey(); |
| 67 | auto cert = key.getDefaultCertificate(); |
| 68 | |
| 69 | CertificateRequest request(Name("/ndn/site1"), "598234759", CertificateRequest::Verifying, |
| 70 | "Email", "NEED_CODE", "111", cert); |
| 71 | request.setChallengeInstruction("Please provide verification code"); |
| 72 | auto result = genResponsePollJson(request); |
| 73 | |
| 74 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "verifying"); |
| 75 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_TYPE), "Email"); |
| 76 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_STATUS), "NEED_CODE"); |
| 77 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_INSTRUCTION), |
| 78 | "Please provide verification code"); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(GenerateErrorJson) |
| 82 | { |
| 83 | auto result = genErrorJson("The certificate name already exists"); |
| 84 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "error"); |
| 85 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_ERROR_INFO), |
| 86 | "The certificate name already exists"); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_SUITE_END() // TestJsonHelper |
| 90 | |
| 91 | } // namespace tests |
| 92 | } // namespace ndncert |
| 93 | } // namespace ndn |