llhost_tut.cpp

Go to the documentation of this file.
00001 
00034 #include <tut/tut.h>
00035 #include "linden_common.h"
00036 #include "lltut.h"
00037 #include "llhost.h"
00038 
00039 namespace tut
00040 {
00041         struct host_data
00042         {
00043         };
00044         typedef test_group<host_data> host_test;
00045         typedef host_test::object host_object;
00046         tut::host_test host_testcase("llhost");
00047 
00048 
00049         template<> template<>
00050         void host_object::test<1>()
00051         {
00052                 LLHost host;
00053                 ensure("IP address is not NULL", (0 == host.getAddress()) && (0 == host.getPort()) && !host.isOk());
00054         }
00055         template<> template<>
00056         void host_object::test<2>()
00057         {
00058                 U32 ip_addr = 0xc098017d;
00059                 U32 port = 8080;
00060                 LLHost host(ip_addr, port);
00061                 ensure("IP address is invalid", ip_addr == host.getAddress());
00062                 ensure("Port Number is invalid", port == host.getPort());
00063                 ensure("IP address and port number both should be ok", host.isOk());
00064         }
00065 
00066         template<> template<>
00067         void host_object::test<3>()
00068         {
00069                 const char* str = "192.168.1.1";
00070                 U32 port = 8080;
00071                 LLHost host(str, port);
00072                 ensure("IP address could not be processed", (host.getAddress() == ip_string_to_u32(str)));
00073                 ensure("Port Number is invalid", (port == host.getPort()));
00074         }
00075 
00076         template<> template<>
00077         void host_object::test<4>()
00078         {
00079                 U32 ip = ip_string_to_u32("192.168.1.1");
00080                 U32 port = 22;
00081                 U64 ip_port = (((U64) ip) << 32) | port;
00082                 LLHost host(ip_port);
00083                 ensure("IP address is invalid", ip == host.getAddress());
00084                 ensure("Port Number is invalid", port == host.getPort());
00085         }
00086 
00087         template<> template<>
00088         void host_object::test<5>()
00089         {
00090                 std::string ip_port_string = "192.168.1.1:8080";
00091                 U32 ip = ip_string_to_u32("192.168.1.1");
00092                 U32 port = 8080;
00093 
00094                 LLHost host(ip_port_string.c_str());
00095                 ensure("IP address from IP:port is invalid", ip == host.getAddress());
00096                 ensure("Port Number from from IP:port is invalid", port == host.getPort());
00097         }
00098 
00099         template<> template<>
00100         void host_object::test<6>()
00101         {
00102                 U32 ip = 0xc098017d, port = 8080;
00103                 LLHost host;
00104                 host.set(ip,port);
00105                 ensure("IP address is invalid", (ip == host.getAddress()));
00106                 ensure("Port Number is invalid", (port == host.getPort()));
00107         }
00108 
00109         template<> template<>
00110         void host_object::test<7>()
00111         {
00112                 const char* str = "192.168.1.1";
00113                 U32 port = 8080, ip;
00114                 LLHost host;
00115                 host.set(str,port);
00116                 ip = ip_string_to_u32(str);
00117                 ensure("IP address is invalid", (ip == host.getAddress()));
00118                 ensure("Port Number is invalid", (port == host.getPort()));
00119                 
00120                 str = "64.233.187.99";
00121                 ip = ip_string_to_u32(str);
00122                 host.setAddress(str);
00123                 ensure("IP address is invalid", (ip == host.getAddress()));
00124 
00125                 ip = 0xc098017b;
00126                 host.setAddress(ip);
00127                 ensure("IP address is invalid", (ip == host.getAddress()));
00128                 // should still use the old port
00129                 ensure("Port Number is invalid", (port == host.getPort()));
00130 
00131                 port = 8084;
00132                 host.setPort(port);
00133                 ensure("Port Number is invalid", (port == host.getPort()));
00134                 // should still use the old address
00135                 ensure("IP address is invalid", (ip == host.getAddress()));
00136         }
00137 
00138         template<> template<>
00139         void host_object::test<8>()
00140         {
00141                 char buffer[50];
00142                 char buffer1[50];
00143                 const char* str = "192.168.1.1";
00144                 U32 port = 8080;
00145                 LLHost host;
00146                 host.set(str,port);
00147                 host.getString(buffer, 50);     
00148                 ensure("Function Failed", (0 == strcmp("192.168.1.1:8080", buffer)));
00149 
00150                 host.getIPString(buffer1, 50);
00151                 ensure("Function Failed", (0 == strcmp(str, buffer1)));
00152 
00153                 std::string ip_string = host.getIPString();
00154                 ensure("Function Failed", (0 == strcmp(str, buffer1)));
00155 
00156                 std::string ip_string_port = host.getIPandPort();
00157                 ensure("Function Failed", (0 == strcmp("192.168.1.1:8080", buffer)));
00158         }
00159 
00160 
00161 //      getHostName()  and setHostByName
00162         template<> template<>
00163         void host_object::test<9>()
00164         {
00165                 std::string hostStr = "google.com";             
00166                 char buffer[50] = {0};
00167                 LLHost host;
00168                 host.setHostByName(hostStr.c_str());    
00169                 host.getHostName(buffer, 50);
00170                 // reverse DNS will likely result in appending of some
00171                 // sub-domain to the main hostname. so look for
00172                 // the main domain name and not do the exact compare
00173                 ensure("getHostName failed", (NULL != strstr(buffer, hostStr.c_str())));
00174                 
00175                 LLString hostname = host.getHostName();
00176                 ensure("getHostName failed", (NULL != strstr(hostname.c_str(), hostStr.c_str())));
00177         }
00178 
00179 //      setHostByName for dotted IP
00180         template<> template<>
00181         void host_object::test<10>()
00182         {
00183                 std::string hostStr = "64.233.167.99";          
00184                 LLHost host;
00185                 host.setHostByName(hostStr.c_str());    
00186                 ensure("SetHostByName for dotted IP Address failed", host.getAddress() == ip_string_to_u32(hostStr.c_str()));
00187         }
00188 
00189         template<> template<>
00190         void host_object::test<11>()
00191         {
00192                 LLHost host1(0xc098017d, 8080);
00193                 LLHost host2 = host1;
00194                 ensure("Both IP addresses are not same", (host1.getAddress() == host2.getAddress()));
00195                 ensure("Both port numbers are not same", (host1.getPort() == host2.getPort()));
00196         }
00197 
00198         template<> template<>
00199         void host_object::test<12>()
00200         {
00201                 LLHost host1("192.168.1.1", 8080);
00202                 std::string str1 = "192.168.1.1:8080";
00203                 std::ostringstream stream;
00204                 stream << host1;
00205                 ensure("Operator << failed", ( stream.str()== str1));
00206 
00207                 // There is no istream >> llhost operator.
00208                 //std::istringstream is(stream.str());
00209                 //LLHost host2;
00210                 //is >> host2;
00211                 //ensure("Operator >> failed. Not compatible with <<", host1 == host2);
00212         }
00213 
00214         // operators ==, !=, <
00215         template<> template<>
00216         void host_object::test<13>()
00217         {
00218                 U32 ip_addr = 0xc098017d;
00219                 U32 port = 8080;
00220                 LLHost host1(ip_addr, port);
00221                 LLHost host2(ip_addr, port);
00222                 ensure("operator== failed", host1 == host2);
00223 
00224                 // change port
00225                 host2.setPort(7070);
00226                 ensure("operator!= failed", host1 != host2);
00227 
00228                 // set port back to 8080 and change IP address now
00229                 host2.setPort(8080);
00230                 host2.setAddress(ip_addr+10);
00231                 ensure("operator!= failed", host1 != host2);
00232 
00233                 ensure("operator<  failed", host1 < host2);
00234 
00235                 // set IP address back to same value and change port
00236                 host2.setAddress(ip_addr);
00237                 host2.setPort(host1.getPort() + 10);
00238                 ensure("operator<  failed", host1 < host2);
00239         }
00240 }

Generated on Thu Jul 1 06:08:41 2010 for Second Life Viewer by  doxygen 1.4.7