1 /*
2  * Copyright (C) 2006-2021 Registro.br. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * 1. Redistribution of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY REGISTRO.BR ``AS IS AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIE OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16  * EVENT SHALL REGISTRO.BR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
19  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
21  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  */
25 /* $Id$ */
26 #if USE_IP_MANAGEMENT
27 #include <memory>
28 #include <string>
29 
30 #include "libepp_nicbr.H"
31 
32 #include "AsnRenewTest.H"
33 #include "AsnRenew.H"
34 #include "FileUtil.H"
35 #include "IoException.H"
36 #include "XmlException.H"
37 
38 #include "StrUtil.H"
39 
40 LIBEPP_NICBR_NS_USE
41 
42 CPPUNIT_TEST_SUITE_REGISTRATION(AsnRenewTest);
43 
AsnRenewTest()44 AsnRenewTest::AsnRenewTest() {}
45 
~AsnRenewTest()46 AsnRenewTest::~AsnRenewTest() {}
47 
setUp()48 void AsnRenewTest::setUp() {}
49 
tearDown()50 void AsnRenewTest::tearDown() {}
51 
set_xml_template_test()52 void AsnRenewTest::set_xml_template_test()
53 {
54 	string to_be_parsed =
55 		"<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
56 		"<epp xmlns='urn:ietf:params:xml:ns:epp-1.0' "
57 		"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
58 		"xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 "
59 		"epp-1.0.xsd'>"
60 		"<command>"
61 		"<renew>"
62 		"<asn:renew "
63 		"xmlns:asn='urn:ietf:params:xml:ns:asn-1.0' "
64 		"xsi:schemaLocation='urn:ietf:params:xml:ns:asn-1.0 "
65 		"asn-1.0.xsd'>"
66 		"<asn:number>$(asn)$</asn:number>"
67 		"<asn:curExpDate>$(expDate)$</asn:curExpDate>"
68 		"$(period)$"
69 		"</asn:renew>"
70 		"</renew>"
71 		"$(clTRID)$"
72 		"</command>"
73 		"</epp>";
74 
75 	AsnRenew asnRenew;
76 	AsnRenewCmd *asnRenewCmd = asnRenew.get_command();
77 	asnRenewCmd->set_asn(64500);
78 	asnRenewCmd->set_expDate("2008-04-03T00:00:00.0Z");
79 	asnRenewCmd->set_period(3, "y");
80 	asnRenewCmd->set_clTRID("ABC-12345");
81 	asnRenew.set_xml_template(to_be_parsed);
82 
83 	string expected =
84 		"<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
85 		"<epp xmlns='urn:ietf:params:xml:ns:epp-1.0' "
86 		"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
87 		"xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 "
88 		"epp-1.0.xsd'>"
89 		"<command>"
90 		"<renew>"
91 		"<asn:renew "
92 		"xmlns:asn='urn:ietf:params:xml:ns:asn-1.0' "
93 		"xsi:schemaLocation='urn:ietf:params:xml:ns:asn-1.0 "
94 		"asn-1.0.xsd'>"
95 		"<asn:number>64500</asn:number>"
96 		"<asn:curExpDate>2008-04-03T00:00:00.0Z</asn:curExpDate>"
97 		"<asn:period unit='y'>3</asn:period>"
98 		"</asn:renew>"
99 		"</renew>"
100 		"<clTRID>ABC-12345</clTRID>"
101 		"</command>"
102 		"</epp>";
103 
104 	CPPUNIT_ASSERT_EQUAL(expected, asnRenew.get_xml());
105 
106 	// tests reset method
107 	asnRenewCmd = asnRenew.get_command();
108 	asnRenewCmd->reset();
109 	CPPUNIT_ASSERT(asnRenewCmd->get_asn() == 0);
110 	CPPUNIT_ASSERT(asnRenewCmd->get_expDate() == "");
111 	CPPUNIT_ASSERT(asnRenewCmd->get_period().time == 0);
112 	CPPUNIT_ASSERT(asnRenewCmd->get_period().unit == "");
113 
114 	// parent attribute
115 	CPPUNIT_ASSERT(asnRenewCmd->get_clTRID() == "");
116 }
117 
command_test()118 void AsnRenewTest::command_test()
119 {
120 	AsnRenew asnRenew;
121 	AsnRenewCmd *asnRenewCmd = asnRenew.get_command();
122 	asnRenewCmd->set_asn(64500);
123 	asnRenewCmd->set_expDate("2008-04-03T00:00:00.0Z");
124 	asnRenewCmd->set_period(3, "y");
125 	asnRenewCmd->set_clTRID("ABC-12345");
126 
127 	bool exception_caught = false;
128 	try {
129 		string xml_template =
130 			FileUtil::read_file("../docs/templates/asn_renew.xml");
131 		asnRenew.set_xml_template(xml_template);
132 
133 		DomParser parser;
134 		parser.enable_validation("../docs/schemas");
135 		parser.parse_command(asnRenew.get_xml());
136 	} catch (const IoException &e) {
137 		exception_caught = true;
138 		printf("\nIO Exception: code [%d] message [%s]",
139 		       e.get_code(), e.get_msg().c_str());
140 	} catch (const XmlException &e) {
141 		exception_caught = true;
142 		printf("\nXml Exception: code [%d] message [%s] low level message [%s]\n",
143 		       e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
144 	}
145 
146 	CPPUNIT_ASSERT(!exception_caught);
147 }
148 
response_test()149 void AsnRenewTest::response_test()
150 {
151 	string expected =
152 		"<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
153 		"<epp xmlns='urn:ietf:params:xml:ns:epp-1.0' "
154 		"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
155 		"xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 "
156 		"epp-1.0.xsd'>"
157 		"<response>"
158 		"<result code='1000'>"
159 		"<msg>Command completed successfully</msg>"
160 		"</result>"
161 		"<resData>"
162 		"<asn:renData "
163 		"xmlns:asn='urn:ietf:params:xml:ns:asn-1.0' "
164 		"xsi:schemaLocation='urn:ietf:params:xml:ns:asn-1.0 "
165 		"asn-1.0.xsd'>"
166 		"<asn:number>64500</asn:number>"
167 		"<asn:exDate>2011-04-03T00:00:00.0Z</asn:exDate>"
168 		"</asn:renData>"
169 		"</resData>"
170 		"<trID>"
171 		"<clTRID>ABC-12345</clTRID>"
172 		"<svTRID>54322-XYZ</svTRID>"
173 		"</trID>"
174 		"</response>"
175 		"</epp>";
176 
177 	DomParser parser;
178 	parser.enable_validation("../docs/schemas");
179 
180 	AsnRenew asnRenew;
181 	CPPUNIT_ASSERT_NO_THROW(asnRenew.set_response(expected, &parser));
182 	AsnRenewRsp *asnRenewRsp = asnRenew.get_response();
183 
184 	map <Response::ResultCode, Response::ResultInfo> results;
185 	map <Response::ResultCode, Response::ResultInfo>::const_iterator r_it;
186 	results = asnRenewRsp->get_result_list();
187 	r_it = results.begin();
188 
189 	CPPUNIT_ASSERT(r_it != results.end());
190 	CPPUNIT_ASSERT_EQUAL(Response::OK, r_it->first);
191 
192 	string result_code =
193 		StrUtil::to_string("%d", (int)r_it->first);
194 
195 	string response =
196 		"<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
197 		"<epp xmlns='urn:ietf:params:xml:ns:epp-1.0' "
198 		"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
199 		"xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 "
200 		"epp-1.0.xsd'>"
201 		"<response>"
202 		"<result code='1000'>"
203 		"<msg";
204 
205 	string result_lang = asnRenewRsp->get_result_lang();
206 	if (result_lang != "en") {
207 		response += " lang='" + result_lang + "'";
208 	}
209 
210 	response += ">" + r_it->second.msg + "</msg>"
211 		"</result>"
212 		"<resData>"
213 		"<asn:renData "
214 		"xmlns:asn='urn:ietf:params:xml:ns:asn-1.0' "
215 		"xsi:schemaLocation='urn:ietf:params:xml:ns:asn-1.0 "
216 		"asn-1.0.xsd'>"
217 		"<asn:number>" + StrUtil::to_string("%d", asnRenewRsp->get_asn()) +
218 		"</asn:number>";
219 
220 	if (asnRenewRsp->get_expDate() != "") {
221 		response += "<asn:exDate>" + asnRenewRsp->get_expDate() + "</asn:exDate>";
222 	}
223 
224 	response += "</asn:renData>"
225 		"</resData>"
226 		"<trID>"
227 		"<clTRID>" + asnRenewRsp->get_clTRID() + "</clTRID>"
228 		"<svTRID>" + asnRenewRsp->get_svTRID() + "</svTRID>"
229 		"</trID>"
230 		"</response>"
231 		"</epp>";
232 
233 	CPPUNIT_ASSERT_EQUAL(expected, response);
234 
235 	// tests reset method
236 	asnRenewRsp->reset();
237 	CPPUNIT_ASSERT(asnRenewRsp->get_asn() == 0);
238 	CPPUNIT_ASSERT_EQUAL((const string)"", asnRenewRsp->get_expDate());
239 }
240 #endif //USE_IP_MANAGEMENT
241