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
// Copyright (C) 2011-2024 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/.

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

#include <config.h>

#include <testutils/gtest_utils.h>
#include <util/buffer.h>
#include <util/io.h>

#include <bitset><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstddef><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdint><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <arpa/inet.h><--- 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 isc;
using namespace isc::util;
using namespace std;

namespace {

struct ReadWriteUintTest : ::testing::Test {
    template <typename uint_t>
    string showDiff(uint_t const left, uint8_t* const right) {
        bitset<8 * sizeof(uint_t)> const bitset_left(left);
        bitset<8 * sizeof(uint_t)> const bitset_right(*(reinterpret_cast<uint_t*>(right)));
        string const bitstring_left(separateBytes(bitset_left.to_string()));
        string const bitstring_right(separateBytes(bitset_right.to_string()));
        string const diff(::testing::internal::edit_distance::CreateUnifiedDiff({bitstring_left},
                                                                                {bitstring_right}));
        return (diff);
    }

private:
    string separateBytes(string const& input) {
        size_t const position(8);
        stringstream ss;
        for (size_t i = 0; i < input.size(); i = i + position) {
            if (i) {
                ss << " ";
            }
            ss << input.substr(i, position);
        }
        return (ss.str());
    }
};

/// @brief Check whether uint16_t can be read from a buffer properly.
TEST_F(ReadWriteUintTest, readUint16) {<--- syntax error
    uint16_t const test16[] = {0, 1, 2000, 0x8000, 0xffff};
    uint8_t data[] = {0, 0, 0, 0};

    // Make sure that we can read data, regardless of the memory alignment.
    // That is why we need to repeat it 2 times.
    for (size_t offset = 0; offset < sizeof(uint16_t); ++offset) {
        for (size_t i = 0; i < sizeof(test16) / sizeof(uint16_t); ++i) {
            SCOPED_TRACE("offset " + to_string(offset) + ", iteration " + to_string(i));

            uint16_t tmp = htons(test16[i]);
            memcpy(&data[offset], &tmp, sizeof(uint16_t));

            EXPECT_EQ(test16[i], readUint16(&data[offset], sizeof(uint16_t)));
        }
    }
}

/// @brief Check whether reading an uint16_t results in an exception.
TEST_F(ReadWriteUintTest, readUint16OutOfRange) {
    uint8_t data[] = {0};
    EXPECT_THROW_MSG(readUint16(data, 1), OutOfRange,
                     "Expected buffer to be long enough to read a 2-byte integer, but got 1 byte "
                     "instead");
}

/// @brief Check whether uint16_t can be written to a buffer properly.
TEST_F(ReadWriteUintTest, writeUint16) {
    uint16_t const test16[] = {0, 1, 2000, 0x8000, 0xffff};
    uint8_t data[4];

    // Make sure that we can write data, regardless of the memory alignment.
    // That's why we need to repeat 2 times.
    for (size_t offset = 0; offset < sizeof(uint16_t); ++offset) {
        for (size_t i = 0; i < sizeof(test16) / sizeof(uint16_t); ++i) {
            SCOPED_TRACE("offset " + to_string(offset) + ", iteration " + to_string(i));

            uint8_t* ptr = writeUint16(test16[i], &data[offset], sizeof(uint16_t));

            EXPECT_EQ(&data[offset] + sizeof(uint16_t), ptr);

            uint16_t tmp = htons(test16[i]);

            EXPECT_EQ(0, memcmp(&tmp, &data[offset], sizeof(uint16_t)))
                << showDiff(tmp, &data[offset]);
        }
    }
}

/// @brief Check whether writing an uint16_t results in an exception.
TEST_F(ReadWriteUintTest, writeUint16OutOfRange) {
    uint16_t i16 = 42;
    uint8_t data[1];
    EXPECT_THROW_MSG(writeUint16(i16, data, sizeof(data)), OutOfRange,
                     "Expected buffer to be long enough to write a 2-byte integer, but got 1 byte "
                     "instead");
}

/// @brief Check whether uint32_t can be read from a buffer properly.
TEST_F(ReadWriteUintTest, readUint32) {
    uint32_t const test32[] = {0, 1, 2000, 0x80000000, 0xffffffff};
    uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0};

    // Make sure that we can read data, regardless of the memory alignment.
    // That is why we need to repeat it 4 times.
    for (size_t offset = 0; offset < sizeof(uint32_t); ++offset) {
        for (size_t i = 0; i < sizeof(test32) / sizeof(uint32_t); ++i) {
            SCOPED_TRACE("offset " + to_string(offset) + ", iteration " + to_string(i));

            uint32_t tmp = htonl(test32[i]);
            memcpy(&data[offset], &tmp, sizeof(uint32_t));

            EXPECT_EQ(test32[i], readUint32(&data[offset], sizeof(uint32_t)));
        }
    }
}

/// @brief Check whether reading an uint32_t results in an exception.
TEST_F(ReadWriteUintTest, readUint32OutOfRange) {
    uint8_t data[] = {0, 0, 0};
    EXPECT_THROW_MSG(readUint32(data, 3), OutOfRange,
                     "Expected buffer to be long enough to read a 4-byte integer, but got 3 bytes "
                     "instead");
}

/// @brief Check whether uint32_t can be written to a buffer properly.
TEST_F(ReadWriteUintTest, writeUint32) {
    uint32_t const test32[] = {0, 1, 2000, 0x80000000, 0xffffffff};
    uint8_t data[8];

    // Make sure that we can write data, regardless of the memory alignment.
    // That's why we need to repeat 4 times.
    for (size_t offset = 0; offset < sizeof(uint32_t); ++offset) {
        for (size_t i = 0; i < sizeof(test32) / sizeof(uint32_t); ++i) {
            SCOPED_TRACE("offset " + to_string(offset) + ", iteration " + to_string(i));

            uint8_t* ptr = writeUint32(test32[i], &data[offset], sizeof(uint32_t));

            EXPECT_EQ(&data[offset] + sizeof(uint32_t), ptr);

            uint32_t tmp = htonl(test32[i]);

            EXPECT_EQ(0, memcmp(&tmp, &data[offset], sizeof(uint32_t)))
                << showDiff(tmp, &data[offset]);
        }
    }
}

/// @brief Check whether writing an uint32_t results in an exception.
TEST_F(ReadWriteUintTest, writeUint32OutOfRange) {
    uint32_t i32 = 28;
    uint8_t data[3];
    EXPECT_THROW_MSG(writeUint32(i32, data, sizeof(data)), OutOfRange,
                     "Expected buffer to be long enough to write a 4-byte integer, but got 3 bytes "
                     "instead");
}

/// @brief Check whether uint64_t can be read from a buffer properly.
TEST_F(ReadWriteUintTest, readUint64) {
    uint64_t const test64[] = {0, 1, 2000, 0x80000000, 0xffffffff, 0xfffffffffffffff};
    uint8_t data[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    // Make sure that we can read data, regardless of the memory alignment.
    // That is why we need to repeat it 8 times.
    for (size_t offset = 0; offset < sizeof(uint64_t); ++offset) {
        for (size_t i = 0; i < sizeof(test64) / sizeof(uint64_t); ++i) {
            SCOPED_TRACE("offset " + to_string(offset) + ", iteration " + to_string(i));

            uint64_t tmp = (uint64_t(htonl(test64[i])) << 32) | htonl(test64[i] >> 32);
            memcpy(&data[offset], &tmp, sizeof(uint64_t));

            EXPECT_EQ(test64[i], readUint64(&data[offset], sizeof(uint64_t)));
        }
    }
}

/// @brief Check whether reading an uint64_t results in an exception.
TEST_F(ReadWriteUintTest, readUint64OutOfRange) {
    uint8_t buf[] = {0, 0, 0, 0, 0, 0, 0};

    EXPECT_THROW_MSG(readUint64(buf, 7), OutOfRange,
                     "Expected buffer to be long enough to read a 8-byte integer, but got 7 bytes "
                     "instead");
}

/// @brief Check whether uint64 can be written to a buffer properly.
TEST_F(ReadWriteUintTest, writeUint64) {
    uint64_t const test64[] = {0, 1, 2000, 0x80000000, 0xffffffff, 0xfffffffffffffff};
    uint8_t data[16];

    // Make sure that we can write data, regardless of the memory alignment.
    // That's why we need to repeat 8 times.
    for (size_t offset = 0; offset < sizeof(uint64_t); ++offset) {
        for (size_t i = 0; i < sizeof(test64) / sizeof(uint64_t); ++i) {
            SCOPED_TRACE("offset " + to_string(offset) + ", iteration " + to_string(i));

            uint8_t* ptr = writeUint64(test64[i], &data[offset], sizeof(uint64_t));

            EXPECT_EQ(&data[offset] + sizeof(uint64_t), ptr);

            uint64_t tmp = (uint64_t(htonl(test64[i])) << 32) | htonl(test64[i] >> 32);

            EXPECT_EQ(0, memcmp(&tmp, &data[offset], sizeof(uint64_t)))
                << showDiff(tmp, &data[offset]);
        }
    }
}

/// @brief Check whether writing an uint64_t results in an exception.
TEST_F(ReadWriteUintTest, writeUint64OutOfRange) {
    uint64_t i64 = 28;
    uint8_t data[7];
    EXPECT_THROW_MSG(writeUint64(i64, data, sizeof(data)), OutOfRange,
                     "Expected buffer to be long enough to write a 8-byte integer, but got 7 bytes "
                     "instead");
}

}  // namespace