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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// 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 <dhcp/dhcp6.h>
#include <dhcp/option.h>
#include <dhcp/option6_iaaddr.h>
#include <dhcp/option_int_array.h>
#include <util/buffer.h>

#include <boost/pointer_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#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.

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

namespace {

/// @brief OptionIntArray test class.
class OptionIntArrayTest : public ::testing::Test {
public:
    /// @brief Constructor.
    ///
    /// Initializes the option buffer with some data.
    OptionIntArrayTest(): buf_(255), out_buf_(255) {
        for (unsigned i = 0; i < 255; i++) {
            buf_[i] = 255 - i;
        }
    }

    /// @brief Test parsing buffer into array of int8_t or uint8_t values.
    ///
    /// @warning this function does not perform type check. Make
    /// sure that only int8_t or uint8_t type is used.
    ///
    /// @param u universe (v4 or V6).
    /// @tparam T int8_t or uint8_t.
    template<typename T>
    void bufferToIntTest8(const Option::Universe u) {
        // Create option that conveys array of multiple uint8_t or int8_t values.
        // In fact there is no need to use this template class for array
        // of uint8_t values because Option class is sufficient - it
        // returns the buffer which is actually the array of uint8_t.
        // However, since we allow using uint8_t types with this template
        // class we have to test it here.
        boost::shared_ptr<OptionIntArray<T> > opt;
        const int opt_len = 10;
        const uint16_t opt_code = 80;

        // Constructor throws exception if provided buffer is empty.
        EXPECT_THROW(
            OptionIntArray<T>(u, opt_code, buf_.begin(), buf_.begin()),
            isc::OutOfRange
        );

        // Provided buffer is not empty so it should not throw exception.
        ASSERT_NO_THROW(
            opt = boost::shared_ptr<
                OptionIntArray<T> >(new OptionIntArray<T>(u, opt_code, buf_.begin(),
                                                          buf_.begin() + opt_len))
        );

        EXPECT_EQ(u, opt->getUniverse());
        EXPECT_EQ(opt_code, opt->getType());
        // Option should return the collection of int8_t or uint8_t values that
        // we can match with the buffer we used to create the option.
        std::vector<T> values = opt->getValues();
        // We need to copy values from the buffer to apply sign if signed
        // type is used.
        std::vector<T> reference_values;
        for (int i = 0; i < opt_len; ++i) {
            // Values have been read from the buffer in network
            // byte order. We put them back in the same order here.
            reference_values.push_back(static_cast<T>(buf_[i]));
        }

        // Compare the values against the reference buffer.
        ASSERT_EQ(opt_len, values.size());
        EXPECT_TRUE(std::equal(reference_values.begin(), reference_values.begin()
                               + opt_len, values.begin()));

        // test for pack()
        opt->pack(out_buf_);

        // Data length is 10 bytes.
        EXPECT_EQ(10, opt->len() - opt->getHeaderLen());
        EXPECT_EQ(opt_code, opt->getType());

        // Check if pack worked properly:
        InputBuffer out(out_buf_.getData(), out_buf_.getLength());

        if (u == Option::V4) {
            // The total length is 10 bytes for data and 2 bytes for a header.
            ASSERT_EQ(12, out_buf_.getLength());
            // if option type is correct
            EXPECT_EQ(opt_code, out.readUint8());
            // if option length is correct
            EXPECT_EQ(10, out.readUint8());
        } else {
            // The total length is 10 bytes for data and 4 bytes for a header.
            ASSERT_EQ(14, out_buf_.getLength());
            // if option type is correct
            EXPECT_EQ(opt_code, out.readUint16());
            // if option length is correct
            EXPECT_EQ(10, out.readUint16());
        }

        // if data is correct
        std::vector<uint8_t> out_data;
        ASSERT_NO_THROW(out.readVector(out_data, opt_len));
        ASSERT_EQ(opt_len, out_data.size());
        EXPECT_TRUE(std::equal(buf_.begin(), buf_.begin() + opt_len, out_data.begin()));;
    }

