unit testing for ptr

This commit is contained in:
Jeff Becker 2018-12-04 11:52:48 -05:00
parent ebb42ca46f
commit 1f104881be
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
2 changed files with 15 additions and 6 deletions

View File

@ -73,22 +73,22 @@ namespace llarp
bool bool
DecodePTR(const Name_t& name, huint32_t& ip) DecodePTR(const Name_t& name, huint32_t& ip)
{ {
auto pos = name.find(".in-addr.arpa."); auto pos = name.find(".in-addr.arpa");
if(pos == Name_t::npos) if(pos == std::string::npos)
return false; return false;
std::string sub = name.substr(0, pos - 1); std::string sub = name.substr(0, pos + 1);
if(std::count(sub.begin(), sub.end(), '.') == 4) if(std::count(sub.begin(), sub.end(), '.') == 4)
{ {
uint8_t a, b, c, d; uint8_t a, b, c, d;
pos = sub.find('.'); pos = sub.find('.');
d = atoi(sub.substr(0, pos).c_str()); d = atoi(sub.substr(0, pos).c_str());
sub = sub.substr(pos); sub = sub.substr(pos + 1);
pos = sub.find('.'); pos = sub.find('.');
c = atoi(sub.substr(0, pos).c_str()); c = atoi(sub.substr(0, pos).c_str());
sub = sub.substr(pos); sub = sub.substr(pos + 1);
pos = sub.find('.'); pos = sub.find('.');
b = atoi(sub.substr(0, pos).c_str()); b = atoi(sub.substr(0, pos).c_str());
sub = sub.substr(pos); sub = sub.substr(pos + 1);
pos = sub.find('.'); pos = sub.find('.');
a = atoi(sub.substr(0, pos).c_str()); a = atoi(sub.substr(0, pos).c_str());
ip = llarp::ipaddr_ipv4_bits(a, b, c, d); ip = llarp::ipaddr_ipv4_bits(a, b, c, d);

View File

@ -1,6 +1,7 @@
#include <llarp/dns/dns.hpp> #include <llarp/dns/dns.hpp>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <algorithm> #include <algorithm>
#include <llarp/net.hpp>
struct DNSLibTest : public ::testing::Test struct DNSLibTest : public ::testing::Test
{ {
@ -22,6 +23,14 @@ struct DNSLibTest : public ::testing::Test
} }
}; };
TEST_F(DNSLibTest, TestPTR)
{
llarp::huint32_t ip = {0};
llarp::huint32_t expected = llarp::ipaddr_ipv4_bits(10, 10, 10, 1);
ASSERT_TRUE(llarp::dns::DecodePTR("1.10.10.10.in-addr.arpa.", ip));
ASSERT_EQ(ip, expected);
};
TEST_F(DNSLibTest, TestSerializeHeader) TEST_F(DNSLibTest, TestSerializeHeader)
{ {
llarp::dns::MessageHeader hdr, other; llarp::dns::MessageHeader hdr, other;