Skip to content

Commit

Permalink
move getsock and sock override tests to non-live
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh352 committed Jan 3, 2025
1 parent 5b1e796 commit ab8d937
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 118 deletions.
116 changes: 0 additions & 116 deletions test/ares-test-live.cc
Original file line number Diff line number Diff line change
Expand Up @@ -669,123 +669,7 @@ VIRT_NONVIRT_TEST_F(DefaultChannelTest, LiveGetNameInfoAllocFail) {
EXPECT_EQ(ARES_ENOMEM, result.status_);
}

VIRT_NONVIRT_TEST_F(DefaultChannelTest, GetSock) {
ares_socket_t socks[3] = {ARES_SOCKET_BAD, ARES_SOCKET_BAD, ARES_SOCKET_BAD};
int bitmask;

EXPECT_EQ(ARES_SUCCESS, (ares_status_t)ares_set_servers_csv(channel_, "127.0.0.1"));

bitmask = ares_getsock(channel_, socks, 3);
EXPECT_EQ(0, bitmask);
bitmask = ares_getsock(channel_, nullptr, 0);
EXPECT_EQ(0, bitmask);

// Ask again with a pending query.
HostResult result;
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
bitmask = ares_getsock(channel_, socks, 3);
EXPECT_NE(0, bitmask);

size_t sock_cnt = 0;
for (size_t i=0; i<3; i++) {
if (ARES_GETSOCK_READABLE(bitmask, i) || ARES_GETSOCK_WRITABLE(bitmask, i)) {
EXPECT_NE(ARES_SOCKET_BAD, socks[i]);
if (socks[i] != ARES_SOCKET_BAD)
sock_cnt++;
}
}
EXPECT_NE((size_t)0, sock_cnt);

bitmask = ares_getsock(channel_, nullptr, 0);
EXPECT_EQ(0, bitmask);

Process();
}

TEST_F(LibraryTest, GetTCPSock) {
ares_channel_t *channel;
struct ares_options opts;
memset(&opts, 0, sizeof(opts));
opts.tcp_port = 53;
opts.flags = ARES_FLAG_USEVC;
int optmask = ARES_OPT_TCP_PORT | ARES_OPT_FLAGS;
EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask));
EXPECT_NE(nullptr, channel);

EXPECT_EQ(ARES_SUCCESS, (ares_status_t)ares_set_servers_csv(channel, "127.0.0.1"));

ares_socket_t socks[3] = {ARES_SOCKET_BAD, ARES_SOCKET_BAD, ARES_SOCKET_BAD};
int bitmask = ares_getsock(channel, socks, 3);
EXPECT_EQ(0, bitmask);
bitmask = ares_getsock(channel, nullptr, 0);
EXPECT_EQ(0, bitmask);

// Ask again with a pending query.
HostResult result;
ares_gethostbyname(channel, "www.google.com.", AF_INET, HostCallback, &result);
bitmask = ares_getsock(channel, socks, 3);
EXPECT_NE(0, bitmask);

size_t sock_cnt = 0;
for (size_t i=0; i<3; i++) {
if (ARES_GETSOCK_READABLE(bitmask, i) || ARES_GETSOCK_WRITABLE(bitmask, i)) {
EXPECT_NE(ARES_SOCKET_BAD, socks[i]);
if (socks[i] != ARES_SOCKET_BAD)
sock_cnt++;
}
}
EXPECT_NE((size_t)0, sock_cnt);

bitmask = ares_getsock(channel, nullptr, 0);
EXPECT_EQ(0, bitmask);

ProcessWork(channel, NoExtraFDs, nullptr);

ares_destroy(channel);
}

TEST_F(DefaultChannelTest, VerifySocketFunctionCallback) {
VirtualizeIO vio(channel_);

auto my_functions = VirtualizeIO::default_functions;
size_t count = 0;

EXPECT_EQ(ARES_SUCCESS, (ares_status_t)ares_set_servers_csv(channel_, "127.0.0.1"));

my_functions.asocket = [](int af, int type, int protocol, void * p) -> ares_socket_t {
EXPECT_NE(nullptr, p);
(*reinterpret_cast<size_t *>(p))++;
return ::socket(af, type, protocol);
};

ares_set_socket_functions(channel_, &my_functions, &count);

{
count = 0;
HostResult result;
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
Process();

EXPECT_TRUE(result.done_);
EXPECT_NE((size_t)0, count);
}

{
count = 0;
ares_channel_t *copy;
EXPECT_EQ(ARES_SUCCESS, ares_dup(&copy, channel_));

HostResult result;
ares_gethostbyname(copy, "www.google.com.", AF_INET, HostCallback, &result);

ProcessWork(copy, NoExtraFDs, nullptr);

EXPECT_TRUE(result.done_);
ares_destroy(copy);
EXPECT_NE((size_t)0, count);
}

}

