PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/tests/llhost_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 251 lines | 172 code | 34 blank | 45 comment | 30 complexity | e8b9e1321d904d45d15fa88eb2ab954f MD5 | raw file
  1/**
  2 * @file llhost_test.cpp
  3 * @author Adroit
  4 * @date 2007-02
  5 * @brief llhost test cases.
  6 *
  7 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28 
 29#include "linden_common.h"
 30
 31#include "../llhost.h"
 32
 33#include "../test/lltut.h"
 34
 35namespace tut
 36{
 37	struct host_data
 38	{
 39	};
 40	typedef test_group<host_data> host_test;
 41	typedef host_test::object host_object;
 42	tut::host_test host_testcase("LLHost");
 43
 44
 45	template<> template<>
 46	void host_object::test<1>()
 47	{
 48		LLHost host;
 49		ensure("IP address is not NULL", (0 == host.getAddress()) && (0 == host.getPort()) && !host.isOk());
 50	}
 51	template<> template<>
 52	void host_object::test<2>()
 53	{
 54		U32 ip_addr = 0xc098017d;
 55		U32 port = 8080;
 56		LLHost host(ip_addr, port);
 57		ensure("IP address is invalid", ip_addr == host.getAddress());
 58		ensure("Port Number is invalid", port == host.getPort());
 59		ensure("IP address and port number both should be ok", host.isOk());
 60	}
 61
 62	template<> template<>
 63	void host_object::test<3>()
 64	{
 65		const char* str = "192.168.1.1";
 66		U32 port = 8080;
 67		LLHost host(str, port);
 68		ensure("IP address could not be processed", (host.getAddress() == ip_string_to_u32(str)));
 69		ensure("Port Number is invalid", (port == host.getPort()));
 70	}
 71
 72	template<> template<>
 73	void host_object::test<4>()
 74	{
 75		U32 ip = ip_string_to_u32("192.168.1.1");
 76		U32 port = 22;
 77		U64 ip_port = (((U64) ip) << 32) | port;
 78		LLHost host(ip_port);
 79		ensure("IP address is invalid", ip == host.getAddress());
 80		ensure("Port Number is invalid", port == host.getPort());
 81	}
 82
 83	template<> template<>
 84	void host_object::test<5>()
 85	{
 86		std::string ip_port_string = "192.168.1.1:8080";
 87		U32 ip = ip_string_to_u32("192.168.1.1");
 88		U32 port = 8080;
 89
 90		LLHost host(ip_port_string);
 91		ensure("IP address from IP:port is invalid", ip == host.getAddress());
 92		ensure("Port Number from from IP:port is invalid", port == host.getPort());
 93	}
 94
 95	template<> template<>
 96	void host_object::test<6>()
 97	{
 98		U32 ip = 0xc098017d, port = 8080;
 99		LLHost host;
100		host.set(ip,port);
101		ensure("IP address is invalid", (ip == host.getAddress()));
102		ensure("Port Number is invalid", (port == host.getPort()));
103	}
104
105	template<> template<>
106	void host_object::test<7>()
107	{
108		const char* str = "192.168.1.1";
109		U32 port = 8080, ip;
110		LLHost host;
111		host.set(str,port);
112		ip = ip_string_to_u32(str);
113		ensure("IP address is invalid", (ip == host.getAddress()));
114		ensure("Port Number is invalid", (port == host.getPort()));
115		
116		str = "64.233.187.99";
117		ip = ip_string_to_u32(str);
118		host.setAddress(str);
119		ensure("IP address is invalid", (ip == host.getAddress()));
120
121		ip = 0xc098017b;
122		host.setAddress(ip);
123		ensure("IP address is invalid", (ip == host.getAddress()));
124		// should still use the old port
125		ensure("Port Number is invalid", (port == host.getPort()));
126
127		port = 8084;
128		host.setPort(port);
129		ensure("Port Number is invalid", (port == host.getPort()));
130		// should still use the old address
131		ensure("IP address is invalid", (ip == host.getAddress()));
132	}
133
134	template<> template<>
135	void host_object::test<8>()
136	{
137		const std::string str("192.168.1.1");
138		U32 port = 8080;
139		LLHost host;
140		host.set(str,port);
141
142		std::string ip_string = host.getIPString();
143		ensure("Function Failed", (ip_string == str));
144
145		std::string ip_string_port = host.getIPandPort();
146		ensure("Function Failed", (ip_string_port == "192.168.1.1:8080"));
147	}
148
149
150//	getHostName()  and setHostByName
151	template<> template<>
152	void host_object::test<9>()
153	{
154		skip("this test is flaky, but we should figure out why...");
155//		skip("setHostByName(\"google.com\"); getHostName() -> (e.g.) \"yx-in-f100.1e100.net\"");
156		std::string hostStr = "lindenlab.com";		
157		LLHost host;
158		host.setHostByName(hostStr);	
159
160		// reverse DNS will likely result in appending of some
161		// sub-domain to the main hostname. so look for
162		// the main domain name and not do the exact compare
163		
164		std::string hostname = host.getHostName();
165		try
166		{
167			ensure("getHostName failed", hostname.find(hostStr) != std::string::npos);
168		}
169		catch (const std::exception&)
170		{
171			std::cerr << "set '" << hostStr << "'; reported '" << hostname << "'" << std::endl;
172			throw;
173		}
174	}
175
176//	setHostByName for dotted IP
177	template<> template<>
178	void host_object::test<10>()
179	{
180		std::string hostStr = "64.233.167.99";		
181		LLHost host;
182		host.setHostByName(hostStr);	
183		ensure("SetHostByName for dotted IP Address failed", host.getAddress() == ip_string_to_u32(hostStr.c_str()));
184	}
185
186	template<> template<>
187	void host_object::test<11>()
188	{
189		LLHost host1(0xc098017d, 8080);
190		LLHost host2 = host1;
191		ensure("Both IP addresses are not same", (host1.getAddress() == host2.getAddress()));
192		ensure("Both port numbers are not same", (host1.getPort() == host2.getPort()));
193	}
194
195	template<> template<>
196	void host_object::test<12>()
197	{
198		LLHost host1("192.168.1.1", 8080);
199		std::string str1 = "192.168.1.1:8080";
200		std::ostringstream stream;
201		stream << host1;
202		ensure("Operator << failed", ( stream.str()== str1));
203
204		// There is no istream >> llhost operator.
205		//std::istringstream is(stream.str());
206		//LLHost host2;
207		//is >> host2;
208		//ensure("Operator >> failed. Not compatible with <<", host1 == host2);
209	}
210
211	// operators ==, !=, <
212	template<> template<>
213	void host_object::test<13>()
214	{
215		U32 ip_addr = 0xc098017d;
216		U32 port = 8080;
217		LLHost host1(ip_addr, port);
218		LLHost host2(ip_addr, port);
219		ensure("operator== failed", host1 == host2);
220
221		// change port
222		host2.setPort(7070);
223		ensure("operator!= failed", host1 != host2);
224
225		// set port back to 8080 and change IP address now
226		host2.setPort(8080);
227		host2.setAddress(ip_addr+10);
228		ensure("operator!= failed", host1 != host2);
229
230		ensure("operator<  failed", host1 < host2);
231
232		// set IP address back to same value and change port
233		host2.setAddress(ip_addr);
234		host2.setPort(host1.getPort() + 10);
235		ensure("operator<  failed", host1 < host2);
236	}
237
238	// invalid ip address string
239	template<> template<>
240	void host_object::test<14>()
241	{
242		LLHost host1("10.0.1.2", 6143);
243		ensure("10.0.1.2 should be a valid address", host1.isOk());
244
245		LLHost host2("booger-brains", 6143);
246		ensure("booger-brains should be an invalid ip addess", !host2.isOk());
247
248		LLHost host3("255.255.255.255", 6143);
249		ensure("255.255.255.255 should be valid broadcast address", host3.isOk());
250	}
251}