Commit 182fdca6 authored by JINMEI Tatuya's avatar JINMEI Tatuya

made lib/testutils clang++-friendly:

 - replaced the ugly macro with real methods the base class using the template method pattern.  as a result srv_unittest.h is not needed anymore and removed.
 - avoided the use of an unnamed namespace in srv_test.h (another bad practice anyway)
 - avoided the use of 'using namespace' in srv_test.h (yet another bad practice anyway)


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac448@4012 e5f2f494-b856-4b98-b285-d166d9295462
parent 9d0b49c7
......@@ -577,6 +577,8 @@ AC_CONFIG_FILES([Makefile
src/lib/Makefile
src/lib/asiolink/Makefile
src/lib/asiolink/tests/Makefile
src/lib/asiolink/internal/Makefile
src/lib/asiolink/internal/tests/Makefile
src/lib/bench/Makefile
src/lib/bench/example/Makefile
src/lib/bench/tests/Makefile
......
......@@ -38,6 +38,7 @@ run_unittests_LDADD += $(top_builddir)/src/lib/config/libcfgclient.la
run_unittests_LDADD += $(top_builddir)/src/lib/cc/libcc.la
run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
run_unittests_LDADD += $(top_builddir)/src/lib/xfr/libxfr.la
run_unittests_LDADD += $(top_builddir)/src/lib/testutils/libtestutils.la
endif
noinst_PROGRAMS = $(TESTS)
......@@ -17,13 +17,16 @@
#include <config.h>
#include <datasrc/memory_datasrc.h>
#include <auth/auth_srv.h>
#include <testutils/srv_unittest.h>
#include <dns/tests/unittest_util.h>
#include <testutils/srv_test.h>
using namespace isc::cc;
using namespace isc::dns;
using namespace isc::data;
using namespace isc::xfr;
using namespace asiolink;
using namespace isc::testutils;
using isc::UnitTestUtil;
namespace {
......@@ -39,6 +42,10 @@ protected:
AuthSrvTest() : server(true, xfrout), rrclass(RRClass::IN()) {
server.setXfrinSession(&notify_session);
}
virtual void processMessage() {
server.processMessage(*io_message, parse_message, response_obuffer,
&dnsserv);
}
MockXfroutClient xfrout;
AuthSrv server;
const RRClass rrclass;
......@@ -46,48 +53,52 @@ protected:
// Unsupported requests. Should result in NOTIMP.
TEST_F(AuthSrvTest, unsupportedRequest) {
UNSUPPORTED_REQUEST_TEST;
unsupportedRequest();
}
// Simple API check
TEST_F(AuthSrvTest, verbose) {
VERBOSE_TEST;
EXPECT_FALSE(server.getVerbose());
server.setVerbose(true);
EXPECT_TRUE(server.getVerbose());
server.setVerbose(false);
EXPECT_FALSE(server.getVerbose());
}
// Multiple questions. Should result in FORMERR.
TEST_F(AuthSrvTest, multiQuestion) {
MULTI_QUESTION_TEST;
multiQuestion();
}
// Incoming data doesn't even contain the complete header. Must be silently
// dropped.
TEST_F(AuthSrvTest, shortMessage) {
SHORT_MESSAGE_TEST;
shortMessage();
}
// Response messages. Must be silently dropped, whether it's a valid response
// or malformed or could otherwise cause a protocol error.
TEST_F(AuthSrvTest, response) {
RESPONSE_TEST;
response();
}
// Query with a broken question
TEST_F(AuthSrvTest, shortQuestion) {
SHORT_QUESTION_TEST;
shortQuestion();
}
// Query with a broken answer section
TEST_F(AuthSrvTest, shortAnswer) {
SHORT_ANSWER_TEST;
shortAnswer();
}
// Query with unsupported version of EDNS.
TEST_F(AuthSrvTest, ednsBadVers) {
EDNS_BADVERS_TEST;
ednsBadVers();
}
TEST_F(AuthSrvTest, AXFROverUDP) {
AXFR_OVER_UDP_TEST;
axfrOverUDP();
}
TEST_F(AuthSrvTest, AXFRSuccess) {
......
......@@ -33,6 +33,7 @@ run_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libasiolink.la
run_unittests_LDADD += $(top_builddir)/src/lib/config/libcfgclient.la
run_unittests_LDADD += $(top_builddir)/src/lib/cc/libcc.la
run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
run_unittests_LDADD += $(top_builddir)/src/lib/testutils/libtestutils.la
run_unittests_LDADD += $(top_builddir)/src/lib/xfr/libxfr.la
run_unittests_LDADD += $(top_builddir)/src/lib/log/liblog.la
endif
......
......@@ -12,10 +12,24 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// $Id$
#include <string>
#include <gtest/gtest.h>
#include <cc/data.h>
#include <asiolink/asiolink.h>
#include <recurse/recursor.h>
#include <testutils/srv_unittest.h>
#include <dns/tests/unittest_util.h>
#include <testutils/srv_test.h>
using namespace std;
using namespace isc::data;
using namespace isc::testutils;
using namespace asiolink;
using isc::UnitTestUtil;
namespace {
class RecursorConfig : public ::testing::Test {
......
......@@ -12,10 +12,15 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// $Id$
#include <dns/name.h>
#include <recurse/recursor.h>
#include <testutils/srv_unittest.h>
#include <dns/tests/unittest_util.h>
#include <testutils/srv_test.h>
using namespace isc::dns;
using namespace isc::testutils;
using isc::UnitTestUtil;
namespace {
const char* const TEST_PORT = "53535";
......@@ -23,48 +28,52 @@ const char* const TEST_PORT = "53535";
class RecursorTest : public SrvTestBase{
protected:
RecursorTest() : server(){}
virtual void processMessage() {
server.processMessage(*io_message, parse_message, response_obuffer,
&dnsserv);
}
Recursor server;
};
// Unsupported requests. Should result in NOTIMP.
TEST_F(RecursorTest, unsupportedRequest) {
UNSUPPORTED_REQUEST_TEST;
unsupportedRequest();
}
// Multiple questions. Should result in FORMERR.
TEST_F(RecursorTest, multiQuestion) {
MULTI_QUESTION_TEST;
multiQuestion();
}
// Incoming data doesn't even contain the complete header. Must be silently
// dropped.
TEST_F(RecursorTest, shortMessage) {
SHORT_MESSAGE_TEST;
shortMessage();
}
// Response messages. Must be silently dropped, whether it's a valid response
// or malformed or could otherwise cause a protocol error.
TEST_F(RecursorTest, response) {
RESPONSE_TEST;
response();
}
// Query with a broken question
TEST_F(RecursorTest, shortQuestion) {
SHORT_QUESTION_TEST;
shortQuestion();
}
// Query with a broken answer section
TEST_F(RecursorTest, shortAnswer) {
SHORT_ANSWER_TEST;
shortAnswer();
}
// Query with unsupported version of EDNS.
TEST_F(RecursorTest, ednsBadVers) {
EDNS_BADVERS_TEST;
ednsBadVers();
}
TEST_F(RecursorTest, AXFROverUDP) {
AXFR_OVER_UDP_TEST;
axfrOverUDP();
}
TEST_F(RecursorTest, AXFRFail) {
......
SUBDIRS = testdata
SUBDIRS = . testdata
EXTRA_DIST = srv_test.h
EXTRA_DIST += srv_unittest.h
EXTRA_DIST += mockups.h
AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
AM_CPPFLAGS += $(BOOST_INCLUDES)
AM_CXXFLAGS=$(B10_CXXFLAGS)
lib_LTLIBRARIES = libtestutils.la
libtestutils_la_SOURCES = srv_test.h srv_test.cc
libtestutils_la_SOURCES += srv_unittest.h
libtestutils_la_SOURCES += mockups.h
Here is some code used by more than one test. No code is used for bind10
itself, only for testing.
As it contains headers only currently, it does not compile here.
// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <netinet/in.h>
#include <dns/message.h>
#include <dns/rcode.h>
#include <asiolink/asiolink.h>
#include <dns/tests/unittest_util.h>
#include <testutils/srv_test.h>
using namespace isc::dns;
using namespace asiolink;
namespace isc {
namespace testutils {
const char* const DEFAULT_REMOTE_ADDRESS = "192.0.2.1";
const unsigned int QR_FLAG = 0x1;
const unsigned int AA_FLAG = 0x2;
const unsigned int TC_FLAG = 0x4;
const unsigned int RD_FLAG = 0x8;
const unsigned int RA_FLAG = 0x10;
const unsigned int AD_FLAG = 0x20;
const unsigned int CD_FLAG = 0x40;
SrvTestBase::SrvTestBase() : request_message(Message::RENDER),
parse_message(new Message(Message::PARSE)),
default_qid(0x1035),
opcode(Opcode(Opcode::QUERY())),
qname("www.example.com"),
qclass(RRClass::IN()),
qtype(RRType::A()), io_sock(NULL),
io_message(NULL), endpoint(NULL),
request_obuffer(0),
request_renderer(request_obuffer),
response_obuffer(new OutputBuffer(0))
{}
SrvTestBase::~SrvTestBase() {
delete io_message;
delete endpoint;
}
void
SrvTestBase::createDataFromFile(const char* const datafile,
const int protocol)
{
delete io_message;
data.clear();
delete endpoint;
endpoint = IOEndpoint::create(protocol,
IOAddress(DEFAULT_REMOTE_ADDRESS), 5300);
UnitTestUtil::readWireData(datafile, data);
io_sock = (protocol == IPPROTO_UDP) ? &IOSocket::getDummyUDPSocket() :
&IOSocket::getDummyTCPSocket();
io_message = new IOMessage(&data[0], data.size(), *io_sock, *endpoint);
}
void
SrvTestBase::createRequestPacket(Message& message,
const int protocol)
{
message.toWire(request_renderer);
delete io_message;
endpoint = IOEndpoint::create(protocol,
IOAddress(DEFAULT_REMOTE_ADDRESS), 5300);
io_sock = (protocol == IPPROTO_UDP) ? &IOSocket::getDummyUDPSocket() :
&IOSocket::getDummyTCPSocket();
io_message = new IOMessage(request_renderer.getData(),
request_renderer.getLength(),
*io_sock, *endpoint);
}
// Unsupported requests. Should result in NOTIMP.
void
SrvTestBase::unsupportedRequest() {
for (unsigned int i = 0; i < 16; ++i) {
// set Opcode to 'i', which iterators over all possible codes except
// the standard query and notify
if (i == isc::dns::Opcode::QUERY().getCode() ||
i == isc::dns::Opcode::NOTIFY().getCode()) {
continue;
}
createDataFromFile("simplequery_fromWire.wire");
data[2] = ((i << 3) & 0xff);
parse_message->clear(isc::dns::Message::PARSE);
processMessage();
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, isc::dns::Rcode::NOTIMP(), i,
QR_FLAG, 0, 0, 0, 0);
}
}
// Multiple questions. Should result in FORMERR.
void
SrvTestBase::multiQuestion() {
createDataFromFile("multiquestion_fromWire.wire");
processMessage();
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, isc::dns::Rcode::FORMERR(),
opcode.getCode(), QR_FLAG, 2, 0, 0, 0);
isc::dns::QuestionIterator qit = parse_message->beginQuestion();
EXPECT_EQ(isc::dns::Name("example.com"), (*qit)->getName());
EXPECT_EQ(isc::dns::RRClass::IN(), (*qit)->getClass());
EXPECT_EQ(isc::dns::RRType::A(), (*qit)->getType());
++qit;
EXPECT_EQ(isc::dns::Name("example.com"), (*qit)->getName());
EXPECT_EQ(isc::dns::RRClass::IN(), (*qit)->getClass());
EXPECT_EQ(isc::dns::RRType::AAAA(), (*qit)->getType());
++qit;
EXPECT_TRUE(qit == parse_message->endQuestion());
}
// Incoming data doesn't even contain the complete header. Must be silently
// dropped.
void
SrvTestBase::shortMessage() {
createDataFromFile("shortmessage_fromWire");
processMessage();
EXPECT_FALSE(dnsserv.hasAnswer());
}
// Response messages. Must be silently dropped, whether it's a valid response
// or malformed or could otherwise cause a protocol error.
void
SrvTestBase::response() {
// A valid (although unusual) response
createDataFromFile("simpleresponse_fromWire.wire");
processMessage();
EXPECT_FALSE(dnsserv.hasAnswer());
// A response with a broken question section. must be dropped rather than
//returning FORMERR.
createDataFromFile("shortresponse_fromWire");
processMessage();
EXPECT_FALSE(dnsserv.hasAnswer());
// A response to iquery. must be dropped rather than returning NOTIMP.
createDataFromFile("iqueryresponse_fromWire.wire");
processMessage();
EXPECT_FALSE(dnsserv.hasAnswer());
}
// Query with a broken question
void
SrvTestBase::shortQuestion() {
createDataFromFile("shortquestion_fromWire");
processMessage();
EXPECT_TRUE(dnsserv.hasAnswer());
// Since the query's question is broken, the question section of the
// response should be empty.
headerCheck(*parse_message, default_qid, isc::dns::Rcode::FORMERR(),
opcode.getCode(), QR_FLAG, 0, 0, 0, 0);
}
// Query with a broken answer section
void
SrvTestBase::shortAnswer() {
createDataFromFile("shortanswer_fromWire.wire");
processMessage();
EXPECT_TRUE(dnsserv.hasAnswer());
// This is a bogus query, but question section is valid. So the response
// should copy the question section.
headerCheck(*parse_message, default_qid, isc::dns::Rcode::FORMERR(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
isc::dns::QuestionIterator qit = parse_message->beginQuestion();
EXPECT_EQ(isc::dns::Name("example.com"), (*qit)->getName());
EXPECT_EQ(isc::dns::RRClass::IN(), (*qit)->getClass());
EXPECT_EQ(isc::dns::RRType::A(), (*qit)->getType());
++qit;
EXPECT_TRUE(qit == parse_message->endQuestion());
}
// Query with unsupported version of EDNS.
void
SrvTestBase::ednsBadVers() {
createDataFromFile("queryBadEDNS_fromWire.wire");
processMessage();
EXPECT_TRUE(dnsserv.hasAnswer());
// The response must have an EDNS OPT RR in the additional section,
// it will be added automatically at the render time.
// Note that the DNSSEC DO bit is cleared even if this bit in the query
// is set. This is a limitation of the current implementation.
headerCheck(*parse_message, default_qid, isc::dns::Rcode::BADVERS(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 1);
EXPECT_FALSE(parse_message->getEDNS()); // EDNS isn't added at this point
isc::dns::InputBuffer ib(response_obuffer->getData(),
response_obuffer->getLength());
isc::dns::Message parsed(isc::dns::Message::PARSE);
parsed.fromWire(ib);
EXPECT_EQ(isc::dns::Rcode::BADVERS(), parsed.getRcode());
isc::dns::ConstEDNSPtr edns(parsed.getEDNS());
ASSERT_TRUE(edns);
EXPECT_FALSE(edns->getDNSSECAwareness());
}
void
SrvTestBase::axfrOverUDP() {
// AXFR over UDP is invalid and should result in FORMERR.
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
isc::dns::Name("example.com"),
isc::dns::RRClass::IN(),
isc::dns::RRType::AXFR());
createRequestPacket(request_message, IPPROTO_UDP);
processMessage();
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, isc::dns::Rcode::FORMERR(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
}
void
headerCheck(const Message& message, const qid_t qid, const Rcode& rcode,
const uint16_t opcodeval, const unsigned int flags,
const unsigned int qdcount,
const unsigned int ancount, const unsigned int nscount,
const unsigned int arcount)
{
EXPECT_EQ(qid, message.getQid());
EXPECT_EQ(rcode, message.getRcode());
EXPECT_EQ(opcodeval, message.getOpcode().getCode());
EXPECT_EQ((flags & QR_FLAG) != 0,
message.getHeaderFlag(Message::HEADERFLAG_QR));
EXPECT_EQ((flags & AA_FLAG) != 0,
message.getHeaderFlag(Message::HEADERFLAG_AA));
EXPECT_EQ((flags & TC_FLAG) != 0,
message.getHeaderFlag(Message::HEADERFLAG_TC));
EXPECT_EQ((flags & RA_FLAG) != 0,
message.getHeaderFlag(Message::HEADERFLAG_RA));
EXPECT_EQ((flags & RD_FLAG) != 0,
message.getHeaderFlag(Message::HEADERFLAG_RD));
EXPECT_EQ((flags & AD_FLAG) != 0,
message.getHeaderFlag(Message::HEADERFLAG_AD));
EXPECT_EQ((flags & CD_FLAG) != 0,
message.getHeaderFlag(Message::HEADERFLAG_CD));
EXPECT_EQ(qdcount, message.getRRCount(Message::SECTION_QUESTION));
EXPECT_EQ(ancount, message.getRRCount(Message::SECTION_ANSWER));
EXPECT_EQ(nscount, message.getRRCount(Message::SECTION_AUTHORITY));
EXPECT_EQ(arcount, message.getRRCount(Message::SECTION_ADDITIONAL));
}
} // end of namespace testutils
} // end of namespace isc
// Local Variables:
// mode: c++
// End:
......@@ -12,10 +12,6 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// $Id: auth_srv_unittest.cc 3310 2010-10-21 23:10:24Z each $
#include <config.h>
#include <gtest/gtest.h>
#include <dns/buffer.h>
......@@ -27,139 +23,99 @@
#include <dns/rrclass.h>
#include <dns/rrtype.h>
#include <cc/data.h>
#include <cc/session.h>
#include <xfr/xfrout_client.h>
#include <auth/auth_srv.h>
#include <asiolink/asiolink.h>
#include <dns/tests/unittest_util.h>
#include "mockups.h"
using namespace std;
using namespace isc::cc;
using namespace isc::dns;
using namespace isc::data;
using namespace isc::xfr;
using namespace asiolink;
using isc::UnitTestUtil;
namespace {
const char* const DEFAULT_REMOTE_ADDRESS = "192.0.2.1";
// The base class for Auth and Recurse test case
class SrvTestBase : public ::testing::Test {
protected:
SrvTestBase() : request_message(Message::RENDER),
parse_message(new Message(Message::PARSE)),
default_qid(0x1035), opcode(Opcode(Opcode::QUERY())),
qname("www.example.com"), qclass(RRClass::IN()),
qtype(RRType::A()), io_sock(NULL),
io_message(NULL), endpoint(NULL),
request_obuffer(0), request_renderer(request_obuffer),
response_obuffer(new OutputBuffer(0))
{}
~SrvTestBase() {
delete io_message;
delete endpoint;
}
MockSession notify_session;
MockServer dnsserv;
Message request_message;
MessagePtr parse_message;
const qid_t default_qid;
const Opcode opcode;
const Name qname;
const RRClass qclass;
const RRType qtype;
IOSocket* io_sock;
IOMessage* io_message;
const IOEndpoint* endpoint;
OutputBuffer request_obuffer;
MessageRenderer request_renderer;
OutputBufferPtr response_obuffer;
vector<uint8_t> data;
void createDataFromFile(const char* const datafile, int protocol);
void createRequestPacket(Message& message, const int protocol);
};
void
SrvTestBase::createDataFromFile(const char* const datafile,
const int protocol = IPPROTO_UDP)
{
delete io_message;
data.clear();
delete endpoint;
endpoint = IOEndpoint::create(protocol,
IOAddress(DEFAULT_REMOTE_ADDRESS), 5300);
UnitTestUtil::readWireData(datafile, data);
io_sock = (protocol == IPPROTO_UDP) ? &IOSocket::getDummyUDPSocket() :
&IOSocket::getDummyTCPSocket();
io_message = new IOMessage(&data[0], data.size(), *io_sock, *endpoint);
namespace asiolink {
class IOSocket;
class IOMessage;
class IOEndpoint;
}
void
SrvTestBase::createRequestPacket(Message& message,