test: Add initial unit tests for c-ares library
The tests are written in C++11, using the GoogleTest and GoogleMock
frameworks. They have their own independent autoconf setup, so that
users of the library need not have a C++ compiler just to get c-ares
working (however, the test/configure.ac file does assume the use of
a shared top-level m4/ directory). However, this autoconf setup has
only been tested on Linux and OSX so far.
Run with "./arestest", or "./arestest -v" to see extra debug info.
The GoogleTest options for running specific tests are also
available (e.g. "./arestest --gtest_filter=*Live*").
The tests are nowhere near complete yet (currently hitting around
60% coverage as reported by gcov), but they do include examples
of a few different styles of testing:
- There are live tests (ares-test-live.cc), which assume that the
current machine has a valid DNS setup and connection to the
internet; these tests issue queries for real domains but don't
particularly check what gets returned. The tests will fail on
an offline machine.
- There a few mock tests (ares-test-mock.cc) that set up a fake DNS
server and inject its port into the c-ares library configuration.
These tests allow specific response messages to be crafted and
injected, and so are likely to be used for many more tests in
future.
- To make this generation/injection easier, the dns-proto.h file
includes C++ helper classes for building DNS packets.
- Other library entrypoints that don't require network activity
(e.g. ares_parse_*_reply) are tested directly.
- There are few tests of library-internal functions that are not
normally visible to API users (in ares-test-internal.cc).
- A couple of the tests use a helper method of the test fixture to
inject memory allocation failures, using the earlier change to the
library to allow override of malloc/realloc/free.
- There is also an entrypoint to allow Clang's libfuzzer to drive
the packet parsing code in ares_parse_*_reply, together with a
standalone wrapper for it (./aresfuzz) to allow use of afl-fuzz
for further fuzz testing.
10 years ago
|
|
|
#include "ares-test.h"
|
|
|
|
#include "dns-proto.h"
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using testing::InvokeWithoutArgs;
|
|
|
|
|
|
|
|
namespace ares {
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
TEST_F(MockChannelTest, Basic) {
|
|
|
|
std::vector<byte> reply = {
|
|
|
|
0x00, 0x00, // qid
|
|
|
|
0x84, // response + query + AA + not-TC + not-RD
|
|
|
|
0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
|
|
|
|
0x00, 0x01, // 1 question
|
|
|
|
0x00, 0x01, // 1 answer RRs
|
|
|
|
0x00, 0x00, // 0 authority RRs
|
|
|
|
0x00, 0x00, // 0 additional RRs
|
|
|
|
// Question
|
|
|
|
0x03, 'w', 'w', 'w',
|
|
|
|
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
|
|
|
|
0x03, 'c', 'o', 'm',
|
|
|
|
0x00,
|
|
|
|
0x00, 0x01, // type A
|
|
|
|
0x00, 0x01, // class IN
|
|
|
|
// Answer
|
|
|
|
0x03, 'w', 'w', 'w',
|
|
|
|
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
|
|
|
|
0x03, 'c', 'o', 'm',
|
|
|
|
0x00,
|
|
|
|
0x00, 0x01, // type A
|
|
|
|
0x00, 0x01, // class IN
|
|
|
|
0x00, 0x00, 0x01, 0x00, // TTL
|
|
|
|
0x00, 0x04, // rdata length
|
|
|
|
0x01, 0x02, 0x03, 0x04
|
|
|
|
};
|
|
|
|
|
|
|
|
EXPECT_CALL(server_, OnRequest("www.google.com", ns_t_a))
|
|
|
|
.WillOnce(SetReplyData(&server_, reply));
|
|
|
|
|
|
|
|
HostResult result;
|
|
|
|
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
|
|
|
|
Process();
|
|
|
|
EXPECT_TRUE(result.done_);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << result.host_;
|
|
|
|
EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MockChannelTest, SearchDomains) {
|
|
|
|
DNSPacket nofirst;
|
|
|
|
nofirst.set_response().set_aa().set_rcode(ns_r_nxdomain)
|
|
|
|
.add_question(new DNSQuestion("www.first.com", ns_t_a));
|
|
|
|
EXPECT_CALL(server_, OnRequest("www.first.com", ns_t_a))
|
|
|
|
.WillOnce(SetReply(&server_, &nofirst));
|
|
|
|
DNSPacket nosecond;
|
|
|
|
nosecond.set_response().set_aa().set_rcode(ns_r_nxdomain)
|
|
|
|
.add_question(new DNSQuestion("www.second.org", ns_t_a));
|
|
|
|
EXPECT_CALL(server_, OnRequest("www.second.org", ns_t_a))
|
|
|
|
.WillOnce(SetReply(&server_, &nosecond));
|
|
|
|
DNSPacket yesthird;
|
|
|
|
yesthird.set_response().set_aa()
|
|
|
|
.add_question(new DNSQuestion("www.third.gov", ns_t_a))
|
|
|
|
.add_answer(new DNSARR("www.third.gov", 0x0200, {2, 3, 4, 5}));
|
|
|
|
EXPECT_CALL(server_, OnRequest("www.third.gov", ns_t_a))
|
|
|
|
.WillOnce(SetReply(&server_, &yesthird));
|
|
|
|
|
|
|
|
HostResult result;
|
|
|
|
ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
|
|
|
|
Process();
|
|
|
|
EXPECT_TRUE(result.done_);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << result.host_;
|
|
|
|
EXPECT_EQ("{'www.third.gov' aliases=[] addrs=[2.3.4.5]}", ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MockChannelTest, SearchDomainsAllocFail) {
|
|
|
|
DNSPacket nofirst;
|
|
|
|
nofirst.set_response().set_aa().set_rcode(ns_r_nxdomain)
|
|
|
|
.add_question(new DNSQuestion("www.first.com", ns_t_a));
|
|
|
|
ON_CALL(server_, OnRequest("www.first.com", ns_t_a))
|
|
|
|
.WillByDefault(SetReply(&server_, &nofirst));
|
|
|
|
DNSPacket nosecond;
|
|
|
|
nosecond.set_response().set_aa().set_rcode(ns_r_nxdomain)
|
|
|
|
.add_question(new DNSQuestion("www.second.org", ns_t_a));
|
|
|
|
ON_CALL(server_, OnRequest("www.second.org", ns_t_a))
|
|
|
|
.WillByDefault(SetReply(&server_, &nosecond));
|
|
|
|
DNSPacket yesthird;
|
|
|
|
yesthird.set_response().set_aa()
|
|
|
|
.add_question(new DNSQuestion("www.third.gov", ns_t_a))
|
|
|
|
.add_answer(new DNSARR("www.third.gov", 0x0200, {2, 3, 4, 5}));
|
|
|
|
ON_CALL(server_, OnRequest("www.third.gov", ns_t_a))
|
|
|
|
.WillByDefault(SetReply(&server_, &yesthird));
|
|
|
|
|
|
|
|
// Fail a variety of different memory allocations, and confirm
|
|
|
|
// that the operation either fails with ENOMEM or succeeds
|
|
|
|
// with the expected result.
|
|
|
|
const int kCount = 34;
|
|
|
|
HostResult results[kCount];
|
|
|
|
for (int ii = 1; ii <= kCount; ii++) {
|
|
|
|
HostResult* result = &(results[ii - 1]);
|
|
|
|
ClearFails();
|
|
|
|
SetAllocFail(ii);
|
|
|
|
ares_gethostbyname(channel_, "www", AF_INET, HostCallback, result);
|
|
|
|
Process();
|
|
|
|
EXPECT_TRUE(result->done_);
|
|
|
|
if (result->status_ != ARES_ENOMEM) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << result->host_;
|
|
|
|
EXPECT_EQ("{'www.third.gov' aliases=[] addrs=[2.3.4.5]}", ss.str()) << " failed alloc #" << ii;
|
|
|
|
if (verbose) std::cerr << "Succeeded despite failure of alloc #" << ii << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly destroy the channel now, so that the HostResult objects
|
|
|
|
// are still valid (in case any pending work refers to them).
|
|
|
|
ares_destroy(channel_);
|
|
|
|
channel_ = nullptr;
|
|
|
|
}
|
|
|
|
|
test: Add initial unit tests for c-ares library
The tests are written in C++11, using the GoogleTest and GoogleMock
frameworks. They have their own independent autoconf setup, so that
users of the library need not have a C++ compiler just to get c-ares
working (however, the test/configure.ac file does assume the use of
a shared top-level m4/ directory). However, this autoconf setup has
only been tested on Linux and OSX so far.
Run with "./arestest", or "./arestest -v" to see extra debug info.
The GoogleTest options for running specific tests are also
available (e.g. "./arestest --gtest_filter=*Live*").
The tests are nowhere near complete yet (currently hitting around
60% coverage as reported by gcov), but they do include examples
of a few different styles of testing:
- There are live tests (ares-test-live.cc), which assume that the
current machine has a valid DNS setup and connection to the
internet; these tests issue queries for real domains but don't
particularly check what gets returned. The tests will fail on
an offline machine.
- There a few mock tests (ares-test-mock.cc) that set up a fake DNS
server and inject its port into the c-ares library configuration.
These tests allow specific response messages to be crafted and
injected, and so are likely to be used for many more tests in
future.
- To make this generation/injection easier, the dns-proto.h file
includes C++ helper classes for building DNS packets.
- Other library entrypoints that don't require network activity
(e.g. ares_parse_*_reply) are tested directly.
- There are few tests of library-internal functions that are not
normally visible to API users (in ares-test-internal.cc).
- A couple of the tests use a helper method of the test fixture to
inject memory allocation failures, using the earlier change to the
library to allow override of malloc/realloc/free.
- There is also an entrypoint to allow Clang's libfuzzer to drive
the packet parsing code in ares_parse_*_reply, together with a
standalone wrapper for it (./aresfuzz) to allow use of afl-fuzz
for further fuzz testing.
10 years ago
|
|
|
TEST_F(MockChannelTest, Resend) {
|
|
|
|
std::vector<byte> nothing;
|
|
|
|
DNSPacket reply;
|
|
|
|
reply.set_response().set_aa()
|
|
|
|
.add_question(new DNSQuestion("www.google.com", ns_t_a))
|
|
|
|
.add_answer(new DNSARR("www.google.com", 0x0100, {0x01, 0x02, 0x03, 0x04}));
|
|
|
|
|
|
|
|
EXPECT_CALL(server_, OnRequest("www.google.com", ns_t_a))
|
|
|
|
.WillOnce(SetReplyData(&server_, nothing))
|
|
|
|
.WillOnce(SetReplyData(&server_, nothing))
|
|
|
|
.WillOnce(SetReply(&server_, &reply));
|
|
|
|
|
|
|
|
HostResult result;
|
|
|
|
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
|
|
|
|
Process();
|
|
|
|
EXPECT_TRUE(result.done_);
|
|
|
|
EXPECT_EQ(2, result.timeouts_);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << result.host_;
|
|
|
|
EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MockChannelTest, CancelImmediate) {
|
|
|
|
HostResult result;
|
|
|
|
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
|
|
|
|
ares_cancel(channel_);
|
|
|
|
EXPECT_TRUE(result.done_);
|
|
|
|
EXPECT_EQ(ARES_ECANCELLED, result.status_);
|
|
|
|
EXPECT_EQ(0, result.timeouts_);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MockChannelTest, CancelLater) {
|
|
|
|
std::vector<byte> nothing;
|
|
|
|
|
|
|
|
// On second request, cancel the channel.
|
|
|
|
EXPECT_CALL(server_, OnRequest("www.google.com", ns_t_a))
|
|
|
|
.WillOnce(SetReplyData(&server_, nothing))
|
|
|
|
.WillOnce(CancelChannel(&server_, channel_));
|
|
|
|
|
|
|
|
HostResult result;
|
|
|
|
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
|
|
|
|
Process();
|
|
|
|
EXPECT_TRUE(result.done_);
|
|
|
|
EXPECT_EQ(ARES_ECANCELLED, result.status_);
|
|
|
|
EXPECT_EQ(0, result.timeouts_);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MockChannelTest, Destroy) {
|
|
|
|
HostResult result;
|
|
|
|
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
|
|
|
|
ares_destroy(channel_);
|
|
|
|
channel_ = nullptr;
|
|
|
|
EXPECT_TRUE(result.done_);
|
|
|
|
EXPECT_EQ(ARES_EDESTRUCTION, result.status_);
|
|
|
|
EXPECT_EQ(0, result.timeouts_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
} // namespace ares
|