mirror of
https://frontier.innolan.net/rainlance/c-ares.git
synced 2025-12-06 13:50:30 +00:00
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.
132 lines
3.7 KiB
C++
132 lines
3.7 KiB
C++
#include "ares-test.h"
|
|
#include "dns-proto.h"
|
|
|
|
#include <vector>
|
|
|
|
namespace ares {
|
|
namespace test {
|
|
|
|
TEST(DNSProto, EncodeQuestions) {
|
|
DNSPacket pkt;
|
|
pkt.set_qid(0x1234).set_response().set_aa()
|
|
.add_question(new DNSQuestion("example.com.", ns_t_a))
|
|
.add_question(new DNSQuestion("www.example.com", ns_t_aaaa, ns_c_chaos));
|
|
|
|
std::vector<byte> data = {
|
|
0x12, 0x34, // qid
|
|
0x84, // response + query + AA + not-TC + not-RD
|
|
0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
|
|
0x00, 0x02, // num questions
|
|
0x00, 0x00, // num answer RRs
|
|
0x00, 0x00, // num authority RRs
|
|
0x00, 0x00, // num additional RRs
|
|
// Question 1
|
|
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
0x00, 0x01, // type A
|
|
0x00, 0x01, // class IN
|
|
// Question 2
|
|
0x03, 'w', 'w', 'w',
|
|
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
0x00, 0x1C, // type AAAA = 28
|
|
0x00, 0x03, // class CHAOS = 3
|
|
};
|
|
EXPECT_EQ(data, pkt.data());
|
|
}
|
|
|
|
TEST(DNSProto, EncodeSingleNameAnswers) {
|
|
DNSPacket pkt;
|
|
pkt.qid_ = 0x1234;
|
|
pkt.response_ = true;
|
|
pkt.aa_ = true;
|
|
pkt.opcode_ = ns_o_query;
|
|
pkt.add_answer(new DNSCnameRR("example.com", 0x01020304, "other.com."));
|
|
pkt.add_auth(new DNSPtrRR("www.example.com", 0x01020304, "www.other.com"));
|
|
|
|
std::vector<byte> data = {
|
|
0x12, 0x34, // qid
|
|
0x84, // response + query + AA + not-TC + not-RD
|
|
0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
|
|
0x00, 0x00, // num questions
|
|
0x00, 0x01, // num answer RRs
|
|
0x00, 0x01, // num authority RRs
|
|
0x00, 0x00, // num additional RRs
|
|
// Answer 1
|
|
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
0x00, 0x05, // RR type
|
|
0x00, 0x01, // class IN
|
|
0x01, 0x02, 0x03, 0x04, // TTL
|
|
0x00, 0x0B, // rdata length
|
|
0x05, 'o', 't', 'h', 'e', 'r',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
// Authority 1
|
|
0x03, 'w', 'w', 'w',
|
|
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
0x00, 0x0c, // RR type
|
|
0x00, 0x01, // class IN
|
|
0x01, 0x02, 0x03, 0x04, // TTL
|
|
0x00, 0x0F, // rdata length
|
|
0x03, 'w', 'w', 'w',
|
|
0x05, 'o', 't', 'h', 'e', 'r',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
};
|
|
EXPECT_EQ(data, pkt.data());
|
|
}
|
|
|
|
TEST(DNSProto, EncodeAddressAnswers) {
|
|
DNSPacket pkt;
|
|
pkt.qid_ = 0x1234;
|
|
pkt.response_ = true;
|
|
pkt.aa_ = true;
|
|
pkt.opcode_ = ns_o_query;
|
|
std::vector<byte> addrv4 = {0x02, 0x03, 0x04, 0x05};
|
|
pkt.add_answer(new DNSARR("example.com", 0x01020304, addrv4));
|
|
byte addrv6[16] = {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
|
|
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04};
|
|
pkt.add_additional(new DNSAaaaRR("www.example.com", 0x01020304, addrv6, 16));
|
|
|
|
std::vector<byte> data = {
|
|
0x12, 0x34, // qid
|
|
0x84, // response + query + AA + not-TC + not-RD
|
|
0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
|
|
0x00, 0x00, // num questions
|
|
0x00, 0x01, // num answer RRs
|
|
0x00, 0x00, // num authority RRs
|
|
0x00, 0x01, // num additional RRs
|
|
// Answer 1
|
|
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
0x00, 0x01, // RR type
|
|
0x00, 0x01, // class IN
|
|
0x01, 0x02, 0x03, 0x04, // TTL
|
|
0x00, 0x04, // rdata length
|
|
0x02, 0x03, 0x04, 0x05,
|
|
// Additional 1
|
|
0x03, 'w', 'w', 'w',
|
|
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
|
|
0x03, 'c', 'o', 'm',
|
|
0x00,
|
|
0x00, 0x1c, // RR type
|
|
0x00, 0x01, // class IN
|
|
0x01, 0x02, 0x03, 0x04, // TTL
|
|
0x00, 0x10, // rdata length
|
|
0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
|
|
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04
|
|
};
|
|
EXPECT_EQ(data, pkt.data());
|
|
}
|
|
|
|
|
|
} // namespace test
|
|
} // namespace ares
|