8 #include <boost/algorithm/string/case_conv.hpp>
10 #include <simgear/simgear_config.h>
12 #include "DNSClient.hxx"
14 #include "test_DNS.hxx"
16 #include <simgear/debug/logstream.hxx>
17 #include <simgear/misc/strutils.hxx>
18 #include <simgear/timing/timestamp.hxx>
24 using namespace simgear;
26 #define COMPARE(a, b) \
28 cerr << "failed:" << #a << " != " << #b << endl; \
29 cerr << "\tgot:'" << a << "'" << endl; \
34 int main(int argc, char* argv[])
36 sglog().setLogLevels( SG_ALL, SG_DEBUG );
39 #define EXISTING_RECORD "terrasync.flightgear.org"
41 // test existing NAPTR
42 // fgtest.t3r.de. 600 IN NAPTR 999 99 "U" "test" "!^.*$!http://dnstest.flightgear.org/!" .
44 DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest(EXISTING_RECORD);
45 DNS::Request_ptr r(naptrRequest);
47 while( !r->isComplete() && !r->isTimeout()) {
48 SGTimeStamp::sleepForMSec(200);
52 if( r->isTimeout() ) {
53 cerr << "timeout testing existing record " EXISTING_RECORD << endl;
56 if(naptrRequest->entries.empty()) {
57 cerr << "no results for " EXISTING_RECORD << endl;
61 // test for ascending preference/order
62 int order = -1, preference = -1;
63 for( DNS::NAPTRRequest::NAPTR_list::const_iterator it = naptrRequest->entries.begin(); it != naptrRequest->entries.end(); ++it ) {
64 // currently only support "U" which implies empty replacement
65 COMPARE((*it)->flags, "U" );
66 COMPARE(naptrRequest->entries[0]->replacement, "" );
68 // currently only support ws20
69 COMPARE((*it)->service, "ws20" );
71 if( (*it)->order < order ) {
72 cerr << "NAPTR entries not ascending for field 'order'" << endl;
74 } else if( (*it)->order > order ) {
76 preference = (*it)->preference;
78 if( (*it)->preference < preference ) {
79 cerr << "NAPTR entries not ascending for field 'preference', order=" << order << endl;
82 preference = (*it)->preference;
85 if( false == simgear::strutils::starts_with( (*it)->regexp, "!^.*$!" ) ) {
86 cerr << "NAPTR entry with unsupported regexp: " << (*it)->regexp << endl;
90 if( false == simgear::strutils::ends_with( (*it)->regexp, "!" ) ) {
91 cerr << "NAPTR entry with unsupported regexp: " << (*it)->regexp << endl;
98 // test non-existing NAPTR
100 DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest("jurkxkqdiufqzpfvzqok.prozhqrlcaavbxifkkhf");
101 DNS::Request_ptr r(naptrRequest);
103 while( !r->isComplete() && !r->isTimeout()) {
104 SGTimeStamp::sleepForMSec(200);
108 if( r->isTimeout() ) {
109 cerr << "timeout testing non-existing record." << endl;
112 COMPARE(naptrRequest->entries.size(), 0 );
115 cout << "all tests passed ok" << endl;