hex_unittest.cc 2.29 KB
Newer Older
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
// Copyright (C) 2010  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.

// $Id: rrtype_unittest.cc 476 2010-01-19 00:29:28Z jinmei $

#include <stdint.h>

#include <vector>
#include <string>

#include <dns/hex.h>

#include <gtest/gtest.h>

26
#include <dns/tests/unittest_util.h>
27 28 29 30 31 32 33 34 35 36 37

using isc::UnitTestUtil;
using namespace std;
using namespace isc::dns;

namespace {
class HexTest : public ::testing::Test {
protected:
    HexTest() {}
};

38
const std::string hex_txt("DEADBEEFDECADE");
39
const std::string hex_txt_space("DEAD BEEF DECADE");
40
const std::string hex_txt_lower("deadbeefdecade");
41 42

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

45 46 47 48 49 50 51 52 53 54
    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));
}

55 56 57 58 59 60 61 62 63 64 65 66
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]);
}

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

70
    decodeHex(hex_txt, result);
71
    compareData(result);
72

73 74 75 76
    // lower case hex digits should be accepted
    result.clear();
    decodeHex(hex_txt_lower, result);
    compareData(result);
77

78 79 80 81 82
    // white space should be ignored
    result.clear();
    decodeHex(hex_txt_space, result);
    compareData(result);

83 84
    // Bogus input: should fail
    result.clear();
85
    EXPECT_THROW(decodeHex("1x", result), BadHexString);
86 87
}

88
}