| 1 |
#include "protoApp.h" |
| 2 |
#include "protoNet.h" |
| 3 |
#include "protoRouteMgr.h" |
| 4 |
#include "testFuncs.h" |
| 5 |
|
| 6 |
#include <stdlib.h> // for atoi() |
| 7 |
#include <stdio.h> // for stdout/stderr printouts |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
class UnitTests : |
| 11 |
public ProtoApp |
| 12 |
{ |
| 13 |
public: |
| 14 |
UnitTests(); |
| 15 |
~UnitTests(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Override from ProtoApp |
| 19 |
*/ |
| 20 |
bool OnStartup(int argc, const char*const* argv); |
| 21 |
/** |
| 22 |
* Override from ProtoApp |
| 23 |
*/ |
| 24 |
bool ProcessCommands(int argc, const char*const* argv); |
| 25 |
/** |
| 26 |
* Override from ProtoApp |
| 27 |
*/ |
| 28 |
void OnShutdown(); |
| 29 |
|
| 30 |
bool DumpIfaceInfo(); |
| 31 |
|
| 32 |
bool AddrTests(); |
| 33 |
|
| 34 |
bool BasicAddrTests(const char* val, int port, ProtoAddress::Type type, bool mcast, bool bcast, bool loop, bool linkLocal, bool siteLocal, bool unspec,bool unicast); |
| 35 |
|
| 36 |
bool EqualityTests(); |
| 37 |
|
| 38 |
bool EqualityTests(ProtoAddress& addr, ProtoAddress& cmpAddr, int equality); |
| 39 |
|
| 40 |
bool PrefixTests(); |
| 41 |
|
| 42 |
bool AddrListTests(); |
| 43 |
|
| 44 |
bool ManetMsgTests(); |
| 45 |
|
| 46 |
int PrintList(ProtoAddressList& theList); |
| 47 |
|
| 48 |
private: |
| 49 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 50 |
static CmdType GetCmdType(const char* string); |
| 51 |
bool OnCommand(const char* cmd, const char* val); |
| 52 |
void Usage(); |
| 53 |
|
| 54 |
static const char* const CMD_LIST[]; |
| 55 |
int addr_listSize; |
| 56 |
// ProtoTimer/ UDP socket demo members |
| 57 |
ProtoTimer tx_timer; |
| 58 |
ProtoAddress dst_addr; |
| 59 |
ProtoAddressList addr_list; |
| 60 |
|
| 61 |
}; // end class UnitTests |
| 62 |
|
| 63 |
|
| 64 |
// Our application instance |
| 65 |
PROTO_INSTANTIATE_APP(UnitTests) |
| 66 |
|
| 67 |
UnitTests::UnitTests() : addr_listSize(0) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
UnitTests::~UnitTests() |
| 72 |
{ |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
void UnitTests::Usage() |
| 77 |
{ |
| 78 |
fprintf(stderr, "unitTests [debug <n>][addr][dumpIfaces][manetMsgTests]\n"); |
| 79 |
|
| 80 |
} // end UnitTests::Usage() |
| 81 |
|
| 82 |
|
| 83 |
const char* const UnitTests::CMD_LIST[] = |
| 84 |
{ |
| 85 |
"-help", // usage |
| 86 |
"+debug", // Debug level |
| 87 |
"-addr", // Run ProtoAddress tests |
| 88 |
"-manetMsgTests", // Run manet message tests |
| 89 |
"-dumpIfaces", // Get all of our ifaces and dump some info (name, MAC addr, IP addrs, etc) |
| 90 |
NULL |
| 91 |
}; |
| 92 |
|
| 93 |
bool UnitTests::DumpIfaceInfo() |
| 94 |
{ |
| 95 |
|
| 96 |
// Here's some code to test the ProtoSocket routines for network interface info |
| 97 |
|
| 98 |
ProtoAddress localAddress; |
| 99 |
char nameBuffer[256]; |
| 100 |
nameBuffer[255] = '\0'; |
| 101 |
|
| 102 |
if (localAddress.ResolveLocalAddress()) |
| 103 |
{ |
| 104 |
TRACE("unitTests: local default IP address: %s\n", localAddress.GetHostString()); |
| 105 |
if (localAddress.ResolveToName(nameBuffer, 255)) |
| 106 |
TRACE("unitTests: local default host name: %s\n", nameBuffer); |
| 107 |
else |
| 108 |
TRACE("unitTests: unable to resolve local default IP address to name\n"); |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
TRACE("unitTests: unable to determine local default IP address\n"); |
| 113 |
} |
| 114 |
|
| 115 |
// Get all of our ifaces and dump some info (name, MAC addr, IP addrs, etc) |
| 116 |
unsigned int ifaceCount = ProtoNet::GetInterfaceCount(); |
| 117 |
if (ifaceCount > 0) |
| 118 |
{ |
| 119 |
// Allocate array to hold the indices |
| 120 |
unsigned int* indexArray = new unsigned int[ifaceCount]; |
| 121 |
if (NULL == indexArray) |
| 122 |
{ |
| 123 |
PLOG(PL_ERROR, "unitTests: new indexArray[%u] error: %s\n", ifaceCount, GetErrorString()); |
| 124 |
return false; |
| 125 |
} |
| 126 |
if (ProtoNet::GetInterfaceIndices(indexArray, ifaceCount) != ifaceCount) |
| 127 |
{ |
| 128 |
PLOG(PL_ERROR, "unitTests: GetInterfaceIndices() error?!\n"); |
| 129 |
delete[] indexArray; |
| 130 |
return false; |
| 131 |
} |
| 132 |
for (unsigned int i = 0; i < ifaceCount; i++) |
| 133 |
{ |
| 134 |
unsigned int index = indexArray[i]; |
| 135 |
TRACE("unitTests: interface index %u ", index); |
| 136 |
// Get the iface name |
| 137 |
if (ProtoNet::GetInterfaceName(index, nameBuffer, 255)) |
| 138 |
TRACE("has name \"%s\" ", nameBuffer); |
| 139 |
else |
| 140 |
TRACE("name \"unknown\" "); |
| 141 |
// Get MAC addr for the iface |
| 142 |
ProtoAddress ifaceAddr; |
| 143 |
if (ProtoNet::GetInterfaceAddress(nameBuffer, ProtoAddress::ETH, ifaceAddr)) |
| 144 |
TRACE("with MAC addr %s\n", ifaceAddr.GetHostString()); |
| 145 |
else |
| 146 |
TRACE("with no MAC addr\n"); |
| 147 |
|
| 148 |
ProtoAddressList addrList; |
| 149 |
// Get IPv4 addrs for the iface |
| 150 |
if (ProtoNet::GetInterfaceAddressList(nameBuffer, ProtoAddress::IPv4, addrList)) |
| 151 |
{ |
| 152 |
TRACE(" IPv4 addresses:"); |
| 153 |
ProtoAddressList::Iterator iterator(addrList); |
| 154 |
while (iterator.GetNextAddress(ifaceAddr)) |
| 155 |
{ |
| 156 |
TRACE(" %s/%d", ifaceAddr.GetHostString(),ProtoNet::GetInterfaceAddressMask(nameBuffer, ifaceAddr)); |
| 157 |
if (ifaceAddr.IsLinkLocal()) TRACE(" (link local)"); |
| 158 |
} |
| 159 |
TRACE("\n"); |
| 160 |
} |
| 161 |
addrList.Destroy(); |
| 162 |
// Get IPv6 addrs for the iface |
| 163 |
if (ProtoNet::GetInterfaceAddressList(nameBuffer, ProtoAddress::IPv6, addrList)) |
| 164 |
{ |
| 165 |
TRACE(" IPv6 addresses:"); |
| 166 |
ProtoAddressList::Iterator iterator(addrList); |
| 167 |
while (iterator.GetNextAddress(ifaceAddr)) |
| 168 |
{ |
| 169 |
|
| 170 |
TRACE(" %s/%d", ifaceAddr.GetHostString(), ProtoNet::GetInterfaceAddressMask(nameBuffer, ifaceAddr)); |
| 171 |
|
| 172 |
if (ifaceAddr.IsLinkLocal()) TRACE(" (link local)"); |
| 173 |
} |
| 174 |
TRACE("\n"); |
| 175 |
} |
| 176 |
addrList.Destroy(); |
| 177 |
|
| 178 |
// Test our interface add routine |
| 179 |
/* |
| 180 |
char * ifaceName = "192.168.1.6"; |
| 181 |
ProtoAddress tmpAddr; |
| 182 |
tmpAddr.ConvertFromString("192.168.1.6"); |
| 183 |
int maskLen = 24; |
| 184 |
bool result = ProtoNet::AddInterfaceAddress(ifaceName, tmpAddr, maskLen); |
| 185 |
*/ |
| 186 |
|
| 187 |
|
| 188 |
#ifndef WIN32 |
| 189 |
// This code should work for Linux and BSD (incl. Mac OSX) |
| 190 |
// Get IPv4 group memberships for interfaces |
| 191 |
if (ProtoNet::GetGroupMemberships(nameBuffer, ProtoAddress::IPv4, addrList)) |
| 192 |
{ |
| 193 |
TRACE(" IPv4 memberships:"); |
| 194 |
ProtoAddressList::Iterator iterator(addrList); |
| 195 |
ProtoAddress groupAddr; |
| 196 |
while (iterator.GetNextAddress(groupAddr)) |
| 197 |
{ |
| 198 |
TRACE(" %s", groupAddr.GetHostString()); |
| 199 |
} |
| 200 |
TRACE("\n"); |
| 201 |
} |
| 202 |
addrList.Destroy(); |
| 203 |
if (ProtoNet::GetGroupMemberships(nameBuffer, ProtoAddress::IPv6, addrList)) |
| 204 |
{ |
| 205 |
TRACE(" IPv6 memberships:"); |
| 206 |
ProtoAddressList::Iterator iterator(addrList); |
| 207 |
ProtoAddress groupAddr; |
| 208 |
while (iterator.GetNextAddress(groupAddr)) |
| 209 |
{ |
| 210 |
TRACE(" %s", groupAddr.GetHostString()); |
| 211 |
} |
| 212 |
TRACE("\n"); |
| 213 |
} |
| 214 |
addrList.Destroy(); |
| 215 |
#endif // WIN32 |
| 216 |
} |
| 217 |
delete[] indexArray; |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
TRACE("unitTests: host has no network interfaces?!\n"); |
| 222 |
} |
| 223 |
// Here's some code to get the system routing table |
| 224 |
ProtoRouteTable routeTable; |
| 225 |
ProtoRouteMgr* routeMgr = ProtoRouteMgr::Create(); |
| 226 |
if (NULL == routeMgr) |
| 227 |
{ |
| 228 |
PLOG(PL_ERROR, "UnitTests::OnStartup() error creating route manager\n"); |
| 229 |
return false; |
| 230 |
} |
| 231 |
if (!routeMgr->Open()) |
| 232 |
{ |
| 233 |
PLOG(PL_ERROR, "UnitTests::OnStartup() error opening route manager\n"); |
| 234 |
return false; |
| 235 |
} |
| 236 |
if (!routeMgr->GetAllRoutes(ProtoAddress::IPv4, routeTable)) |
| 237 |
PLOG(PL_ERROR, "UnitTests::OnStartup() warning getting system routes\n"); |
| 238 |
// Display whatever routes we got |
| 239 |
ProtoRouteTable::Iterator iterator(routeTable); |
| 240 |
ProtoRouteTable::Entry* entry; |
| 241 |
PLOG(PL_ALWAYS, "IPv4 Routing Table:\n"); |
| 242 |
PLOG(PL_ALWAYS, "%16s/Prefix %-12s ifIndex Metric\n", "Destination", "Gateway"); |
| 243 |
while (NULL != (entry = iterator.GetNextEntry())) |
| 244 |
{ |
| 245 |
PLOG(PL_ALWAYS, "%16s/%-3u ", |
| 246 |
entry->GetDestination().GetHostString(), entry->GetPrefixSize()); |
| 247 |
const ProtoAddress& gw = entry->GetGateway(); |
| 248 |
PLOG(PL_ALWAYS, "%-16s %-02u %d\n", |
| 249 |
gw.IsValid() ? gw.GetHostString() : "0.0.0.0", |
| 250 |
entry->GetInterfaceIndex(), |
| 251 |
entry->GetMetric()); |
| 252 |
} |
| 253 |
//ProtoAddress addr; |
| 254 |
//addr.ResolveFromString("10.1.2.3"); |
| 255 |
//ProtoAddress gw; |
| 256 |
//routeMgr->SetRoute(addr, 32, gw, 4, 0); |
| 257 |
routeMgr->Close(); |
| 258 |
|
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
UnitTests::CmdType UnitTests::GetCmdType(const char* cmd) |
| 263 |
{ |
| 264 |
if (!cmd) return CMD_INVALID; |
| 265 |
unsigned int len = strlen(cmd); |
| 266 |
bool matched = false; |
| 267 |
CmdType type = CMD_INVALID; |
| 268 |
const char* const* nextCmd = CMD_LIST; |
| 269 |
while (*nextCmd) |
| 270 |
{ |
| 271 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 272 |
{ |
| 273 |
if (matched) |
| 274 |
{ |
| 275 |
// ambiguous command (command should match only once) |
| 276 |
return CMD_INVALID; |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
matched = true; |
| 281 |
if ('+' == *nextCmd[0]) |
| 282 |
type = CMD_ARG; |
| 283 |
else |
| 284 |
type = CMD_NOARG; |
| 285 |
} |
| 286 |
} |
| 287 |
nextCmd++; |
| 288 |
} |
| 289 |
return type; |
| 290 |
} // end UnitTests::GetCmdType() |
| 291 |
|
| 292 |
bool UnitTests::OnStartup(int argc, const char*const* argv) |
| 293 |
{ |
| 294 |
if (!ProcessCommands(argc, argv)) |
| 295 |
{ |
| 296 |
PLOG(PL_ERROR, "unitTests: Error! bad command line\n"); |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
return true; |
| 301 |
} // end UnitTests::OnStartup() |
| 302 |
|
| 303 |
void UnitTests::OnShutdown() |
| 304 |
{ |
| 305 |
PLOG(PL_INFO, "unitTests: Done.\n"); |
| 306 |
|
| 307 |
CloseDebugLog(); |
| 308 |
} // end UnitTests::OnShutdown() |
| 309 |
|
| 310 |
bool UnitTests::ProcessCommands(int argc, const char*const* argv) |
| 311 |
{ |
| 312 |
// Dispatch command-line commands to our OnCommand() method |
| 313 |
int i = 1; |
| 314 |
while ( i < argc) |
| 315 |
{ |
| 316 |
// Is it a class UnitTests command? |
| 317 |
switch (GetCmdType(argv[i])) |
| 318 |
{ |
| 319 |
case CMD_INVALID: |
| 320 |
{ |
| 321 |
PLOG(PL_ERROR, "UnitTests::ProcessCommands() Invalid command:%s\n", |
| 322 |
argv[i]); |
| 323 |
|
| 324 |
return false; |
| 325 |
} |
| 326 |
case CMD_NOARG: |
| 327 |
if (!OnCommand(argv[i], NULL)) |
| 328 |
{ |
| 329 |
PLOG(PL_ERROR, "UnitTests::ProcessCommands() ProcessCommand(%s) error\n", |
| 330 |
argv[i]); |
| 331 |
return false; |
| 332 |
} |
| 333 |
i++; |
| 334 |
break; |
| 335 |
case CMD_ARG: |
| 336 |
if (!OnCommand(argv[i], argv[i+1])) |
| 337 |
{ |
| 338 |
PLOG(PL_ERROR, "UnitTests::ProcessCommands() ProcessCommand(%s, %s) error\n", |
| 339 |
argv[i], argv[i+1]); |
| 340 |
return false; |
| 341 |
} |
| 342 |
i += 2; |
| 343 |
break; |
| 344 |
} |
| 345 |
|
| 346 |
} |
| 347 |
return true; |
| 348 |
} // end UnitTests::ProcessCommands() |
| 349 |
|
| 350 |
bool UnitTests::OnCommand(const char* cmd, const char* val) |
| 351 |
{ |
| 352 |
|
| 353 |
CmdType type = GetCmdType(cmd); |
| 354 |
ASSERT(CMD_INVALID != type); |
| 355 |
unsigned int len = strlen(cmd); |
| 356 |
if ((CMD_ARG == type) && !val) |
| 357 |
{ |
| 358 |
PLOG(PL_ERROR, "UnitTests::ProcessCommand(%s) missing argument\n", cmd); |
| 359 |
return false; |
| 360 |
} |
| 361 |
else if (!strncmp("addr", cmd, len)) |
| 362 |
{ |
| 363 |
if (!AddrTests()) |
| 364 |
{ |
| 365 |
PLOG(PL_ERROR,"FAIL: AddrTests() \n\n"); |
| 366 |
} |
| 367 |
} |
| 368 |
else if (!strncmp("manetMsgTests",cmd,len)) |
| 369 |
{ |
| 370 |
if (!ManetMsgTests()) |
| 371 |
{ |
| 372 |
PLOG(PL_ERROR,"FAIL: ManetMsgTests()\n\n"); |
| 373 |
} |
| 374 |
} |
| 375 |
else if (!strncmp("dumpIfaces",cmd,len)) |
| 376 |
{ |
| 377 |
if (!DumpIfaceInfo()) |
| 378 |
{ |
| 379 |
PLOG(PL_ERROR,"FAIL: DumpIfaceInfo()\n\n"); |
| 380 |
} |
| 381 |
} |
| 382 |
else if (!strncmp("debug",cmd,len)) |
| 383 |
{ |
| 384 |
SetDebugLevel(atoi(val)); |
| 385 |
} |
| 386 |
else if (!strncmp("help",cmd,len)) |
| 387 |
{ |
| 388 |
Usage(); |
| 389 |
} |
| 390 |
return true; |
| 391 |
} // end UnitTests::OnCommand() |
| 392 |
|
| 393 |
bool UnitTests::EqualityTests() |
| 394 |
{ |
| 395 |
bool result = true; |
| 396 |
// Test equality |
| 397 |
ProtoAddress addr = ProtoAddress("10.0.0.10"); |
| 398 |
ProtoAddress cmpAddr = ProtoAddress("10.0.0.10"); |
| 399 |
char nameBuffer[256]; |
| 400 |
nameBuffer[255] = '\0'; |
| 401 |
char cmpNameBuffer[256]; |
| 402 |
cmpNameBuffer[255] = '\0'; |
| 403 |
|
| 404 |
// addr == cmpAddr |
| 405 |
if (!EqualityTests(addr,cmpAddr,0)) |
| 406 |
{ |
| 407 |
result = false; |
| 408 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d == cmpAddr %s/%d (default port assignment)\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 409 |
} |
| 410 |
addr.SetPort(5000); |
| 411 |
cmpAddr.SetPort(5000); |
| 412 |
// addr == cmpAddr, ports equal |
| 413 |
if (!EqualityTests(addr,cmpAddr,0)) |
| 414 |
{ |
| 415 |
result = false; |
| 416 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d == cmpAddr %s/%d (port assignment)\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 417 |
} |
| 418 |
addr.SetPort(5000); |
| 419 |
cmpAddr.SetPort(5001); |
| 420 |
// addr == cmpAddr, ports != |
| 421 |
// We have the same address but different ports, check address equality |
| 422 |
if (addr.IsEqual(cmpAddr) |
| 423 |
|| addr==cmpAddr |
| 424 |
|| !(addr!=cmpAddr)) |
| 425 |
{ |
| 426 |
result = false; |
| 427 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d == cmpAddr %s/%d port mismatch should not pass equality test\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 428 |
} |
| 429 |
|
| 430 |
cmpAddr.Reset(ProtoAddress::IPv4,0); |
| 431 |
cmpAddr.ResolveFromString("10.0.0.11"); |
| 432 |
// addr < cmpAddr |
| 433 |
if (!EqualityTests(addr,cmpAddr,-1)) |
| 434 |
{ |
| 435 |
result = false; |
| 436 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d < cmpAddr %s/%d \n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 437 |
} |
| 438 |
|
| 439 |
cmpAddr.Reset(ProtoAddress::IPv4,0); |
| 440 |
cmpAddr.ResolveFromString("192.168.0.1"); |
| 441 |
// addr < cmpAddr different networks |
| 442 |
if (!EqualityTests(addr,cmpAddr,-1)) |
| 443 |
{ |
| 444 |
result = false; |
| 445 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d < cmpAddr %s/%d (different networks) \n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 446 |
} |
| 447 |
addr.Reset(ProtoAddress::IPv4,0); |
| 448 |
addr.ResolveFromString("10.0.0.1"); |
| 449 |
cmpAddr.Reset(ProtoAddress::IPv4,0); |
| 450 |
cmpAddr.ResolveFromString("10.0.0.0"); |
| 451 |
// addr > cmpAddr |
| 452 |
if (!EqualityTests(addr,cmpAddr,1)) |
| 453 |
{ |
| 454 |
result = false; |
| 455 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d > cmpAddr %s/%d \n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 456 |
} |
| 457 |
|
| 458 |
addr.Reset(ProtoAddress::IPv4,0); |
| 459 |
addr.ResolveFromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); |
| 460 |
cmpAddr.Reset(ProtoAddress::IPv4,0); |
| 461 |
cmpAddr.ResolveFromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); |
| 462 |
// addr == cmpAddr |
| 463 |
if (!EqualityTests(addr,cmpAddr,0)) |
| 464 |
{ |
| 465 |
result = false; |
| 466 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d == cmpAddr %s/%d (IPv6)\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 467 |
} |
| 468 |
|
| 469 |
addr.Reset(ProtoAddress::IPv6,0); |
| 470 |
addr.ResolveFromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); |
| 471 |
cmpAddr.Reset(ProtoAddress::IPv6,0); |
| 472 |
cmpAddr.ResolveFromString("2001:0db8:85a3:0000:0000:8a2e:0380:7334"); |
| 473 |
// addr < cmpAddr |
| 474 |
if (!EqualityTests(addr,cmpAddr,-1)) |
| 475 |
{ |
| 476 |
result = false; |
| 477 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d < cmpAddr %s/%d (IPv6)\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 478 |
} |
| 479 |
|
| 480 |
addr.Reset(ProtoAddress::IPv6,0); |
| 481 |
addr.ResolveFromString("2001:0db8:85a3:0000:0000:8a2e:0470:7334"); |
| 482 |
cmpAddr.Reset(ProtoAddress::IPv6,0); |
| 483 |
cmpAddr.ResolveFromString("2001:0db8:85a3:0000:0000:8a2e:0380:7334"); |
| 484 |
// addr > cmpAddr |
| 485 |
if (!EqualityTests(addr,cmpAddr,1)) |
| 486 |
{ |
| 487 |
result = false; |
| 488 |
PLOG(PL_ERROR,"FAIL: EqualityTests() addr %s/%d > cmpAddr %s/%d (IPv6)\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 489 |
} |
| 490 |
|
| 491 |
return result; |
| 492 |
} // UnitTests:EqualityTests |
| 493 |
|
| 494 |
bool UnitTests::EqualityTests(ProtoAddress& addr, ProtoAddress& cmpAddr, int equality) |
| 495 |
{ |
| 496 |
/* |
| 497 |
* equality 0 == addresses are equal |
| 498 |
* 1 == addr > cmpAddr |
| 499 |
* -1 == addr < cmpAddr |
| 500 |
*/ |
| 501 |
|
| 502 |
char nameBuffer[256]; |
| 503 |
nameBuffer[255] = '\0'; |
| 504 |
char cmpNameBuffer[256]; |
| 505 |
cmpNameBuffer[255] = '\0'; |
| 506 |
|
| 507 |
PLOG(PL_INFO,"INFO: EqualityTests() addr %s/%d == cmpAddr %s/%d (default port assignment) equality>%d\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort(),equality); |
| 508 |
|
| 509 |
const char* test = "EqualityTests(addr,cmpAddr,equality)"; |
| 510 |
bool result = true; |
| 511 |
|
| 512 |
if (addr.IsEqual(cmpAddr)) |
| 513 |
{ |
| 514 |
if (equality != 0) |
| 515 |
{ |
| 516 |
PLOG(PL_ERROR,"FAIL: %s IsEqual()\n",test); |
| 517 |
result = false; |
| 518 |
} |
| 519 |
} else |
| 520 |
{ |
| 521 |
if (equality == 0) |
| 522 |
{ |
| 523 |
PLOG(PL_ERROR,"FAIL: %s IsEqual()\n",test); |
| 524 |
result = false; |
| 525 |
} |
| 526 |
|
| 527 |
} |
| 528 |
|
| 529 |
if (addr.HostIsEqual(cmpAddr)) |
| 530 |
{ |
| 531 |
if (equality != 0) |
| 532 |
{ |
| 533 |
PLOG(PL_ERROR,"FAIL: %s HostIsEqual()\n",test); |
| 534 |
result = false; |
| 535 |
} |
| 536 |
} else |
| 537 |
{ |
| 538 |
if (equality == 0) |
| 539 |
{ |
| 540 |
PLOG(PL_ERROR,"FAIL: %s HostIsEqual()\n",test); |
| 541 |
result = false; |
| 542 |
} |
| 543 |
} |
| 544 |
if (addr.CompareHostAddr(cmpAddr) == 0) |
| 545 |
{ |
| 546 |
if (equality != 0) |
| 547 |
{ |
| 548 |
PLOG(PL_ERROR,"FAIL: %s CompareHostAddr() %d\n",test,addr.CompareHostAddr(cmpAddr)); |
| 549 |
result = false; |
| 550 |
} |
| 551 |
} else |
| 552 |
{ |
| 553 |
if (equality == 0) |
| 554 |
{ |
| 555 |
PLOG(PL_ERROR,"FAIL: %s CompareHostAddr() %d\n",test,addr.CompareHostAddr(cmpAddr)); |
| 556 |
result = false; |
| 557 |
} |
| 558 |
} |
| 559 |
if (addr==cmpAddr) |
| 560 |
{ |
| 561 |
if (equality != 0) |
| 562 |
{ |
| 563 |
PLOG(PL_ERROR,"FAIL: %s addr ==\n",test); |
| 564 |
result = false; |
| 565 |
} |
| 566 |
} else |
| 567 |
{ |
| 568 |
if (equality == 0) |
| 569 |
{ |
| 570 |
PLOG(PL_ERROR,"FAIL: %s addr ==\n",test); |
| 571 |
result = false; |
| 572 |
} |
| 573 |
} |
| 574 |
if (addr!=cmpAddr) |
| 575 |
{ |
| 576 |
if (equality == 0) |
| 577 |
{ |
| 578 |
PLOG(PL_ERROR,"FAIL: %s addr != \n",test); |
| 579 |
result = false; |
| 580 |
} |
| 581 |
} else |
| 582 |
{ |
| 583 |
if (equality != 0) |
| 584 |
{ |
| 585 |
PLOG(PL_ERROR,"FAIL: %s !=\n",test); |
| 586 |
result = false; |
| 587 |
} |
| 588 |
} |
| 589 |
if (addr<cmpAddr) |
| 590 |
{ |
| 591 |
if (equality != -1) |
| 592 |
{ |
| 593 |
PLOG(PL_ERROR,"FAIL: %s addr < \n",test); |
| 594 |
result = false; |
| 595 |
} |
| 596 |
} else |
| 597 |
{ |
| 598 |
if (equality == -1) |
| 599 |
{ |
| 600 |
PLOG(PL_ERROR,"FAIL: %s addr <\n",test); |
| 601 |
result = false; |
| 602 |
} |
| 603 |
} |
| 604 |
if (addr>=cmpAddr) |
| 605 |
{ |
| 606 |
if ((equality != 0) && (equality != 1)) |
| 607 |
{ |
| 608 |
PLOG(PL_ERROR,"FAIL: %s addr >= %d \n",test,addr>=cmpAddr); |
| 609 |
result = false; |
| 610 |
} |
| 611 |
} else |
| 612 |
{ |
| 613 |
if ((equality == 0) || (equality == 1)) |
| 614 |
{ |
| 615 |
PLOG(PL_ERROR,"FAIL: %s addr >= %d \n",test,addr>=cmpAddr); |
| 616 |
result = false; |
| 617 |
} |
| 618 |
} |
| 619 |
if (addr<=cmpAddr) |
| 620 |
{ |
| 621 |
if ((equality != 0) && (equality != -1)) |
| 622 |
{ |
| 623 |
PLOG(PL_ERROR,"FAIL: %s addr <= %d\n",test,addr<=cmpAddr); |
| 624 |
result = false; |
| 625 |
} |
| 626 |
} else |
| 627 |
{ |
| 628 |
if ((equality == 0) || (equality == -1)) |
| 629 |
{ |
| 630 |
PLOG(PL_ERROR,"FAIL: %s addr <= %d\n",test,addr<=cmpAddr); |
| 631 |
result = false; |
| 632 |
} |
| 633 |
} |
| 634 |
if (result == false) |
| 635 |
{ |
| 636 |
PLOG(PL_ERROR,"FAIL: EqualityTests(%s/%d,%s/%d,%d) module failure \n\n", addr.GetHostString(nameBuffer,255),addr.GetPort(),cmpAddr.GetHostString(cmpNameBuffer,255),cmpAddr.GetPort()); |
| 637 |
|
| 638 |
} |
| 639 |
// TODO: Force equality on comparisons |
| 640 |
return result; |
| 641 |
} |
| 642 |
bool UnitTests::PrefixTests() |
| 643 |
{ |
| 644 |
const char* test = "PrefixTests()"; |
| 645 |
bool result = true; |
| 646 |
char nameBuffer[256]; |
| 647 |
nameBuffer[255] = '\0'; |
| 648 |
char cmpNameBuffer[256]; |
| 649 |
cmpNameBuffer[255] = '\0'; |
| 650 |
|
| 651 |
ProtoAddress addr = ProtoAddress("169.254.0.1"); |
| 652 |
ProtoAddress cmpAddr = ProtoAddress("169.254.0.2"); |
| 653 |
|
| 654 |
ProtoAddress subnetAddr; |
| 655 |
addr.GetSubnetAddress(8,subnetAddr); |
| 656 |
if (strcmp(subnetAddr.GetHostString(),"169.0.0.0")) |
| 657 |
{ |
| 658 |
result = false; |
| 659 |
PLOG(PL_ERROR,"FAIL: %s Subnet %s != 169.0.0.0\n",test,subnetAddr.GetHostString()); |
| 660 |
} |
| 661 |
addr.GetSubnetAddress(16,subnetAddr); |
| 662 |
if (strcmp(subnetAddr.GetHostString(),"169.254.0.0")) |
| 663 |
{ |
| 664 |
result = false; |
| 665 |
PLOG(PL_ERROR,"FAIL: %s GetSubnetAddress %s != 169.254.0.0 \n",test,subnetAddr.GetHostString()); |
| 666 |
} |
| 667 |
ProtoAddress bcastAddr; |
| 668 |
addr.GetBroadcastAddress(16,bcastAddr); |
| 669 |
if (strcmp(bcastAddr.GetHostString(),"169.254.255.255")) |
| 670 |
{ |
| 671 |
result = false; |
| 672 |
PLOG(PL_ERROR,"FAIL: %s GetBroadcastAddress %s != 169.254.255.255 \n",test,bcastAddr.GetHostString()); |
| 673 |
} |
| 674 |
if (!addr.PrefixIsEqual(cmpAddr,16)) |
| 675 |
{ |
| 676 |
result = false; |
| 677 |
PLOG(PL_ERROR,"FAIL: %s PrefixIsEqual/16 \n",test); |
| 678 |
} |
| 679 |
|
| 680 |
cmpAddr = ProtoAddress("192.168.1.1"); |
| 681 |
if (addr.PrefixIsEqual(cmpAddr,8)) |
| 682 |
{ |
| 683 |
result = false; |
| 684 |
PLOG(PL_ERROR,"FAIL: %s PrefixIsEqual/8 \n",test); |
| 685 |
} |
| 686 |
|
| 687 |
addr = ProtoAddress("169.254.0.1"); |
| 688 |
cmpAddr = ProtoAddress("169.254.0.2"); |
| 689 |
|
| 690 |
addr.SetCommonHead(cmpAddr); |
| 691 |
if (strcmp(addr.GetHostString(),"169.254.0.0")) |
| 692 |
{ |
| 693 |
result = false; |
| 694 |
PLOG(PL_ERROR,"FAIL: %s SetCommonHead %s != 169.254.0.0\n",test,addr.GetHostString()); |
| 695 |
} |
| 696 |
|
| 697 |
addr = ProtoAddress("169.254.0.1"); |
| 698 |
cmpAddr = ProtoAddress("169.132.0.2"); |
| 699 |
|
| 700 |
addr.SetCommonHead(cmpAddr); |
| 701 |
if (strcmp(addr.GetHostString(),"169.0.0.0")) |
| 702 |
{ |
| 703 |
result = false; |
| 704 |
PLOG(PL_ERROR,"FAIL: %s SetCommonHead %s != 169.0.0.0\n",test,addr.GetHostString()); |
| 705 |
} |
| 706 |
|
| 707 |
addr = ProtoAddress("169.254.1.12"); |
| 708 |
cmpAddr = ProtoAddress("169.132.1.12"); |
| 709 |
|
| 710 |
addr.SetCommonTail(cmpAddr); |
| 711 |
if (strcmp(addr.GetHostString(),"0.0.1.12")) |
| 712 |
{ |
| 713 |
result = false; |
| 714 |
PLOG(PL_ERROR,"FAIL: %s SetCommonTail %s != 0.0.1.12\n",test,addr.GetHostString()); |
| 715 |
} |
| 716 |
|
| 717 |
addr = ProtoAddress("fe80::426c"); |
| 718 |
cmpAddr = ProtoAddress("fe80::423c"); |
| 719 |
|
| 720 |
addr.SetCommonHead(cmpAddr); |
| 721 |
if (strcmp(addr.GetHostString(),"fe80::4200")) |
| 722 |
{ |
| 723 |
result = false; |
| 724 |
PLOG(PL_ERROR,"FAIL: %s SetCommonHead %s != fe80::4200 \n",test,addr.GetHostString()); |
| 725 |
} |
| 726 |
|
| 727 |
// This works! |
| 728 |
addr = ProtoAddress("2001:db8:85a3:1212:2121:8a2e:3701:7334"); |
| 729 |
cmpAddr = ProtoAddress("2001:db8:85a3:2121:1212:8a2e:3701:7334"); |
| 730 |
addr.SetCommonTail(cmpAddr); |
| 731 |
if (strcmp(addr.GetHostString(),"::8a2e:3701:7334")) |
| 732 |
{ |
| 733 |
result = false; |
| 734 |
PLOG(PL_ERROR,"FAIL: %s SetCommonTail %s\n",test,addr.GetHostString()); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
// This returns the host in ipv4 notation.. |
| 739 |
addr = ProtoAddress("2001:db8:85a3:1212:2121:8a2e:3701:7334"); |
| 740 |
cmpAddr = ProtoAddress("2001:db8:85a3:2121:1212:8a2f:3701:7334"); |
| 741 |
addr.SetCommonTail(cmpAddr); |
| 742 |
if (strcmp(addr.GetHostString(),"::3701:7334")) |
| 743 |
{ |
| 744 |
result = false; |
| 745 |
PLOG(PL_ERROR,"FAIL: %s SetCommonTail %s\n",test,addr.GetHostString()); |
| 746 |
} |
| 747 |
|
| 748 |
addr.Invalidate(); |
| 749 |
cmpAddr.Invalidate(); |
| 750 |
//addr = ProtoAddress("192.168.1.12"); |
| 751 |
addr.ApplyPrefixMask(16); |
| 752 |
if (strcmp(addr.GetHostString(),"192.168.0.0")) |
| 753 |
{ |
| 754 |
result = false; |
| 755 |
PLOG(PL_ERROR,"FAIL: %s ApplyPrefixMask \n",test); |
| 756 |
} |
| 757 |
addr = ProtoAddress("192.168.1.12"); |
| 758 |
addr.ApplySuffixMask(16); |
| 759 |
if (strcmp(addr.GetHostString(),"0.0.1.12")) |
| 760 |
{ |
| 761 |
result = false; |
| 762 |
PLOG(PL_ERROR,"FAIL: %s ApplyPrefixMask \n",test); |
| 763 |
} |
| 764 |
|
| 765 |
addr = ProtoAddress("132.250.5.100"); |
| 766 |
char* theName = new char[20]; |
| 767 |
addr.ResolveToName(theName,20); |
| 768 |
if (strcmp(theName,"www.nrl.navy.mil")) |
| 769 |
{ |
| 770 |
result = false; |
| 771 |
PLOG(PL_ERROR,"FAIL: %s ResolveToName %s\n",test,addr.GetHostString()); |
| 772 |
} |
| 773 |
|
| 774 |
addr.Reset(ProtoAddress::IPv4,0); |
| 775 |
addr.ResolveFromString("www.nrl.navy.mil"); |
| 776 |
if (strcmp(addr.GetHostString(),"132.250.5.100")) |
| 777 |
{ |
| 778 |
result = false; |
| 779 |
PLOG(PL_ERROR,"FAIL: %s ResolveFromString %s\n",test,addr.GetHostString()); |
| 780 |
} |
| 781 |
|
| 782 |
addr.Reset(ProtoAddress::IPv4,0); |
| 783 |
addr.ConvertFromString("192.168.1.6"); |
| 784 |
if (strcmp(addr.GetHostString(),"192.168.1.6")) |
| 785 |
{ |
| 786 |
result = false; |
| 787 |
PLOG(PL_ERROR,"FAIL: %s ConvertFromString %s\n",test,addr.GetHostString()); |
| 788 |
} |
| 789 |
addr.Reset(ProtoAddress::IPv6,0); |
| 790 |
addr.ConvertFromString("2001:db8:85a3:0000:0000:8a2e:370:7334"); |
| 791 |
if (strcmp(addr.GetHostString(),"2001:db8:85a3::8a2e:370:7334")) |
| 792 |
{ |
| 793 |
result = false; |
| 794 |
PLOG(PL_ERROR,"FAIL: %s ConvertFromString %s\n",test,addr.GetHostString()); |
| 795 |
} |
| 796 |
addr.Reset(ProtoAddress::IPv4,0); |
| 797 |
addr.ResolveEthFromString("18:36:F3:98:4F:9A"); |
| 798 |
if (strcasecmp(addr.GetHostString(),"18:36:F3:98:4F:9A")) |
| 799 |
{ |
| 800 |
result = false; |
| 801 |
PLOG(PL_ERROR,"FAIL: %s ResolveEthFromString %s\n",test,addr.GetHostString()); |
| 802 |
} |
| 803 |
|
| 804 |
addr.Reset(ProtoAddress::IPv4,0); |
| 805 |
addr = ProtoAddress("192.168.1.12"); |
| 806 |
if (addr.GetEndIdentifier() != 3232235788) |
| 807 |
{ |
| 808 |
result = false; |
| 809 |
PLOG(PL_ERROR,"FAIL: %s GetEndIdentifier() %lu %s\n",test,addr.GetEndIdentifier(),addr.GetHostString()); |
| 810 |
} |
| 811 |
addr.Reset(ProtoAddress::IPv4,0); |
| 812 |
addr.SetEndIdentifier(3232235788); |
| 813 |
if ((addr.GetEndIdentifier() == 3232235788) && (strcmp(addr.GetHostString(),"192.168.1.12"))) |
| 814 |
{ |
| 815 |
result = false; |
| 816 |
PLOG(PL_ERROR,"FAIL: %s SetEndIdentifier() %lu %s\n",test,addr.GetEndIdentifier(),addr.GetHostString()); |
| 817 |
|
| 818 |
} |
| 819 |
|
| 820 |
addr.Reset(ProtoAddress::IPv4,0); |
| 821 |
addr = ProtoAddress("192.168.1.12"); |
| 822 |
if ((addr.GetEndIdentifier() == 3232235788) && (strcmp(addr.GetHostString(),"192.168.1.12"))) |
| 823 |
{ |
| 824 |
result = false; |
| 825 |
PLOG(PL_ERROR,"FAIL: %s IPv4GetAddress() %lu %s\n",test,addr.IPv4GetAddress(),addr.GetHostString()); |
| 826 |
} |
| 827 |
|
| 828 |
addr.Reset(ProtoAddress::IPv4,0); |
| 829 |
addr = ProtoAddress("192.168.1.12"); |
| 830 |
const char* rawAddress = addr.GetRawHostAddress(); |
| 831 |
cmpAddr.Reset(ProtoAddress::IPv4,0); |
| 832 |
cmpAddr.SetRawHostAddress(ProtoAddress::IPv4,rawAddress,4); |
| 833 |
if (strcmp(cmpAddr.GetHostString(),"192.168.1.12")) |
| 834 |
{ |
| 835 |
result = false; |
| 836 |
PLOG(PL_ERROR,"FAIL: %s Set/GetRawHostAddress %s %s\n",test,addr.GetRawHostAddress(),addr.GetHostString()); |
| 837 |
} |
| 838 |
|
| 839 |
addr.Invalidate(); |
| 840 |
addr = ProtoAddress("192.168.1.12"); |
| 841 |
const struct sockaddr& theSockAddr= addr.GetSockAddr(); |
| 842 |
|
| 843 |
ProtoAddress tmpAddr; |
| 844 |
tmpAddr.SetSockAddr(theSockAddr); |
| 845 |
if (strcmp(tmpAddr.GetHostString(),"192.168.1.12")) |
| 846 |
{ |
| 847 |
result = false; |
| 848 |
PLOG(PL_ERROR,"FAIL: %s SetSockAddr() %s\n", test, tmpAddr.GetHostString()); |
| 849 |
} |
| 850 |
|
| 851 |
cmpAddr.Invalidate(); |
| 852 |
cmpAddr.SetSockAddr(tmpAddr.AccessSockAddr()); |
| 853 |
if (strcmp(cmpAddr.GetHostString(),"192.168.1.12")) |
| 854 |
{ |
| 855 |
result = false; |
| 856 |
PLOG(PL_ERROR,"FAIL: %s AccessSockAddr() failed to return correct addr %s\n", test, cmpAddr.GetHostString()); |
| 857 |
} |
| 858 |
|
| 859 |
/* |
| 860 |
ProtoAddress maskAddr; |
| 861 |
UINT8 maskLen = 8; |
| 862 |
maskAddr.GeneratePrefixMask(ProtoAddress::IPv4,maskLen); |
| 863 |
PLOG(PL_ERROR,"maskLen>%d prefixLen>%d %s \n",maskLen,addr.GetPrefixLength(),maskAddr.GetHostString()); |
| 864 |
|
| 865 |
maskLen = 16; |
| 866 |
maskAddr.GeneratePrefixMask(ProtoAddress::IPv4,maskLen); |
| 867 |
PLOG(PL_ERROR,"maskLen>%d prefixLen>%d %s \n",maskLen,addr.GetPrefixLength(),maskAddr.GetHostString()); |
| 868 |
|
| 869 |
ProtoAddress mcastAddr; |
| 870 |
addr = ProtoAddress("224.225.1.2"); |
| 871 |
addr.GetEthernetMulticastAddress(mcastAddr); |
| 872 |
PLOG(PL_ERROR,"FAIL: GetEthernetMulticastAddress %s\n",mcastAddr.GetHostString()); |
| 873 |
|
| 874 |
SIMULATED ADDRESSES? |
| 875 |
|
| 876 |
check copyconstructors equality tests work |
| 877 |
|
| 878 |
*/ |
| 879 |
if (result == false) |
| 880 |
{ |
| 881 |
PLOG(PL_ERROR,"FAIL: PrefixTests() module failure \n\n"); |
| 882 |
} |
| 883 |
|
| 884 |
return result; |
| 885 |
} |
| 886 |
|
| 887 |
bool UnitTests::BasicAddrTests(const char* val, int port, ProtoAddress::Type type, bool mcast, bool bcast, bool loop, bool linkLocal, bool siteLocal, bool unspec, bool unicast) |
| 888 |
{ |
| 889 |
const char* test = "BasicAddrTests()"; |
| 890 |
ProtoAddress addr; |
| 891 |
char nameBuffer[256]; |
| 892 |
nameBuffer[255] = '\0'; |
| 893 |
char cmpNameBuffer[256]; |
| 894 |
cmpNameBuffer[255] = '\0'; |
| 895 |
|
| 896 |
bool result = true; |
| 897 |
|
| 898 |
if (type != ProtoAddress::ETH) |
| 899 |
{ |
| 900 |
if (!addr.ResolveFromString(val)) |
| 901 |
{ |
| 902 |
PLOG(PL_ERROR,"FAIL: %s ResolveFromString(%s)\n",test,val); |
| 903 |
result = false; |
| 904 |
} |
| 905 |
} |
| 906 |
else |
| 907 |
{ |
| 908 |
// Resolve from string doesn't work for eth? |
| 909 |
addr = ProtoAddress(val); |
| 910 |
} |
| 911 |
|
| 912 |
if (addr.GetType() != type) |
| 913 |
{ |
| 914 |
PLOG(PL_ERROR,"FAIL: %s %d type != %d \n", test, type, addr.GetType()); |
| 915 |
result = false; |
| 916 |
} |
| 917 |
addr.SetPort(port); |
| 918 |
|
| 919 |
if (addr.GetPort() != port) |
| 920 |
{ |
| 921 |
PLOG(PL_ERROR,"FAIL: %s port %d != addr.GetPort(%d)\n",test, port, addr.GetPort()); |
| 922 |
result = false; |
| 923 |
} |
| 924 |
|
| 925 |
// Check out some constructors - by reference |
| 926 |
ProtoAddress cmpAddr = ProtoAddress(addr); |
| 927 |
if (strcasecmp(addr.GetHostString(nameBuffer,255),cmpAddr.GetHostString(cmpNameBuffer,255))) |
| 928 |
{ |
| 929 |
PLOG(PL_ERROR,"FAIL: %s ProtoAddress(ProtoAddress& addr) addr %s != cmpAddr %s\n", test, addr.GetHostString(nameBuffer,255),cmpAddr.GetHostString(cmpNameBuffer,255)); |
| 930 |
result = false; |
| 931 |
} |
| 932 |
|
| 933 |
if (!cmpAddr.IsValid()) |
| 934 |
{ |
| 935 |
PLOG(PL_ERROR,"FAIL: %s ProtoAddress(addr) %s is not valid\n",test, cmpAddr.GetHostString()); |
| 936 |
result = false; |
| 937 |
} |
| 938 |
// by string |
| 939 |
cmpAddr = ProtoAddress(val); |
| 940 |
if (strcasecmp(addr.GetHostString(nameBuffer,255),cmpAddr.GetHostString(cmpNameBuffer,255))) |
| 941 |
{ |
| 942 |
PLOG(PL_ERROR,"FAIL: %s ProtoAddress(char* addr) addr %s != cmpAddr %s)\n",test, val,cmpAddr.GetHostString()); |
| 943 |
result = false; |
| 944 |
} |
| 945 |
if (!cmpAddr.IsValid()) |
| 946 |
{ |
| 947 |
PLOG(PL_ERROR,"FAIL: %s ProtoAddress(*addr) constructor %s is not valid\n",test, val); |
| 948 |
result = false; |
| 949 |
} |
| 950 |
|
| 951 |
// Test our length/type settings from resolveFromString? |
| 952 |
switch (type) |
| 953 |
{ |
| 954 |
case ProtoAddress::IPv4: |
| 955 |
if (addr.GetLength(type) != 4) |
| 956 |
{ |
| 957 |
PLOG(PL_ERROR,"FAIL: %s addr.getLength(IPV4) ! valid for type %d \n",test, addr.GetLength()); |
| 958 |
result = false; |
| 959 |
} |
| 960 |
break; |
| 961 |
case ProtoAddress::IPv6: |
| 962 |
if (addr.GetLength(type) != 16) |
| 963 |
{ |
| 964 |
PLOG(PL_ERROR,"FAIL: %s addr.getLength(IPV6) ! valid for type %d \n", test, addr.GetLength()); |
| 965 |
result = false; |
| 966 |
} |
| 967 |
break; |
| 968 |
case ProtoAddress::ETH: |
| 969 |
if (addr.GetLength(type) != 6) |
| 970 |
{ |
| 971 |
PLOG(PL_ERROR,"FAIL: %s addr.getLength(ETH) ! valid for type %d \n",test, addr.GetLength()); |
| 972 |
result = false; |
| 973 |
} |
| 974 |
// case SIM not implemented |
| 975 |
break; |
| 976 |
default: |
| 977 |
PLOG(PL_ERROR,"FAIL: %s no valid type available val: %s\n",test, val); |
| 978 |
result = false; |
| 979 |
} |
| 980 |
switch (type) |
| 981 |
{ |
| 982 |
case ProtoAddress::IPv4: |
| 983 |
if (addr.GetLength() != 4) |
| 984 |
{ |
| 985 |
PLOG(PL_ERROR,"FAIL: %s addr.getLength() ! valid for type IPV4 %d \n",test, addr.GetLength()); |
| 986 |
result = false; |
| 987 |
} |
| 988 |
break; |
| 989 |
case ProtoAddress::IPv6: |
| 990 |
if (addr.GetLength() != 16) |
| 991 |
{ |
| 992 |
PLOG(PL_ERROR,"FAIL: %s addr.getLength() ! valid for type IPV6 %d \n", test, addr.GetLength()); |
| 993 |
result = false; |
| 994 |
} |
| 995 |
break; |
| 996 |
case ProtoAddress::ETH: |
| 997 |
if (addr.GetLength() != 6) |
| 998 |
{ |
| 999 |
PLOG(PL_ERROR,"FAIL: %s addr.getLength() ! valid for type ETH %d \n",test, addr.GetLength()); |
| 1000 |
result = false; |
| 1001 |
} |
| 1002 |
break; |
| 1003 |
// case SIM not implemented |
| 1004 |
default: |
| 1005 |
PLOG(PL_ERROR,"FAIL: %s no valid type available val: %s\n",test, val); |
| 1006 |
result = false; |
| 1007 |
} |
| 1008 |
switch (type) |
| 1009 |
{ |
| 1010 |
case ProtoAddress::IPv4: |
| 1011 |
if (addr.GetType(addr.GetLength()) != ProtoAddress::IPv4) |
| 1012 |
{ |
| 1013 |
PLOG(PL_ERROR,"FAIL: %s addr.GetType(addr.getLength()) ! valid for type IPV4 %d \n",test, addr.GetLength()); |
| 1014 |
result = false; |
| 1015 |
} |
| 1016 |
break; |
| 1017 |
case ProtoAddress::IPv6: |
| 1018 |
if (addr.GetType(addr.GetLength()) != ProtoAddress::IPv6) |
| 1019 |
{ |
| 1020 |
PLOG(PL_ERROR,"FAIL: %s addr.GetType(addr.getLength()) ! valid for type IPV6 %d \n",test, addr.GetLength()); |
| 1021 |
result = false; |
| 1022 |
} |
| 1023 |
break; |
| 1024 |
case ProtoAddress::ETH: |
| 1025 |
if (addr.GetType(addr.GetLength()) != ProtoAddress::ETH) |
| 1026 |
{ |
| 1027 |
PLOG(PL_ERROR,"FAIL: %s addr.GetType(addr.getLength()) ! valid for type ETH %d \n",test, addr.GetLength()); |
| 1028 |
result = false; |
| 1029 |
} |
| 1030 |
// case SIM not implemented |
| 1031 |
break; |
| 1032 |
default: |
| 1033 |
PLOG(PL_ERROR,"FAIL: %s no valid type available val: %s\n",test, val); |
| 1034 |
result = false; |
| 1035 |
} |
| 1036 |
if (port != addr.GetPort()) |
| 1037 |
{ |
| 1038 |
PLOG(PL_ERROR,"FAIL: %s port %s != getPort(%d)\n",test, port, addr.GetPort()); |
| 1039 |
result = false; |
| 1040 |
} |
| 1041 |
|
| 1042 |
if (addr.IsMulticast() != mcast) |
| 1043 |
{ |
| 1044 |
PLOG(PL_ERROR,"FAIL: %s %s failed mcast test\n",test, val); |
| 1045 |
result = false; |
| 1046 |
} |
| 1047 |
if (addr.IsBroadcast() != bcast) |
| 1048 |
{ |
| 1049 |
PLOG(PL_ERROR,"FAIL: %s %s failed broadcast test\n",test, val); |
| 1050 |
result = false; |
| 1051 |
} |
| 1052 |
|
| 1053 |
if (addr.IsLoopback() != loop) |
| 1054 |
{ |
| 1055 |
PLOG(PL_ERROR,"FAIL: %s %s failed loopback test\n",test, val); |
| 1056 |
result = false; |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (addr.IsLinkLocal() != linkLocal) |
| 1060 |
{ |
| 1061 |
PLOG(PL_ERROR,"FAIL: %s %s failed linkLocal test\n",test, val); |
| 1062 |
result = false; |
| 1063 |
} |
| 1064 |
|
| 1065 |
if (addr.IsSiteLocal() != siteLocal) |
| 1066 |
{ |
| 1067 |
PLOG(PL_ERROR,"FAIL: %s %s failed siteLocal test\n",test, val); |
| 1068 |
result = false; |
| 1069 |
} |
| 1070 |
|
| 1071 |
if (addr.IsUnspecified() != unspec) |
| 1072 |
{ |
| 1073 |
PLOG(PL_ERROR,"FAIL: %s %s failed unspec test\n",test, val); |
| 1074 |
result = false; |
| 1075 |
} |
| 1076 |
if (addr.IsUnicast() != unicast) |
| 1077 |
{ |
| 1078 |
PLOG(PL_ERROR,"FAIL: %s %s failed unicast test addr %d input %d\n",test, val,addr.IsUnicast(),unicast); |
| 1079 |
result = false; |
| 1080 |
} |
| 1081 |
|
| 1082 |
addr_list.Insert(addr); |
| 1083 |
addr_listSize++; |
| 1084 |
|
| 1085 |
const char* addrtype; |
| 1086 |
|
| 1087 |
if (result == false) |
| 1088 |
{ |
| 1089 |
// Isn't there already a function for this? |
| 1090 |
switch (type) |
| 1091 |
{ |
| 1092 |
case ProtoAddress::IPv4: |
| 1093 |
addrtype = "IPv4"; |
| 1094 |
break; |
| 1095 |
case ProtoAddress::IPv6: |
| 1096 |
addrtype = "IPv6"; |
| 1097 |
break; |
| 1098 |
case ProtoAddress::ETH: |
| 1099 |
addrtype = "ETH"; |
| 1100 |
break; |
| 1101 |
case ProtoAddress::SIM: |
| 1102 |
addrtype = "SIM"; |
| 1103 |
break; |
| 1104 |
default: |
| 1105 |
addrtype = "No address type"; |
| 1106 |
|
| 1107 |
} |
| 1108 |
} |
| 1109 |
if (result == false) |
| 1110 |
{ |
| 1111 |
PLOG(PL_ERROR,"FAIL: BasicAddTests() addr>%s port>%d type>%s mcast>%d bcast>%d loopback>%d linkLocal>%d siteLocal>%d unspecified>%d unicast>%d module failure \n\n",val,port,addrtype,mcast,bcast,loop,linkLocal, siteLocal,unspec,unicast); |
| 1112 |
} |
| 1113 |
|
| 1114 |
return result; |
| 1115 |
} // BasicAddresTests |
| 1116 |
int UnitTests::PrintList(ProtoAddressList& theList) |
| 1117 |
{ |
| 1118 |
ProtoAddressList::Iterator iterator(theList); |
| 1119 |
ProtoAddress iterAddr; |
| 1120 |
int listSize = 0; |
| 1121 |
while (iterator.GetNextAddress(iterAddr)) |
| 1122 |
{ |
| 1123 |
PLOG(PL_INFO,"NextAddr %s\n",iterAddr.GetHostString()); |
| 1124 |
listSize++; |
| 1125 |
} |
| 1126 |
|
| 1127 |
return listSize; |
| 1128 |
} |
| 1129 |
bool UnitTests::AddrListTests() |
| 1130 |
{ |
| 1131 |
const char* test = "AddrListTests()"; |
| 1132 |
bool result = true; |
| 1133 |
if (addr_list.IsEmpty()) |
| 1134 |
{ |
| 1135 |
result = false; |
| 1136 |
PLOG(PL_ERROR,"FAIL: %s addr_list.IsEmpty() \n",test); |
| 1137 |
} |
| 1138 |
ProtoAddress firstAddr; |
| 1139 |
if (!addr_list.GetFirstAddress(firstAddr)) |
| 1140 |
{ |
| 1141 |
result = false; |
| 1142 |
PLOG(PL_ERROR,"FAIL: %s addr_list.GetFirstAddress()\n",test); |
| 1143 |
} |
| 1144 |
PLOG(PL_INFO,"FirstAddr %s\n",firstAddr.GetHostString()); |
| 1145 |
|
| 1146 |
int listSize = PrintList(addr_list); |
| 1147 |
// Does list size equal what we originally added? |
| 1148 |
if (addr_listSize != listSize) |
| 1149 |
{ |
| 1150 |
result = false; |
| 1151 |
PLOG(PL_ERROR,"FAIL: %s addr_listSize %d != listSize %d\n",test,addr_listSize, listSize); |
| 1152 |
} |
| 1153 |
PLOG(PL_INFO,"FirstAddr %s\n",firstAddr.GetHostString()); |
| 1154 |
|
| 1155 |
// Does address list contain first address? |
| 1156 |
if (!addr_list.Contains(firstAddr)) |
| 1157 |
{ |
| 1158 |
result = false; |
| 1159 |
PLOG(PL_ERROR,"FAIL: %s addr_list.contains(%s) (firstAddr) \n",test,firstAddr.GetHostString()); |
| 1160 |
} |
| 1161 |
ProtoAddress fakeAddr; |
| 1162 |
fakeAddr.ResolveFromString("192.168.1.1"); |
| 1163 |
if (addr_list.Contains(fakeAddr)) |
| 1164 |
{ |
| 1165 |
result = false; |
| 1166 |
PLOG(PL_ERROR,"FAIL: %s addr_list.contains(%s) (fakeAddr) \n",test,fakeAddr.GetHostString()); |
| 1167 |
} |
| 1168 |
// Insert fakeAddr |
| 1169 |
if (!addr_list.Insert(fakeAddr)) |
| 1170 |
{ |
| 1171 |
result = false; |
| 1172 |
PLOG(PL_ERROR,"FAIL: %s addr_list.insert(fakeAddr) \n",test); |
| 1173 |
} |
| 1174 |
// Remove fakeAddr - is it gone? |
| 1175 |
addr_list.Remove(fakeAddr); |
| 1176 |
if (addr_list.Contains(fakeAddr)) |
| 1177 |
{ |
| 1178 |
result = false; |
| 1179 |
PLOG(PL_ERROR,"FAIL: %s addr_list.contains(%s) (removed fakeAddr) \n",test,fakeAddr.GetHostString()); |
| 1180 |
} |
| 1181 |
// Create a second list to add to the first |
| 1182 |
ProtoAddressList new_addr_list; |
| 1183 |
new_addr_list.Insert(fakeAddr); |
| 1184 |
fakeAddr.ResolveFromString("192.168.1.2"); |
| 1185 |
new_addr_list.Insert(fakeAddr); |
| 1186 |
fakeAddr.ResolveFromString("192.168.1.3"); |
| 1187 |
new_addr_list.Insert(fakeAddr); |
| 1188 |
fakeAddr.ResolveFromString("192.168.1.4"); |
| 1189 |
new_addr_list.Insert(fakeAddr); |
| 1190 |
|
| 1191 |
|
| 1192 |
int new_listSize = PrintList(new_addr_list); |
| 1193 |
|
| 1194 |
// add new list to original list |
| 1195 |
addr_list.AddList(new_addr_list); |
| 1196 |
|
| 1197 |
int combined_listSize = PrintList(addr_list); |
| 1198 |
|
| 1199 |
PLOG(PL_INFO,"Combined list size %d new list size %d\n",combined_listSize,new_listSize); |
| 1200 |
if (combined_listSize != addr_listSize + new_listSize) |
| 1201 |
{ |
| 1202 |
result = false; |
| 1203 |
PLOG(PL_ERROR,"FAIL: %s combined list (%d) != (addr_list(%d) + new_list(%d))\n", |
| 1204 |
test,combined_listSize,addr_listSize, new_listSize); |
| 1205 |
} |
| 1206 |
// Now test that adding a list only adds new items |
| 1207 |
|
| 1208 |
// Add a new element to the new_addrList |
| 1209 |
fakeAddr.ResolveFromString("192.168.1.5"); |
| 1210 |
new_addr_list.Insert(fakeAddr); |
| 1211 |
|
| 1212 |
// Add new list a second time - only 192.168.1.5 should be added |
| 1213 |
addr_list.AddList(new_addr_list); |
| 1214 |
combined_listSize = PrintList(addr_list); |
| 1215 |
|
| 1216 |
PLOG(PL_INFO,"Combined list size %d new list size %d addr_list %d\n",combined_listSize,new_listSize, addr_listSize); |
| 1217 |
|
| 1218 |
if (combined_listSize != addr_listSize + new_listSize + 1) |
| 1219 |
{ |
| 1220 |
result = false; |
| 1221 |
PLOG(PL_ERROR,"FAIL: %s combined list (%d) != (addr_list(%d) + new_list(%d) + 1)\n", |
| 1222 |
test,combined_listSize,addr_listSize, new_listSize); |
| 1223 |
} |
| 1224 |
|
| 1225 |
// Now remove the new_addr_list |
| 1226 |
addr_list.RemoveList(new_addr_list); |
| 1227 |
combined_listSize = PrintList(addr_list); |
| 1228 |
PLOG(PL_INFO,"Combined list size after removal %d \n",combined_listSize); |
| 1229 |
|
| 1230 |
// New list size should equal original list size |
| 1231 |
if (combined_listSize != addr_listSize) |
| 1232 |
{ |
| 1233 |
result = false; |
| 1234 |
PLOG(PL_ERROR,"FAIL: %s combined list (%d) != (addr_list(%d) + new_list(%d) + 1) \n", |
| 1235 |
test,combined_listSize,addr_listSize, new_listSize); |
| 1236 |
} |
| 1237 |
fakeAddr.ResolveFromString("192.168.1.6"); |
| 1238 |
addr_list.Insert(fakeAddr,"eth0"); |
| 1239 |
|
| 1240 |
char* text = (char*)(addr_list.GetUserData(fakeAddr)); |
| 1241 |
if (strcasecmp(text,"eth0")) |
| 1242 |
{ |
| 1243 |
PLOG(PL_ERROR,"FAIL: %s %s addr_list.GetUserData() %s != eth0\n",test,text); |
| 1244 |
result = false; |
| 1245 |
} |
| 1246 |
if (result == false) |
| 1247 |
{ |
| 1248 |
PLOG(PL_ERROR,"FAIL: AddrListTest() module failiure \n\n"); |
| 1249 |
} |
| 1250 |
|
| 1251 |
return result; |
| 1252 |
} |
| 1253 |
|
| 1254 |
bool UnitTests::AddrTests() |
| 1255 |
{ |
| 1256 |
bool result = true; |
| 1257 |
char nameBuffer[256]; |
| 1258 |
nameBuffer[255] = '\0'; |
| 1259 |
|
| 1260 |
const char* test = "AddrTests()"; |
| 1261 |
PLOG(PL_ERROR,"INFO: %s Need to add GeneratePrefixMask() test\n",test); |
| 1262 |
PLOG(PL_ERROR,"INFO: %s Need to add GetEthernetMulciastAddress() test\n",test); |
| 1263 |
PLOG(PL_ERROR,"INFO: %s Need to test SIM addresses \n",test); |
| 1264 |
const char* eth = "18:36:f3:98:4F:9A"; |
| 1265 |
ProtoAddress addr; |
| 1266 |
|
| 1267 |
if (!addr.ResolveFromString(eth)) |
| 1268 |
{ |
| 1269 |
PLOG(PL_ERROR,"INFO: %s ResolveFromString(%s) not working for ETH addresses. Should it?\n",test,eth); |
| 1270 |
} |
| 1271 |
addr.Invalidate(); |
| 1272 |
addr.ResolveLocalAddress(nameBuffer,20); |
| 1273 |
{ |
| 1274 |
// TODO: automate |
| 1275 |
// Planned failure |
| 1276 |
PLOG(PL_ERROR,"INFO: ** User Action ** Verify that local host name and address are correct\n"); |
| 1277 |
PLOG(PL_ERROR," ResolveLocalAddress %s name %s\n\n",addr.GetHostString(), nameBuffer); |
| 1278 |
} |
| 1279 |
|
| 1280 |
// Look up public nrl address & test get ResolveToName |
| 1281 |
addr.Invalidate(); |
| 1282 |
const char* val = "132.250.5.100"; |
| 1283 |
|
| 1284 |
if (!addr.ResolveFromString(val)) |
| 1285 |
{ |
| 1286 |
PLOG(PL_ERROR,"FAIL: AddrTests() ResolveFromString val %s hostName: %s %s\n", val, nameBuffer,addr.GetHostString()); |
| 1287 |
result = false; |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (!addr.ResolveToName(nameBuffer,255)) |
| 1291 |
{ |
| 1292 |
PLOG(PL_ERROR,"FAIL: AddrTests() ResolveToName val %s hostName: %s %s\n", val, nameBuffer,addr.GetHostString()); |
| 1293 |
result = false; |
| 1294 |
} |
| 1295 |
// ipv6 node address |
| 1296 |
val = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; |
| 1297 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv6,false,false,false,false,false,false,true)) |
| 1298 |
{ |
| 1299 |
result = false; |
| 1300 |
} |
| 1301 |
|
| 1302 |
// ipv4 node address |
| 1303 |
val = "192.168.10.1"; |
| 1304 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv4,false,false,false,false,false,false,true)) |
| 1305 |
{ |
| 1306 |
result = false; |
| 1307 |
} |
| 1308 |
|
| 1309 |
// ipv6 mcast |
| 1310 |
val = "FF01:0:0:0:0:0:0:1"; |
| 1311 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv6,true,false,false,false,false,false,false)) |
| 1312 |
{ |
| 1313 |
result = false; |
| 1314 |
} |
| 1315 |
|
| 1316 |
// ipv4 mcast |
| 1317 |
val = "224.225.1.2"; |
| 1318 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv4,true,false,false,false,false,false,false)) |
| 1319 |
{ |
| 1320 |
result = false; |
| 1321 |
} |
| 1322 |
|
| 1323 |
// NO ipv6 broadcast address |
| 1324 |
|
| 1325 |
// ipv4 broadcast |
| 1326 |
val = "255.255.255.255"; |
| 1327 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv4,false,true,false,false,false,false,false)) |
| 1328 |
{ |
| 1329 |
result = false; |
| 1330 |
} |
| 1331 |
|
| 1332 |
// ipv6 loopback |
| 1333 |
val = "::1"; |
| 1334 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv6,false,false,true,false,false,false,true)) |
| 1335 |
{ |
| 1336 |
result = false; |
| 1337 |
} |
| 1338 |
|
| 1339 |
// ipv4 loopback |
| 1340 |
val = "127.0.0.1"; |
| 1341 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv4,false,false,true,false,false,false,true)) |
| 1342 |
{ |
| 1343 |
result = false; |
| 1344 |
} |
| 1345 |
|
| 1346 |
// ipv6 link local (prefix 1111111010) |
| 1347 |
val = "fe80::426c:8fff:fe31:4f8e"; |
| 1348 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv6,false,false,false,true,false,false,true)) |
| 1349 |
{ |
| 1350 |
result = false; |
| 1351 |
} |
| 1352 |
|
| 1353 |
// ipv4 link local |
| 1354 |
val = "169.254.0.1"; |
| 1355 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv4,false,false,false,true,false,false,true)) |
| 1356 |
{ |
| 1357 |
result = false; |
| 1358 |
} |
| 1359 |
|
| 1360 |
// ipv6 site local (first 10 bits are 1111111011) |
| 1361 |
val = "fec0::426c:8fff:fe31:4f8e"; |
| 1362 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv6,false,false,false,false,true,false,true)) |
| 1363 |
{ |
| 1364 |
result = false; |
| 1365 |
} |
| 1366 |
|
| 1367 |
// ipv4 site local (no IPv4 site local) |
| 1368 |
val = "10.0.0.1"; |
| 1369 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv4,false,false,false,false,false,false,true)) |
| 1370 |
{ |
| 1371 |
result = false; |
| 1372 |
} |
| 1373 |
|
| 1374 |
// ipv6 unspecified |
| 1375 |
val = "00::"; |
| 1376 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv6,false,false,false,false,false,true,false)) |
| 1377 |
{ |
| 1378 |
result = false; |
| 1379 |
} |
| 1380 |
|
| 1381 |
// ipv4 unspecified |
| 1382 |
val = "0.0.0.0"; |
| 1383 |
if (!BasicAddrTests(val,5000,ProtoAddress::IPv4,false,false,false,false,false,true,false)) |
| 1384 |
{ |
| 1385 |
result = false; |
| 1386 |
} |
| 1387 |
|
| 1388 |
// eth addr |
| 1389 |
val = "18:36:F3:98:4F:9A"; |
| 1390 |
if (!BasicAddrTests(val,0,ProtoAddress::ETH,false,false,false,false,false,false,true)) |
| 1391 |
{ |
| 1392 |
result = false; |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (!AddrListTests()) |
| 1396 |
{ |
| 1397 |
result = false; |
| 1398 |
} |
| 1399 |
|
| 1400 |
if (!EqualityTests()) |
| 1401 |
{ |
| 1402 |
result = false; |
| 1403 |
} |
| 1404 |
|
| 1405 |
if (!PrefixTests()) |
| 1406 |
{ |
| 1407 |
result = false; |
| 1408 |
} |
| 1409 |
|
| 1410 |
return result; |
| 1411 |
} |
| 1412 |
|
| 1413 |
bool UnitTests::ManetMsgTests() |
| 1414 |
{ |
| 1415 |
bool result = true; |
| 1416 |
const char* test = "ManetMsgTests()"; |
| 1417 |
|
| 1418 |
UINT32 buffer[PACKET_SIZE_MAX/4]; |
| 1419 |
ManetPkt pkt; |
| 1420 |
if (!BuildPacket((UINT32*)buffer,pkt)) |
| 1421 |
{ |
| 1422 |
result = false; |
| 1423 |
PLOG(PL_ERROR,"FAIL: %s BuildPacket() failed \n",test); |
| 1424 |
} |
| 1425 |
|
| 1426 |
// OK, let's parse a "received" packet using sent "buffer" and "pktLen" |
| 1427 |
PLOG(PL_INFO,"msgTests: Parsing \"recvPkt\" ...\n"); |
| 1428 |
UINT16 pktLen = pkt.GetLength(); |
| 1429 |
|
| 1430 |
if (GetDebugLevel() == PL_INFO) |
| 1431 |
MakeDump((char*)buffer, pktLen); |
| 1432 |
|
| 1433 |
if (ParseBuffer(buffer, pktLen) != 0) |
| 1434 |
{ |
| 1435 |
result = false; |
| 1436 |
PLOG(PL_ERROR,"FAIL: %s ParseBuffer() failed \n",test); |
| 1437 |
} |
| 1438 |
|
| 1439 |
PLOG(PL_INFO,"msgTests: \"recvPkt\" packet parsing completed.\n\n"); |
| 1440 |
|
| 1441 |
PLOG(PL_INFO,"Generated buffer:\n"); |
| 1442 |
if (GetDebugLevel() == PL_INFO) |
| 1443 |
MakeDump((char*)buffer, pktLen); |
| 1444 |
|
| 1445 |
PLOG(PL_INFO,"msgTests: Read in reference packet dump \n"); |
| 1446 |
|
| 1447 |
UINT32 refBuffer[1024]; |
| 1448 |
unsigned int refLen = 0; |
| 1449 |
|
| 1450 |
char data[] = "04 00 02 06 00 01 83 00 45 c0 a8 01 01 00 09 01 10 01 01 02 10 02 00 02 08 c0 02 c0 a8 01 01 02 03 04 05 06 07 08 09 00 15 04 14 08 02 03 02 03 02 03 02 03 03 30 02 05 01 02 05 10 01 01 04 80 03 c0 a8 02 02 03 04 05 00 00 "; |
| 1451 |
char* referenceDump = data; |
| 1452 |
|
| 1453 |
unsigned int val; |
| 1454 |
int offset; |
| 1455 |
while (1 == sscanf(referenceDump,"%x%n",&val,&offset)) |
| 1456 |
{ |
| 1457 |
((char*)refBuffer)[refLen++] = (char)val; |
| 1458 |
referenceDump += offset; |
| 1459 |
} |
| 1460 |
|
| 1461 |
if (refLen == 0) |
| 1462 |
{ |
| 1463 |
PLOG(PL_ERROR,"FAIL: %s ParseBuffer(%s) failed \n",test,data); |
| 1464 |
result = false; |
| 1465 |
} |
| 1466 |
|
| 1467 |
PLOG(PL_INFO,"Reference buffer:\n"); |
| 1468 |
if (GetDebugLevel() == PL_INFO) |
| 1469 |
MakeDump((char*)refBuffer,refLen); |
| 1470 |
|
| 1471 |
// TODO: compare results of referencePktDump w/ours and move common code out of |
| 1472 |
// message example into unittests? |
| 1473 |
|
| 1474 |
// Compare our buffer with reference buffer |
| 1475 |
if (pktLen == refLen) |
| 1476 |
{ |
| 1477 |
if (memcmp((const char*)buffer,(const char*)refBuffer,pktLen) != 0) |
| 1478 |
{ |
| 1479 |
PLOG(PL_ERROR,"FAIL: %s ParseBuffer() buffer != refBuffer\n",test); |
| 1480 |
result = false; |
| 1481 |
} |
| 1482 |
} else |
| 1483 |
{ |
| 1484 |
PLOG(PL_ERROR,"FAIL: %s Parsed msg buffer != reference buffer\n",test); |
| 1485 |
result = false; |
| 1486 |
} |
| 1487 |
|
| 1488 |
return result; |
| 1489 |
|
| 1490 |
} |
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|