Commit 6643603e authored by Marcin Siodelski's avatar Marcin Siodelski

[1956] Unit tests updated, added duid option.

parent 689b2866
......@@ -22,6 +22,7 @@
#include <dhcp/option.h>
#include <dhcp/dhcp6.h>
#include "../localized_option.h"
#include "../perf_pkt6.h"
using namespace std;
......@@ -29,8 +30,7 @@ using namespace isc;
using namespace isc::dhcp;
using namespace isc::perfdhcp;
typedef PerfPkt6::PositionedOptionPtr PositionedOptionPtr;
typedef PerfPkt6::PositionedOption PositionedOption;
typedef PerfPkt6::LocalizedOptionPtr LocalizedOptionPtr;
namespace {
class PerfPkt6Test : public ::testing::Test {
......@@ -38,58 +38,51 @@ public:
PerfPkt6Test() {
}
/// @brief returns captured actual SOLICIT packet
///
/// Captured SOLICIT packet with transid=0x3d79fb and options: client-id,
/// in_na, dns-server, elapsed-time, option-request
/// This code was autogenerated (see src/bin/dhcp6/tests/iface_mgr_unittest.c),
/// but we spent some time to make is less ugly than it used to be.
///
/// @return pointer to Pkt6 that represents received SOLICIT
PerfPkt6* capture1() {
uint8_t data[98];
data[0] = 1;
data[1] = 1; data[2] = 2; data[3] = 3; data[4] = 0;
data[5] = 1; data[6] = 0; data[7] = 14; data[8] = 0;
data[9] = 1; data[10] = 0; data[11] = 1; data[12] = 21;
data[13] = 158; data[14] = 60; data[15] = 22; data[16] = 0;
data[17] = 30; data[18] = 140; data[19] = 155; data[20] = 115;
data[21] = 73; data[22] = 0; data[23] = 3; data[24] = 0;
data[25] = 40; data[26] = 0; data[27] = 0; data[28] = 0;
data[29] = 1; data[30] = 255; data[31] = 255; data[32] = 255;
data[33] = 255; data[34] = 255; data[35] = 255; data[36] = 255;
data[37] = 255; data[38] = 0; data[39] = 5; data[40] = 0;
data[41] = 24; data[42] = 32; data[43] = 1; data[44] = 13;
data[45] = 184; data[46] = 0; data[47] = 1; data[48] = 0;
data[49] = 0; data[50] = 0; data[51] = 0; data[52] = 0;
data[53] = 0; data[54] = 0; data[55] = 0; data[56] = 18;
data[57] = 52; data[58] = 255; data[59] = 255; data[60] = 255;
data[61] = 255; data[62] = 255; data[63] = 255; data[64] = 255;
data[65] = 255; data[66] = 0; data[67] = 23; data[68] = 0;
data[69] = 16; data[70] = 32; data[71] = 1; data[72] = 13;
data[73] = 184; data[74] = 0; data[75] = 1; data[76] = 0;
data[77] = 0; data[78] = 0; data[79] = 0; data[80] = 0;
data[81] = 0; data[82] = 0; data[83] = 0; data[84] = 221;
data[85] = 221; data[86] = 0; data[87] = 8; data[88] = 0;
data[89] = 2; data[90] = 0; data[91] = 100; data[92] = 0;
data[93] = 6; data[94] = 0; data[95] = 2; data[96] = 0;
data[97] = 23;
PerfPkt6* pkt = new PerfPkt6(data, sizeof(data), 0);
/* pkt->setRemotePort(546);
pkt->setRemoteAddr(IOAddress("fe80::21e:8cff:fe9b:7349"));
pkt->setLocalPort(0);
pkt->setLocalAddr(IOAddress("ff02::1:2"));
pkt->setIndex(2);
pkt->setIface("eth0");*/
return (pkt);
}
/// @brief returns captured actual SOLICIT packet
///
/// Captured SOLICIT packet with transid=0x3d79fb and options: client-id,
/// in_na, dns-server, elapsed-time, option-request
/// This code was autogenerated (see src/bin/dhcp6/tests/iface_mgr_unittest.c),
/// but we spent some time to make is less ugly than it used to be.
///
/// @return pointer to Pkt6 that represents received SOLICIT
PerfPkt6* capture1() {
uint8_t data[98];
data[0] = 1;
data[1] = 1; data[2] = 2; data[3] = 3; data[4] = 0;
data[5] = 1; data[6] = 0; data[7] = 14; data[8] = 0;
data[9] = 1; data[10] = 0; data[11] = 1; data[12] = 21;
data[13] = 158; data[14] = 60; data[15] = 22; data[16] = 0;
data[17] = 30; data[18] = 140; data[19] = 155; data[20] = 115;
data[21] = 73; data[22] = 0; data[23] = 3; data[24] = 0;
data[25] = 40; data[26] = 0; data[27] = 0; data[28] = 0;
data[29] = 1; data[30] = 255; data[31] = 255; data[32] = 255;
data[33] = 255; data[34] = 255; data[35] = 255; data[36] = 255;
data[37] = 255; data[38] = 0; data[39] = 5; data[40] = 0;
data[41] = 24; data[42] = 32; data[43] = 1; data[44] = 13;
data[45] = 184; data[46] = 0; data[47] = 1; data[48] = 0;
data[49] = 0; data[50] = 0; data[51] = 0; data[52] = 0;
data[53] = 0; data[54] = 0; data[55] = 0; data[56] = 18;
data[57] = 52; data[58] = 255; data[59] = 255; data[60] = 255;
data[61] = 255; data[62] = 255; data[63] = 255; data[64] = 255;
data[65] = 255; data[66] = 0; data[67] = 23; data[68] = 0;
data[69] = 16; data[70] = 32; data[71] = 1; data[72] = 13;
data[73] = 184; data[74] = 0; data[75] = 1; data[76] = 0;
data[77] = 0; data[78] = 0; data[79] = 0; data[80] = 0;
data[81] = 0; data[82] = 0; data[83] = 0; data[84] = 221;
data[85] = 221; data[86] = 0; data[87] = 8; data[88] = 0;
data[89] = 2; data[90] = 0; data[91] = 100; data[92] = 0;
data[93] = 6; data[94] = 0; data[95] = 2; data[96] = 0;
data[97] = 23;
PerfPkt6* pkt = new PerfPkt6(data, sizeof(data), 0);
return (pkt);
}
};
TEST_F(PerfPkt6Test, constructor) {
TEST_F(PerfPkt6Test, Constructor) {
uint8_t data[] = { 0, 1, 2, 3, 4, 5 };
timespec zero_t_spec;
memset(&zero_t_spec, 0, sizeof(zero_t_spec));
......@@ -103,38 +96,62 @@ TEST_F(PerfPkt6Test, constructor) {
EXPECT_EQ(0, memcmp(&packet_t_spec, &zero_t_spec, sizeof(zero_t_spec)));
}
TEST_F(PerfPkt6Test, UnpackRawSolicit) {
// Create packetPositionedOptionPtr
TEST_F(PerfPkt6Test, RawPackUnpack) {
// Create first packet.
boost::scoped_ptr<PerfPkt6> pkt1(capture1());
// Create some input buffer to initialize options.
uint8_t buf_elapsed_time[] = { 8, 2, 4, 5 };
// Create some input buffers to initialize options.
uint8_t buf_elapsed_time[] = { 1, 1 };
uint8_t buf_duid[14] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
// Create options.
dhcp::OptionBuffer buf(buf_elapsed_time, buf_elapsed_time + 4);
PositionedOptionPtr pkt1_elapsed_time(new PositionedOption(Option::V6, 8, buf, 86));
dhcp::OptionBuffer vec_elapsed_time(buf_elapsed_time, buf_elapsed_time + 2);
dhcp::OptionBuffer vec_duid(buf_duid, buf_duid + 14);
LocalizedOptionPtr pkt1_elapsed_time(new LocalizedOption(Option::V6, 8, vec_elapsed_time, 86));
LocalizedOptionPtr pkt1_duid(new LocalizedOption(Option::V6, D6O_CLIENTID, vec_duid, 4));
// Add option to packet and create on-wire format from added options.
// Contents of options will override contents of packet buffer.
ASSERT_NO_THROW(pkt1->addOption(pkt1_elapsed_time));
ASSERT_NO_THROW(pkt1->stampedRawPack());
ASSERT_NO_THROW(pkt1->addOption(pkt1_duid));
ASSERT_TRUE(pkt1->rawPack());
// Reset so as we can reuse them for another packet.
vec_elapsed_time.clear();
vec_duid.clear();
// Get output buffer from packet 1 to create new packet
// that will be later validated.
isc::util::OutputBuffer pkt1_output = pkt1->getBuffer();
ASSERT_EQ(pkt1_output.getLength(), pkt1->getData().size());
boost::scoped_ptr<PerfPkt6> pkt2(new PerfPkt6(static_cast<const uint8_t*>(pkt1_output.getData()),
pkt1_output.getLength(), 1));
// Create objects specifying options offset in a packet.
// Offsets will inform pkt2 object where to read data from.
LocalizedOptionPtr pkt2_elapsed_time(new LocalizedOption(Option::V6, D6O_ELAPSED_TIME, vec_elapsed_time, 86));
LocalizedOptionPtr pkt2_duid(new LocalizedOption(Option::V6, D6O_CLIENTID, vec_duid, 4));
// Add options to packet to pass their offsets.
pkt2->addOption(pkt2_elapsed_time);
pkt2->addOption(pkt2_duid);
// Let's create another packet using packed data stored in first packet.
// We will try to unpack data into option objects.
buf.clear();
OptionBuffer pkt1_output = pkt1->getData();
boost::scoped_ptr<PerfPkt6> pkt2(new PerfPkt6(&pkt1_output[0], pkt1_output.size(), 1));
// Unpack: get relevant parts of buffer data into option objects.
ASSERT_TRUE(pkt2->rawUnpack());
// Create partial options to specify offsets of certain options in packet.
PositionedOptionPtr pkt2_elapsed_time(new PositionedOption(Option::V6, D6O_ELAPSED_TIME, buf, 86));
// Add partial options.
pkt2->addOption(pkt2_elapsed_time);
// Get relevant parts of buffer data into option objects.
ASSERT_NO_THROW(pkt2->stampedRawUnpack());
// Once option data is updated we pull it out.
pkt2_elapsed_time = boost::dynamic_pointer_cast<PositionedOption>(pkt2->getOption(D6O_ELAPSED_TIME));
// Once option data is stored in options objects we pull it out.
pkt2_elapsed_time = boost::dynamic_pointer_cast<LocalizedOption>(pkt2->getOption(D6O_ELAPSED_TIME));
pkt2_duid = boost::dynamic_pointer_cast<LocalizedOption>(pkt2->getOption(D6O_CLIENTID));
// Check if options are present. They have to be there since
// we have added them ourselfs.
ASSERT_TRUE(pkt2_elapsed_time);
ASSERT_TRUE(pkt2_duid);
// Expecting option contents be the same as original.
OptionBuffer pkt2_elapsed_time_data = pkt2_elapsed_time->getData();
ASSERT_EQ(100, pkt2_elapsed_time->getUint16());
OptionBuffer pkt2_duid_data = pkt2_duid->getData();
EXPECT_EQ(0x0101, pkt2_elapsed_time->getUint16());
EXPECT_TRUE(std::equal(pkt2_duid_data.begin(), pkt2_duid_data.end(), buf_duid));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment