summaryrefslogtreecommitdiffstats
path: root/src/lib/util/tests/hex_unittest.cc
blob: 1e611b51a30aab3b99c9cbcbeee78ce8b2ac2883 (plain)
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
// Copyright (C) 2010-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 <stdint.h>

#include <vector>
#include <string>

#include <exceptions/exceptions.h>

#include <util/encode/hex.h>

#include <gtest/gtest.h>

using namespace std;
using namespace isc;
using namespace isc::util::encode;

namespace {
const string hex_txt("DEADBEEFDECADE");
const string hex_txt_space("DEAD BEEF DECADE");
const string hex_txt_lower("deadbeefdecade");

class HexTest : public ::testing::Test {
protected:
    HexTest() : encoding_chars("0123456789ABCDEF") {}
    vector<uint8_t> decoded_data;
    const string encoding_chars;
};

TEST_F(HexTest, encodeHex) {
    std::vector<uint8_t> data;

    data.push_back(0xde);
    data.push_back(0xad);
    data.push_back(0xbe);
    data.push_back(0xef);
    data.push_back(0xde);
    data.push_back(0xca);
    data.push_back(0xde);
    EXPECT_EQ(hex_txt, encodeHex(data));
}

void
compareData(const std::vector<uint8_t>& data) {
    EXPECT_EQ(0xde, data[0]);
    EXPECT_EQ(0xad, data[1]);
    EXPECT_EQ(0xbe, data[2]);
    EXPECT_EQ(0xef, data[3]);
    EXPECT_EQ(0xde, data[4]);
    EXPECT_EQ(0xca, data[5]);
    EXPECT_EQ(0xde, data[6]);
}

TEST_F(HexTest, decodeHex) {
    std::vector<uint8_t> result;

    decodeHex(hex_txt, result);
    compareData(result);

    // lower case hex digits should be accepted
    result.clear();
    decodeHex(hex_txt_lower, result);
    compareData(result);

    // white space should be ignored
    result.clear();
    decodeHex(hex_txt_space, result);
    compareData(result);

    // Bogus input: should fail
    result.clear();
    EXPECT_THROW(decodeHex("1x", result), BadValue);

    // Bogus input: encoded string must have an even number of characters.
    result.clear();
    EXPECT_THROW(decodeHex("dea", result), BadValue);
}

// For Hex encode/decode we use handmade mappings, so it's prudent to test the
// entire mapping table explicitly.
TEST_F(HexTest, decodeMap) {
    string input("00");       // input placeholder

    // See Base32HexTest.decodeMap for details of the following tests.
    for (int i = 0; i < 256; ++i) {
        input[1] = i;

        const char ch = toupper(i);
        const size_t pos = encoding_chars.find(ch);
        if (pos == string::npos) {
            EXPECT_THROW(decodeHex(input, decoded_data), BadValue);
        } else {
            decodeHex(input, decoded_data);
            EXPECT_EQ(1, decoded_data.size());
            EXPECT_EQ(pos, decoded_data[0]);
        }
    }
}

TEST_F(HexTest, encodeMap) {
    for (uint8_t i = 0; i < 16; ++i) {
        decoded_data.clear();
        decoded_data.push_back(i);
        EXPECT_EQ(encoding_chars[i], encodeHex(decoded_data)[1]);
    }
}

}