io_utilities_unittest.cc 3.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// 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.

/// \brief Test of asiolink utilties
///
/// Tests the fuctionality of the asiolink utilities code by comparing them
/// with the equivalent methods in isc::dns::[Input/Output]Buffer.

#include <cstddef>

22
#include <arpa/inet.h>
23
24
#include <gtest/gtest.h>

25
26
#include <util/buffer.h>
#include <util/io_utilities.h>
27

28
using namespace isc::util;
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

TEST(asioutil, readUint16) {

    // Reference buffer
    uint8_t data[2];
    InputBuffer buffer(data, sizeof(data));

    // Avoid possible compiler warnings by only setting uint8_t variables to
    // uint8_t values.
    uint8_t i8 = 0;
    uint8_t j8 = 0;
    for (int i = 0; i < (2 << 8); ++i, ++i8) {
        for (int j = 0; j < (2 << 8); ++j, ++j8) {
            data[0] = i8;
            data[1] = j8;
            buffer.setPosition(0);
            EXPECT_EQ(buffer.readUint16(), readUint16(data));
        }
    }
}


TEST(asioutil, writeUint16) {

    // Reference buffer
    OutputBuffer buffer(2);
    uint8_t test[2];

    // Avoid possible compiler warnings by only setting uint16_t variables to
    // uint16_t values.
    uint16_t i16 = 0;
    for (uint32_t i = 0; i < (2 << 16); ++i, ++i16) {

        // Write the reference data
        buffer.clear();
        buffer.writeUint16(i16);

        // ... and the test data
        writeUint16(i16, test);

        // ... and compare
        const uint8_t* ref = static_cast<const uint8_t*>(buffer.getData());
        EXPECT_EQ(ref[0], test[0]);
        EXPECT_EQ(ref[1], test[1]);
    }
}
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

// test data shared amount readUint32 and writeUint32 tests
const static uint32_t test32[] = {
    0,
    1,
    2000,
    0x80000000,
    0xffffffff
};

TEST(asioutil, readUint32) {
    uint8_t data[8];

    // make sure that we can read data, regardless of
    // the memory alignment. That' why we need to repeat
    // it 4 times.
91
    for (int offset=0; offset < 4; offset++) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
92
        for (int i=0; i < sizeof(test32)/sizeof(uint32_t); i++) {
93
            uint32_t tmp = htonl(test32[i]);
94
            memcpy(&data[offset], &tmp, sizeof(uint32_t));
95

96
            EXPECT_EQ(test32[i], readUint32(&data[offset]));
97
98
99
100
101
102
103
104
105
106
107
        }
    }
}


TEST(asioutil, writeUint32) {
    uint8_t data[8];

    // make sure that we can write data, regardless of
    // the memory alignment. That's why we need to repeat
    // it 4 times.
108
    for (int offset=0; offset < 4; offset++) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
109
        for (int i=0; i < sizeof(test32)/sizeof(uint32_t); i++) {
110
111
            uint8_t* ptr = writeUint32(test32[i], &data[offset]);

112
            EXPECT_EQ(&data[offset]+sizeof(uint32_t), ptr);
113
114
115

            uint32_t tmp = htonl(test32[i]);

116
            EXPECT_EQ(0, memcmp(&tmp, &data[offset], sizeof(uint32_t)));
117
118
119
        }
    }
}