pkt6_unittest.cc 7.43 KB
Newer Older
1
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15
16
17
18
#include <config.h>
#include <iostream>
#include <sstream>
#include <arpa/inet.h>
19
20
#include <gtest/gtest.h>

21
22
#include "io_address.h"
#include "dhcp/option.h"
23
#include "dhcp/pkt6.h"
24
#include "dhcp/dhcp6.h"
25
26
27

using namespace std;
using namespace isc;
28
using namespace isc::asiolink;
29
using namespace isc::dhcp;
30

31
namespace {
32
33
34
35
36
37
38
39
40
// empty class for now, but may be extended once Addr6 becomes bigger
class Pkt6Test : public ::testing::Test {
public:
    Pkt6Test() {
    }
};

TEST_F(Pkt6Test, constructor) {
    Pkt6 * pkt1 = new Pkt6(17);
41

42
    EXPECT_EQ(pkt1->data_len_, 17);
43

44
    delete pkt1;
45
}
46

47
48
49
50
51
52
53
54
55
56
57
58
59
// captured actual SOLICIT packet: transid=0x3d79fb
// options: client-id, in_na, dns-server, elapsed-time, option-request
// this code is autogenerated (see src/bin/dhcp6/tests/iface_mgr_unittest.c)
Pkt6 *capture1() {
    Pkt6* pkt;
    pkt = new Pkt6(98);
    pkt->remote_port_ = 546;
    pkt->remote_addr_ = IOAddress("fe80::21e:8cff:fe9b:7349");
    pkt->local_port_ = 0;
    pkt->local_addr_ = IOAddress("ff02::1:2");
    pkt->ifindex_ = 2;
    pkt->iface_ = "eth0";
    pkt->data_[0]=1;
60
    pkt->data_[1]=01;     pkt->data_[2]=02;     pkt->data_[3]=03;     pkt->data_[4]=0;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    pkt->data_[5]=1;     pkt->data_[6]=0;     pkt->data_[7]=14;     pkt->data_[8]=0;
    pkt->data_[9]=1;     pkt->data_[10]=0;     pkt->data_[11]=1;     pkt->data_[12]=21;
    pkt->data_[13]=158;     pkt->data_[14]=60;     pkt->data_[15]=22;     pkt->data_[16]=0;
    pkt->data_[17]=30;     pkt->data_[18]=140;     pkt->data_[19]=155;     pkt->data_[20]=115;
    pkt->data_[21]=73;     pkt->data_[22]=0;     pkt->data_[23]=3;     pkt->data_[24]=0;
    pkt->data_[25]=40;     pkt->data_[26]=0;     pkt->data_[27]=0;     pkt->data_[28]=0;
    pkt->data_[29]=1;     pkt->data_[30]=255;     pkt->data_[31]=255;     pkt->data_[32]=255;
    pkt->data_[33]=255;     pkt->data_[34]=255;     pkt->data_[35]=255;     pkt->data_[36]=255;
    pkt->data_[37]=255;     pkt->data_[38]=0;     pkt->data_[39]=5;     pkt->data_[40]=0;
    pkt->data_[41]=24;     pkt->data_[42]=32;     pkt->data_[43]=1;     pkt->data_[44]=13;
    pkt->data_[45]=184;     pkt->data_[46]=0;     pkt->data_[47]=1;     pkt->data_[48]=0;
    pkt->data_[49]=0;     pkt->data_[50]=0;     pkt->data_[51]=0;     pkt->data_[52]=0;
    pkt->data_[53]=0;     pkt->data_[54]=0;     pkt->data_[55]=0;     pkt->data_[56]=18;
    pkt->data_[57]=52;     pkt->data_[58]=255;     pkt->data_[59]=255;     pkt->data_[60]=255;
    pkt->data_[61]=255;     pkt->data_[62]=255;     pkt->data_[63]=255;     pkt->data_[64]=255;
    pkt->data_[65]=255;     pkt->data_[66]=0;     pkt->data_[67]=23;     pkt->data_[68]=0;
    pkt->data_[69]=16;     pkt->data_[70]=32;     pkt->data_[71]=1;     pkt->data_[72]=13;
    pkt->data_[73]=184;     pkt->data_[74]=0;     pkt->data_[75]=1;     pkt->data_[76]=0;
    pkt->data_[77]=0;     pkt->data_[78]=0;     pkt->data_[79]=0;     pkt->data_[80]=0;
    pkt->data_[81]=0;     pkt->data_[82]=0;     pkt->data_[83]=0;     pkt->data_[84]=221;
    pkt->data_[85]=221;     pkt->data_[86]=0;     pkt->data_[87]=8;     pkt->data_[88]=0;
    pkt->data_[89]=2;     pkt->data_[90]=0;     pkt->data_[91]=100;     pkt->data_[92]=0;
    pkt->data_[93]=6;     pkt->data_[94]=0;     pkt->data_[95]=2;     pkt->data_[96]=0;
    pkt->data_[97]=23;
    return (pkt);
}

88
89
/// TODO Reenable this once ticket #1313 is implemented
TEST_F(Pkt6Test, DISABLED_unpack_solicit1) {
90
91
92
93
    Pkt6 * sol = capture1();

    ASSERT_EQ(true, sol->unpack());

94
95
96
97
98
    // check for length
    EXPECT_EQ(98, sol->len() );

    // check for type
    EXPECT_EQ(DHCPV6_SOLICIT, sol->getType() );
99
100

    // check that all present options are returned
101
102
103
104
105
    EXPECT_TRUE(sol->getOption(D6O_CLIENTID)); // client-id is present
    EXPECT_TRUE(sol->getOption(D6O_IA_NA));    // IA_NA is present
    EXPECT_TRUE(sol->getOption(D6O_ELAPSED_TIME));  // elapsed is present
    EXPECT_TRUE(sol->getOption(D6O_NAME_SERVERS));
    EXPECT_TRUE(sol->getOption(D6O_ORO));
106
107

    // let's check that non-present options are not returned
108
109
110
    EXPECT_FALSE(sol->getOption(D6O_SERVERID)); // server-id is missing
    EXPECT_FALSE(sol->getOption(D6O_IA_TA));
    EXPECT_FALSE(sol->getOption(D6O_IAADDR));
111
112
113
114
115
116

    std::cout << sol->toText();

    delete sol;
}

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
TEST_F(Pkt6Test, packUnpack) {

    Pkt6 * parent = new Pkt6(100);

    parent->setType(DHCPV6_SOLICIT);

    boost::shared_ptr<Option> opt1(new Option(Option::V6, 1));
    boost::shared_ptr<Option> opt2(new Option(Option::V6, 2));
    boost::shared_ptr<Option> opt3(new Option(Option::V6, 100));
    // let's not use zero-length option type 3 as it is IA_NA

    parent->addOption(opt1);
    parent->addOption(opt2);
    parent->addOption(opt3);

    EXPECT_EQ(DHCPV6_SOLICIT, parent->getType());
    int transid = parent->getTransid();
    // transaction-id was randomized, let's remember it

    // calculated length should be 16
    EXPECT_EQ( Pkt6::DHCPV6_PKT_HDR_LEN + 3*Option::OPTION6_HDR_LEN, 
               parent->len() );

    EXPECT_TRUE( parent->pack() );

    //
    EXPECT_EQ( Pkt6::DHCPV6_PKT_HDR_LEN + 3*Option::OPTION6_HDR_LEN, 
               parent->len() );

    // let's delete options from options_ collection
    // they still be defined in packed 
    parent->options_.clear();

    // that that removed options are indeed are gone
    EXPECT_EQ( 4, parent->len() );

    // now recreate options list
    EXPECT_TRUE( parent->unpack() );

    // transid, message-type should be the same as before
    EXPECT_EQ(transid, parent->getTransid());
    EXPECT_EQ(DHCPV6_SOLICIT, parent->getType());
    
    EXPECT_TRUE( parent->getOption(1));
    EXPECT_TRUE( parent->getOption(2));
    EXPECT_TRUE( parent->getOption(100));
    EXPECT_FALSE( parent->getOption(4));
    
    delete parent;
}

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
TEST_F(Pkt6Test, addGetDelOptions) {
    Pkt6 * parent = new Pkt6(100);

    boost::shared_ptr<Option> opt1(new Option(Option::V6, 1));
    boost::shared_ptr<Option> opt2(new Option(Option::V6, 2));
    boost::shared_ptr<Option> opt3(new Option(Option::V6, 2));

    parent->addOption(opt1);
    parent->addOption(opt2);

    // getOption() test
    EXPECT_EQ(opt1, parent->getOption(1));
    EXPECT_EQ(opt2, parent->getOption(2));

    // expect NULL
    EXPECT_EQ(boost::shared_ptr<Option>(), parent->getOption(4));

    // now there are 2 options of type 2
    parent->addOption(opt3);

    // let's delete one of them
    EXPECT_EQ(true, parent->delOption(2));

    // there still should be the other option 2
    EXPECT_NE(boost::shared_ptr<Option>(), parent->getOption(2));

    // let's delete the other option 2
    EXPECT_EQ(true, parent->delOption(2));

    // no more options with type=2
    EXPECT_EQ(boost::shared_ptr<Option>(), parent->getOption(2));

    // let's try to delete - should fail
    EXPECT_TRUE(false ==  parent->delOption(2));

    delete parent;
}


207
}