00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: test.cpp 1243 2014-11-21 01:01:24Z sella $";
00010
00011 #include "test.h"
00012
00013 #include <stdio.h>
00014 #include <signal.h>
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <pthread.h>
00018 #include <syscall.h>
00019
00020 #include <iostream>
00021 #include <string>
00022 #include <vector>
00023
00024 #include "alias.h"
00025 #include "version.h"
00026 #include "sella/net/IPAddressPort.h"
00027 #include "sella/net/IPPrefix.h"
00028 #include "sella/net/SocketMux.h"
00029 #include "sella/net/EtherSocket.h"
00030 #include "sella/net/DNS.h"
00031 #include "sella/net/DNSCallback.h"
00032 #include "sella/net/Pipe.h"
00033 #include "sella/net/SocketPair.h"
00034 #include "sella/net/SocketTrigger.h"
00035 #include "sella/util/Log.h"
00036 #include "sella/util/SimpleLogFormat.h"
00037 #include "sella/util/MixedLogFormat.h"
00038 #include "sella/util/VerboseLogFormat.h"
00039 #include "sella/util/Log.h"
00040 #include "sella/util/String.h"
00041 #include "sella/util/Process.h"
00042 #include "sella/util/File.h"
00043 #include "sella/util/LockFile.h"
00044 #include "sella/util/Interface.h"
00045 #include "sella/util/Packet.h"
00046 #include "sella/util/UUID.h"
00047 #include "sella/variant/Variant.h"
00048 #include "sella/util/ThreadPool.h"
00049 #include "sella/util/Time.h"
00050
00051 #include <linux/if_packet.h>
00052 #include <netinet/ip_icmp.h>
00053
00054 using namespace sella::util;
00055 using namespace sella::net;
00056
00057
00058
00059 int main(int argc, char **argv) {
00060 printf("Using %s v%s\n\n", libutilxx__package(), libutilxx__version());
00061
00062 SET_FORMAT(SimpleLogFormat());
00063 SET_IDENTIFIER("test");
00064 SET_OPTION(Log::WithStderrOption);
00065 SET_LOGMASK(LOG_UPTO(LOG_DEBUG));
00066
00067 #if 0
00068 testLog();
00069 testString();
00070 testProcess();
00071 testFile();
00072 testLockFile();
00073 testInterface();
00074 testSocket();
00075 testIPPrefix();
00076 testIPAddress();
00077 testIPAddressPort();
00078 testVariant();
00079 testException();
00080 testDNS();
00081 testThreadPool();
00082 testSocketPair();
00083 testSocketTrigger();
00084 testPipe();
00085 #else
00086 testIPPrefix();
00087 #endif
00088
00089 #if 0
00090 try {
00091 std::string buf = "This is a test ${foo}. Time to make the ${donuts}[bob=1] ${bar} and ${front}[${sub}[jack=1]]. Yawn.";
00092
00093 std::vector<std::string> v;
00094 su::String::regex_search(buf, "(\\$\\{[^{}]+\\}(?:\\[[^\\]]*\\]+)?)", v, true, false);
00095
00096 for (auto it = v.begin(); it != v.end(); ++it) {
00097 INFO("MATCH: %s", it->c_str());
00098 }
00099 } catch (sella::Exception &e) {
00100 WARNING("Got: %s", e.what());
00101 }
00102 #endif
00103
00104 return EXIT_SUCCESS;
00105 }
00106
00107 void testLog(void) {
00108 printf("--------------------\n");
00109 printf("- sella/util/Log.h -\n");
00110 printf("--------------------\n");
00111
00112 DLOG("Debug message which only shows with ./configure --enable-debug");
00113
00114 sella::Exception e(false, __func__, __FILE__, __LINE__, 100, "exception message");
00115 SET_TRACE("foo");
00116
00117 {
00118 printf("Using SimpleLogFormat:\n");
00119 SET_FORMAT(SimpleLogFormat());
00120 SET_IDENTIFIER("test");
00121 SET_OPTION(Log::WithStderrOption);
00122
00123 INFO("Informational message");
00124 WARNING("Warning message");
00125 ERROR("Error message");
00126 EXCEPTION(e, NULL);
00127
00128 SET_TRACE("test-trace");
00129 TRACE("test-trace", "#1: Display when 'test-trace' is set") || INFO("#1: Display when 'test-trace' is not set");
00130 UNSET_TRACE("test-trace");
00131 TRACE("test-trace", "#2: Display when 'test-trace' is set") || INFO("#2: Display when 'test-trace' is not set");
00132
00133 if (IS_TRACE("test-trace")) {
00134 printf("Perform action only when 'test-trace' is set\n");
00135 }
00136 }
00137
00138 {
00139 printf("Using MixedLogFormat:\n");
00140 SET_FORMAT(MixedLogFormat());
00141
00142 INFO("Informational message");
00143 WARNING("Warning message");
00144 ERROR("Error message");
00145 TRACE("foo", "Trace message");
00146 EXCEPTION(e, NULL);
00147 }
00148
00149 {
00150 printf("Using VerboseLogFormat:\n");
00151 SET_FORMAT(VerboseLogFormat());
00152
00153 INFO("Informational message");
00154 WARNING("Warning message");
00155 ERROR("Error message");
00156 TRACE("foo", "Trace message");
00157 EXCEPTION(e, NULL);
00158 }
00159
00160 SET_FORMAT(SimpleLogFormat());
00161
00162 printf("\n");
00163 }
00164
00165 void testString(void) {
00166 using namespace sella::util;
00167
00168 boost::cmatch m;
00169 std::string in, match;
00170 std::vector<std::string> matches;
00171
00172 printf("-----------------------\n");
00173 printf("- sella/util/String.h -\n");
00174 printf("-----------------------\n");
00175
00176 printf("String::sprintf(): %s\n", String::sprintf("Dulce et %s est pro %s mori", "decorum", "patria").c_str());
00177
00178 in = "Dulce Et Decorum Est Pro Patria Mori";
00179 printf("\nString::substr_replace(): %s\n", String::substr_replace((const std::string) in, "e", "X", false).c_str());
00180 printf("String::substr_ireplace(): %s\n", String::substr_ireplace((const std::string) in, "MORI", "VITA").c_str());
00181
00182 String::substr_ireplace(in, "D", "XXX");
00183 printf("String::substr_ireplace(): %s\n", in.c_str());
00184 String::substr_ireplace(in, "XXX", "D");
00185
00186 printf("\nString::regex_replace(): %s\n", String::regex_replace((const std::string) in, "e", "X", String::DefaultREFlags, boost::regex_constants::format_first_only).c_str());
00187 printf("String::regex_ireplace(): %s\n", String::regex_ireplace((const std::string) in, "(M.RI)", "(was: $1, now: VITA)").c_str());
00188
00189 in = "Dulce Et Decorum Est Pro Patria Mori";
00190 printf("\nString::regex_match(): %s\n", (String::regex_match(in, ".*Dulce.*", m)) ? "MATCH" : "no match");
00191 printf("String::regex_imatch(): %s\n", (String::regex_imatch(in, ".*DULCE.*")) ? "MATCH" : "no match");
00192
00193 printf("\nString::regex_search(): %s\n", (String::regex_search(in, "D.lce", m)) ? "MATCH" : "no match");
00194 printf("String::regex_isearch(): %s\n", (String::regex_isearch(in, "D.LCE")) ? "MATCH" : "no match");
00195
00196 in = "Dulce Et Decorum Est Pro Patria Mori";
00197 if (String::regex_match(in, ".*Decorum.*Patria.*", match)) {
00198 printf("\nString::regex_match(): %s\n", match.c_str());
00199 } else {
00200 printf("\nString::regex_match(): no match\n");
00201 }
00202
00203 if (String::regex_imatch(in, ".*DECORUM.*PATRIA.*", match)) {
00204 printf("String::regex_imatch(): %s\n", match.c_str());
00205 } else {
00206 printf("String::regex_imatch(): no match\n");
00207 }
00208
00209 if (String::regex_search(in, "([DP][^ ]+|Dul)(ce)?", matches, true, false)) {
00210 printf("\nString::regex_search(): ");
00211
00212 for (auto it = matches.begin(); it != matches.end(); ++it) {
00213 printf("%s, ", it->c_str());
00214 }
00215 matches.clear();
00216
00217 printf("\b\b \n");
00218 } else {
00219 printf("\nString::regex_search(): no match\n");
00220 }
00221
00222 if (String::regex_isearch(in, "([dp][^ ]+|dul)(ce)?", matches, true, false)) {
00223 printf("String::regex_isearch(): ");
00224
00225 for (auto it = matches.begin(); it != matches.end(); ++it) {
00226 printf("%s, ", it->c_str());
00227 }
00228 matches.clear();
00229
00230 printf("\b\b \n");
00231 } else {
00232 printf("String::regex_isearch(): no match\n");
00233 }
00234
00235 try {
00236 String::regex_isearch("foobar", "^bad-regex [a-z]{");
00237 } catch (RegexException &e) {
00238 printf("RegexException: %s\n", e.what());
00239 }
00240
00241 in = " dulce et DECORUM est pro pAtRiA mori ";
00242 printf("\nString::rtrim(): |%s|\n", String::rtrim(in).c_str());
00243 printf("String::ltrim(): |%s|\n", String::ltrim(in).c_str());
00244
00245 printf("String::lower(): %s\n", String::lower(in).c_str());
00246 printf("String::upper(): %s\n", String::upper(in).c_str());
00247 printf("String::ucfirst(): %s\n", String::ucfirst(in).c_str());
00248 printf("String::ucword(): %s\n", String::ucword(in).c_str());
00249
00250 std::vector<std::string> v;
00251 in = "first,second,,forth,";
00252 in = "first::second::::forth::";
00253 v = String::split(in, "::", true, true);
00254 printf("\n");
00255 for (auto it = v.begin(); it != v.end(); ++it) {
00256 printf("String::split(): '%s'\n", it->c_str());
00257 }
00258
00259 in = "jack=jill=olaf";
00260 v = String::split(in, '=', false, 2);
00261 printf("\n");
00262 for (auto it = v.begin(); it != v.end(); ++it) {
00263 printf("String::split(): '%s'\n", it->c_str());
00264 }
00265
00266 in = ".jack.jill..olaf";
00267 v = String::split(in, '.', false);
00268 printf("\n");
00269 for (auto it = v.begin(); it != v.end(); ++it) {
00270 printf("String::split(): '%s'\n", it->c_str());
00271 }
00272
00273 printf("\nString::join(): %s\n", String::join(v, ", ").c_str());
00274
00275 std::list<std::string> l = { "foo", "bar" };
00276 printf("String::join(): %s\n", String::join(l, ", ").c_str());
00277
00278 std::set<std::string> s = { "Sucker", "Punch" };
00279 printf("String::join(): %s\n", String::join(s, ", ").c_str());
00280
00281 printf("String::bandwidth(): %s\n", String::bandwidth(1250000000LL).c_str());
00282 printf("String::bandwidth(): %s\n", String::bandwidth(1250000000LL, 2).c_str());
00283 printf("String::dataRate(): %s\n", String::dataRate(1250000000LL).c_str());
00284 printf("String::dataRate(): %s\n", String::dataRate(1250000000LL, 2).c_str());
00285 printf("String::dataSize(): %s\n", String::dataSize(5772800LL).c_str());
00286 printf("String::dataSize(): %s\n", String::dataSize(5772800LL, 1).c_str());
00287 printf("String::duration(): %s\n", String::duration(1250000LL).c_str());
00288 printf("String::duration(): %s\n", String::duration(1250000LL, 2).c_str());
00289
00290 printf("\n");
00291 }
00292
00293 void testProcess(void) {
00294 printf("------------------------\n");
00295 printf("- sella/util/Process.h -\n");
00296 printf("------------------------\n");
00297
00298 printf("Process::getBaseName(): %s\n", Process::getBaseName().c_str());
00299 printf("Process::getDirName(): %s\n", Process::getDirName().c_str());
00300 printf("Process::getPath(): %s\n", Process::getPath().c_str());
00301
00302 {
00303 std::string org = Process::getName();
00304
00305 Process::setName("new-name");
00306 printf("Process::setName(): %s\n", Process::getName().c_str());
00307 Process::setName(org);
00308 printf("Process::getName(): %s\n", Process::getName().c_str());
00309 }
00310
00311 printf("Process::runCommand(): %s\n", Process::runCommand("echo -n \"it works\" 2> /dev/null").c_str());
00312 printf("Process::stackTrace(): Output below:\n%s\n", Process::stackTrace(Process::getPath()).c_str());
00313
00314 printf("\n");
00315 }
00316
00317 void testLockFile(void) {
00318 printf("-------------------------\n");
00319 printf("- sella/util/LockFile.h -\n");
00320 printf("-------------------------\n");
00321
00322 try {
00323 LockFile lockfile("/var/tmp/test.lock");
00324
00325 if (lockfile.isLocked() && !lockfile.ownLock()) {
00326 lockfile.remove();
00327 }
00328
00329 printf("LockFile::tryLock(): %s == true\n", (lockfile.tryLock()) ? "true" : "false");
00330 printf("LockFile::ownLock(): %s == true\n", (lockfile.ownLock()) ? "true" : "false");
00331 printf("LockFile::isLocked(): %s == true\n", (lockfile.isLocked()) ? "true" : "false");
00332 printf("LockFile::isStale(): %s == false\n", (lockfile.isStale()) ? "true" : "false");
00333
00334 if (lockfile.ownLock()) {
00335 printf("LockFile::unlock(): %s == true\n", (lockfile.unlock()) ? "true" : "false");
00336 }
00337
00338 printf("LockFile::ownLock(): %s == true\n", (lockfile.ownLock()) ? "true" : "false");
00339 printf("LockFile::isLocked(): %s == true\n", (lockfile.isLocked()) ? "true" : "false");
00340 } catch (FileException &e) {
00341 ERROR(e);
00342 }
00343
00344 printf("\n");
00345 }
00346
00347 void testFile(void) {
00348 printf("---------------------\n");
00349 printf("- sella/util/File.h -\n");
00350 printf("---------------------\n");
00351
00352 printf("File::getBaseName(): %s\n", File::getBaseName("/proc/cpuinfo").c_str());
00353 printf("File::getDirName(): %s\n", File::getDirName("/proc/cpuinfo").c_str());
00354 printf("File::getRealPath(): %s\n", File::getRealPath("/proc/./self").c_str());
00355 printf("File::exists(): %s\n", (File::exists("/proc/cpuinfo")) ? "true" : "false");
00356 printf("File::isRegularFile(): %s\n", (File::isRegularFile("/proc/cpuinfo")) ? "true" : "false");
00357 printf("File::isDirectory(): %s\n", (File::isDirectory("/proc/self")) ? "true" : "false");
00358 printf("File::isSymLink(): %s\n", (File::isSymLink("/proc/self")) ? "true" : "false");
00359 printf("File::getSize(): %"PRId64"B (%s)\n", File::getSize("/proc/self/exe"), String::dataSize(File::getSize("/proc/self/exe"), 1).c_str());
00360
00361 printf("\n");
00362 }
00363
00364 void testInterface(void) {
00365 printf("--------------------------\n");
00366 printf("- sella/util/Interface.h -\n");
00367 printf("--------------------------\n");
00368
00369 printf("Interface::getNames():\n");
00370 auto names = Interface::getNames(0);
00371
00372 for (auto it = names.begin(); it != names.end(); ++it) {
00373 printf("name: %s\n", it->c_str());
00374 }
00375
00376
00377 printf("\nInterface::getEthernetNames():\n");
00378 auto enames = Interface::getEthernetNames(0);
00379
00380 for (auto it = enames.begin(); it != enames.end(); ++it) {
00381 printf("name: %s\n", it->c_str());
00382 }
00383
00384
00385 printf("\nInterface::getIPAddresses(\"%s\"):\n", names.front().c_str());
00386 auto addresses = Interface::getIPAddresses(names.front());
00387
00388 for (auto it = addresses.begin(); it != addresses.end(); ++it) {
00389 printf("address: %s\n", (*it)->c_str());
00390 }
00391
00392
00393 printf("\nInterface::getIPAddresses():\n");
00394 addresses = Interface::getIPAddresses();
00395
00396 for (auto it = addresses.begin(); it != addresses.end(); ++it) {
00397 printf("address: %s\n", (*it)->c_str());
00398 }
00399
00400
00401 printf("\nInterface::getMAC(\"%s\"):\n", names.front().c_str());
00402 std::string mac = Interface::getMAC(names.front());
00403 printf("mac: %s\n", mac.c_str());
00404
00405
00406 printf("\nInterface::getMACs():\n");
00407 auto macs = Interface::getMACs();
00408
00409 for (auto it = macs.begin(); it != macs.end(); ++it) {
00410 printf("mac: %s\n", it->c_str());
00411 }
00412
00413
00414 printf("\nInterface::getIndex():\n");
00415
00416 for (auto it = names.begin(); it != names.end(); ++it) {
00417 int idx = Interface::getIndex(*it);
00418 printf("%s: %d (reverse: %s)\n", it->c_str(), idx, Interface::getName(idx).c_str());
00419 }
00420
00421 {
00422 printf("\nInterface::convertMAC():\n");
00423
00424 unsigned char addr[6] = { 0, 1, 2, 3, 4, 5 };
00425 uint64_t mac1, mac2;
00426 std::string buf1, buf2;
00427
00428 Interface::convertMAC(mac1, (const unsigned char*) addr);
00429 Interface::convertMAC(addr, mac1);
00430 Interface::convertMAC(mac2, (const unsigned char*) addr);
00431 printf("%"PRIu64" == %"PRIu64"\n", mac1, mac2);
00432 printf("%s == %s\n", Interface::convertMAC(buf1, mac1).c_str(), Interface::convertMAC(buf2, mac2).c_str());
00433 Interface::convertMAC(buf1, mac1);
00434 Interface::convertMAC(mac2, buf1);
00435 Interface::convertMAC(buf2, mac2);
00436 printf("%s == %s\n", buf1.c_str(), buf2.c_str());
00437 }
00438
00439 printf("\n");
00440 }
00441
00442 void testSocket(void) {
00443 size_t c;
00444 unsigned char buf[9216] = { 0 };
00445 size_t ethhdr_size = 18;
00446 struct ethhdr *eh = (struct ethhdr*) buf;
00447 struct ip *iphdr = (struct ip*) (buf + ethhdr_size);
00448 struct icmphdr *ich = (struct icmphdr*) (buf + ethhdr_size + sizeof(struct ip));
00449 size_t pktlen = ethhdr_size + sizeof(struct ip) + sizeof(struct icmphdr);
00450
00451 printf("--------------------------\n");
00452 printf("- sella/net/Socket.h -\n");
00453 printf("--------------------------\n");
00454
00455 auto names = Interface::getNames();
00456
00457
00458
00459
00460
00461 try {
00462 std::string destination = "00:11:22:33:44:55";
00463 std::string name = names.front();
00464 name = "eth2";
00465
00466 printf("EtherSocket(): %s\n", name.c_str());
00467 EtherSocket::shared s = std::make_shared<EtherSocket>(ETH_P_ALL, name, true);
00468 SocketMux mux;
00469
00470 s->setPromiscous();
00471 s->setSoReuseAddr();
00472 s->bindInterface();
00473 s->setSoBindToDevice();
00474
00475
00476 #ifdef ZEROCOPY
00477 size_t ring_frames = 256;
00478
00479
00480 struct tpacket_req req;
00481 req.tp_block_size = ring_frames * getpagesize();
00482 req.tp_block_nr = 1;
00483 req.tp_frame_size = getpagesize();
00484 req.tp_frame_nr = ring_frames;
00485
00486 unsigned char *tx, *rx;
00487 s->mmapRingBuffer(&tx, req);
00488 s->mmapRingBuffer(&rx, req);
00489
00490 s->setPacketRxRing(req);
00491 s->setPacketTxRing(req);
00492
00493 s->munmapRingBuffer(&tx, req);
00494 s->munmapRingBuffer(&rx, req);
00495 #endif
00496
00497 unsigned char dst_mac[6], src_mac[6];
00498
00499 su::Interface::convertMAC(dst_mac, destination);
00500 su::Interface::convertMAC(src_mac, su::Interface::getMAC(name));
00501
00502 memset(&buf, 0, sizeof(buf));
00503 memcpy((void*) (buf), (void*) &dst_mac, 6);
00504 memcpy((void*) (buf + 6), (void*) &src_mac, 6);
00505
00506 eh->h_proto = htons(0x8100);
00507 unsigned short int vid = htons(200);
00508 memcpy((void*) (buf + 14), (void*) &vid, 2);
00509
00510 unsigned short int vlanproto = htons(0x0800);
00511 memcpy((void*) (buf + 16), (void*) &vlanproto, 2);
00512
00513 iphdr->ip_hl = 5;
00514 iphdr->ip_v = 4;
00515 iphdr->ip_tos = 0;
00516 iphdr->ip_len = htons(pktlen - ethhdr_size);
00517 iphdr->ip_id = 0;
00518 iphdr->ip_off = 0;
00519 iphdr->ip_ttl = 64;
00520 iphdr->ip_p = 1;
00521 iphdr->ip_sum = 0;
00522 iphdr->ip_src.s_addr = inet_addr(su::Interface::getIPAddress(name).c_str());
00523 iphdr->ip_dst.s_addr = inet_addr(destination.c_str());
00524
00525 ich->type = 8;
00526 ich->code = 0;
00527 ich->checksum = 0;
00528 ich->un.echo.id = 0;
00529 ich->un.echo.sequence = htons(11);
00530
00531 ich->checksum = su::Packet::csum((unsigned short*) (buf + ethhdr_size + sizeof(struct ip)), 4);
00532 iphdr->ip_sum = su::Packet::csum((unsigned short*) (buf + ethhdr_size), iphdr->ip_hl << 1);
00533
00534 printf("Sending packet on vlan %d to %s (size: %zd)...\n", ntohs(vid), su::Interface::getMAC(name).c_str(), pktlen);
00535 for (size_t i = 0; i < pktlen; i++) {
00536 printf("%02X", buf[i]);
00537 }
00538 printf("\n");
00539
00540 s->sendto(&buf, pktlen, destination);
00541
00542 mux.insertRead(s);
00543
00544 struct timeval delay = { 0, 5000 };
00545
00546 if (mux.select(&delay) > 0) {
00547 const auto &list = mux.getReadList();
00548
00549 for (auto it = list.begin(); it != list.end(); ++it) {
00550 s = std::static_pointer_cast<EtherSocket>(*it);
00551 std::string mac;
00552
00553 if ((c = s->recvfrom(&buf, sizeof(buf), mac)) > 0) {
00554
00555 printf("Received packet on from %s.\n", mac.c_str());
00556 for (size_t i = 0; i < c; i++) {
00557 printf("%X", buf[i]);
00558 }
00559 printf("\n");
00560
00561 } else {
00562 printf("Received no packet.\n");
00563 }
00564 }
00565 } else {
00566 printf("Receive timed out (nothing to receive)\n");
00567 }
00568 } catch (sella::Exception &e) {
00569 printf("Exception: %s\n", e.what());
00570 }
00571
00572 #if 0
00573 try {
00574 printf("\nEtherSocket(): %s\n", names.front().c_str());
00575 EtherSocket s(ETH_P_MPLS_UC, "eth2");
00576 SocketMux mux;
00577
00578 s.setSoReuseAddr(true);
00579
00580 s.setSoBindToDevice("eth2");
00581
00582
00583
00584 printf("sendto...\n");
00585
00586 struct header_8021q {
00587 uint32_t vid:12;
00588 } __attribute__((packed));
00589
00590 union mpls_shim {
00591 uint32_t shim;
00592 struct mpls_label {
00593 uint32_t label:20;
00594 uint8_t exp:3;
00595 uint8_t s:1;
00596 uint8_t ttl:8;
00597 } __attribute__((__packed__)) bits;
00598 };
00599
00600 mpls_shim *h = (mpls_shim*) buf;
00601
00602 h->bits.label = htons(500);
00603 h->bits.exp = 0;
00604 h->bits.s = 1;
00605 h->bits.ttl = 10;
00606
00607 h->shim = htonl(h->shim);
00608
00609 s.sendto(&buf, 4, "68:05:CA:0F:C5:FF");
00610
00611 #if 0
00612 printf("GET GET\n");
00613 read(s.getFD(), buf, sizeof(buf));
00614 #endif
00615
00616 printf("recvfrom()...\n");
00617 if (s.recvfrom(&buf, sizeof(buf)) > 0) {
00618 printf("Got some packets\n");
00619 } else {
00620 printf("Got no packets\n");
00621 }
00622 } catch (sella::Exception &e) {
00623 printf("Exception: %s\n", e.what());
00624 }
00625 #endif
00626
00627 printf("\n");
00628 }
00629
00630 void testIPAddress(void) {
00631 printf("------------------------\n");
00632 printf("- sella/net/IPAddress.h -\n");
00633 printf("------------------------\n");
00634
00635 try {
00636 printf("IPAddress Constructors\n");
00637 IPAddress a("192.168.0.0");
00638 IPAddress b("192.168.0.25");
00639 IPAddress c("192.168.0.75");
00640 IPAddress e("fe80::6a05:caff:fe0f:0000");
00641 IPAddress f("fe80::6a05:caff:fe0f:5555");
00642 IPAddress g("ffff::ffff:ffff:ffff:ffff");
00643 IPAddress m("0.0.0.255");
00644 IPAddress j("::FFFF");
00645
00646 printf("a: %s\n", a.c_str());
00647 printf("b: %s\n", b.c_str());
00648 printf("c: %s\n", c.c_str());
00649 printf("e: %s\n", e.c_str());
00650 printf("f: %s\n", f.c_str());
00651 printf("g: %s\n", g.c_str());
00652 printf("m: %s\n", m.c_str());
00653 printf("j: %s\n", j.c_str());
00654
00655 printf("\nIPAddress::operator==()\n");
00656 printf("(%s == %s) == %s\n", a.c_str(), b.c_str(), (a == b) ? "true" : "false");
00657 printf("(%s == %s) == %s\n", b.c_str(), c.c_str(), (b == c) ? "true" : "false");
00658 printf("(%s == %s) == %s\n", b.c_str(), b.c_str(), (b == b) ? "true" : "false");
00659 printf("(%s == %s) == %s\n", e.c_str(), f.c_str(), (e == f) ? "true" : "false");
00660 printf("(%s == %s) == %s\n", f.c_str(), g.c_str(), (f == g) ? "true" : "false");
00661 printf("(%s == %s) == %s\n", f.c_str(), f.c_str(), (f == f) ? "true" : "false");
00662
00663 printf("\nIPAddress::operator>()\n");
00664 printf("(%s > %s) == %s\n", a.c_str(), b.c_str(), (a > b) ? "true" : "false");
00665 printf("(%s > %s) == %s\n", c.c_str(), b.c_str(), (c > b) ? "true" : "false");
00666 printf("(%s > %s) == %s\n", f.c_str(), e.c_str(), (f > e) ? "true" : "false");
00667 printf("(%s > %s) == %s\n", f.c_str(), g.c_str(), (f > g) ? "true" : "false");
00668
00669 printf("\nIPAddress::operator<()\n");
00670 printf("(%s < %s) == %s\n", a.c_str(), b.c_str(), (a < b) ? "true" : "false");
00671 printf("(%s < %s) == %s\n", b.c_str(), c.c_str(), (b < c) ? "true" : "false");
00672 printf("(%s < %s) == %s\n", f.c_str(), e.c_str(), (f < e) ? "true" : "false");
00673 printf("(%s < %s) == %s\n", f.c_str(), g.c_str(), (f < g) ? "true" : "false");
00674
00675 printf("\nIPAddress::operator>=()\n");
00676 printf("(%s >= %s) == %s\n", a.c_str(), b.c_str(), (a >= b) ? "true" : "false");
00677 printf("(%s >= %s) == %s\n", c.c_str(), b.c_str(), (c >= b) ? "true" : "false");
00678 printf("(%s >= %s) == %s\n", f.c_str(), e.c_str(), (f >= e) ? "true" : "false");
00679 printf("(%s >= %s) == %s\n", f.c_str(), g.c_str(), (f >= g) ? "true" : "false");
00680
00681 printf("\nIPAddress::operator<=()\n");
00682 printf("(%s <= %s) == %s\n", a.c_str(), b.c_str(), (a <= b) ? "true" : "false");
00683 printf("(%s <= %s) == %s\n", b.c_str(), c.c_str(), (b <= c) ? "true" : "false");
00684 printf("(%s <= %s) == %s\n", f.c_str(), e.c_str(), (f <= e) ? "true" : "false");
00685 printf("(%s <= %s) == %s\n", f.c_str(), g.c_str(), (f <= g) ? "true" : "false");
00686
00687 printf("\nIPAddress::operator&()\n");
00688 printf("(%s & %s) == %s\n", a.c_str(), m.c_str(), (a & m).str().c_str());
00689 printf("(%s & %s) == %s\n", b.c_str(), m.c_str(), (b & m).str().c_str());
00690 printf("(%s & %s) == %s\n", e.c_str(), j.c_str(), (e & j).str().c_str());
00691 printf("(%s & %s) == %s\n", f.c_str(), j.c_str(), (f & j).str().c_str());
00692
00693 printf("\nIPAddress::operator|()\n");
00694 printf("(%s | %s) == %s\n", a.c_str(), m.c_str(), (a | m).str().c_str());
00695 printf("(%s | %s) == %s\n", b.c_str(), m.c_str(), (b | m).str().c_str());
00696 printf("(%s | %s) == %s\n", e.c_str(), j.c_str(), (e | j).str().c_str());
00697 printf("(%s | %s) == %s\n", f.c_str(), j.c_str(), (f | j).str().c_str());
00698
00699 printf("\nIPAddress::operator^()\n");
00700 printf("(%s ^ %s) == %s\n", a.c_str(), m.c_str(), (a ^ m).str().c_str());
00701 printf("(%s ^ %s) == %s\n", b.c_str(), m.c_str(), (b ^ m).str().c_str());
00702 printf("(%s ^ %s) == %s\n", e.c_str(), j.c_str(), (e ^ j).str().c_str());
00703 printf("(%s ^ %s) == %s\n", f.c_str(), j.c_str(), (f ^ j).str().c_str());
00704
00705 printf("\nIPAddress::operator~()\n");
00706 printf("(~ %s) == %s\n", a.c_str(), (~a).str().c_str());
00707 printf("(~ %s) == %s\n", b.c_str(), (~b).str().c_str());
00708 printf("(~ %s) == %s\n", e.c_str(), (~e).str().c_str());
00709 printf("(~ %s) == %s\n", f.c_str(), (~f).str().c_str());
00710
00711 printf("\nIPAddress::getU32()\n");
00712 printf("%s => %u\n", a.c_str(), a.getU32());
00713 printf("%s => %u\n", b.c_str(), b.getU32());
00714 printf("%s => %u\n", e.c_str(), e.getU32());
00715 printf("%s => %u\n", f.c_str(), f.getU32());
00716
00717 printf("\nIPAddress::getHash()\n");
00718 printf("%s => %u\n", a.c_str(), a.getHash());
00719 printf("%s => %u\n", b.c_str(), b.getHash());
00720 printf("%s => %u\n", e.c_str(), e.getHash());
00721 printf("%s => %u\n", f.c_str(), f.getHash());
00722
00723 printf("\nIPAddress::operator++()\n");
00724 printf("%s++ == %s\n", a.c_str(), (a++).c_str());
00725 printf("%s++ == %s\n", e.c_str(), (e++).c_str());
00726 g = IPAddress("::ffff:ffff:ffff:ffff");
00727 printf("%s++ == %s\n", g.c_str(), (g++).c_str());
00728 g = IPAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
00729 printf("%s++ == %s\n", g.c_str(), (g++).c_str());
00730
00731 printf("\nIPAddress::operator+()\n");
00732 printf("%s++ == %s\n", a.c_str(), (a++).c_str());
00733 printf("%s++ == %s\n", e.c_str(), (e++).c_str());
00734 g = IPAddress("::ffff:ffff:ffff:ffff");
00735 printf("%s + 256 == %s\n", g.c_str(), (g + 256).c_str());
00736 g = IPAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
00737 printf("%s + 256 == %s\n", g.c_str(), (g + 256).c_str());
00738
00739 printf("\nIPAddress::operator--()\n");
00740 printf("%s-- == %s\n", a.c_str(), (a--).c_str());
00741 printf("%s-- == %s\n", e.c_str(), (e--).c_str());
00742 g = IPAddress("::1");
00743 printf("%s-- == %s\n", g.c_str(), (g--).c_str());
00744 g = IPAddress("::ffff:ffff:ffff:ffff");
00745 printf("%s-- == %s\n", g.c_str(), (g--).c_str());
00746 g = IPAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
00747 printf("%s-- == %s\n", g.c_str(), (g--).c_str());
00748
00749 printf("\nIPAddress::operator-()\n");
00750 printf("%s-- == %s\n", a.c_str(), (a--).c_str());
00751 printf("%s-- == %s\n", e.c_str(), (e--).c_str());
00752 g = IPAddress("::ffff:ffff:ffff:ffff");
00753 printf("%s - 1 == %s\n", g.c_str(), (g - 1).c_str());
00754 g = IPAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
00755 printf("%s - 1 == %s\n", g.c_str(), (g - 1).c_str());
00756 g = IPAddress("::");
00757 printf("%s - 1 == %s\n", g.c_str(), (g - 1).c_str());
00758 g = IPAddress("::1:ffff:ffff:ffff:ffff");
00759 printf("%s - 2 == %s\n", g.c_str(), (g - 2).c_str());
00760 g = IPAddress("ffff:ffff:ffff:ffff::");
00761 printf("%s - 1 == %s\n", g.c_str(), (g - 1).c_str());
00762 g = IPAddress("0:0:0:1::");
00763 printf("%s - 1 == %s\n", g.c_str(), (g - 1).c_str());
00764
00765 printf("\nIPAddress::operator+=()\n");
00766 a = IPAddress("10.0.0.5") += 1;
00767 printf("%s += 1 == %s\n", IPAddress("10.0.0.5").c_str(), a.c_str());
00768 a = IPAddress("::ffff:ffff:ffff:ffff") += 2;
00769 printf("%s += 2 == %s\n", IPAddress("::ffff:ffff:ffff:ffff").c_str(), a.c_str());
00770
00771 printf("\nIPAddress::operator-=()\n");
00772 a = IPAddress("10.0.0.6") -= 1;
00773 printf("%s -= 1 == %s\n", IPAddress("10.0.0.6").c_str(), a.c_str());
00774 a = IPAddress("0:0:0:2::") -= 2;
00775 printf("%s -= 2 == %s\n", IPAddress("0:0:0:2::").c_str(), a.c_str());
00776
00777 } catch (AddressException &e) {
00778 printf("AddressException: %s\n", e.what());
00779
00780 exit(1);
00781 }
00782
00783 printf("\n");
00784 }
00785
00786 void testIPAddressPort(void) {
00787 printf("-----------------------------\n");
00788 printf("- sella/net/IPAddressPort.h -\n");
00789 printf("-----------------------------\n");
00790
00791 try {
00792 printf("\nIPAddressPort Constructors\n");
00793 IPAddressPort a("192.168.0.0");
00794 IPAddressPort b("[192.168.0.25]:80");
00795 IPAddressPort c("192.168.0.75:8080", 9999);
00796 IPAddressPort e("fe80::6a05:caff:fe0f:5151");
00797 IPAddressPort f("[fe80::6a05:caff:fe0f:5555]:8080", 9999);
00798 IPAddressPort g("ffff::ffff:ffff:ffff:ffff", 9999);
00799 IPAddressPort h("[::]:0");
00800 IPAddressPort m("[0.0.0.255]");
00801 IPAddressPort j("[::FFFF]");
00802
00803 printf("a: %s\n", a.c_str());
00804 printf("b: %s\n", b.c_str());
00805 printf("c: %s\n", c.c_str());
00806 printf("e: %s\n", e.c_str());
00807 printf("f: %s\n", f.c_str());
00808 printf("g: %s\n", g.c_str());
00809 printf("h: %s\n", h.c_str());
00810 printf("m: %s\n", m.c_str());
00811 printf("j: %s\n", j.c_str());
00812
00813 printf("\nIPAddressPort::operator==()\n");
00814 printf("(%s == %s) == %s\n", a.c_str(), b.c_str(), (a == b) ? "true" : "false");
00815 printf("(%s == %s) == %s\n", b.c_str(), c.c_str(), (b == c) ? "true" : "false");
00816 printf("(%s == %s) == %s\n", b.c_str(), b.c_str(), (b == b) ? "true" : "false");
00817 printf("(%s == %s) == %s\n", e.c_str(), f.c_str(), (e == f) ? "true" : "false");
00818 printf("(%s == %s) == %s\n", f.c_str(), g.c_str(), (f == g) ? "true" : "false");
00819 printf("(%s == %s) == %s\n", f.c_str(), f.c_str(), (f == f) ? "true" : "false");
00820
00821 printf("\nIPAddressPort::operator>()\n");
00822 printf("(%s > %s) == %s\n", a.c_str(), b.c_str(), (a > b) ? "true" : "false");
00823 printf("(%s > %s) == %s\n", c.c_str(), b.c_str(), (c > b) ? "true" : "false");
00824 printf("(%s > %s) == %s\n", f.c_str(), e.c_str(), (f > e) ? "true" : "false");
00825 printf("(%s > %s) == %s\n", f.c_str(), g.c_str(), (f > g) ? "true" : "false");
00826
00827 printf("\nIPAddressPort::operator<()\n");
00828 printf("(%s < %s) == %s\n", a.c_str(), b.c_str(), (a < b) ? "true" : "false");
00829 printf("(%s < %s) == %s\n", b.c_str(), c.c_str(), (b < c) ? "true" : "false");
00830 printf("(%s < %s) == %s\n", f.c_str(), e.c_str(), (f < e) ? "true" : "false");
00831 printf("(%s < %s) == %s\n", f.c_str(), g.c_str(), (f < g) ? "true" : "false");
00832
00833 printf("\nIPAddressPort::operator>=()\n");
00834 printf("(%s >= %s) == %s\n", a.c_str(), b.c_str(), (a >= b) ? "true" : "false");
00835 printf("(%s >= %s) == %s\n", c.c_str(), b.c_str(), (c >= b) ? "true" : "false");
00836 printf("(%s >= %s) == %s\n", f.c_str(), e.c_str(), (f >= e) ? "true" : "false");
00837 printf("(%s >= %s) == %s\n", f.c_str(), g.c_str(), (f >= g) ? "true" : "false");
00838
00839 printf("\nIPAddressPort::operator<=()\n");
00840 printf("(%s <= %s) == %s\n", a.c_str(), b.c_str(), (a <= b) ? "true" : "false");
00841 printf("(%s <= %s) == %s\n", b.c_str(), c.c_str(), (b <= c) ? "true" : "false");
00842 printf("(%s <= %s) == %s\n", f.c_str(), e.c_str(), (f <= e) ? "true" : "false");
00843 printf("(%s <= %s) == %s\n", f.c_str(), g.c_str(), (f <= g) ? "true" : "false");
00844
00845 printf("\nIPAddressPort::getU32()\n");
00846 printf("%s => %u\n", b.c_str(), b.getU32());
00847 printf("%s => %u\n", e.c_str(), e.getU32());
00848
00849 printf("\nIPAddressPort::getHash()\n");
00850 printf("%s => %u\n", b.c_str(), b.getHash());
00851 printf("%s => %u\n", e.c_str(), e.getHash());
00852 } catch (AddressException &e) {
00853 printf("AddressException: %s\n", e.what());
00854
00855 exit(1);
00856 }
00857
00858 printf("\n");
00859 }
00860
00861 void testIPPrefix(void) {
00862 printf("------------------------\n");
00863 printf("- sella/net/IPPrefix.h -\n");
00864 printf("------------------------\n");
00865
00866 try {
00867 printf("IPPrefix Constructors\n");
00868 IPPrefix a("192.168.50.25");
00869 IPPrefix b("192.168.50.1/24");
00870 IPPrefix c("192.168.50.25", 24);
00871 IPPrefix d(IPAddress("192.168.50.25"), 16);
00872 IPPrefix e("fe80::6a05:caff:fe0f:c55f", 128);
00873 IPPrefix f("fe80::6a05:caff:fe0f:c55f");
00874 IPPrefix g("fe80::6a05:caff:fe0f:dead/128");
00875 IPPrefix h("fe80::6a05:caff/64");
00876 IPPrefix m("0.0.0.255");
00877 IPPrefix j("::FFFF");
00878 IPPrefix y("fe80::6a05:caff:fe0f:dead/36");
00879 IPPrefix z("fe80::6a05:caff:fe0f:dead/56");
00880 IPAddress aa("192.168.50.1");
00881
00882 printf("a: %s\n", a.c_str());
00883 printf("b: %s\n", b.c_str());
00884 printf("c: %s\n", c.c_str());
00885 printf("d: %s\n", d.c_str());
00886 printf("e: %s\n", e.c_str());
00887 printf("f: %s\n", f.c_str());
00888 printf("g: %s\n", g.c_str());
00889 printf("h: %s\n", h.c_str());
00890
00891 printf("\nIPPrefix::operator==()\n");
00892 printf("(%s == %s) == %s\n", a.c_str(), b.c_str(), (a == b) ? "true" : "false");
00893 printf("(%s == %s) == %s\n", b.c_str(), c.c_str(), (b == c) ? "true" : "false");
00894 printf("(%s == %s) == %s\n", e.c_str(), f.c_str(), (e == f) ? "true" : "false");
00895 printf("(%s == %s) == %s\n", f.c_str(), g.c_str(), (f == g) ? "true" : "false");
00896
00897 printf("\nIPPrefix::operator>()\n");
00898 printf("(%s > %s) == %s\n", a.c_str(), b.c_str(), (a > b) ? "true" : "false");
00899 printf("(%s > %s) == %s\n", b.c_str(), c.c_str(), (b > c) ? "true" : "false");
00900 printf("(%s > %s) == %s\n", e.c_str(), f.c_str(), (e > f) ? "true" : "false");
00901 printf("(%s > %s) == %s\n", f.c_str(), g.c_str(), (f > g) ? "true" : "false");
00902
00903 printf("\nIPPrefix::operator<()\n");
00904 printf("(%s < %s) == %s\n", a.c_str(), b.c_str(), (a < b) ? "true" : "false");
00905 printf("(%s < %s) == %s\n", b.c_str(), c.c_str(), (b < c) ? "true" : "false");
00906 printf("(%s < %s) == %s\n", e.c_str(), f.c_str(), (e < f) ? "true" : "false");
00907 printf("(%s < %s) == %s\n", f.c_str(), g.c_str(), (f < g) ? "true" : "false");
00908
00909 printf("\nIPPrefix::operator>=()\n");
00910 printf("(%s >= %s) == %s\n", a.c_str(), b.c_str(), (a >= b) ? "true" : "false");
00911 printf("(%s >= %s) == %s\n", b.c_str(), c.c_str(), (b >= c) ? "true" : "false");
00912 printf("(%s >= %s) == %s\n", e.c_str(), f.c_str(), (e >= f) ? "true" : "false");
00913 printf("(%s >= %s) == %s\n", f.c_str(), g.c_str(), (f >= g) ? "true" : "false");
00914
00915 printf("\nIPPrefix::operator<=()\n");
00916 printf("(%s <= %s) == %s\n", a.c_str(), b.c_str(), (a <= b) ? "true" : "false");
00917 printf("(%s <= %s) == %s\n", b.c_str(), c.c_str(), (b <= c) ? "true" : "false");
00918 printf("(%s <= %s) == %s\n", e.c_str(), f.c_str(), (e <= f) ? "true" : "false");
00919 printf("(%s <= %s) == %s\n", f.c_str(), g.c_str(), (f <= g) ? "true" : "false");
00920
00921 printf("\nIPPrefix::contains()\n");
00922 printf("(%s contains %s) == %s\n", a.c_str(), b.c_str(), (a.contains(b)) ? "true" : "false");
00923 printf("(%s contains %s) == %s\n", b.c_str(), a.c_str(), (b.contains(a)) ? "true" : "false");
00924 printf("(%s contains %s) == %s\n", b.c_str(), c.c_str(), (b.contains(c)) ? "true" : "false");
00925 printf("(%s contains %s) == %s\n", g.c_str(), h.c_str(), (g.contains(h)) ? "true" : "false");
00926 printf("(%s contains %s) == %s\n", h.c_str(), g.c_str(), (h.contains(g)) ? "true" : "false");
00927 printf("(%s contains %s) == %s\n", b.c_str(), aa.c_str(), (b.contains(aa)) ? "true" : "false");
00928
00929 printf("\nIPPrefix::operator>>=()\n");
00930 printf("(%s >>= %s) == %s\n", a.c_str(), b.c_str(), (a >>= b) ? "true" : "false");
00931 printf("(%s >>= %s) == %s\n", b.c_str(), a.c_str(), (b >>= a) ? "true" : "false");
00932 printf("(%s >>= %s) == %s\n", b.c_str(), c.c_str(), (b >>= c) ? "true" : "false");
00933 printf("(%s >>= %s) == %s\n", g.c_str(), h.c_str(), (g >>= h) ? "true" : "false");
00934 printf("(%s >>= %s) == %s\n", h.c_str(), g.c_str(), (h >>= g) ? "true" : "false");
00935 printf("(%s >>= %s) == %s\n", b.c_str(), aa.c_str(), (b >>= aa) ? "true" : "false");
00936
00937 printf("\nIPPrefix::operator>>()\n");
00938 printf("(%s >> %s) == %s\n", a.c_str(), b.c_str(), (a >> b) ? "true" : "false");
00939 printf("(%s >> %s) == %s\n", b.c_str(), a.c_str(), (b >> a) ? "true" : "false");
00940 printf("(%s >> %s) == %s\n", b.c_str(), c.c_str(), (b >> c) ? "true" : "false");
00941 printf("(%s >> %s) == %s\n", g.c_str(), h.c_str(), (g >> h) ? "true" : "false");
00942 printf("(%s >> %s) == %s\n", h.c_str(), g.c_str(), (h >> g) ? "true" : "false");
00943 printf("(%s >> %s) == %s\n", b.c_str(), aa.c_str(), (b >> aa) ? "true" : "false");
00944
00945 printf("\nIPPrefix::operator<<=()\n");
00946 printf("(%s <<= %s) == %s\n", a.c_str(), b.c_str(), (a <<= b) ? "true" : "false");
00947 printf("(%s <<= %s) == %s\n", b.c_str(), a.c_str(), (b <<= a) ? "true" : "false");
00948 printf("(%s <<= %s) == %s\n", b.c_str(), c.c_str(), (b <<= c) ? "true" : "false");
00949 printf("(%s <<= %s) == %s\n", g.c_str(), h.c_str(), (g <<= h) ? "true" : "false");
00950 printf("(%s <<= %s) == %s\n", h.c_str(), g.c_str(), (h <<= g) ? "true" : "false");
00951 printf("(%s <<= %s) == %s\n", b.c_str(), aa.c_str(), (b <<= aa) ? "true" : "false");
00952
00953 printf("\nIPPrefix::operator<<()\n");
00954 printf("(%s << %s) == %s\n", a.c_str(), b.c_str(), (a << b) ? "true" : "false");
00955 printf("(%s << %s) == %s\n", b.c_str(), a.c_str(), (b << a) ? "true" : "false");
00956 printf("(%s << %s) == %s\n", b.c_str(), c.c_str(), (b << c) ? "true" : "false");
00957 printf("(%s << %s) == %s\n", g.c_str(), h.c_str(), (g << h) ? "true" : "false");
00958 printf("(%s << %s) == %s\n", h.c_str(), g.c_str(), (h << g) ? "true" : "false");
00959 printf("(%s << %s) == %s\n", b.c_str(), aa.c_str(), (b << aa) ? "true" : "false");
00960
00961 printf("\nIPPrefix::getU32()\n");
00962 printf("%s => %u\n", a.c_str(), a.getU32());
00963 printf("%s => %u\n", b.c_str(), b.getU32());
00964 printf("%s => %u\n", e.c_str(), e.getU32());
00965 printf("%s => %u\n", h.c_str(), h.getU32());
00966
00967 printf("\nIPPrefix::getHash()\n");
00968 printf("%s => %u\n", a.c_str(), a.getHash());
00969 printf("%s => %u\n", b.c_str(), b.getHash());
00970 printf("%s => %u\n", e.c_str(), e.getHash());
00971 printf("%s => %u\n", h.c_str(), h.getHash());
00972
00973 printf("\nIPPrefix::getNetmask()\n");
00974 printf("%s => %s\n", a.c_str(), a.getNetmask().c_str());
00975 printf("%s => %s\n", b.c_str(), b.getNetmask().c_str());
00976 printf("%s => %s\n", d.c_str(), d.getNetmask().c_str());
00977 printf("%s => %s\n", e.c_str(), e.getNetmask().c_str());
00978 printf("%s => %s\n", h.c_str(), h.getNetmask().c_str());
00979
00980 printf("\nIPPrefix::getInverseMask()\n");
00981 printf("%s => %s\n", a.c_str(), a.getInverseMask().c_str());
00982 printf("%s => %s\n", b.c_str(), b.getInverseMask().c_str());
00983 printf("%s => %s\n", d.c_str(), d.getInverseMask().c_str());
00984 printf("%s => %s\n", e.c_str(), e.getInverseMask().c_str());
00985 printf("%s => %s\n", h.c_str(), h.getInverseMask().c_str());
00986
00987 printf("\nIPPrefix::getIPAddresses()\n");
00988 a = IPPrefix("192.168.50.0/29");
00989 auto addresses = a.getIPAddresses(true);
00990
00991 for (auto ip = addresses.begin(); ip != addresses.end(); ++ip) {
00992 printf("%s => %s\n", a.c_str(), ip->c_str());
00993 }
00994
00995 } catch (AddressException &e) {
00996 printf("AddressException: %s\n", e.what());
00997
00998 exit(1);
00999 }
01000
01001 printf("\n");
01002 }
01003
01004 void testVariant(void) {
01005 using namespace sella::variant;
01006
01007 printf("---------------------------\n");
01008 printf("- sella/variant/Variant.h -\n");
01009 printf("---------------------------\n");
01010
01011 try {
01012 Variant vvv(std::make_shared<sv::Null>(), 1);
01013 printf("Variant:null => null: %s (%s)\n", vvv.c_str(), vvv.getTypeName());
01014
01015 Variant n;
01016 Variant a(true);
01017 Variant b; b = false;
01018 Variant c; c = 1.23;
01019 Variant c2(1.23);
01020 Variant d("abc");
01021 Variant d2; d2 = "bogus";
01022 Variant e = -100;
01023 Variant e2 = e;
01024 Variant f = 100;
01025 Variant v("[ 100, [ 200, \"str\" ], 300 ]", Variant::TypeAuto);
01026 Variant o("{ \"foo\": \"bar\", \"100\": 200 }", Variant::TypeAuto);
01027 Variant vv("[{ \"a\": 100, \"b\": { \"c\": [100,200] } }, 50.1,[ 200, \"aa\\\"bb\", [0,\f\r\n1, [ 2, \t3]]] ]", Variant::TypeAuto);
01028 Variant u(sella::util::UUID("35898CBD-BAC6-468b-a0cb-efa700954925"));
01029 Variant m;
01030
01031 printf("Variant:cast<const std::string&> => null: %s (%s)\n", static_cast<const std::string &>(n).c_str(), n.getTypeName());
01032 printf("Variant:(const char*) => true: %s (%s)\n", (const char*) a, a.getTypeName());
01033 printf("Variant:c_str() => false: %s (%s)\n", b.c_str(), b.getTypeName());
01034 printf("Variant:operator+ => 2.25: %s (%s)\n", (++c + 0.02).c_str(), c.getTypeName());
01035 printf("Variant:contains() => true: %s\n", (d.contains("abc")) ? "true" : "false");
01036 printf("Variant:contains() => false: %s\n", (d.contains("zozo")) ? "true" : "false");
01037 printf("Variant:operator+ => abcabc: %s (%s)\n", (d + d).c_str(), d.getTypeName());
01038 printf("Variant:operator+ => abcxxx: %s (%s)\n", (d + "xxx").c_str(), d.getTypeName());
01039 printf("Variant:operator+ => abc100: %s (%s)\n", (d + 100).c_str(), d.getTypeName());
01040 printf("Variant:cast<int64_t> => -100: %" PRId64 " (%s)\n", e.cast<int64_t>(), e.getTypeName());
01041 printf("Variant:contains() => true: %s\n", (e.contains(-100)) ? "true" : "false");
01042 printf("Variant:contains() => false: %s\n", (e.contains(919)) ? "true" : "false");
01043 printf("Variant:cast<uint64_t> => 100: %" PRIu64 " (%s)\n", f.cast<uint64_t>(), f.getTypeName());
01044 printf("Variant:operator+ => 200: %" PRId64 " (%s)\n", static_cast<int64_t>(f + f), f.getTypeName());
01045 printf("Variant:operator+,/%%^ => 25: %" PRId64 " (%s)\n", static_cast<int64_t>((400 + f) / 100 % 10 ^ 2), f.getTypeName());
01046 printf("Variant:(int64_t) => 100: %" PRId64 " (%s)\n", (int64_t) f, f.getTypeName());
01047 printf("Variant:operator- => 99.0: %.1f (%s)\n", static_cast<double>(f - true), f.getTypeName());
01048 printf("Variant:c_str() => [ 100, [ 200, \"str\" ], 300 ]: %s (%s)\n", v.c_str(), v.getTypeName());
01049 printf("Variant:c_str() => { \"foo\": \"bar\", \"100\": 200 }: %s (%s)\n", o.c_str(), o.getTypeName());
01050 printf("Variant:c_str() => [ { messy } ]: %s (%s)\n", vv.c_str(), vv.getTypeName());
01051 printf("Variant:(const char*) => 35898cbd-bac6-468b-a0cb-efa700954925: %s (%s)\n", (const char*) u, u.getTypeName());
01052 u.shrink_to_fit();
01053 printf("Variant:contains() => true: %s\n", (u.contains(u)) ? "true" : "false");
01054 printf("Variant:contains() => false: %s\n", (u.contains("bogus")) ? "true" : "false");
01055 f = "150";
01056 f.setType(Variant::TypeDouble);
01057 printf("Variant:c_str() => 150.0: %s (%s)\n", f.c_str(), f.getTypeName());
01058 f.setType(Variant::TypeUnsigned);
01059 std::cout << "Variant:operator<< => 151: " << f << " (" << f.getTypeName() << ")" << std::endl;
01060
01061 vv.clear();
01062 printf("Variant:clear() => null: %s (%s)\n", vv.c_str(), vv.getTypeName());
01063
01064 printf("Variant:operator== => true: %s\n", (c == c2) ? "true" : "false");
01065 printf("Variant:operator== => true: %s\n", (e == e2) ? "true" : "false");
01066 printf("Variant:operator!= => true: %s\n", (d != d2) ? "true" : "false");
01067 printf("Variant:operator== => false: %s\n", (b == c) ? "true" : "false");
01068 printf("Variant:operator!= => true: %s\n", (b != c) ? "true" : "false");
01069 printf("Variant:operator! => false: %s\n", (!c) ? "true" : "false");
01070 printf("Variant:operator>= => false: %s\n", (b >= a) ? "true" : "false");
01071 printf("Variant:operator<= => false: %s\n", (c <= c2) ? "true" : "false");
01072 printf("Variant:operator>= => true: %s\n", (b >= "false") ? "true" : "false");
01073
01074 m["foo"] = 100;
01075 m["bar"] = "zoo";
01076 m["nullptr"].clear();
01077
01078 printf("Variant:c_str() => { \"bar\": \"zoo\", \"foo\": 100, \"nullptr\": null }: %s\n", m.c_str());
01079 printf("Variant:operator[] => 100: %s\n", m["foo"].c_str());
01080 printf("Variant:contains() => true: %s\n", (m.contains("foo")) ? "true" : "false");
01081 printf("Variant:contains() => false: %s\n", (m.contains("zzz")) ? "true" : "false");
01082
01083 v.clear();
01084 v[0] = 100;
01085 v[2] = 300;
01086 v[3] = 350;
01087 v.erase(3);
01088 v.push_back(400);
01089 v.erase("400");
01090 v += 500;
01091
01092 for (Variant::iterator it = v.begin(); it != v.end(); ++it) {
01093 printf("iterator => key: %s, value: %s\n", it.key().c_str(), it->c_str());
01094 }
01095
01096 printf("*Variant:c_str() => [ 100, null, 300, 500 ]: %s\n", v.c_str());
01097 printf("Variant[2]:c_str() => 300: %s\n", v[2].c_str());
01098 printf("Variant:contains() => true: %s\n", (v.contains(100)) ? "true" : "false");
01099 printf("Variant:contains() => false: %s\n", (v.contains(919)) ? "true" : "false");
01100
01101 c.clear();
01102 printf("Variant:empty() => true: %s\n", (c.empty()) ? "true" : "false");
01103 printf("Variant:empty() => false: %s\n", (m.empty()) ? "true" : "false");
01104
01105 printf("really have: %s\n", m.c_str());
01106 for (auto it = m.begin(); it != m.end(); ++it) {
01107 printf("key: %s, value: %s\n", it.key().c_str(), it->c_str());
01108 }
01109
01110 m.clear();
01111 m["test"] = 100; m.erase("test");
01112 printf("Variant:empty() => true: %s\n", (m.empty()) ? "true" : "false");
01113
01114 printf("really have: %s\n", f.c_str());
01115 for (auto it = f.begin(); it != f.end(); ++it) {
01116 printf("key: %s, value: %s\n", it.key().c_str(), it->c_str());
01117 }
01118
01119 f = 100;
01120 printf("Variant:c_str() => 101: %" PRIu64 " (%s)\n", (int64_t) ++f, f.getTypeName());
01121
01122 f = 65535;
01123 printf("Variant:>> => 3: %" PRIu64 " (%s)\n", (int64_t) (f >> 14), f.getTypeName());
01124
01125 printf("Variant:&& => true: %s\n", (m && f && true) ? "true" : "false");
01126
01127 f = (void*) NULL;
01128 printf("Variant:NULL => null: %s (%s)\n", f.c_str(), f.getTypeName());
01129
01130 {
01131 Variant data;
01132 Variant d2;
01133 Variant o;
01134
01135
01136
01137
01138 o[0] = 5;
01139
01140 printf("data: %s\n", data.c_str());
01141
01142
01143
01144
01145
01146 printf("\n+= first\n");
01147 data = o;
01148 printf("data3: %s\n", data.c_str());
01149
01150
01151
01152
01153 printf("\n+= second\n");
01154 data += o;
01155 printf("data4: %s\n", data.c_str());
01156
01157 data.clear();
01158 void *x = (void*) new int8_t(4);
01159 data["foo"] = *((int8_t*) x);
01160
01161 printf("data: %s\n", data.c_str());
01162 }
01163
01164 b.clear();
01165
01166 #if 0
01167 printf("Variant:operator== NULL => true: %s\n", (b == NULL) ? "true" : "false");
01168 #endif
01169 printf("Variant:isNull => true: %s\n", (b.isNull()) ? "true" : "false");
01170
01171 Variant bin("0x01AABBCCEEFF");
01172
01173 printf("Variant:c_str() => binary: %s (%s)\n", bin.c_str(), bin.getTypeName());
01174
01175 Variant str2("{ \"x\": \"35898CBD-BAC6-468b-a0cb-efa700954925\" }", Variant::TypeAuto);
01176
01177 str2["x"] = u;
01178
01179 printf("str2: %s\n", str2.c_str());
01180
01181 Variant ip("192.168.0.2");
01182 printf("ip: %s (%s)\n", ip.c_str(), ip.getTypeName());
01183
01184 Variant ip2("2601:1:af00:28f:6a05:caff:fe0f:c55f");
01185 printf("ip2: %s (%s)\n", ip2.c_str(), ip2.getTypeName());
01186
01187 Variant prefix("2601:1:af00:28f:6a05:caff:fe0f:c55f/64");
01188 printf("prefix: %s (%s)\n", prefix.c_str(), prefix.getTypeName());
01189
01190 Variant prefix2("192.168.0.2/24");
01191 printf("prefix2: %s (%s)\n", prefix2.c_str(), prefix2.getTypeName());
01192 } catch (su::UUIDException &e) {
01193 printf("UUIDException: %s\n", e.what());
01194
01195 exit(1);
01196 } catch (TypeCastException &e) {
01197 printf("TypeCastException: %s\n", e.what());
01198
01199 exit(1);
01200 } catch (sella::Exception &e) {
01201 printf("sella::Exception: %s\n", e.what());
01202
01203 exit(1);
01204 } catch (...) {
01205 printf("Unknown Exception\n");
01206
01207 exit(1);
01208 }
01209
01210 printf("\n");
01211 }
01212
01213 namespace sella {
01214 namespace test {
01215 BUILD_EXCEPTION(TestException);
01216 }
01217 }
01218
01219 void testException(void) {
01220 printf("---------------------\n");
01221 printf("- sella/Exception.h -\n");
01222 printf("---------------------\n");
01223
01224 SET_FORMAT(MixedLogFormat());
01225
01226 try {
01227 THROW(sella::test::TestException, "TestException test!");
01228 } catch (sella::test::TestException &e) {
01229 ERROR(e);
01230 WARNING(e);
01231
01232
01233 INFO("code: %d, what: %s", e.getCode(), e.what());
01234
01235 try {
01236 RETHROW2_CODE(sella::test::TestException, 200, e, "Added rethrow text.");
01237 } catch (sella::test::TestException &e) {
01238 ERROR(e);
01239 }
01240 }
01241
01242 try {
01243 std::string x = "bobert";
01244 std::string y = "janert";
01245 THROW(sella::test::TestException, "TestException with Bob %s and Jane %s", x.c_str(), y.c_str());
01246 } catch (sella::test::TestException &e) {
01247 INFO("ExceptionName: %s", e.getName().c_str());
01248 ERROR(e);
01249 }
01250
01251 SET_FORMAT(SimpleLogFormat());
01252
01253 try {
01254 printf("\n");
01255 INFO("throwing...");
01256 THROW(sella::Exception, "Important message text!");
01257 } catch (sella::Exception &e) {
01258 INFO("caught...");
01259 try {
01260 RETHROW(sella::Exception, e);
01261 } catch (sella::Exception &e) {
01262 WARNING(e);
01263 }
01264 }
01265
01266 try {
01267 printf("\n");
01268 INFO("throwing with code...");
01269 THROW_CODE(sella::Exception, 100, "Important message text!");
01270 } catch (sella::Exception &e) {
01271 INFO("caught...");
01272 try {
01273 RETHROW(sella::Exception, e);
01274 } catch (sella::Exception &e) {
01275 INFO("found code: %d", e.getCode());
01276 WARNING(e);
01277 }
01278 }
01279
01280 printf("\n");
01281 }
01282
01283 class TestCallback : public sn::DNSCallback {
01284 virtual void success(const std::string &qname, const std::string &cname, int ttl, const std::vector<sn::IPAddress> &response) const throw () {
01285 if (cname.empty()) {
01286 INFO("DNS::resolve[async/success]: %s [ttl: %d]", qname.c_str(), ttl);
01287 } else {
01288 INFO("DNS::resolve[async/success]: %s <- %s [ttl: %d]", qname.c_str(), cname.c_str(), ttl);
01289 }
01290
01291 for (auto r = response.begin(); r != response.end(); ++r) {
01292 INFO(" - response: %s", r->c_str());
01293 }
01294 }
01295
01296 virtual void failure(const std::string &qname, Error error) const throw () {
01297 INFO("DNS::resolve[async/failure](%s): %s => %s", qname.c_str(), getErrorName(error), getErrorDetail(error));
01298 }
01299 };
01300
01301 void testDNS(void) {
01302 using namespace sella::net;
01303
01304 printf("-------------------\n");
01305 printf("- sella/net/DNS.h -\n");
01306 printf("-------------------\n");
01307
01308
01309
01310
01311 try {
01312 if (1) {
01313 INFO("[Sync]");
01314 INFO("");
01315
01316 DNS dns({ std::string("216.136.9.22"), std::string("8.8.8.8") }, { std::string("digital-genesis.com"), std::string("google.com") });
01317
01318 auto ipv4a = dns.resolve("www.digital-genesis.com", DNS::IPv4Required);
01319 INFO("DNS::resolve(www.digital-genesis.com, DNS::IPv4Required): %s", ipv4a.front().c_str());
01320
01321 auto ipv4b = dns.resolve("www.digital-genesis.com", DNS::IPv4Preferred);
01322 INFO("DNS::resolve(www.digital-genesis.com, DNS::IPv4Preferred): %s", ipv4b.front().c_str());
01323
01324 auto ipv6a = dns.resolve("www.digital-genesis.com", DNS::IPv6Required);
01325 INFO("DNS::resolve(www.digital-genesis.com, DNS::IPv6Required): %s", ipv6a.front().c_str());
01326
01327 auto ipv6b = dns.resolve("www.digital-genesis.com", DNS::IPv6Preferred);
01328 INFO("DNS::resolve(www.digital-genesis.com, DNS::IPv6Preferred): %s", ipv6b.front().c_str());
01329
01330 try {
01331 auto bogus = dns.resolve("bogus.digital-genesis.com", DNS::AnyProtocol);
01332 INFO("DNS::resolve(bogus.digital-genesis.com, DNS::AnyProtocol): %s", bogus.front().c_str());
01333 } catch (DNSException &e) {
01334 INFO("DNS::resolve(bogus.digital-genesis.com, DNS::AnyProtocol): %s", e.what());
01335 }
01336 }
01337
01338 if (1) {
01339 INFO("[Async]");
01340 INFO("");
01341
01342 DNS dns({ std::string("216.136.9.22"), std::string("8.8.8.8") }, { std::string("digital-genesis.com"), std::string("google.com") });
01343 TestCallback c1, c2, c3, c4, c5, c6;
01344 std::vector<TestCallback> callbacks(10);
01345
01346 dns.setTimeout(1);
01347 dns.setNoSearch(true);
01348 dns.setAuthoritativeOnly(true);
01349
01350 dns.resolve(&callbacks[0], "www.google.com", DNS::IPv4Preferred);
01351 dns.resolve(&callbacks[1], "bogus", DNS::IPv4Required);
01352 dns.resolve(&callbacks[2], "www.digital-genesis.com", DNS::IPv4Required);
01353 dns.resolve(&callbacks[3], "www.digital-genesis.com", DNS::IPv4Preferred);
01354 dns.resolve(&callbacks[4], "www.digital-genesis.com", DNS::IPv6Required);
01355 dns.resolve(&callbacks[5], "www.digital-genesis.com", DNS::IPv6Preferred);
01356
01357 while (dns.getActive() > 0) {
01358 dns.receive(1);
01359 }
01360 }
01361
01362 if (1) {
01363 using namespace sella::util;
01364
01365 INFO("[Async with ThreadPool]");
01366 INFO("");
01367
01368 ThreadPool pool(10);
01369 DNS dns(&pool, { std::string("8.8.8.8") }, { std::string("digital-genesis.com") });
01370 TestCallback c1, c2, c3, c4, c5, c6;
01371 std::vector<TestCallback> callbacks(10);
01372
01373 dns.setTimeout(1);
01374
01375 dns.resolve(&callbacks[0], "www.google.com", DNS::IPv4Preferred);
01376 dns.resolve(&callbacks[1], "bogus", DNS::IPv4Required);
01377 dns.resolve(&callbacks[2], "www.digital-genesis.com", DNS::IPv4Required);
01378 dns.resolve(&callbacks[3], "www.digital-genesis.com", DNS::IPv4Preferred);
01379 dns.resolve(&callbacks[4], "www.digital-genesis.com", DNS::IPv6Required);
01380 dns.resolve(&callbacks[5], "www.digital-genesis.com", DNS::IPv6Preferred);
01381
01382 INFO("Waiting for ThreadPool to complete all tasks.");
01383 pool.wait();
01384 INFO("ThreadPool has no remaining tasks");
01385 }
01386 } catch (DNSException &e) {
01387 WARNING(e);
01388 }
01389
01390 printf("\n");
01391 }
01392
01393 void example(int &s) {
01394 #if 0
01395 sleep(seconds);
01396 #else
01397 long i = 0; double res = 0;
01398 while (i < (s * 1000000000LL)) { i++; res += sqrt(i); }
01399 #endif
01400
01401
01402 }
01403
01404 void testThreadPool(void) {
01405 using namespace sella::util;
01406
01407 printf("--------------------------\n");
01408 printf("- sella/util/ThreadPool.h -\n");
01409 printf("--------------------------\n");
01410
01411 SET_TRACE(PACKAGE);
01412 SET_TRACE(PACKAGE "-detail");
01413
01414 {
01415 #if 1
01416 ThreadPool pool(8, 2, 2);
01417 #else
01418 ThreadPool pool(8);
01419 #endif
01420 Task::shared task;
01421 Task::callback function;
01422
01423 INFO("Setting ThreadPool CPU Affinity");
01424 pool.addCPUAffinity({2,99});
01425
01426 INFO("Pausing ThreadPool");
01427 pool.pause();
01428
01429
01430 for (size_t i = 0; i < 15; i++) {
01431 task = Task::shared_ptr();
01432 function = std::bind(&example, 2);
01433 task->setCallback(function);
01434
01435 pool.schedule(task, 10 * i);
01436 }
01437
01438
01439 task = Task::shared_ptr();
01440 function = std::bind(&example, 1);
01441 task->setCallback(function);
01442
01443 pool.schedule(task, std::chrono::seconds(2), 64);
01444
01445
01446 task = Task::shared_ptr();
01447 function = std::bind(&example, 1);
01448 task->setCallback(function);
01449
01450 pool.schedule(task, std::chrono::system_clock::now(), 32);
01451
01452 pool.print();
01453 usleep(10000);
01454
01455
01456 INFO("Resuming ThreadPool");
01457 pool.resume();
01458
01459 sleep(2);
01460 pool.clearCPUAffinity({1,2});
01461
01462 INFO("Waiting for ThreadPool to complete all tasks.");
01463 pool.wait();
01464 INFO("ThreadPool has no remaining tasks");
01465 }
01466
01467 printf("\n");
01468 }
01469
01470 void testSocketPair(void) {
01471 using namespace sella::net;
01472
01473 printf("--------------------------\n");
01474 printf("- sella/net/SocketPair.h -\n");
01475 printf("--------------------------\n");
01476
01477 SET_TRACE(PACKAGE);
01478 SET_TRACE(PACKAGE "-detail");
01479
01480 try {
01481 SocketPair pair;
01482 UnixSocket::shared first = pair.getFirst();
01483 UnixSocket::shared second = pair.getSecond();
01484
01485 printf("Send: test\n");
01486 first->send("test");
01487
01488 std::string data;
01489 if (second->recv(data) > 0) {
01490 printf("Recv: %s\n", data.c_str());
01491 } else {
01492 printf("Recv: <empty>\n");
01493 }
01494
01495 } catch (SocketException &e) {
01496 ERROR(e);
01497 }
01498
01499 printf("\n");
01500 }
01501
01502 void testSocketTrigger(void) {
01503 using namespace sella::net;
01504
01505 printf("-----------------------------\n");
01506 printf("- sella/net/SocketTrigger.h -\n");
01507 printf("-----------------------------\n");
01508
01509 SET_TRACE(PACKAGE);
01510 SET_TRACE(PACKAGE "-detail");
01511
01512 try {
01513 SocketTrigger trigger;
01514
01515 printf("trigger::isSet(): %d\n", trigger.isSet());
01516
01517 printf("trigger::set() [2x]\n");
01518 trigger.set();
01519 trigger.set();
01520 printf("trigger::isSet(): %d\n", trigger.isSet());
01521
01522 printf("trigger::clear()\n");
01523 trigger.clear();
01524 printf("trigger::isSet(): %d\n", trigger.isSet());
01525 } catch (SocketException &e) {
01526 ERROR(e);
01527 }
01528
01529 printf("\n");
01530 }
01531
01532 void testPipe(void) {
01533 using namespace sella::net;
01534
01535 printf("--------------------\n");
01536 printf("- sella/net/Pipe.h -\n");
01537 printf("--------------------\n");
01538
01539 SET_TRACE(PACKAGE);
01540 SET_TRACE(PACKAGE "-detail");
01541
01542 try {
01543 std::string buf;
01544 Pipe pipe;
01545
01546 printf("Write: test\n");
01547 pipe.write("test");
01548
01549 if (pipe.read(buf) > 0) {
01550 printf("Read: %s\n", buf.c_str());
01551 } else {
01552 printf("Read: <empty>\n");
01553 }
01554 } catch (SocketException &e) {
01555 ERROR(e);
01556 }
01557
01558 printf("\n");
01559 }
01560
01561
01562
01563