blob: b089e4e05342f7aac02f9f202c815eaddfcb84fc [file] [log] [blame]
Zhiyi Zhang19768a52017-01-18 14:44:15 -08001/* -*- 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 Zhang3b267e62017-02-09 17:59:34 -080021#include "boost-test.hpp"
Zhiyi Zhang19768a52017-01-18 14:44:15 -080022#include "json-helper.hpp"
23
24namespace ndn {
25namespace ndncert {
26namespace tests {
27
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080028BOOST_AUTO_TEST_SUITE(TestJsonHelper)
Zhiyi Zhang19768a52017-01-18 14:44:15 -080029
30BOOST_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
38BOOST_AUTO_TEST_CASE(GenerateNewResponseJson)
39{
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080040 std::list<std::string> challenges;
41 challenges.push_back("PIN");
42 challenges.push_back("EMAIL");
Zhiyi Zhang0fd71cc2017-03-01 10:18:58 -080043 auto result = genResponseNewJson("598234759", "wait-selection", challenges);
Zhiyi Zhang19768a52017-01-18 14:44:15 -080044
Zhiyi Zhang19768a52017-01-18 14:44:15 -080045 BOOST_CHECK_EQUAL(result.get<std::string>(JSON_REQUEST_ID), "598234759");
Zhiyi Zhang0fd71cc2017-03-01 10:18:58 -080046 BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "wait-selection");
Zhiyi Zhang19768a52017-01-18 14:44:15 -080047 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 Zhang19768a52017-01-18 14:44:15 -080050 it++;
51 BOOST_CHECK_EQUAL(it->second.get<std::string>(JSON_CHALLENGE_TYPE), "EMAIL");
Zhiyi Zhang19768a52017-01-18 14:44:15 -080052}
53
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080054BOOST_AUTO_TEST_CASE(GenerateChallengeResponseJson)
Zhiyi Zhang19768a52017-01-18 14:44:15 -080055{
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080056 auto result = genResponseChallengeJson("598234759", "EMAIL", "need-code");
Zhiyi Zhang19768a52017-01-18 14:44:15 -080057
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080058 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 Zhang19768a52017-01-18 14:44:15 -080061
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080062 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 Zhang19768a52017-01-18 14:44:15 -080068}
69
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070070BOOST_AUTO_TEST_CASE(GenerateFailureJson)
Zhiyi Zhang19768a52017-01-18 14:44:15 -080071{
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070072 auto result = genFailureJson("598234759", "EMAIL", "failure",
73 "The certificate name already exists");
74 BOOST_CHECK_EQUAL(result.get<std::string>(JSON_STATUS), "failure");
75 BOOST_CHECK_EQUAL(result.get<std::string>(JSON_FAILURE_INFO),
Zhiyi Zhang19768a52017-01-18 14:44:15 -080076 "The certificate name already exists");
77}
78
79BOOST_AUTO_TEST_SUITE_END() // TestJsonHelper
80
81} // namespace tests
82} // namespace ndncert
83} // namespace ndn