    /// @brief Test parsing buffer into array of int16_t or uint16_t values.
    ///
    /// @warning this function does not perform type check. Make
    /// sure that only int16_t or uint16_t type is used.
    ///
    /// @param u universe (V4 or V6).
    /// @tparam T int16_t or uint16_t.
    template<typename T>
    void bufferToIntTest16(const Option::Universe u) {
        // Create option that conveys array of multiple uint16_t or int16_t values.
        boost::shared_ptr<OptionIntArray<T> > opt;
        const int opt_len = 20;
        const uint16_t opt_code = 81;

        // Constructor throws exception if provided buffer is empty.
        EXPECT_THROW(
            OptionIntArray<T>(u, opt_code, buf_.begin(), buf_.begin()),
            isc::OutOfRange
        );

        // Constructor throws exception if provided buffer's length is not
        // multiple of 2-bytes.
        EXPECT_THROW(
            OptionIntArray<T>(u, opt_code, buf_.begin(), buf_.begin() + 5),
            isc::OutOfRange
        );

        // Now the buffer length is correct.
        ASSERT_NO_THROW(
            opt = boost::shared_ptr<
                OptionIntArray<T> >(new OptionIntArray<T>(u, opt_code, buf_.begin(),
                                                          buf_.begin() + opt_len))
        );

        EXPECT_EQ(u, opt->getUniverse());
        EXPECT_EQ(opt_code, opt->getType());
        // Option should return vector of uint16_t values which should be
        // constructed from the buffer we provided.
        std::vector<T> values = opt->getValues();
        ASSERT_EQ(opt_len, values.size() * sizeof(T));
        // Create reference values from the buffer so as we can
        // simply compare two vectors.
        std::vector<T> reference_values;
        for (int i = 0; i < opt_len; i += 2) {
            reference_values.push_back((buf_[i] << 8) |
                                       buf_[i + 1]);
        }
        EXPECT_TRUE(std::equal(reference_values.begin(), reference_values.end(),
                               values.begin()));

        // Test for pack()
        opt->pack(out_buf_);

        // Data length is 20 bytes.
        EXPECT_EQ(20, opt->len() - opt->getHeaderLen());
        EXPECT_EQ(opt_code, opt->getType());

        // Check if pack worked properly:
        InputBuffer out(out_buf_.getData(), out_buf_.getLength());

        if (u == Option::V4) {
            // The total length is 20 bytes for data and 2 bytes for a header.
            ASSERT_EQ(22, out_buf_.getLength());
            // if option type is correct
            EXPECT_EQ(opt_code, out.readUint8());
            // if option length is correct
            EXPECT_EQ(20, out.readUint8());
        } else {
            // The total length is 20 bytes for data and 4 bytes for a header.
            ASSERT_EQ(24, out_buf_.getLength());
            // if option type is correct
            EXPECT_EQ(opt_code, out.readUint16());
            // if option length is correct
            EXPECT_EQ(20, out.readUint16());
        }
        // if data is correct
        std::vector<uint8_t> out_data;
        ASSERT_NO_THROW(out.readVector(out_data, opt_len));
        ASSERT_EQ(opt_len, out_data.size());
        EXPECT_TRUE(std::equal(buf_.begin(), buf_.begin() + opt_len, out_data.begin()));;
    }

