📄 testdns.cxx
字号:
#if defined(HAVE_CONFIG_H)#include "resip/stack/config.hxx"#endif#if defined (HAVE_POPT_H) #include <popt.h>#else#ifndef WIN32#warning "will not work very well without libpopt"#endif#endif#include <iostream>#include <iomanip>#include <list>#include "rutil/Lock.hxx"#include "rutil/Mutex.hxx"#include "rutil/Socket.hxx"#include "rutil/Logger.hxx"#include "rutil/ThreadIf.hxx"#include "rutil/ParseBuffer.hxx"#include "rutil/DnsUtil.hxx"#include "resip/stack/DnsInterface.hxx"#include "resip/stack/DnsResult.hxx"#include "resip/stack/SipStack.hxx"#include "resip/stack/TupleMarkManager.hxx"#include "resip/stack/MarkListener.hxx"#include "rutil/dns/RRVip.hxx"#include "rutil/dns/DnsStub.hxx"#include "rutil/dns/DnsHandler.hxx"#include "resip/stack/test/tassert.h"using namespace std;#define RESIPROCATE_SUBSYSTEM resip::Subsystem::TESTconst char bf[] = "\033[01;34m";const char gf[] = "\033[01;32m";const char rf[] = "\033[01;31m";const char ub[] = "\033[01;00m";#ifdef WIN32#define usleep(x) Sleep(x/1000)#define sleep(x) Sleep(x*1000)#endifnamespace resip{// called on the DNS processing thread, therefore state must be lockedclass TestDnsHandler : public DnsHandler{ public: TestDnsHandler() : mComplete(false), mCheckExpectedResults(false), mPermutationNumber(0) {} TestDnsHandler(const std::vector<Tuple>& expectedResults, const resip::Uri& uri) : mComplete(false), mExpectedResults(expectedResults), mCheckExpectedResults(true), mUri(uri), mPermutationNumber(0) {} TestDnsHandler(const std::vector<Tuple>& expectedResults, const std::set<Tuple>& resultsToBlacklist, const std::set<Tuple>& resultsToGreylist, const resip::Uri& uri) : mComplete(false), mExpectedResults(expectedResults), mCheckExpectedResults(true), mUri(uri), mPermutationNumber(0), mResultsToBlacklist(resultsToBlacklist), mResultsToGreylist(resultsToGreylist) {} void handle(DnsResult* result) { std::cout << gf << "DnsHandler received " << result->target() << ub << std::endl; Lock lock(mutex); DnsResult::Type type; while ((type=result->available()) == DnsResult::Available) { Tuple tuple = result->next(); results.push_back(tuple); std::cout << gf << result->target() << " -> " << tuple << ub << std::endl; if(mResultsToGreylist.count(tuple)!=0) { result->greylistLast(Timer::getTimeMs()+15000); } if(mResultsToBlacklist.count(tuple)!=0) { result->blacklistLast(Timer::getTimeMs()+15000); } } if (type != DnsResult::Pending) { mComplete = true; if(mCheckExpectedResults) { checkExpectedResults(); } } } void rewriteRequest(const Uri& rewrite) { std::cout << "Rewriting uri (enum) to " << rewrite << std::endl; } bool complete() { Lock lock(mutex); return mComplete; } void checkExpectedResults() { std::cout << "Input Uri was " << mUri << endl; tassert(mExpectedResults.size() == results.size()); tassert_reset(); std::cout << "Expected " << mExpectedResults.size() << ", got " << results.size() << endl; std::vector<Tuple>::const_iterator e; std::vector<Tuple>::const_iterator o; for(e=mExpectedResults.begin();e!=mExpectedResults.end();++e) { int p=0; std::cout << "Looking for " << *e << endl; bool found=false; for(o=results.begin();(o!=results.end() && !found);++o) { ++p; if(*e==*o) { found=true; std::cout << *o << " matched!" << endl; mPermutation.push_back(p); } else { std::cout << *o << " didn't match." << endl; } } tassert(found); tassert_reset(); } } int getPermutationNumber() { if(mPermutationNumber!=0) { return mPermutationNumber; } int result=1; // .bwc. Please forgive me for my use of permutation-group-foo. for(int i=mPermutation.size();i>0;--i) { int foundAt=0; for(std::list<int>::iterator j=mPermutation.begin();j!=mPermutation.end();++j) { ++foundAt; if(*j==i) { result*=((foundAt-i)%i+1); mPermutation.erase(j); j=mPermutation.end(); } } } mPermutationNumber=result; return result; } std::vector<Tuple> results; private: bool mComplete; std::vector<Tuple> mExpectedResults; bool mCheckExpectedResults; Mutex mutex; Uri mUri; std::list<int> mPermutation; int mPermutationNumber; std::set<resip::Tuple> mResultsToBlacklist; std::set<resip::Tuple> mResultsToGreylist;};/*class VipListener : public RRVip::Listener{ void onVipInvalidated(int rrType, const Data& vip) const { cout << rf << "VIP " << " -> " << vip << " type" << " -> " << rrType << " has been invalidated." << ub << endl; }};*/class TestMarkListener : public MarkListener{ public: TestMarkListener(const resip::Tuple& listenFor) : mTuple(listenFor), mGotOkCallback(false), mGotGreylistCallback(false), mGotBlacklistCallback(false) {} virtual ~TestMarkListener(){} virtual void onMark(const Tuple& tuple, TupleMarkManager::MarkType mark) { if(mTuple == tuple) { switch(mark) { case TupleMarkManager::OK: mGotOkCallback=true; break; case TupleMarkManager::GREY: mGotGreylistCallback=true; break; case TupleMarkManager::BLACK: mGotBlacklistCallback=true; break; default: ; } } } bool gotOkCallback() const {return mGotOkCallback;} bool gotGreylistCallback() const {return mGotGreylistCallback;} bool gotBlacklistCallback() const {return mGotBlacklistCallback;} void resetAll() { mGotOkCallback=false; mGotGreylistCallback=false; mGotBlacklistCallback=false; } private: Tuple mTuple; bool mGotOkCallback; bool mGotGreylistCallback; bool mGotBlacklistCallback; }; class TestDns : public DnsInterface, public ThreadIf{ public: TestDns(DnsStub& stub) : DnsInterface(stub), mStub(stub) { addTransportType(TCP, V4); addTransportType(UDP, V4); addTransportType(TLS, V4);#ifdef IPV6 addTransportType(TCP, V6); addTransportType(UDP, V6); addTransportType(TLS, V6);#endif } void thread() { while (!waitForShutdown(100)) { FdSet fdset; buildFdSet(fdset); mStub.buildFdSet(fdset); fdset.selectMilliSeconds(1); process(fdset); mStub.process(fdset); } } DnsStub& mStub;}; }using namespace resip;typedef struct { DnsResult* result; Uri uri; TestDnsHandler* handler; //VipListener* listener;} Query;int main(int argc, const char** argv){ char* logType = "cout"; char* logLevel = "STACK"; char* enumSuffix = "e164.arpa"; #if defined(HAVE_POPT_H) struct poptOption table[] = { {"log-type", 'l', POPT_ARG_STRING, &logType, 0, "where to send logging messages", "syslog|cerr|cout"}, {"log-level", 'v', POPT_ARG_STRING, &logLevel, 0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"}, {"enum-suffix", 'e', POPT_ARG_STRING, &enumSuffix, 0, "specify what enum domain to search in", "e164.arpa"}, POPT_AUTOHELP { NULL, 0, 0, NULL, 0 } }; poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0); poptGetNextOpt(context);#endif Log::initialize(logType, logLevel, argv[0]); initNetwork(); DnsStub* stub = new DnsStub; TestDns dns(*stub); dns.run(); cerr << "Starting" << endl; std::list<Query> queries;#if defined(HAVE_POPT_H) const char** args = poptGetArgs(context);#else const char** args = argv;#endif std::vector<Data> enumSuffixes; enumSuffixes.push_back(enumSuffix); stub->setEnumSuffixes(enumSuffixes); resip::Data uris[16]={ "sip:127.0.0.1:5070;transport=udp", "sips:127.0.0.1:5071;transport=tls", "sip:127.0.0.1;transport=udp", "sips:127.0.0.1;transport=tls", "sip:user.test.resiprocate.org:5070;transport=udp", "sips:user.test.resiprocate.org:5071;transport=tls", "sip:user.test.resiprocate.org;transport=udp", "sips:user.test.resiprocate.org;transport=tls", "sip:127.0.0.1:5070", "sips:127.0.0.1:5071", "sip:127.0.0.1", "sips:127.0.0.1", "sip:user.test.resiprocate.org:5070", "sips:user.test.resiprocate.org:5071", "sip:user-tcp.test.resiprocate.org", "sips:user-tcp.test.resiprocate.org" }; int expectedPorts[16][3]={ {5070,0,0}, {5071,0,0}, {5060,0,0}, {5061,0,0}, {5070,0,0}, {5071,0,0}, {5060,5070,5080}, {5061,5071,5081}, {5070,0,0}, {5071,0,0}, {5060,0,0}, {5061,0,0}, {5070,0,0}, {5071,0,0}, {5060,5070,5080}, {5061,5071,5081} }; TransportType expectedTransports[16][3]={ {UDP,UDP,UDP}, {TLS,TLS,TLS}, {UDP, UDP, UDP}, {TLS,TLS,TLS}, {UDP, UDP, UDP}, {TLS,TLS,TLS}, {UDP, UDP, UDP}, {TLS,TLS,TLS}, {UDP, UDP, UDP}, {TLS,TLS,TLS}, {UDP, UDP, UDP}, {TLS,TLS,TLS}, {UDP, UDP, UDP}, {TLS,TLS,TLS}, {TCP, TCP, TCP}, {TLS,TLS,TLS} }; resip::Data subUris[8]={ "sip:<hostname>:5080;transport=TCP", "sips:<hostname>:5081;transport=TLS", "sip:<hostname>;transport=TCP", "sips:<hostname>;transport=TLS",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -