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 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 21 | #include "boost-test.hpp" |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 22 | #include "json-helper.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | namespace tests { |
| 27 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 28 | BOOST_AUTO_TEST_SUITE(TestJsonHelper) |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 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 | { |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 40 | std::list<std::string> challenges; |
| 41 | challenges.push_back("PIN"); |
| 42 | challenges.push_back("EMAIL"); |
Zhiyi Zhang | 0fd71cc | 2017-03-01 10:18:58 -0800 | [diff] [blame] | 43 | auto result = genResponseNewJson("598234759", "wait-selection", challenges); |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 44 | |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759"); |
Zhiyi Zhang | 0fd71cc | 2017-03-01 10:18:58 -0800 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "wait-selection"); |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 47 | auto child = result.get_child(JSON_CHALLENGES); |
| 48 | auto it = child.begin(); |
| 49 | BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_TYPE), "PIN"); |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 50 | it++; |
| 51 | BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL"); |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 54 | BOOST_AUTO_TEST_CASE(GenerateChallengeResponseJson) |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 55 | { |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 56 | auto result = genResponseChallengeJson("598234759", "EMAIL", "need-code"); |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 57 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759"); |
| 59 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL"); |
| 60 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "need-code"); |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 61 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 62 | result = genResponseChallengeJson("598234759", "EMAIL", "need-code", Name("/ndn/test")); |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759"); |
| 65 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL"); |
| 66 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "need-code"); |
| 67 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_CERTIFICATE), "/ndn/test"); |
Zhiyi Zhang | 19768a5 | 2017-01-18 14:44:15 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE(GenerateErrorJson) |
| 71 | { |
| 72 | auto result = genErrorJson("The certificate name already exists"); |
| 73 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "error"); |
| 74 | BOOST_CHECK_EQUAL(result.get<std::string>(JSON_ERROR_INFO), |
| 75 | "The certificate name already exists"); |
| 76 | } |
| 77 | |
| 78 | BOOST_AUTO_TEST_SUITE_END() // TestJsonHelper |
| 79 | |
| 80 | } // namespace tests |
| 81 | } // namespace ndncert |
| 82 | } // namespace ndn |