1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
168
169
170
// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <asiolink/io_address.h>
#include <dhcp/hwaddr.h>
#include <dhcp/dhcp4.h>
#include <exceptions/exceptions.h>

#include <boost/scoped_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::dhcp;
using namespace isc::asiolink;

using boost::scoped_ptr;

namespace {

// This test verifies if the constructors are working as expected
// and process passed parameters.
TEST(HWAddrTest, constructor) {

    const uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6};
    const uint8_t htype = HTYPE_ETHER;
    vector<uint8_t> data2(data1, data1 + sizeof(data1));

    // Over the limit data 
    vector<uint8_t> big_data_vector(HWAddr::MAX_HWADDR_LEN + 1, 0); 

    scoped_ptr<HWAddr> hwaddr1(new HWAddr(data1, sizeof(data1), htype));
    scoped_ptr<HWAddr> hwaddr2(new HWAddr(data2, htype));
    scoped_ptr<HWAddr> hwaddr3(new HWAddr());

    EXPECT_TRUE(data2 == hwaddr1->hwaddr_);
    EXPECT_EQ(htype, hwaddr1->htype_);

    EXPECT_TRUE(data2 == hwaddr2->hwaddr_);
    EXPECT_EQ(htype, hwaddr2->htype_);

    EXPECT_EQ(0, hwaddr3->hwaddr_.size());
    EXPECT_EQ(htype, hwaddr3->htype_);

    // Check that over the limit data length throws exception 
    EXPECT_THROW(HWAddr(&big_data_vector[0], big_data_vector.size(), HTYPE_ETHER), 
        BadValue);

    // Check that over the limit vector throws exception
    EXPECT_THROW(HWAddr(big_data_vector, HTYPE_ETHER), BadValue);
}

// This test checks if the comparison operators are sane.
TEST(HWAddrTest, operators) {
    uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6};
    uint8_t data2[] = {0, 1, 2, 3, 4};
    uint8_t data3[] = {0, 1, 2, 3, 4, 5, 7}; // last digit different
    uint8_t data4[] = {0, 1, 2, 3, 4, 5, 6}; // the same as 1

    uint8_t htype1 = HTYPE_ETHER;
    uint8_t htype2 = HTYPE_FDDI;

    scoped_ptr<HWAddr> hw1(new HWAddr(data1, sizeof(data1), htype1));
    scoped_ptr<HWAddr> hw2(new HWAddr(data2, sizeof(data2), htype1));
    scoped_ptr<HWAddr> hw3(new HWAddr(data3, sizeof(data3), htype1));
    scoped_ptr<HWAddr> hw4(new HWAddr(data4, sizeof(data4), htype1));

    // MAC address the same as data1 and data4, but different hardware type
    scoped_ptr<HWAddr> hw5(new HWAddr(data4, sizeof(data4), htype2));

    EXPECT_TRUE(*hw1 == *hw4);
    EXPECT_FALSE(*hw1 == *hw2);
    EXPECT_FALSE(*hw1 == *hw3);

    EXPECT_FALSE(*hw1 != *hw4);
    EXPECT_TRUE(*hw1 != *hw2);
    EXPECT_TRUE(*hw1 != *hw3);

    EXPECT_FALSE(*hw1 == *hw5);
    EXPECT_FALSE(*hw4 == *hw5);

    EXPECT_TRUE(*hw1 != *hw5);
    EXPECT_TRUE(*hw4 != *hw5);
}

// Checks that toText() method produces appropriate text representation
TEST(HWAddrTest, toText) {
    uint8_t data[] = {0, 1, 2, 3, 4, 5};
    uint8_t htype = 15;

    HWAddrPtr hw(new HWAddr(data, sizeof(data), htype));

    EXPECT_EQ("hwtype=15 00:01:02:03:04:05", hw->toText());

    // In some cases we don't want htype value to be included. Check that
    // it can be forced.
    EXPECT_EQ("00:01:02:03:04:05", hw->toText(false));
}

TEST(HWAddrTest, stringConversion) {

    // Check that an empty vector returns an appropriate string
    HWAddr hwaddr;
    std::string result = hwaddr.toText();
    EXPECT_EQ(std::string("hwtype=1 "), result);

    // ... that a single-byte string is OK
    hwaddr.hwaddr_.push_back(0xc3);
    result = hwaddr.toText();
    EXPECT_EQ(std::string("hwtype=1 c3"), result);

    // ... and that a multi-byte string works
    hwaddr.hwaddr_.push_back(0x7);
    hwaddr.hwaddr_.push_back(0xa2);
    hwaddr.hwaddr_.push_back(0xe8);
    hwaddr.hwaddr_.push_back(0x42);
    result = hwaddr.toText();
    EXPECT_EQ(std::string("hwtype=1 c3:07:a2:e8:42"), result);
}

// Checks that the HW address can be created from the textual format.
TEST(HWAddrTest, fromText) {
    scoped_ptr<HWAddr> hwaddr;
    // Create HWAddr from text.
    ASSERT_NO_THROW(<--- There is an unknown macro here somewhere. Configuration is required. If ASSERT_NO_THROW is a macro then please configure it.
        hwaddr.reset(new HWAddr(HWAddr::fromText("00:01:A:bc:d:67")));
    );
    EXPECT_EQ("00:01:0a:bc:0d:67", hwaddr->toText(false));

    // HWAddr class should allow empty address.
    ASSERT_NO_THROW(
        hwaddr.reset(new HWAddr(HWAddr::fromText("")));
    );
    EXPECT_TRUE(hwaddr->toText(false).empty());

    // HWAddr should not allow multiple consecutive colons.
    EXPECT_THROW(
       hwaddr.reset(new HWAddr(HWAddr::fromText("00::01:00:bc:0d:67"))),
       isc::BadValue
    );

    // There should be no more than two digits per byte of the HW addr.
    EXPECT_THROW(
       hwaddr.reset(new HWAddr(HWAddr::fromText("00:01:00A:bc:0d:67"))),
       isc::BadValue
    );

}

// Checks that 16 bits values can be stored in HWaddr
TEST(HWAddrTest, 16bits) {

    uint8_t data[] = {0, 1, 2, 3, 4, 5};
    uint16_t htype = 257;
    HWAddrPtr hw(new HWAddr(data, sizeof(data), htype));

    EXPECT_EQ("hwtype=257 00:01:02:03:04:05", hw->toText());


}

} // end of anonymous namespace