1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-30 05:24:26 +01:00

Fixed some bugs, added some tests.

This commit is contained in:
Matias Fontanini
2012-08-02 21:21:10 -03:00
parent 383deb8641
commit c4a92d2b96
10 changed files with 223 additions and 152 deletions

View File

@@ -18,6 +18,8 @@ public:
static const string ip_addr1;
static const uint8_t expected_packet[];
static const IPv4Address addr1, addr2;
void test_equals(const ARP &arp1, const ARP &arp2);
};
const uint8_t ARPTest::empty_addr[] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00'};
@@ -26,6 +28,18 @@ const uint8_t ARPTest::hw_addr2[] = {'\x7a', '\x1f', '\xf4', '\x39', '\xab', '\x
const uint8_t ARPTest::expected_packet[] = {'\x00', '\x01', '\x08', '\x00', '\x06', '\x04', '\x00', '\x02', '\x03', '\xde', '\xf5', '\x12', '\t', '\xfa', '\xc0', '\xa8', '-', '\xe7', '\xf5', '\x12', '\xda', 'g', '\xbd', '\r', ' ', '\x9b', 'Q', '\xfe'};
const IPv4Address ARPTest::addr1(0x1234), ARPTest::addr2(0xa3f1);
void ARPTest::test_equals(const ARP &arp1, const ARP &arp2) {
EXPECT_EQ(arp1.opcode(), arp2.opcode());
ASSERT_EQ(arp1.hw_addr_length(), arp2.hw_addr_length());
EXPECT_EQ(arp1.hw_addr_format(), arp2.hw_addr_format());
ASSERT_EQ(arp1.prot_addr_length(), arp2.prot_addr_length());
EXPECT_EQ(arp1.prot_addr_format(), arp2.prot_addr_format());
EXPECT_EQ(arp1.sender_ip_addr(), arp2.sender_ip_addr());
EXPECT_EQ(arp1.target_ip_addr(), arp2.target_ip_addr());
EXPECT_TRUE(memcmp(arp1.sender_hw_addr(), arp2.sender_hw_addr(), arp2.hw_addr_length()) == 0);
EXPECT_TRUE(memcmp(arp1.target_hw_addr(), arp2.target_hw_addr(), arp2.hw_addr_length()) == 0);
}
TEST_F(ARPTest, DefaultContructor) {
ARP arp;
EXPECT_EQ(arp.target_ip_addr(), 0);
@@ -38,29 +52,22 @@ TEST_F(ARPTest, DefaultContructor) {
TEST_F(ARPTest, CopyContructor) {
ARP arp1(addr1, addr2, hw_addr1, hw_addr2);
ARP arp2(arp1);
EXPECT_EQ(arp1.opcode(), arp2.opcode());
ASSERT_EQ(arp1.hw_addr_length(), arp2.hw_addr_length());
EXPECT_EQ(arp1.hw_addr_format(), arp2.hw_addr_format());
ASSERT_EQ(arp1.prot_addr_length(), arp2.prot_addr_length());
EXPECT_EQ(arp1.prot_addr_format(), arp2.prot_addr_format());
EXPECT_EQ(arp1.sender_ip_addr(), arp2.sender_ip_addr());
EXPECT_EQ(arp1.target_ip_addr(), arp2.target_ip_addr());
EXPECT_TRUE(memcmp(arp1.sender_hw_addr(), arp2.sender_hw_addr(), arp2.hw_addr_length()) == 0);
EXPECT_TRUE(memcmp(arp1.target_hw_addr(), arp2.target_hw_addr(), arp2.hw_addr_length()) == 0);
test_equals(arp1, arp2);
}
TEST_F(ARPTest, CopyAssignmentOperator) {
ARP arp1(addr1, addr2, hw_addr1, hw_addr2);
ARP arp2 = arp1;
EXPECT_EQ(arp1.opcode(), arp2.opcode());
ASSERT_EQ(arp1.hw_addr_length(), arp2.hw_addr_length());
EXPECT_EQ(arp1.hw_addr_format(), arp2.hw_addr_format());
ASSERT_EQ(arp1.prot_addr_length(), arp2.prot_addr_length());
EXPECT_EQ(arp1.prot_addr_format(), arp2.prot_addr_format());
EXPECT_EQ(arp1.sender_ip_addr(), arp2.sender_ip_addr());
EXPECT_EQ(arp1.target_ip_addr(), arp2.target_ip_addr());
EXPECT_TRUE(memcmp(arp1.sender_hw_addr(), arp2.sender_hw_addr(), arp2.hw_addr_length()) == 0);
EXPECT_TRUE(memcmp(arp1.target_hw_addr(), arp2.target_hw_addr(), arp2.hw_addr_length()) == 0);
test_equals(arp1, arp2);
}
TEST_F(ARPTest, NestedCopy) {
ARP *nested_arp = new ARP(addr1, addr2, hw_addr1, hw_addr2);
ARP arp1(addr1, addr2, hw_addr1, hw_addr2);
arp1.inner_pdu(nested_arp);
ARP arp2(arp1);
test_equals(arp1, arp2);
test_equals(arp1, *nested_arp);
}
TEST_F(ARPTest, CompleteContructor) {

View File

@@ -64,6 +64,23 @@ TEST_F(DHCPTest, DefaultConstructor) {
EXPECT_EQ(dhcp.hlen(), EthernetII::ADDR_SIZE);
}
TEST_F(DHCPTest, CopyConstructor) {
DHCP dhcp1(expected_packet, sizeof(expected_packet));
DHCP dhcp2(dhcp1);
test_equals(dhcp1, dhcp2);
}
TEST_F(DHCPTest, CopyAssignmentOperator) {
DHCP dhcp1(expected_packet, sizeof(expected_packet));
DHCP dhcp2 = dhcp1;
test_equals(dhcp1, dhcp2);
}
TEST_F(DHCPTest, NestedCopy) {
}
TEST_F(DHCPTest, OpCode) {
DHCP dhcp;
dhcp.opcode(0x71);
@@ -152,9 +169,9 @@ void DHCPTest::test_option(const DHCP &dhcp, DHCP::Options opt, uint32_t len, ui
const DHCP::DHCPOption *option = dhcp.search_option(opt);
ASSERT_TRUE(option != 0);
EXPECT_EQ(option->option, opt);
EXPECT_EQ(option->length, len);
ASSERT_EQ(option->value.size(), len);
if(len)
EXPECT_TRUE(memcmp(option->value, value, len) == 0);
EXPECT_TRUE(std::equal(option->value.begin(), option->value.end(), value));
}
TEST_F(DHCPTest, TypeOption) {
@@ -261,8 +278,8 @@ void DHCPTest::test_equals(const DHCP &dhcp1, const DHCP &dhcp2) {
it2 = options2.begin();
while(it1 != options1.end()) {
EXPECT_EQ(it1->option, it2->option);
ASSERT_EQ(it1->length, it2->length);
EXPECT_TRUE(memcmp(it1->value, it2->value, it1->length) == 0);
ASSERT_EQ(it1->value.size(), it2->value.size());
EXPECT_TRUE(std::equal(it1->value.begin(), it1->value.end(), it2->value.begin()));
it1++; it2++;
}
}
@@ -278,12 +295,12 @@ TEST_F(DHCPTest, ConstructorFromBuffer) {
EXPECT_EQ(dhcp1.xid(), 0x3fab23de);
EXPECT_EQ(dhcp1.secs(), 0x9f1a);
EXPECT_EQ(dhcp1.padding(), 0);
EXPECT_EQ(dhcp1.ciaddr(), Utils::ip_to_int("192.168.0.102"));
EXPECT_EQ(dhcp1.yiaddr(), Utils::ip_to_int("243.22.34.98"));
EXPECT_EQ(dhcp1.giaddr(), Utils::ip_to_int("123.43.55.254"));
EXPECT_EQ(dhcp1.siaddr(), Utils::ip_to_int("167.32.11.154"));
EXPECT_EQ(dhcp1.ciaddr(), IPv4Address("192.168.0.102"));
EXPECT_EQ(dhcp1.yiaddr(), IPv4Address("243.22.34.98"));
EXPECT_EQ(dhcp1.giaddr(), IPv4Address("123.43.55.254"));
EXPECT_EQ(dhcp1.siaddr(), IPv4Address("167.32.11.154"));
ASSERT_TRUE(dhcp1.search_server_identifier(&ip));
EXPECT_EQ(ip, Utils::net_to_host_l(Utils::ip_to_int("192.168.4.2")));
EXPECT_EQ(ip, Utils::net_to_host_l(IPv4Address("192.168.4.2")));
uint32_t size;
uint8_t *buffer = dhcp1.serialize(size);

View File

@@ -172,4 +172,5 @@ TEST_F(IPTest, ConstructorFromBuffer) {
ASSERT_TRUE(buffer);
IP ip2(buffer, size);
delete[] buffer;
}

18
tests/src/ipaddress.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include <stdint.h>
#include "ipaddress.h"
#include "utils.h"
using namespace Tins;
std::string ip_string("192.168.0.225");
TEST(IPAddressTest, Constructor) {
IPv4Address addr1(ip_string);
IPv4Address addr2(Utils::ip_to_int(ip_string));
EXPECT_EQ(addr2, addr1);
EXPECT_EQ((std::string)addr1, ip_string);
EXPECT_EQ((std::string)addr2, ip_string);
}

View File

@@ -42,6 +42,15 @@ TEST_F(TCPTest, CopyAssignmentOperator) {
test_equals(tcp1, tcp2);
}
TEST_F(TCPTest, NestedCopy) {
TCP *nested_tcp = new TCP(0x6d1f, 0x78f2);
TCP tcp1(0x6d1f, 0x78f2);
tcp1.inner_pdu(nested_tcp);
TCP tcp2(tcp1);
test_equals(tcp1, tcp2);
test_equals(tcp1, *nested_tcp);
}
TEST_F(TCPTest, CompleteConstructor) {
TCP tcp(0x6d1f, 0x78f2);
EXPECT_EQ(tcp.dport(), 0x6d1f);