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 "AsnCheckTest.H"
33 #include "AsnCheck.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(AsnCheckTest);
43 
AsnCheckTest()44 AsnCheckTest::AsnCheckTest() {}
45 
~AsnCheckTest()46 AsnCheckTest::~AsnCheckTest() {}
47 
setUp()48 void AsnCheckTest::setUp() {}
49 
tearDown()50 void AsnCheckTest::tearDown() {}
51 
set_xml_template_test()52 void AsnCheckTest::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 		"<check>"
62 		"<asn:check "
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_list)$"
67 		"</asn:check>"
68 		"</check>"
69 		"$(clTRID)$"
70 		"</command>"
71 		"</epp>";
72 
73 	AsnCheck asnCheck;
74 	AsnCheckCmd *asnCheckCmd = asnCheck.get_command();
75 	asnCheckCmd->add_asn(64500);
76 	asnCheckCmd->set_clTRID("ABC-12345");
77 	asnCheck.set_xml_template(to_be_parsed);
78 
79 	string expected =
80 		"<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
81 		"<epp xmlns='urn:ietf:params:xml:ns:epp-1.0' "
82 		"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
83 		"xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 "
84 		"epp-1.0.xsd'>"
85 		"<command>"
86 		"<check>"
87 		"<asn:check "
88 		"xmlns:asn='urn:ietf:params:xml:ns:asn-1.0' "
89 		"xsi:schemaLocation='urn:ietf:params:xml:ns:asn-1.0 "
90 		"asn-1.0.xsd'>"
91 		"<asn:number>64500</asn:number>"
92 		"</asn:check>"
93 		"</check>"
94 		"<clTRID>ABC-12345</clTRID>"
95 		"</command>"
96 		"</epp>";
97 
98 	CPPUNIT_ASSERT_EQUAL(expected, asnCheck.get_xml());
99 
100 	// tests reset method
101 	asnCheckCmd = asnCheck.get_command();
102 	asnCheckCmd->reset();
103 	CPPUNIT_ASSERT_EQUAL(asnCheckCmd->get_asn_list().empty(), true);
104 
105 	// parent attribute
106 	CPPUNIT_ASSERT(asnCheckCmd->get_clTRID() == "");
107 }
108 
command_test()109 void AsnCheckTest::command_test()
110 {
111 	AsnCheck asnCheck;
112 	AsnCheckCmd *asnCheckCmd = asnCheck.get_command();
113 	asnCheckCmd->add_asn(64500);
114 	asnCheckCmd->set_clTRID("ABC-12345");
115 
116 	bool exception_caught = false;
117 	try {
118 		string xml_template =
119 			FileUtil::read_file("../docs/templates/asn_check.xml");
120 		asnCheck.set_xml_template(xml_template);
121 
122 		DomParser parser;
123 		parser.enable_validation("../docs/schemas");
124 		parser.parse_command(asnCheck.get_xml());
125 	} catch (const IoException &e) {
126 		exception_caught = true;
127 		printf("\nIO Exception: code [%d] message [%s]",
128 		       e.get_code(), e.get_msg().c_str());
129 	} catch (const XmlException &e) {
130 		exception_caught = true;
131 		printf("\nXml Exception: code [%d] message [%s] low level message [%s]\n",
132 		       e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
133 	}
134 
135 	CPPUNIT_ASSERT(!exception_caught);
136 }
137 
response_test()138 void AsnCheckTest::response_test()
139 {
140 	string expected =
141 		"<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
142 		"<epp xmlns='urn:ietf:params:xml:ns:epp-1.0' "
143 		"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
144 		"xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 "
145 		"epp-1.0.xsd'>"
146 		"<response>"
147 		"<result code='1000'>"
148 		"<msg>Command completed successfully</msg>"
149 		"</result>"
150 		"<resData>"
151 		"<asn:chkData "
152 		"xmlns:asn='urn:ietf:params:xml:ns:asn-1.0' "
153 		"xsi:schemaLocation='urn:ietf:params:xml:ns:asn-1.0 "
154 		"asn-1.0.xsd'>"
155 		"<asn:cd>"
156 		"<asn:number avail='0'>64500</asn:number>"
157 		"<asn:reason>In use</asn:reason>"
158 		"</asn:cd>"
159 		"</asn:chkData>"
160 		"</resData>"
161 		"<trID>"
162 		"<clTRID>ABC-12345</clTRID>"
163 		"<svTRID>54322-XYZ</svTRID>"
164 		"</trID>"
165 		"</response>"
166 		"</epp>";
167 
168 	DomParser parser;
169 	parser.enable_validation("../docs/schemas");
170 
171 	AsnCheck asnCheck;
172 	CPPUNIT_ASSERT_NO_THROW(asnCheck.set_response(expected, &parser));
173 	AsnCheckRsp *asnCheckRsp = asnCheck.get_response();
174 
175 	map <Response::ResultCode, Response::ResultInfo> results;
176 	map <Response::ResultCode, Response::ResultInfo>::const_iterator r_it;
177 	results = asnCheckRsp->get_result_list();
178 	r_it = results.begin();
179 
180 	CPPUNIT_ASSERT(r_it != results.end());
181 	CPPUNIT_ASSERT_EQUAL(Response::OK, r_it->first);
182 
183 	string result_code =
184 		StrUtil::to_string("%d", (int)r_it->first);
185 
186 	string response =
187 		"<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
188 		"<epp xmlns='urn:ietf:params:xml:ns:epp-1.0' "
189 		"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
190 		"xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 "
191 		"epp-1.0.xsd'>"
192 		"<response>"
193 		"<result code='1000'>"
194 		"<msg";
195 
196 	string result_lang = asnCheckRsp->get_result_lang();
197 	if (result_lang != "en") {
198 		response += " lang='" + result_lang + "'";
199 	}
200 
201 	response += ">" + r_it->second.msg + "</msg>"
202 		"</result>"
203 		"<resData>"
204 		"<asn:chkData "
205 		"xmlns:asn='urn:ietf:params:xml:ns:asn-1.0' "
206 		"xsi:schemaLocation='urn:ietf:params:xml:ns:asn-1.0 "
207 		"asn-1.0.xsd'>";
208 
209 	vector<AsnCheckRsp::CheckData> checkData =
210 		asnCheckRsp->get_availability_list();
211 
212 	for (unsigned int i = 0; i < checkData.size(); i++) {
213 		response += "<asn:cd>"
214 			"<asn:number";
215 
216 		if (checkData[i].available == true) {
217 			response += " avail='1'";
218 		} else {
219 			response += " avail='0'";
220 		}
221 
222 		response += ">" + StrUtil::to_string("%d", checkData[i].asn) +
223 			"</asn:number>";
224 
225 		if (checkData[i].reason != "") {
226 			response += "<asn:reason";
227 
228 			if (checkData[i].reason_lang != "") {
229 				response += " lang='" + checkData[i].reason_lang + "'";
230 			}
231 
232 			response += ">" + checkData[i].reason + "</asn:reason>";
233 		}
234 
235 		response += "</asn:cd>";
236 	}
237 
238 	response += "</asn:chkData>"
239 		"</resData>"
240 		"<trID>"
241 		"<clTRID>" + asnCheckRsp->get_clTRID() + "</clTRID>"
242 		"<svTRID>" + asnCheckRsp->get_svTRID() + "</svTRID>"
243 		"</trID>"
244 		"</response>"
245 		"</epp>";
246 
247 	CPPUNIT_ASSERT_EQUAL(expected, response);
248 
249 	// tests reset method
250 	asnCheckRsp->reset();
251 	CPPUNIT_ASSERT(asnCheckRsp->get_availability_list().empty());
252 }
253 #endif //USE_IP_MANAGEMENT
254