📄 testdns.cxx
字号:
} sleep(1); } assert(queries.empty()); assert(!dns.requiresProcess()); std::map<resip::Tuple,int> ipAddrToNum; ipAddrToNum[Tuple("127.0.0.1",5060,V4,TCP)]=0; ipAddrToNum[Tuple("127.0.0.2",5060,V4,TCP)]=1; ipAddrToNum[Tuple("127.0.0.3",5060,V4,TCP)]=2; ipAddrToNum[Tuple("127.0.0.4",5060,V4,TCP)]=3; // .bwc. Test load-leveling. for(int numSRV=2;numSRV<5;++numSRV) { resip::Data hostname("loadlevel"); hostname+=Data::from(numSRV)+".test.resiprocate.org"; Uri uri; uri.host()=hostname; uri.scheme()="sip"; for(int i=0; i<1000;++i) { Query query; query.handler = new TestDnsHandler(); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); if(i%20==0) { // .bwc. Let things have some time to cache, so we don't hammer the // DNS to death. (Odds are good that we have hit every NAPTR at // least once by now) sleep(2); RRCache::instance()->logCache(); } } // .bwc. first index is the order (1st=0, 2nd=1, etc), and second index // is the last tuple in the IP address (127.0.0.1 is 0, 127.0.0.2 is 1) // The value stored is the number of times this combination was encountered. int table[numSRV][numSRV]; for(int i=0;i<numSRV;++i) { for(int j=0;j<numSRV;++j) { table[i][j]=0; } } int count = queries.size(); while (count>0) { for (std::list<Query>::iterator it = queries.begin(); it != queries.end(); ) { if ((*it).handler->complete()) { cerr << rf << "DNS results for " << (*it).uri << ub << endl; assert(it->handler->results.size()==numSRV); for(int i=0;i<numSRV;++i) { assert(ipAddrToNum[it->handler->results[i]] >=0); assert(ipAddrToNum[it->handler->results[i]] <numSRV); ++table[i][ipAddrToNum[it->handler->results[i]]]; cerr << rf << it->handler->results[i] << ub << endl; } --count; (*it).result->destroy(); delete (*it).handler; std::list<Query>::iterator temp = it; ++it; queries.erase(temp); } else { ++it; } } sleep(1); } assert(queries.empty()); assert(!dns.requiresProcess()); std::cout << "Tabulated results:" << std::endl; for(int i=0;i<numSRV;++i) { for(int j=0;j<numSRV;++j) { std::cout << table[i][j] << std::setw(6); } std::cout << std::endl; } } // .bwc. Test blacklisting std::cout << "Testing blacklisting." << std::endl; { Tuple toBlacklist("127.0.0.1",5060,V4,TCP); Tuple ok2("127.0.0.2",5060,V4,TCP); Tuple ok3("127.0.0.3",5060,V4,TCP); Tuple ok4("127.0.0.4",5060,V4,TCP); std::vector<Tuple> expected; expected.push_back(ok2); expected.push_back(ok3); expected.push_back(ok4); expected.push_back(toBlacklist); std::set<Tuple> blacklist; blacklist.insert(toBlacklist); std::set<Tuple> greylist; Uri uri; uri.scheme()="sip"; uri.host()="loadlevel4.test.resiprocate.org"; TestMarkListener* listener = new TestMarkListener(toBlacklist); dns.getMarkManager().registerMarkListener(listener); Query query; query.handler = new TestDnsHandler(expected,blacklist,greylist,uri); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); // .bwc. Give this query plenty of time. sleep(2); assert(listener->gotBlacklistCallback()); listener->resetAll(); // This removes the Tuple toBlacklist expected.pop_back(); for(int i=0;i<20;++i) { Query query; query.handler = new TestDnsHandler(expected,uri); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); } // .bwc. Wait for blacklist to expire. sleep(16); // Put the blacklisted Tuple back. expected.push_back(toBlacklist); for(int i=0;i<20;++i) { Query query; query.handler = new TestDnsHandler(expected,uri); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); } int count = queries.size(); while (count>0) { for (std::list<Query>::iterator it = queries.begin(); it != queries.end(); ) { if ((*it).handler->complete()) { cerr << rf << "DNS results for " << (*it).uri << ub << endl; for (std::vector<Tuple>::iterator i = (*it).handler->results.begin(); i != (*it).handler->results.end(); ++i) { cerr << rf << (*i) << ub << endl; } --count; (*it).result->destroy(); delete (*it).handler; std::list<Query>::iterator temp = it; ++it; queries.erase(temp); } else { ++it; } } sleep(1); } assert(queries.empty()); assert(listener->gotOkCallback()); listener->resetAll(); dns.getMarkManager().unregisterMarkListener(listener); delete listener; } // .bwc. Test greylisting std::cout << "Testing greylisting." << std::endl; { Tuple toGreylist("127.0.0.1",5060,V4,TCP); Tuple ok2("127.0.0.2",5060,V4,TCP); Tuple ok3("127.0.0.3",5060,V4,TCP); Tuple ok4("127.0.0.4",5060,V4,TCP); std::vector<Tuple> expected; expected.push_back(ok2); expected.push_back(ok3); expected.push_back(ok4); expected.push_back(toGreylist); std::set<Tuple> blacklist; std::set<Tuple> greylist; greylist.insert(toGreylist); Uri uri; uri.scheme()="sip"; uri.host()="loadlevel4.test.resiprocate.org"; TestMarkListener* listener = new TestMarkListener(toGreylist); dns.getMarkManager().registerMarkListener(listener); { Query query; query.handler = new TestDnsHandler(expected,blacklist,greylist,uri); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); } // .bwc. Give this query plenty of time. sleep(2); assert(listener->gotGreylistCallback()); listener->resetAll(); std::cout << toGreylist << " was greylisted." << std::endl; for(int i=0;i<20;++i) { Query query; query.handler = new TestDnsHandler(expected,uri); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); } // .bwc. Wait for greylist to expire. sleep(16); { Query query; query.handler = new TestDnsHandler(expected,uri); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); } // .bwc. Give this query plenty of time. sleep(2); assert(listener->gotOkCallback()); listener->resetAll(); std::cout << "greylist on " << toGreylist << " has expired." << std::endl; for(int i=0;i<20;++i) { Query query; query.handler = new TestDnsHandler(expected,uri); query.uri = uri; cerr << "Creating DnsResult" << endl; DnsResult* res = dns.createDnsResult(query.handler); query.result = res; queries.push_back(query); cerr << rf << "Looking up" << ub << endl; dns.lookup(res, uri); } int count = queries.size(); while (count>0) { for (std::list<Query>::iterator it = queries.begin(); it != queries.end(); ) { if ((*it).handler->complete()) { cerr << rf << "DNS results for " << (*it).uri << ub << endl; for (std::vector<Tuple>::iterator i = (*it).handler->results.begin(); i != (*it).handler->results.end(); ++i) { cerr << rf << (*i) << ub << endl; } --count; (*it).result->destroy(); delete (*it).handler; std::list<Query>::iterator temp = it; ++it; queries.erase(temp); } else { ++it; } } sleep(1); } assert(queries.empty()); dns.getMarkManager().unregisterMarkListener(listener); delete listener; } dns.shutdown(); dns.join(); delete stub; return 0;}/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -