mirror of
https://frontier.innolan.net/rainlance/c-ares.git
synced 2025-11-23 22:06:48 +00:00
test: Tweak tests, add alloc failure test
This commit is contained in:
@ -246,7 +246,7 @@ TEST_F(DefaultChannelTest, LiveSearchSRV) {
|
||||
|
||||
TEST_F(DefaultChannelTest, LiveSearchANY) {
|
||||
SearchResult result;
|
||||
ares_search(channel_, "facebook.com.", ns_c_in, ns_t_any,
|
||||
ares_search(channel_, "google.com.", ns_c_in, ns_t_any,
|
||||
SearchCallback, &result);
|
||||
Process();
|
||||
EXPECT_TRUE(result.done_);
|
||||
|
||||
@ -78,6 +78,50 @@ TEST_F(MockChannelTest, SearchDomains) {
|
||||
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_F(MockChannelTest, Resend) {
|
||||
std::vector<byte> nothing;
|
||||
DNSPacket reply;
|
||||
|
||||
@ -174,6 +174,10 @@ void MockServer::Process(int fd) {
|
||||
char *name = nullptr;
|
||||
long enclen;
|
||||
ares_expand_name(question, buffer, len, &name, &enclen);
|
||||
if (!name) {
|
||||
std::cerr << "Failed to retrieve name" << std::endl;
|
||||
return;
|
||||
}
|
||||
qlen -= enclen;
|
||||
question += enclen;
|
||||
std::string namestr(name);
|
||||
@ -287,9 +291,11 @@ std::ostream& operator<<(std::ostream& os, const HostResult& result) {
|
||||
return os;
|
||||
}
|
||||
|
||||
HostEnt::HostEnt(const struct hostent *hostent) {
|
||||
if (!hostent) return;
|
||||
if (hostent->h_name) name_ = hostent->h_name;
|
||||
HostEnt::HostEnt(const struct hostent *hostent) : addrtype_(-1) {
|
||||
if (!hostent)
|
||||
return;
|
||||
if (hostent->h_name)
|
||||
name_ = hostent->h_name;
|
||||
if (hostent->h_aliases) {
|
||||
char** palias = hostent->h_aliases;
|
||||
while (*palias != nullptr) {
|
||||
@ -326,6 +332,7 @@ std::ostream& operator<<(std::ostream& os, const HostEnt& host) {
|
||||
os << '}';
|
||||
return os;
|
||||
}
|
||||
|
||||
void HostCallback(void *data, int status, int timeouts,
|
||||
struct hostent *hostent) {
|
||||
EXPECT_NE(nullptr, data);
|
||||
|
||||
@ -233,6 +233,10 @@ std::string QuestionToString(const std::vector<byte>& packet,
|
||||
char *name = nullptr;
|
||||
long enclen;
|
||||
ares_expand_name(*data, packet.data(), packet.size(), &name, &enclen);
|
||||
if (enclen > *len) {
|
||||
ss << "(error, encoded name len " << enclen << "bigger than remaining data " << *len << " bytes)";
|
||||
return ss.str();
|
||||
}
|
||||
*len -= enclen;
|
||||
*data += enclen;
|
||||
ss << "'" << name << "' ";
|
||||
@ -261,6 +265,10 @@ std::string RRToString(const std::vector<byte>& packet,
|
||||
char *name = nullptr;
|
||||
long enclen;
|
||||
ares_expand_name(*data, packet.data(), packet.size(), &name, &enclen);
|
||||
if (enclen > *len) {
|
||||
ss << "(error, encoded name len " << enclen << "bigger than remaining data " << *len << " bytes)";
|
||||
return ss.str();
|
||||
}
|
||||
*len -= enclen;
|
||||
*data += enclen;
|
||||
ss << "'" << name << "' ";
|
||||
|
||||
Reference in New Issue
Block a user