blob: 00106eac2bfab2edda1092bfae9786b431f565c8 [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;
33 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "EMAIL");
34}
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);
68 std::string jsonString = ss.str();
69 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
70
71 Name interestName("/ndn/site1/CA");
72 interestName.append("_SELECT").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
73 Interest interest(interestName);
74
75 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
76 challenge.handleChallengeRequest(interest, request);
77
78 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::NEED_CODE);
79 BOOST_CHECK_EQUAL(request.getChallengeType(), "EMAIL");
80}
81
82BOOST_AUTO_TEST_CASE(OnSelectInterestComingWithInvalidEmail)
83{
84 auto identity = addIdentity(Name("/ndn/site1"));
85 auto key = identity.getDefaultKey();
86 auto cert = key.getDefaultCertificate();
87 CertificateRequest request(Name("/ndn/site1"), "123", cert);
88
89 JsonSection emailJson;
90 emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs");
91 std::stringstream ss;
92 boost::property_tree::write_json(ss, emailJson);
93 std::string jsonString = ss.str();
94 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
95
96 Name interestName("/ndn/site1/CA");
97 interestName.append("_SELECT").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
98 Interest interest(interestName);
99
100 ChallengeEmail challenge;
101 challenge.handleChallengeRequest(interest, request);
102
103 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::FAILURE_INVALID_EMAIL);
104 BOOST_CHECK_EQUAL(request.getChallengeType(), "EMAIL");
105}
106
107BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithCode)
108{
109 auto identity = addIdentity(Name("/ndn/site1"));
110 auto key = identity.getDefaultKey();
111 auto cert = key.getDefaultCertificate();
112 CertificateRequest request(Name("/ndn/site1"), "123", cert);
113 request.setChallengeType("EMAIL");
114 request.setStatus(ChallengeEmail::NEED_CODE);
115
116 time::system_clock::TimePoint tp = time::system_clock::now();
117 JsonSection json;
118 json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
119 json.put(ChallengeEmail::JSON_CODE, "4567");
120 json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
121
122 request.setChallengeSecrets(json);
123
124 JsonSection infoJson;
125 infoJson.put(ChallengeEmail::JSON_CODE, "4567");
126 std::stringstream ss;
127 boost::property_tree::write_json(ss, infoJson);
128 std::string jsonString = ss.str();
129 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
130
131 Name interestName("/ndn/site1/CA");
132 interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
133 Interest interest(interestName);
134
135 ChallengeEmail challenge;
136 challenge.handleChallengeRequest(interest, request);
137
138 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS);
139 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true);
140}
141
142BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
143{
144 auto identity = addIdentity(Name("/ndn/site1"));
145 auto key = identity.getDefaultKey();
146 auto cert = key.getDefaultCertificate();
147 CertificateRequest request(Name("/ndn/site1"), "123", cert);
148 request.setChallengeType("EMAIL");
149 request.setStatus(ChallengeEmail::NEED_CODE);
150
151 time::system_clock::TimePoint tp = time::system_clock::now();
152 JsonSection json;
153 json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
154 json.put(ChallengeEmail::JSON_CODE, "4567");
155 json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
156
157 request.setChallengeSecrets(json);
158
159 JsonSection infoJson;
160 infoJson.put(ChallengeEmail::JSON_CODE, "1234");
161 std::stringstream ss;
162 boost::property_tree::write_json(ss, infoJson);
163 std::string jsonString = ss.str();
164 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
165
166 Name interestName("/ndn/site1/CA");
167 interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
168 Interest interest(interestName);
169
170 ChallengeEmail challenge;
171 challenge.handleChallengeRequest(interest, request);
172
173 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::WRONG_CODE);
174 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), false);
175}
176
177BOOST_AUTO_TEST_CASE(ClientSendSelect)
178{
179 ChallengeEmail challenge;
180 auto requirementList = challenge.getSelectRequirements();
181 BOOST_CHECK_EQUAL(requirementList.size(), 1);
182
183 requirementList.clear();
184 requirementList.push_back("zhiyi@cs.ucla.edu");
185
186 auto json = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, requirementList);
187 BOOST_CHECK_EQUAL(json.empty(), false);
188 BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_EMAIL), "zhiyi@cs.ucla.edu");
189}
190
191BOOST_AUTO_TEST_CASE(ClientSendValidate)
192{
193 ChallengeEmail challenge;
194 auto requirementList = challenge.getValidateRequirements(ChallengeEmail::NEED_CODE);
195 BOOST_CHECK_EQUAL(requirementList.size(), 1);
196
197 requirementList.clear();
198 requirementList.push_back("123");
199
200 auto json = challenge.genValidateParamsJson(ChallengeEmail::NEED_CODE, requirementList);
201 BOOST_CHECK_EQUAL(json.empty(), false);
202 BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_CODE), "123");
203}
204
205BOOST_AUTO_TEST_SUITE_END()
206
207} // namespace tests
208} // namespace ndncert
209} // namespace ndn