blob: f048f61b97d9a5d9dad67fc698e3f6f404507378 [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
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080021#include "challenge-module/challenge-email.hpp"
Zhiyi Zhang576aad12017-10-03 15:41:53 -070022#include "identity-management-fixture.hpp"
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080023
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 Zhang576aad12017-10-03 15:41:53 -070079
80 std::string line = "";
81 std::string delimiter = " ";
82 std::ifstream emailFile("tmp.txt");
83 if (emailFile.is_open())
84 {
85 getline(emailFile, line);
86 emailFile.close();
87 }
88 std::string recipientEmail = line.substr(0, line.find(delimiter));
89 std::string secret = line.substr(line.find(delimiter) + 1);
90
91 BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu");
92 auto stored_secret = request.getChallengeSecrets().get<std::string>(ChallengeEmail::JSON_CODE);
93 BOOST_CHECK_EQUAL(secret, stored_secret);
94
95 std::remove("tmp.txt");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080096}
97
98BOOST_AUTO_TEST_CASE(OnSelectInterestComingWithInvalidEmail)
99{
100 auto identity = addIdentity(Name("/ndn/site1"));
101 auto key = identity.getDefaultKey();
102 auto cert = key.getDefaultCertificate();
103 CertificateRequest request(Name("/ndn/site1"), "123", cert);
104
105 JsonSection emailJson;
106 emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs");
107 std::stringstream ss;
108 boost::property_tree::write_json(ss, emailJson);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800109 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
110
111 Name interestName("/ndn/site1/CA");
112 interestName.append("_SELECT").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
113 Interest interest(interestName);
114
115 ChallengeEmail challenge;
116 challenge.handleChallengeRequest(interest, request);
117
118 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::FAILURE_INVALID_EMAIL);
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700119 BOOST_CHECK_EQUAL(request.getChallengeType(), "Email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800120}
121
122BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithCode)
123{
124 auto identity = addIdentity(Name("/ndn/site1"));
125 auto key = identity.getDefaultKey();
126 auto cert = key.getDefaultCertificate();
127 CertificateRequest request(Name("/ndn/site1"), "123", cert);
128 request.setChallengeType("EMAIL");
129 request.setStatus(ChallengeEmail::NEED_CODE);
130
131 time::system_clock::TimePoint tp = time::system_clock::now();
132 JsonSection json;
133 json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
134 json.put(ChallengeEmail::JSON_CODE, "4567");
135 json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
136
137 request.setChallengeSecrets(json);
138
139 JsonSection infoJson;
140 infoJson.put(ChallengeEmail::JSON_CODE, "4567");
141 std::stringstream ss;
142 boost::property_tree::write_json(ss, infoJson);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800143 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
144
145 Name interestName("/ndn/site1/CA");
146 interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
147 Interest interest(interestName);
148
149 ChallengeEmail challenge;
150 challenge.handleChallengeRequest(interest, request);
151
152 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS);
153 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true);
154}
155
156BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
157{
158 auto identity = addIdentity(Name("/ndn/site1"));
159 auto key = identity.getDefaultKey();
160 auto cert = key.getDefaultCertificate();
161 CertificateRequest request(Name("/ndn/site1"), "123", cert);
162 request.setChallengeType("EMAIL");
163 request.setStatus(ChallengeEmail::NEED_CODE);
164
165 time::system_clock::TimePoint tp = time::system_clock::now();
166 JsonSection json;
167 json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
168 json.put(ChallengeEmail::JSON_CODE, "4567");
169 json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
170
171 request.setChallengeSecrets(json);
172
173 JsonSection infoJson;
174 infoJson.put(ChallengeEmail::JSON_CODE, "1234");
175 std::stringstream ss;
176 boost::property_tree::write_json(ss, infoJson);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800177 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
178
179 Name interestName("/ndn/site1/CA");
180 interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
181 Interest interest(interestName);
182
183 ChallengeEmail challenge;
184 challenge.handleChallengeRequest(interest, request);
185
186 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::WRONG_CODE);
187 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), false);
188}
189
190BOOST_AUTO_TEST_CASE(ClientSendSelect)
191{
192 ChallengeEmail challenge;
193 auto requirementList = challenge.getSelectRequirements();
194 BOOST_CHECK_EQUAL(requirementList.size(), 1);
195
196 requirementList.clear();
197 requirementList.push_back("zhiyi@cs.ucla.edu");
198
199 auto json = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, requirementList);
200 BOOST_CHECK_EQUAL(json.empty(), false);
201 BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_EMAIL), "zhiyi@cs.ucla.edu");
202}
203
204BOOST_AUTO_TEST_CASE(ClientSendValidate)
205{
206 ChallengeEmail challenge;
207 auto requirementList = challenge.getValidateRequirements(ChallengeEmail::NEED_CODE);
208 BOOST_CHECK_EQUAL(requirementList.size(), 1);
209
210 requirementList.clear();
211 requirementList.push_back("123");
212
213 auto json = challenge.genValidateParamsJson(ChallengeEmail::NEED_CODE, requirementList);
214 BOOST_CHECK_EQUAL(json.empty(), false);
215 BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_CODE), "123");
216}
217
218BOOST_AUTO_TEST_SUITE_END()
219
220} // namespace tests
221} // namespace ndncert
222} // namespace ndn