blob: ad3ef83235b2b35282c7d5fddcd5c14d591bc46f [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -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
21#include "identity-management-fixture.hpp"
22#include "challenge-module/challenge-email.hpp"
23
24namespace ndn {
25namespace ndncert {
26namespace tests {
27
28BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementV2Fixture)
29
30BOOST_AUTO_TEST_CASE(TestChallengeType)
31{
32 ChallengeEmail challenge;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070033 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080034}
35
36BOOST_AUTO_TEST_CASE(ParseStoredSecret)
37{
38 time::system_clock::TimePoint tp = time::fromIsoString("20170207T120000");
39 JsonSection json;
40 json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
41 json.put(ChallengeEmail::JSON_CODE, "1234");
42 json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
43
44 auto result = ChallengeEmail::parseStoredSecrets(json);
45 BOOST_CHECK_EQUAL(std::get<0>(result), tp);
46 BOOST_CHECK_EQUAL(std::get<1>(result), "1234");
47 BOOST_CHECK_EQUAL(std::get<2>(result), 3);
48}
49
50BOOST_AUTO_TEST_CASE(EmailAddressChecker)
51{
52 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true);
53 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false);
54 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false);
55}
56
57BOOST_AUTO_TEST_CASE(OnSelectInterestComingWithEmail)
58{
59 auto identity = addIdentity(Name("/ndn/site1"));
60 auto key = identity.getDefaultKey();
61 auto cert = key.getDefaultCertificate();
62 CertificateRequest request(Name("/ndn/site1"), "123", cert);
63
64 JsonSection emailJson;
65 emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs.ucla.edu");
66 std::stringstream ss;
67 boost::property_tree::write_json(ss, emailJson);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080068 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
69
70 Name interestName("/ndn/site1/CA");
71 interestName.append("_SELECT").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
72 Interest interest(interestName);
73
74 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
75 challenge.handleChallengeRequest(interest, request);
76
77 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::NEED_CODE);
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070078 BOOST_CHECK_EQUAL(request.getChallengeType(), "Email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080079}
80
81BOOST_AUTO_TEST_CASE(OnSelectInterestComingWithInvalidEmail)
82{
83 auto identity = addIdentity(Name("/ndn/site1"));
84 auto key = identity.getDefaultKey();
85 auto cert = key.getDefaultCertificate();
86 CertificateRequest request(Name("/ndn/site1"), "123", cert);
87
88 JsonSection emailJson;
89 emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs");
90 std::stringstream ss;
91 boost::property_tree::write_json(ss, emailJson);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080092 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
93
94 Name interestName("/ndn/site1/CA");
95 interestName.append("_SELECT").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
96 Interest interest(interestName);
97
98 ChallengeEmail challenge;
99 challenge.handleChallengeRequest(interest, request);
100
101 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::FAILURE_INVALID_EMAIL);
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700102 BOOST_CHECK_EQUAL(request.getChallengeType(), "Email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800103}
104
105BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithCode)
106{
107 auto identity = addIdentity(Name("/ndn/site1"));
108 auto key = identity.getDefaultKey();
109 auto cert = key.getDefaultCertificate();
110 CertificateRequest request(Name("/ndn/site1"), "123", cert);
111 request.setChallengeType("EMAIL");
112 request.setStatus(ChallengeEmail::NEED_CODE);
113
114 time::system_clock::TimePoint tp = time::system_clock::now();
115 JsonSection json;
116 json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
117 json.put(ChallengeEmail::JSON_CODE, "4567");
118 json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
119
120 request.setChallengeSecrets(json);
121
122 JsonSection infoJson;
123 infoJson.put(ChallengeEmail::JSON_CODE, "4567");
124 std::stringstream ss;
125 boost::property_tree::write_json(ss, infoJson);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800126 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
127
128 Name interestName("/ndn/site1/CA");
129 interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
130 Interest interest(interestName);
131
132 ChallengeEmail challenge;
133 challenge.handleChallengeRequest(interest, request);
134
135 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS);
136 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true);
137}
138
139BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
140{
141 auto identity = addIdentity(Name("/ndn/site1"));
142 auto key = identity.getDefaultKey();
143 auto cert = key.getDefaultCertificate();
144 CertificateRequest request(Name("/ndn/site1"), "123", cert);
145 request.setChallengeType("EMAIL");
146 request.setStatus(ChallengeEmail::NEED_CODE);
147
148 time::system_clock::TimePoint tp = time::system_clock::now();
149 JsonSection json;
150 json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
151 json.put(ChallengeEmail::JSON_CODE, "4567");
152 json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
153
154 request.setChallengeSecrets(json);
155
156 JsonSection infoJson;
157 infoJson.put(ChallengeEmail::JSON_CODE, "1234");
158 std::stringstream ss;
159 boost::property_tree::write_json(ss, infoJson);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800160 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
161
162 Name interestName("/ndn/site1/CA");
163 interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
164 Interest interest(interestName);
165
166 ChallengeEmail challenge;
167 challenge.handleChallengeRequest(interest, request);
168
169 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::WRONG_CODE);
170 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), false);
171}
172
173BOOST_AUTO_TEST_CASE(ClientSendSelect)
174{
175 ChallengeEmail challenge;
176 auto requirementList = challenge.getSelectRequirements();
177 BOOST_CHECK_EQUAL(requirementList.size(), 1);
178
179 requirementList.clear();
180 requirementList.push_back("zhiyi@cs.ucla.edu");
181
182 auto json = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, requirementList);
183 BOOST_CHECK_EQUAL(json.empty(), false);
184 BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_EMAIL), "zhiyi@cs.ucla.edu");
185}
186
187BOOST_AUTO_TEST_CASE(ClientSendValidate)
188{
189 ChallengeEmail challenge;
190 auto requirementList = challenge.getValidateRequirements(ChallengeEmail::NEED_CODE);
191 BOOST_CHECK_EQUAL(requirementList.size(), 1);
192
193 requirementList.clear();
194 requirementList.push_back("123");
195
196 auto json = challenge.genValidateParamsJson(ChallengeEmail::NEED_CODE, requirementList);
197 BOOST_CHECK_EQUAL(json.empty(), false);
198 BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_CODE), "123");
199}
200
201BOOST_AUTO_TEST_SUITE_END()
202
203} // namespace tests
204} // namespace ndncert
205} // namespace ndn