From 7bc143e290df2da85131b411b13509f24f7ba1e2 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 15 Sep 2011 11:41:34 -0300 Subject: [PATCH] Fixed endiannes buf in ARP. --- include/arp.h | 2 +- tests/src/udp.cpp | 70 +++++++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/include/arp.h b/include/arp.h index bfbbd0c..819eea0 100644 --- a/include/arp.h +++ b/include/arp.h @@ -122,7 +122,7 @@ namespace Tins { * * \return Returns the ARP opcode in an uint16_t. */ - inline uint16_t opcode() { return this->_arp.ar_op; } + inline uint16_t opcode() { return Utils::net_to_host_s(this->_arp.ar_op); } /** \brief Getter for the header size. * \return Returns the ARP header size. diff --git a/tests/src/udp.cpp b/tests/src/udp.cpp index 83e8135..dcc2d59 100644 --- a/tests/src/udp.cpp +++ b/tests/src/udp.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "udp.h" #include "pdu.h" @@ -8,41 +9,56 @@ using namespace std; using namespace Tins; +class UDPTest : public testing::Test { +public: + static const uint8_t expected_packet[]; +}; -TEST(UDP, DefaultContructor) { +const uint8_t UDPTest::expected_packet[] = {'\xf5', '\x1a', 'G', '\xf1', '\x04', 'S', '\x00', '\x00'}; + + +TEST_F(UDPTest, DefaultContructor) { UDP udp; EXPECT_EQ(udp.dport(), 0); EXPECT_EQ(udp.sport(), 0); EXPECT_FALSE(udp.inner_pdu()); } -TEST(UDP, DPort) { +TEST_F(UDPTest, CompleteConstructor) { + UDP *inner = new UDP(0x48fa, 0x716b); + UDP udp(0x1234, 0x4321, inner); + EXPECT_EQ(udp.dport(), 0x1234); + EXPECT_EQ(udp.sport(), 0x4321); + EXPECT_TRUE(udp.inner_pdu() == inner); +} + +TEST_F(UDPTest, DPort) { UDP udp; uint16_t port = 0x1234; udp.dport(port); ASSERT_EQ(udp.dport(), port); } -TEST(UDP, SPort) { +TEST_F(UDPTest, SPort) { UDP udp; uint16_t port = 0x1234; udp.sport(port); ASSERT_EQ(udp.sport(), port); } -TEST(UDP, Length) { +TEST_F(UDPTest, Length) { UDP udp; uint16_t length = 0x1234; udp.length(length); ASSERT_EQ(udp.length(), length); } -TEST(UDP, PDUType) { +TEST_F(UDPTest, PDUType) { UDP udp; EXPECT_EQ(udp.pdu_type(), PDU::UDP); } -TEST(UDP, CopyConstructor) { +TEST_F(UDPTest, CopyConstructor) { UDP udp1; udp1.dport(0x1234); udp1.sport(0x4321); @@ -52,9 +68,11 @@ TEST(UDP, CopyConstructor) { EXPECT_EQ(udp2.sport(), udp1.sport()); EXPECT_EQ(udp2.dport(), udp1.dport()); EXPECT_EQ(udp2.length(), udp1.length()); + EXPECT_EQ(udp2.size(), udp1.size()); + EXPECT_EQ(udp2.header_size(), udp1.header_size()); } -TEST(UDP, CopyAssignmentOperator) { +TEST_F(UDPTest, CopyAssignmentOperator) { UDP udp1; udp1.dport(0x1234); udp1.sport(0x4321); @@ -64,9 +82,11 @@ TEST(UDP, CopyAssignmentOperator) { EXPECT_EQ(udp2.sport(), udp1.sport()); EXPECT_EQ(udp2.dport(), udp1.dport()); EXPECT_EQ(udp2.length(), udp1.length()); + EXPECT_EQ(udp2.size(), udp1.size()); + EXPECT_EQ(udp2.header_size(), udp1.header_size()); } -TEST(UDP, Clone) { +TEST_F(UDPTest, ClonePDU) { UDP udp1; uint16_t sport = 0x1234, dport = 0x4321, length = 0xdead; udp1.dport(dport); @@ -82,7 +102,7 @@ TEST(UDP, Clone) { delete udp2; } -TEST(UDP, Serialize) { +TEST_F(UDPTest, Serialize) { UDP udp1; uint16_t sport = 0x1234, dport = 0x4321, length = 0xdead; udp1.dport(dport); @@ -92,29 +112,25 @@ TEST(UDP, Serialize) { uint32_t size; uint8_t *buffer = udp1.serialize(size); ASSERT_TRUE(buffer); + + UDP udp2(udp1); + uint32_t size2; + uint8_t *buffer2 = udp2.serialize(size2); + ASSERT_EQ(size, size2); + EXPECT_TRUE(memcmp(buffer, buffer2, size) == 0); delete[] buffer; } -TEST(UDP, BufferClone) { - UDP udp1; - uint16_t sport = 0x1234, dport = 0x4321, length = 0xdead; - udp1.dport(dport); - udp1.sport(sport); - udp1.length(length); - +TEST_F(UDPTest, ConstructorFromBuffer) { + UDP udp1(expected_packet, sizeof(expected_packet)); uint32_t size; uint8_t *buffer = udp1.serialize(size); - ASSERT_TRUE(buffer); + EXPECT_EQ(size, sizeof(expected_packet)); UDP udp2(buffer, size); - EXPECT_EQ(udp2.sport(), udp1.sport()); - EXPECT_EQ(udp2.dport(), udp1.dport()); - EXPECT_EQ(udp2.length(), udp1.length()); - EXPECT_EQ(udp2.pdu_type(), PDU::UDP); - - delete[] buffer; + EXPECT_EQ(udp1.dport(), udp2.dport()); + EXPECT_EQ(udp1.sport(), udp2.sport()); + EXPECT_EQ(udp1.length(), udp2.length()); + EXPECT_EQ(udp1.size(), udp2.size()); + EXPECT_EQ(udp1.header_size(), udp2.header_size()); } - - - -