TEST_F(DefaultChannelTest, LiveSetServers) {
struct ares_addr_node server1;
Expand Down
125 changes: 125 additions & 0 deletions test/ares-test-mock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,131 @@ TEST_P(MockChannelTest, GetHostByAddrDestroy) {
EXPECT_EQ(0, result.timeouts_);
}

TEST_P(MockUDPChannelTest, GetSock) {
DNSPacket reply;
reply.set_response().set_aa()
.add_question(new DNSQuestion("www.google.com", T_A))
.add_answer(new DNSARR("www.google.com", 0x0100, {0x01, 0x02, 0x03, 0x04}));
ON_CALL(server_, OnRequest("www.google.com", T_A))
.WillByDefault(SetReply(&server_, &reply));

ares_socket_t socks[3] = {ARES_SOCKET_BAD, ARES_SOCKET_BAD, ARES_SOCKET_BAD};
int bitmask;

bitmask = ares_getsock(channel_, socks, 3);
EXPECT_EQ(0, bitmask);
bitmask = ares_getsock(channel_, nullptr, 0);
EXPECT_EQ(0, bitmask);

// Ask again with a pending query.
HostResult result;
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
bitmask = ares_getsock(channel_, socks, 3);
EXPECT_NE(0, bitmask);

size_t sock_cnt = 0;
for (size_t i=0; i<3; i++) {
if (ARES_GETSOCK_READABLE(bitmask, i) || ARES_GETSOCK_WRITABLE(bitmask, i)) {
EXPECT_NE(ARES_SOCKET_BAD, socks[i]);
if (socks[i] != ARES_SOCKET_BAD)
sock_cnt++;
}
}
EXPECT_NE((size_t)0, sock_cnt);

bitmask = ares_getsock(channel_, nullptr, 0);
EXPECT_EQ(0, bitmask);
}

TEST_P(MockTCPChannelTest, GetSock) {
DNSPacket reply;
reply.set_response().set_aa()
.add_question(new DNSQuestion("www.google.com", T_A))
.add_answer(new DNSARR("www.google.com", 0x0100, {0x01, 0x02, 0x03, 0x04}));
ON_CALL(server_, OnRequest("www.google.com", T_A))
.WillByDefault(SetReply(&server_, &reply));

ares_socket_t socks[3] = {ARES_SOCKET_BAD, ARES_SOCKET_BAD, ARES_SOCKET_BAD};
int bitmask;

bitmask = ares_getsock(channel_, socks, 3);
EXPECT_EQ(0, bitmask);
bitmask = ares_getsock(channel_, nullptr, 0);
EXPECT_EQ(0, bitmask);

// Ask again with a pending query.
HostResult result;
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
bitmask = ares_getsock(channel_, socks, 3);
EXPECT_NE(0, bitmask);

size_t sock_cnt = 0;
for (size_t i=0; i<3; i++) {
if (ARES_GETSOCK_READABLE(bitmask, i) || ARES_GETSOCK_WRITABLE(bitmask, i)) {
EXPECT_NE(ARES_SOCKET_BAD, socks[i]);
if (socks[i] != ARES_SOCKET_BAD)
sock_cnt++;
}
}
EXPECT_NE((size_t)0, sock_cnt);

bitmask = ares_getsock(channel_, nullptr, 0);
EXPECT_EQ(0, bitmask);
}


TEST_P(MockChannelTest, VerifySocketFunctionCallback) {
ares_socket_functions sock_funcs;
memset(&sock_funcs, 0, sizeof(sock_funcs));

DNSPacket reply;
reply.set_response().set_aa()
.add_question(new DNSQuestion("www.google.com", T_A))
.add_answer(new DNSARR("www.google.com", 0x0100, {0x01, 0x02, 0x03, 0x04}));
ON_CALL(server_, OnRequest("www.google.com", T_A))
.WillByDefault(SetReply(&server_, &reply));

size_t count = 0;

sock_funcs.asocket = [](int af, int type, int protocol, void * p) -> ares_socket_t {
EXPECT_NE(nullptr, p);
(*reinterpret_cast<size_t *>(p))++;
return ::socket(af, type, protocol);
};

ares_set_socket_functions(channel_, &sock_funcs, &count);

{
count = 0;
HostResult result;
ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
Process();

EXPECT_TRUE(result.done_);
EXPECT_EQ(ARES_SUCCESS, result.status_);
EXPECT_EQ(0, result.timeouts_);
EXPECT_NE((size_t)0, count);
}

{
count = 0;
ares_channel_t *copy;
EXPECT_EQ(ARES_SUCCESS, ares_dup(&copy, channel_));

HostResult result;
ares_gethostbyname(copy, "www.google.com.", AF_INET, HostCallback, &result);

ProcessAltChannel(copy);

EXPECT_TRUE(result.done_);
ares_destroy(copy);
EXPECT_NE((size_t)0, count);
EXPECT_EQ(ARES_SUCCESS, result.status_);
EXPECT_EQ(0, result.timeouts_);
}

}

static const unsigned char *
fetch_server_cookie(const ares_dns_record_t *dnsrec, size_t *len)
{
Expand Down
8 changes: 6 additions & 2 deletions test/ares-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -888,14 +888,18 @@ void MockChannelOptsTest::ProcessFD(ares_socket_t fd) {
}
}

void MockChannelOptsTest::Process(unsigned int cancel_ms) {
void MockChannelOptsTest::ProcessAltChannel(ares_channel_t *chan, unsigned int cancel_ms) {
using namespace std::placeholders;
ProcessWork(channel_,
ProcessWork(chan,
std::bind(&MockChannelOptsTest::fds, this),
std::bind(&MockChannelOptsTest::ProcessFD, this, _1),
cancel_ms);
}

void MockChannelOptsTest::Process(unsigned int cancel_ms) {
ProcessAltChannel(channel_);
}

void MockEventThreadOptsTest::Process(unsigned int cancel_ms) {
std::set<ares_socket_t> fds;

Expand Down
1 change: 1 addition & 0 deletions test/ares-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class MockChannelOptsTest : public LibraryTest {

// Process all pending work on ares-owned and mock-server-owned file
// descriptors.
void ProcessAltChannel(ares_channel_t *chan, unsigned int cancel_ms = 0);
void Process(unsigned int cancel_ms = 0);

protected:
Expand Down

0 comments on commit ab8d937

Please sign in to comment.