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 #include <string>
27 #include <memory>
28 #include <iostream>
29 
30 #include "libepp_nicbr.H"
31 
32 #include "ContactCreateTest.H"
33 #include "ContactCreate.H"
34 #include "FileUtil.H"
35 #include "IoException.H"
36 #include "XmlException.H"
37 
38 LIBEPP_NICBR_NS_USE
39 
40 CPPUNIT_TEST_SUITE_REGISTRATION(ContactCreateTest);
41 
ContactCreateTest()42 ContactCreateTest::ContactCreateTest() {}
43 
~ContactCreateTest()44 ContactCreateTest::~ContactCreateTest() {}
45 
setUp()46 void ContactCreateTest::setUp() {}
47 
tearDown()48 void ContactCreateTest::tearDown() {}
49 
set_xml_template_test()50 void ContactCreateTest::set_xml_template_test()
51 {
52 	string to_be_parsed = "<command>"
53 		"<create>"
54 		"<contact:create"
55 		"xmlns:contact='urn:ietf:params:xml:ns:contact-1.0'"
56 		"xsi:schemaLocation='urn:ietf:params:xml:ns:contact-1.0"
57 		"contact-1.0.xsd'>"
58 		"<contact:id>$(id)$</contact:id>"
59 		"$(postal_info)$"
60 		"$(voice)$"
61 		"$(fax)$"
62 		"<contact:email>$(email)$</contact:email>"
63 		"$(auth_info)$"
64 		"$(disclose)$"
65 		"</contact:create>"
66 		"</create>"
67 		"$(ext_begin)$"
68 		"$(lacnic_contact_ext)$"
69 		"$(ext_end)$"
70 		"$(clTRID)$"
71 		"</command>";
72 
73 	ContactCreate contact_create;
74 	ContactCreateCmd *cmd = contact_create.get_command();
75 	CommonData common;
76 
77 	common.set_id("cme254");
78 	PostalInfo postal_info;
79 	postal_info.set_type("int");
80 	postal_info.set_name("John Doe");
81 	postal_info.set_org("Example Inc.");
82 	postal_info.set_str1("123 Example Dr.");
83 	postal_info.set_str2("Suite 100");
84 	postal_info.set_str3("");
85 	postal_info.set_city( "Dulles");
86 	postal_info.set_sp("VA");
87 	postal_info.set_pc("20166-6503");
88 	postal_info.set_cc("US");
89 	common.insert_postal_info(postal_info);
90 	postal_info.set_type("loc");
91 	postal_info.set_name("John Doe");
92 	postal_info.set_org("Other Inc.");
93 	postal_info.set_str1("123 Street");
94 	postal_info.set_str2("7th floor");
95 	postal_info.set_str3("Suite 123");
96 	postal_info.set_city( "Miami");
97 	postal_info.set_sp("");
98 	postal_info.set_pc("");
99 	postal_info.set_cc("US");
100 	common.insert_postal_info(postal_info);
101 
102 	CommonData::Phone voice;
103 	voice.ext = "1234";
104 	voice.number =  "+1.7035555555";
105 	CommonData::Phone fax;
106 	fax.ext = "3456";
107 	fax.number = "+1.7035555556";
108 	common.set_voice(voice);
109 	common.set_fax(fax);
110 	common.set_email("jdoe@example.com");
111 	AuthInfo authInfo;
112 	authInfo.set_pw("2fooBAR");
113 	cmd->set_authInfo(authInfo);
114 
115 	CommonData::Disclose disclose;
116 	disclose.flag = false;
117 	disclose.name_int = true;
118 	disclose.voice = true;
119 	disclose.email = true;
120 	common.set_disclose(disclose);
121 
122 	cmd->set_common_data(common);
123 
124 	//LacnicContact specific
125 	cmd->set_password("abc123");
126 	cmd->set_reminder("Default");
127 	cmd->set_language("pt");
128 
129 	contact_create.get_command()->set_clTRID("ABC-12345");
130 	contact_create.set_xml_template(to_be_parsed);
131 
132 	string expected = "<command>"
133 		"<create>"
134 		"<contact:create"
135 		"xmlns:contact='urn:ietf:params:xml:ns:contact-1.0'"
136 		"xsi:schemaLocation='urn:ietf:params:xml:ns:contact-1.0"
137 		"contact-1.0.xsd'>"
138 		"<contact:id>cme254</contact:id>"
139 		"<contact:postalInfo type='int'>"
140 		"<contact:name>John Doe</contact:name>"
141 		"<contact:org>Example Inc.</contact:org>"
142 		"<contact:addr>"
143 		"<contact:street>123 Example Dr.</contact:street>"
144 		"<contact:street>Suite 100</contact:street>"
145 		"<contact:city>Dulles</contact:city>"
146 		"<contact:sp>VA</contact:sp>"
147 		"<contact:pc>20166-6503</contact:pc>"
148 		"<contact:cc>US</contact:cc>"
149 		"</contact:addr>"
150 		"</contact:postalInfo>"
151 		"<contact:postalInfo type='loc'>"
152 		"<contact:name>John Doe</contact:name>"
153 		"<contact:org>Other Inc.</contact:org>"
154 		"<contact:addr>"
155 		"<contact:street>123 Street</contact:street>"
156 		"<contact:street>7th floor</contact:street>"
157 		"<contact:street>Suite 123</contact:street>"
158 		"<contact:city>Miami</contact:city>"
159 		"<contact:cc>US</contact:cc>"
160 		"</contact:addr>"
161 		"</contact:postalInfo>"
162 		"<contact:voice x='1234'>+1.7035555555</contact:voice>"
163 		"<contact:fax x='3456'>+1.7035555556</contact:fax>"
164 		"<contact:email>jdoe@example.com</contact:email>"
165 		"<contact:authInfo>"
166 		"<contact:pw>2fooBAR</contact:pw>"
167 		"</contact:authInfo>"
168 		"<contact:disclose flag='0'>"
169 		"<contact:name type =\"int\"/>"
170 		"<contact:voice/>"
171 		"<contact:email/>"
172 		"</contact:disclose>"
173 		"</contact:create>"
174 		"</create>"
175 		"<extension>"
176 		"<lacniccontact:create "
177 		"xmlns:lacniccontact='urn:ietf:params:xml:ns:lacniccontact-1.0' "
178 		"xsi:schemaLocation='urn:ietf:params:xml:ns:lacniccontact-1.0 "
179 		"lacniccontact-1.0.xsd'>"
180 		"<lacniccontact:password>abc123</lacniccontact:password>"
181 		"<lacniccontact:reminder>Default</lacniccontact:reminder>"
182 		"<lacniccontact:language>pt</lacniccontact:language>"
183 		"</lacniccontact:create>"
184 		"</extension>"
185 		"<clTRID>ABC-12345</clTRID>"
186 		"</command>";
187 
188 	CPPUNIT_ASSERT(expected == contact_create.get_xml());
189 
190 	// tests reset method
191 	cmd->reset();
192 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_common_data().get_id());
193 	CPPUNIT_ASSERT(cmd->get_common_data().get_postal_info().empty());
194 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_common_data().get_voice().ext);
195 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_common_data().get_voice().number);
196 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_common_data().get_fax().ext);
197 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_common_data().get_fax().number);
198 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_common_data().get_email());
199 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_authInfo().get_roid());
200 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_authInfo().get_pw());
201 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().flag == -1);
202 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().name_int == false);
203 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().name_loc == false);
204 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().org_int == false);
205 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().org_loc == false);
206 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().addr_int == false);
207 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().addr_loc == false);
208 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().email == false);
209 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().voice == false);
210 	CPPUNIT_ASSERT(cmd->get_common_data().get_disclose().fax == false);
211 
212 	//LacnicContactCreate specific information
213 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_password());
214 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_reminder());
215 	CPPUNIT_ASSERT_EQUAL((string) "", cmd->get_language());
216 
217 	// parent attribute
218 	CPPUNIT_ASSERT(cmd->get_clTRID() == "");
219 }
220 
command_test()221 void ContactCreateTest::command_test()
222 {
223 	ContactCreate contact_create;
224 	ContactCreateCmd *cmd = contact_create.get_command();
225 	CommonData common;
226 
227 	common.set_id("cme254");
228 	PostalInfo postal_info;
229 	postal_info.set_type("int");
230 	postal_info.set_name("John Doe");
231 	postal_info.set_org("Example Inc.");
232 	postal_info.set_str1("123 Example Dr.");
233 	postal_info.set_str2("Suite 100");
234 	postal_info.set_str3("");
235 	postal_info.set_city("Dulles");
236 	postal_info.set_sp("VA");
237 	postal_info.set_pc("20166-6503");
238 	postal_info.set_cc("US");
239 	common.insert_postal_info(postal_info);
240 	postal_info.set_type("loc");
241 	postal_info.set_name("John Doe");
242 	postal_info.set_org("Other Inc.");
243 	postal_info.set_str1("123 Street");
244 	postal_info.set_str2("7th floor");
245 	postal_info.set_str3("Suite 123");
246 	postal_info.set_city("Miami");
247 	postal_info.set_sp("");
248 	postal_info.set_pc("");
249 	postal_info.set_cc("US");
250 	common.insert_postal_info(postal_info);
251 
252 	CommonData::Phone voice;
253 	voice.ext = "1234";
254 	voice.number =  "+1.7035555555";
255 	CommonData::Phone fax;
256 	fax.ext = "3456";
257 	fax.number = "+1.7035555556";
258 	common.set_voice(voice);
259 	common.set_fax(fax);
260 	common.set_email("jdoe@example.com");
261 	AuthInfo authInfo;
262 	authInfo.set_roid("SH8013-REP");
263 	authInfo.set_pw("2fooBAR");
264 	cmd->set_authInfo(authInfo);
265 
266 	CommonData::Disclose disclose;
267 	disclose.flag = false;
268 	disclose.name_int = true;
269 	disclose.voice = true;
270 	disclose.email = true;
271 	common.set_disclose(disclose);
272 
273 	cmd->set_common_data(common);
274 
275 	//LacnicContactCreate specific
276 	cmd->set_password("abc123");
277 	cmd->set_reminder("Default");
278 	cmd->set_language("pt");
279 
280 	bool exception_caught = false;
281 	try {
282 		string xml_template =
283 			FileUtil::read_file("../docs/templates/contact_create.xml");
284 
285 		contact_create.get_command()->set_clTRID("ABC-12345");
286 		contact_create.set_xml_template(xml_template);
287 
288 		DomParser parser;
289 		parser.enable_validation("../docs/schemas");
290 		parser.parse_command(contact_create.get_xml());
291 	} catch (const IoException &e) {
292 		exception_caught = true;
293 		printf("\nIO Exception: code [%d] message [%s]",
294 		       e.get_code(), e.get_msg().c_str());
295 	} catch (const XmlException &e) {
296 		exception_caught = true;
297 		printf("\nXml Exception: code [%d] message [%s] low level message [%s]\n",
298 		       e.get_code(), e.get_msg().c_str(), e.get_low_level_msg().c_str());
299 	}
300 
301 	CPPUNIT_ASSERT(!exception_caught);
302 }
303 
response_test()304 void ContactCreateTest::response_test()
305 {
306 	string expected =
307 		"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
308 		"<epp xmlns=\"urn:ietf:params:xml:ns:epp-1.0\" "
309 		"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
310 		"xsi:schemaLocation=\"urn:ietf:params:xml:ns:epp-1.0 "
311 		"epp-1.0.xsd\">"
312 		"<response>"
313 		"<result code=\"1000\">"
314 		"<msg>Command completed successfully</msg>"
315 		"</result>"
316 		"<resData>"
317 		"<contact:creData "
318 		"xmlns:contact=\"urn:ietf:params:xml:ns:contact-1.0\" "
319 		"xsi:schemaLocation=\"urn:ietf:params:xml:ns:contact-1.0 "
320 		"contact-1.0.xsd\">"
321 		"<contact:id>cem456</contact:id>"
322 		"<contact:crDate>2006-01-30T22:00:00.0Z</contact:crDate>"
323 		"</contact:creData>"
324 		"</resData>"
325 		"<trID>"
326 		"<clTRID>ABC-12345</clTRID>"
327 		"<svTRID>DEF-54321</svTRID>"
328 		"</trID>"
329 		"</response>"
330 		"</epp>";
331 
332 	DomParser parser;
333 	parser.enable_validation("../docs/schemas");
334 
335 	ContactCreate contact_create;
336 	CPPUNIT_ASSERT_NO_THROW(contact_create.set_response(expected, &parser));
337 	ContactCreateRsp *contact_create_rsp = contact_create.get_response();
338 
339 	map <Response::ResultCode, Response::ResultInfo> results;
340 	map <Response::ResultCode, Response::ResultInfo>::const_iterator r_it;
341 	results = contact_create_rsp->get_result_list();
342 	r_it = results.begin();
343 
344 	CPPUNIT_ASSERT(r_it != results.end());
345 	CPPUNIT_ASSERT_EQUAL(Response::OK, r_it->first);
346 
347 	string result_code = "1000";
348 
349 	string response =
350 		"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
351 		"<epp xmlns=\"urn:ietf:params:xml:ns:epp-1.0\" "
352 		"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
353 		"xsi:schemaLocation=\"urn:ietf:params:xml:ns:epp-1.0 "
354 		"epp-1.0.xsd\">"
355 		"<response>"
356 		"<result code=\"" + result_code + "\">"
357 		"<msg";
358 
359 	string result_lang = contact_create_rsp->get_result_lang();
360 	if (result_lang != "en") {
361 		response += " lang='" + result_lang + "'";
362 	}
363 
364 	response +=
365 		">" + r_it->second.msg + "</msg>"
366 		"</result>"
367 		"<resData>"
368 		"<contact:creData "
369 		"xmlns:contact=\"urn:ietf:params:xml:ns:contact-1.0\" "
370 		"xsi:schemaLocation=\"urn:ietf:params:xml:ns:contact-1.0 "
371 		"contact-1.0.xsd\">"
372 		"<contact:id>" + contact_create_rsp->get_id() + "</contact:id>"
373 		"<contact:crDate>" + contact_create_rsp->get_crDate() + "</contact:crDate>"
374 		"</contact:creData>"
375 		"</resData>"
376 		"<trID>"
377 		"<clTRID>" + contact_create_rsp->get_clTRID() +"</clTRID>"
378 		"<svTRID>" + contact_create_rsp->get_svTRID() + "</svTRID>"
379 		"</trID>"
380 		"</response>"
381 		"</epp>";
382 
383 	CPPUNIT_ASSERT_EQUAL(response, expected);
384 
385 	/* Reset Test */
386 	CPPUNIT_ASSERT(contact_create_rsp->get_id() != "");
387 	CPPUNIT_ASSERT(contact_create_rsp->get_crDate() != "");
388 
389 	contact_create_rsp->reset();
390 
391 	CPPUNIT_ASSERT(contact_create_rsp->get_id() == "");
392 	CPPUNIT_ASSERT(contact_create_rsp->get_crDate() == "");
393 }
394