1
0
mirror of https://frontier.innolan.net/rainlance/c-ares.git synced 2025-12-06 18:35:17 +00:00
Files
c-ares/test/ares-test-internal.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

57 lines
1.5 KiB
C++

#include "ares-test.h"
#include "dns-proto.h"
extern "C" {
#include "ares_nowarn.h"
#include "ares_inet_net_pton.h"
#include "bitncmp.h"
}
#include <string>
#include <vector>
namespace ares {
namespace test {
TEST_F(LibraryTest, InetPtoN) {
struct in_addr a4;
struct in6_addr a6;
#ifdef DISABLED
EXPECT_EQ(1, ares_inet_net_pton(AF_INET, "1.2.3.4", &a4, sizeof(a4)));
EXPECT_EQ(1, ares_inet_net_pton(AF_INET6, "12:34::ff", &a6, sizeof(a6)));
EXPECT_EQ(1, ares_inet_net_pton(AF_INET6, "12:34::ffff:1.2.3.4", &a6, sizeof(a6)));
EXPECT_EQ(0, ares_inet_net_pton(AF_INET, "xyzzy", &a4, sizeof(a4)));
EXPECT_EQ(-1, ares_inet_net_pton(AF_INET+AF_INET6, "1.2.3.4", &a4, sizeof(a4)));
#endif
EXPECT_EQ(1, ares_inet_pton(AF_INET, "1.2.3.4", &a4));
EXPECT_EQ(1, ares_inet_pton(AF_INET6, "12:34::ff", &a6));
EXPECT_EQ(1, ares_inet_pton(AF_INET6, "12:34::ffff:1.2.3.4", &a6));
EXPECT_EQ(0, ares_inet_pton(AF_INET, "xyzzy", &a4));
EXPECT_EQ(-1, ares_inet_pton(AF_INET+AF_INET6, "1.2.3.4", &a4));
}
#ifdef DISABLED
TEST(Misc, Bitncmp) {
byte a[4] = {0x80, 0x01, 0x02, 0x03};
byte b[4] = {0x80, 0x01, 0x02, 0x04};
EXPECT_EQ(-1, ares__bitncmp(a, b, sizeof(a)*8));
EXPECT_EQ(1, ares__bitncmp(b, a, sizeof(a)*8));
EXPECT_EQ(0, ares__bitncmp(a, a, sizeof(a)*8));
}
TEST_F(LibraryTest, Casts) {
ssize_t ssz = 100;
unsigned int u = 100;
int i = 100;
unsigned int ru = aresx_sztoui(ssz);
EXPECT_EQ(u, ru);
int ri = aresx_sztosi(ssz);
EXPECT_EQ(i, ri);
}
#endif
} // namespace test
} // namespace ares