    /// @brief Test parsing buffer into array of int32_t or uint32_t values.
    ///
    /// @warning this function does not perform type check. Make
    /// sure that only int32_t or uint32_t type is used.
    ///
    /// @param u universe (V4 or V6)
    /// @tparam T int32_t or uint32_t.
    template<typename T>
    void bufferToIntTest32(const Option::Universe u) {
        // Create option that conveys array of multiple uint16_t values.
        boost::shared_ptr<OptionIntArray<T> > opt;
        const int opt_len = 40;
        const uint16_t opt_code = 82;

        // Constructor throws exception if provided buffer is empty.
        EXPECT_THROW(
            OptionIntArray<T>(u, opt_code, buf_.begin(), buf_.begin()),
            isc::OutOfRange
        );

        // Constructor throws exception if provided buffer's length is not
        // multiple of 4-bytes.
        EXPECT_THROW(
            OptionIntArray<T>(u, opt_code, buf_.begin(), buf_.begin() + 9),
            isc::OutOfRange
        );

        // Now the buffer length is correct.
        ASSERT_NO_THROW(
            opt = boost::shared_ptr<
                OptionIntArray<T> >(new OptionIntArray<T>(u, opt_code, buf_.begin(),
                                                          buf_.begin() + opt_len))
        );

        EXPECT_EQ(u, opt->getUniverse());
        EXPECT_EQ(opt_code, opt->getType());
        // Option should return vector of uint32_t values which should be
        // constructed from the buffer we provided.
        std::vector<T> values = opt->getValues();
        ASSERT_EQ(opt_len, values.size() * sizeof(T));
        // Create reference values from the buffer so as we can
        // simply compare two vectors.
        std::vector<T> reference_values;
        for (int i = 0; i < opt_len; i += 4) {
            reference_values.push_back((buf_[i] << 24) |
                                       (buf_[i + 1] << 16 & 0x00FF0000) |
                                       (buf_[i + 2] << 8 & 0xFF00) |
                                       (buf_[i + 3] & 0xFF));
        }
        EXPECT_TRUE(std::equal(reference_values.begin(), reference_values.end(),
                               values.begin()));

        // Test for pack()
        opt->pack(out_buf_);

        // Data length is 40 bytes.
        EXPECT_EQ(40, opt->len() - opt->getHeaderLen());
        EXPECT_EQ(opt_code, opt->getType());

        // Check if pack worked properly:
        InputBuffer out(out_buf_.getData(), out_buf_.getLength());

        if (u == Option::V4) {
            // The total length is 40 bytes for data and 2 bytes for a header.
            ASSERT_EQ(42, out_buf_.getLength());
            // if option type is correct
            EXPECT_EQ(opt_code, out.readUint8());
            // if option length is correct
            EXPECT_EQ(40, out.readUint8());
        } else {
            // The total length is 40 bytes for data and 4 bytes for a header.
            ASSERT_EQ(44, out_buf_.getLength());
            // if option type is correct
            EXPECT_EQ(opt_code, out.readUint16());
            // if option length is correct
            EXPECT_EQ(40, out.readUint16());
        }

        // if data is correct
        std::vector<uint8_t> out_data;
        ASSERT_NO_THROW(out.readVector(out_data, opt_len));
        ASSERT_EQ(opt_len, out_data.size());
        EXPECT_TRUE(std::equal(buf_.begin(), buf_.begin() + opt_len, out_data.begin()));;
    }

    /// @brief Test ability to set all values.
    ///
    /// @tparam T numeric type to perform the test for.
    template<typename T>
    void setValuesTest() {
        const uint16_t opt_code = 100;
        // Create option with empty vector of values.
        boost::shared_ptr<OptionIntArray<T> >
            opt(new OptionIntArray<T>(Option::V6, opt_code));
        // Initialize vector with some data and pass to the option.
        std::vector<T> values;
        for (int i = 0; i < 10; ++i) {
            values.push_back(numeric_limits<uint8_t>::max() - i);
        }
        opt->setValues(values);

        // Check if universe, option type and data was set correctly.
        EXPECT_EQ(Option::V6, opt->getUniverse());
        EXPECT_EQ(opt_code, opt->getType());
        std::vector<T> returned_values = opt->getValues();
        EXPECT_TRUE(std::equal(values.begin(), values.end(), returned_values.begin()));
    }

    /// @brief Test ability to add values one by one.
    ///
    /// @tparam T numeric type to perform the test for.
    template<typename T>
    void addValuesTest() {
        const uint16_t opt_code = 100;
        // Create option with empty vector of values.
        boost::shared_ptr<OptionIntArray<T> >
            opt(new OptionIntArray<T>(Option::V6, opt_code));
        // Initialize vector with some data and add the same data
        // to the option.
        std::vector<T> values;
        for (int i = 0; i < 10; ++i) {
            values.push_back(numeric_limits<T>::max() - i);
            opt->addValue(numeric_limits<T>::max() - i);
        }

        // Check if universe, option type and data was set correctly.
        EXPECT_EQ(Option::V6, opt->getUniverse());
        EXPECT_EQ(opt_code, opt->getType());
        std::vector<T> returned_values = opt->getValues();
        EXPECT_TRUE(std::equal(values.begin(), values.end(), returned_values.begin()));
    }

