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