]> git.mxchange.org Git - simgear.git/blob - simgear/io/test_DNS.cxx
More libCurl version guards.
[simgear.git] / simgear / io / test_DNS.cxx
1 #include <cstdlib>
2
3 #include <iostream>
4 #include <map>
5 #include <sstream>
6 #include <errno.h>
7
8 #include <boost/algorithm/string/case_conv.hpp>
9
10 #include <simgear/simgear_config.h>
11
12 #include "DNSClient.hxx"
13
14 #include "test_DNS.hxx"
15
16 #include <simgear/debug/logstream.hxx>
17 #include <simgear/misc/strutils.hxx>
18 #include <simgear/timing/timestamp.hxx>
19
20 using std::cout;
21 using std::cerr;
22 using std::endl;
23
24 using namespace simgear;
25
26 #define COMPARE(a, b) \
27     if ((a) != (b))  { \
28         cerr << "failed:" << #a << " != " << #b << endl; \
29         cerr << "\tgot:'" << a << "'" << endl; \
30         exit(1); \
31     }
32
33
34 int main(int argc, char* argv[])
35 {
36     sglog().setLogLevels( SG_ALL, SG_DEBUG );
37
38     DNS::Client cl;
39 #define EXISTING_RECORD "terrasync.flightgear.org"
40
41     // test existing NAPTR
42     // fgtest.t3r.de.                600        IN        NAPTR        999 99 "U" "test" "!^.*$!http://dnstest.flightgear.org/!" .
43     {
44         DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest(EXISTING_RECORD);
45         DNS::Request_ptr r(naptrRequest);
46         cl.makeRequest(r);
47         while( !r->isComplete() && !r->isTimeout()) {
48             SGTimeStamp::sleepForMSec(200);
49             cl.update(0);
50         }
51
52         if( r->isTimeout() ) {
53             cerr << "timeout testing existing record " EXISTING_RECORD << endl;
54             return EXIT_FAILURE;
55         }
56         if(naptrRequest->entries.empty()) {
57             cerr << "no results for " EXISTING_RECORD << endl;
58             return EXIT_FAILURE;
59         }
60
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, "" );
67
68             // currently only support ws20
69             COMPARE((*it)->service, "ws20" );
70
71             if( (*it)->order < order ) {
72                 cerr << "NAPTR entries not ascending for field 'order'" << endl;
73                 return EXIT_FAILURE;
74             } else if( (*it)->order > order ) {
75                 order = (*it)->order;
76                 preference = (*it)->preference;
77             } else {
78                 if( (*it)->preference < preference ) {
79                     cerr << "NAPTR entries not ascending for field 'preference', order=" << order << endl;
80                     return EXIT_FAILURE;
81                 }
82                 preference = (*it)->preference;
83             }
84
85             if( false == simgear::strutils::starts_with( (*it)->regexp, "!^.*$!" ) ) {
86                 cerr << "NAPTR entry with unsupported regexp: " << (*it)->regexp << endl;
87                 return EXIT_FAILURE;
88             }
89
90             if( false == simgear::strutils::ends_with( (*it)->regexp, "!" ) ) {
91                 cerr << "NAPTR entry with unsupported regexp: " << (*it)->regexp << endl;
92                 return EXIT_FAILURE;
93             }
94
95         }
96     }
97
98     // test non-existing NAPTR
99     {
100         DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest("jurkxkqdiufqzpfvzqok.prozhqrlcaavbxifkkhf");
101         DNS::Request_ptr r(naptrRequest);
102         cl.makeRequest(r);
103         while( !r->isComplete() && !r->isTimeout()) {
104             SGTimeStamp::sleepForMSec(200);
105             cl.update(0);
106         }
107
108         if( r->isTimeout() ) {
109             cerr << "timeout testing non-existing record." << endl;
110             return EXIT_FAILURE;
111         }
112         COMPARE(naptrRequest->entries.size(), 0 );
113     }
114
115     cout << "all tests passed ok" << endl;
116     return EXIT_SUCCESS;
117 }