1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 04:34:27 +01:00

Refactored several classes.

This commit is contained in:
Matias Fontanini
2012-08-03 13:08:24 -03:00
parent c4a92d2b96
commit c5fce38c3a
18 changed files with 204 additions and 306 deletions

View File

@@ -1,14 +1,12 @@
#include <algorithm>
#include <gtest/gtest.h>
#include "ethernetII.h"
#include "utils.h"
#include <gtest/gtest.h>
using namespace Tins;
class EthernetIITest : public ::testing::Test {
public:
public:
static const uint8_t expected_packet[];
static const uint8_t s_addr[];
static const uint8_t d_addr[];
@@ -16,6 +14,7 @@ class EthernetIITest : public ::testing::Test {
static const uint16_t p_type;
static const uint32_t iface;
void test_equals(const EthernetII &eth1, const EthernetII &eth2);
};
const uint8_t EthernetIITest::expected_packet[] = {
@@ -39,6 +38,14 @@ const uint16_t EthernetIITest::p_type = 0xd0ab;
const uint32_t EthernetIITest::iface = 0x12345678;
void EthernetIITest::test_equals(const EthernetII &eth1, const EthernetII &eth2) {
EXPECT_TRUE(std::equal(eth1.dst_addr(), eth1.dst_addr() + EthernetII::ADDR_SIZE, eth2.dst_addr()));
EXPECT_TRUE(std::equal(eth1.src_addr(), eth1.src_addr() + EthernetII::ADDR_SIZE, eth2.src_addr()));
EXPECT_EQ(eth1.payload_type(), eth2.payload_type());
EXPECT_EQ(eth1.iface(), eth2.iface());
EXPECT_EQ((bool)eth1.inner_pdu(), (bool)eth2.inner_pdu());
}
TEST_F(EthernetIITest, DefaultConstructor) {
EthernetII eth(0);
EXPECT_EQ(eth.iface(), 0);
@@ -49,6 +56,31 @@ TEST_F(EthernetIITest, DefaultConstructor) {
EXPECT_EQ(eth.pdu_type(), PDU::ETHERNET_II);
}
TEST_F(EthernetIITest, CopyConstructor) {
EthernetII eth1(expected_packet, sizeof(expected_packet));
eth1.iface(0);
EthernetII eth2(eth1);
test_equals(eth1, eth2);
}
TEST_F(EthernetIITest, CopyAssignmentOperator) {
EthernetII eth1(expected_packet, sizeof(expected_packet));
eth1.iface(0);
EthernetII eth2(0);
eth2 = eth1;
test_equals(eth1, eth2);
}
TEST_F(EthernetIITest, NestedCopy) {
EthernetII *nested = new EthernetII(expected_packet, sizeof(expected_packet));
nested->iface(0);
EthernetII eth1(expected_packet, sizeof(expected_packet));
eth1.iface(0);
eth1.inner_pdu(nested);
EthernetII eth2(eth1);
test_equals(eth1, eth2);
}
TEST_F(EthernetIITest, SourceAddress) {
EthernetII eth(0);
eth.src_addr(s_addr);
@@ -103,16 +135,4 @@ TEST_F(EthernetIITest, ConstructorFromBuffer) {
EXPECT_EQ(eth.payload_type(), p_type);
}
TEST_F(EthernetIITest, ClonePDU) {
EthernetII eth(0, d_addr, s_addr);
eth.payload_type(p_type);
EthernetII* eth2 = static_cast<EthernetII*>(eth.clone_pdu());
EXPECT_TRUE(memcmp(eth.src_addr(), eth2->src_addr(), 6) == 0);
EXPECT_TRUE(memcmp(eth.dst_addr(), eth2->dst_addr(), 6) == 0);
EXPECT_EQ(eth.payload_type(), eth2->payload_type());
delete eth2;
}