    OptionBuffer buf_;     ///< Option buffer
    OutputBuffer out_buf_; ///< Output buffer
};

/// @todo: below, there is a bunch of tests for options that
/// convey unsigned values. We should maybe extend these tests for
/// signed types too.

TEST_F(OptionIntArrayTest, useInvalidType) {<--- syntax error
    const uint16_t opt_code = 80;
    EXPECT_THROW(
        boost::scoped_ptr<
            OptionIntArray<bool> >(new OptionIntArray<bool>(Option::V6, opt_code,
                                                            OptionBuffer(5))),
        InvalidDataType
    );

    EXPECT_THROW(
        boost::scoped_ptr<
            OptionIntArray<int64_t> >(new OptionIntArray<int64_t>(Option::V6,
                                                                  opt_code,
                                                                  OptionBuffer(10))),
        InvalidDataType
    );

}

TEST_F(OptionIntArrayTest, bufferToUint8V4) {
    bufferToIntTest8<uint8_t>(Option::V4);
}

TEST_F(OptionIntArrayTest, bufferToUint8V6) {
    bufferToIntTest8<uint8_t>(Option::V6);
}

TEST_F(OptionIntArrayTest, bufferToInt8V4) {
    bufferToIntTest8<int8_t>(Option::V4);
}

TEST_F(OptionIntArrayTest, bufferToInt8V6) {
    bufferToIntTest8<int8_t>(Option::V6);
}

TEST_F(OptionIntArrayTest, bufferToUint16V4) {
    bufferToIntTest16<uint16_t>(Option::V4);
}

TEST_F(OptionIntArrayTest, bufferToUint16V6) {
    bufferToIntTest16<uint16_t>(Option::V6);
}

TEST_F(OptionIntArrayTest, bufferToInt16V4) {
    bufferToIntTest16<int16_t>(Option::V4);
}

TEST_F(OptionIntArrayTest, bufferToInt16V6) {
    bufferToIntTest16<int16_t>(Option::V6);
}

TEST_F(OptionIntArrayTest, bufferToUint32V4) {
    bufferToIntTest32<uint32_t>(Option::V4);
}

TEST_F(OptionIntArrayTest, bufferToUint32V6) {
    bufferToIntTest32<uint32_t>(Option::V6);
}

TEST_F(OptionIntArrayTest, bufferToInt32V4) {
    bufferToIntTest32<int32_t>(Option::V4);
}

TEST_F(OptionIntArrayTest, bufferToInt32V6) {
    bufferToIntTest32<int32_t>(Option::V6);
}

TEST_F(OptionIntArrayTest, setValuesUint8) {
    setValuesTest<uint8_t>();
}

TEST_F(OptionIntArrayTest, setValuesInt8) {
    setValuesTest<int8_t>();
}

TEST_F(OptionIntArrayTest, setValuesUint16) {
    setValuesTest<uint16_t>();
}

TEST_F(OptionIntArrayTest, setValuesInt16) {
    setValuesTest<int16_t>();
}

TEST_F(OptionIntArrayTest, setValuesUint32) {
    setValuesTest<uint16_t>();
}

TEST_F(OptionIntArrayTest, setValuesInt32) {
    setValuesTest<int16_t>();
}

TEST_F(OptionIntArrayTest, addValuesUint8) {
    addValuesTest<uint8_t>();
}

TEST_F(OptionIntArrayTest, addValuesInt8) {
    addValuesTest<int8_t>();
}

TEST_F(OptionIntArrayTest, addValuesUint16) {
    addValuesTest<uint16_t>();
}

TEST_F(OptionIntArrayTest, addValuesInt16) {
    addValuesTest<int16_t>();
}

TEST_F(OptionIntArrayTest, addValuesUint32) {
    addValuesTest<uint16_t>();
}

TEST_F(OptionIntArrayTest, addValuesInt32) {
    addValuesTest<int16_t>();
}

// This test checks that the option is correctly converted into
// the textual format.
TEST_F(OptionIntArrayTest, toText) {
    OptionUint32Array option(Option::V4, 128);
    option.addValue(1);
    option.addValue(32);
    option.addValue(324);

    EXPECT_EQ("type=128, len=012: 1(uint32) 32(uint32) 324(uint32)",
              option.toText());
}

// This test checks that the option holding multiple uint8 values
// is correctly converted to the textual format.
TEST_F(OptionIntArrayTest, toTextUint8) {
    OptionUint8Array option(Option::V4, 128);
    option.addValue(1);
    option.addValue(7);
    option.addValue(15);

    EXPECT_EQ("type=128, len=003: 1(uint8) 7(uint8) 15(uint8)",
              option.toText());
}



} // anonymous namespace