1
0
mirror of https://frontier.innolan.net/rainlance/c-ares.git synced 2025-10-06 02:59:37 +00:00
Files
c-ares/test/ares-test-fuzz.cc
David Drysdale af3ee9a8ba 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.
2016-02-02 10:13:46 +00:00

45 lines
1.3 KiB
C++

#include "ares-test.h"
#include <vector>
// Entrypoint for Clang's libfuzzer
extern "C" void LLVMFuzzerTestOneInput(const unsigned char *data,
unsigned long size) {
// Feed the data into each of the ares_parse_*_reply functions.
struct hostent *host = nullptr;
struct ares_addrttl info[5];
int count = 5;
ares_parse_a_reply(data, size, &host, info, &count);
if (host) ares_free_hostent(host);
host = nullptr;
struct ares_addr6ttl info6[5];
count = 5;
ares_parse_aaaa_reply(data, size, &host, info6, &count);
if (host) ares_free_hostent(host);
host = nullptr;
ares::byte addrv4[4] = {0x10, 0x20, 0x30, 0x40};
ares_parse_ptr_reply(data, size, addrv4, sizeof(addrv4), AF_INET, &host);
if (host) ares_free_hostent(host);
host = nullptr;
ares_parse_ns_reply(data, size, &host);
if (host) ares_free_hostent(host);
struct ares_srv_reply* srv = nullptr;
ares_parse_srv_reply(data, size, &srv);
if (srv) ares_free_data(srv);
struct ares_mx_reply* mx = nullptr;
ares_parse_mx_reply(data, size, &mx);
if (mx) ares_free_data(mx);
struct ares_txt_reply* txt = nullptr;
ares_parse_txt_reply(data, size, &txt);
if (txt) ares_free_data(txt);
struct ares_soa_reply* soa = nullptr;
ares_parse_soa_reply(data, size, &soa);
if (soa) ares_free_data(soa);
}