summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/tests/opcode_unittest.cc
blob: 9bc60b5b7ea3c667e986e3e6e544921fc56c814a (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
// 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 <vector>
#include <sstream>

#include <exceptions/exceptions.h>

#include <dns/opcode.h>

#include <gtest/gtest.h>

using namespace std;
using namespace isc::dns;

namespace {
TEST(OpcodeTest, construct) {
    // This test also tests getCode()
    EXPECT_EQ(0, Opcode(0).getCode());
    EXPECT_EQ(15, Opcode(Opcode::RESERVED15_CODE).getCode());

    EXPECT_THROW(Opcode(16), isc::OutOfRange);
}

TEST(OpcodeTest, constants) {
    // We'll only test arbitrarily chosen subsets of the codes.
    // This class is quite simple, so it should be suffice.

    EXPECT_EQ(Opcode::QUERY_CODE, Opcode(0).getCode());
    EXPECT_EQ(Opcode::IQUERY_CODE, Opcode(1).getCode());
    EXPECT_EQ(Opcode::NOTIFY_CODE, Opcode(4).getCode());
    EXPECT_EQ(Opcode::UPDATE_CODE, Opcode(5).getCode());
    EXPECT_EQ(Opcode::RESERVED15_CODE, Opcode(15).getCode());

    EXPECT_EQ(Opcode::QUERY_CODE, Opcode::QUERY().getCode());
    EXPECT_EQ(Opcode::IQUERY_CODE, Opcode::IQUERY().getCode());
    EXPECT_EQ(Opcode::NOTIFY_CODE, Opcode::NOTIFY().getCode());
    EXPECT_EQ(Opcode::UPDATE_CODE, Opcode::UPDATE().getCode());
    EXPECT_EQ(Opcode::RESERVED15_CODE, Opcode::RESERVED15().getCode());
}

TEST(OpcodeTest, equal) {
    EXPECT_TRUE(Opcode::QUERY() == Opcode(Opcode::QUERY_CODE));
    EXPECT_TRUE(Opcode::QUERY().equals(Opcode(Opcode::QUERY_CODE)));
    EXPECT_TRUE(Opcode::IQUERY() == Opcode(Opcode::IQUERY_CODE));
    EXPECT_TRUE(Opcode::IQUERY().equals(Opcode(Opcode::IQUERY_CODE)));
    EXPECT_TRUE(Opcode::NOTIFY() == Opcode(Opcode::NOTIFY_CODE));
    EXPECT_TRUE(Opcode::NOTIFY().equals(Opcode(Opcode::NOTIFY_CODE)));
    EXPECT_TRUE(Opcode::UPDATE() == Opcode(Opcode::UPDATE_CODE));
    EXPECT_TRUE(Opcode::UPDATE().equals(Opcode(Opcode::UPDATE_CODE)));
    EXPECT_TRUE(Opcode::RESERVED15() == Opcode(Opcode::RESERVED15()));
    EXPECT_TRUE(Opcode::RESERVED15().equals(Opcode(Opcode::RESERVED15())));
}

TEST(OpcodeTest, nequal) {
    EXPECT_TRUE(Opcode::QUERY() != Opcode::IQUERY());
    EXPECT_TRUE(Opcode::QUERY().nequals(Opcode::IQUERY()));
    EXPECT_TRUE(Opcode::NOTIFY() != Opcode(1));
    EXPECT_TRUE(Opcode::NOTIFY().nequals(Opcode(1)));
    EXPECT_TRUE(Opcode(10) != Opcode(11));
    EXPECT_TRUE(Opcode(10).nequals(Opcode(11)));
}

TEST(OpcodeTest, toText) {
    vector<const char*> expects;
    expects.resize(Opcode::RESERVED15_CODE + 1);
    expects[Opcode::QUERY_CODE] = "QUERY";
    expects[Opcode::IQUERY_CODE] = "IQUERY";
    expects[Opcode::STATUS_CODE] = "STATUS";
    expects[Opcode::RESERVED3_CODE] = "RESERVED3";
    expects[Opcode::NOTIFY_CODE] = "NOTIFY";
    expects[Opcode::UPDATE_CODE] = "UPDATE";
    expects[Opcode::RESERVED6_CODE] = "RESERVED6";
    expects[Opcode::RESERVED7_CODE] = "RESERVED7";
    expects[Opcode::RESERVED8_CODE] = "RESERVED8";
    expects[Opcode::RESERVED9_CODE] = "RESERVED9";
    expects[Opcode::RESERVED10_CODE] = "RESERVED10";
    expects[Opcode::RESERVED11_CODE] = "RESERVED11";
    expects[Opcode::RESERVED12_CODE] = "RESERVED12";
    expects[Opcode::RESERVED13_CODE] = "RESERVED13";
    expects[Opcode::RESERVED14_CODE] = "RESERVED14";
    expects[Opcode::RESERVED15_CODE] = "RESERVED15";

    for (unsigned int i = 0; i <= Opcode::RESERVED15_CODE; ++i) {
        EXPECT_EQ(expects.at(i), Opcode(i).toText());
    }
}

// test operator<<.  We simply confirm it appends the result of toText().
TEST(OpcodeTest, LeftShiftOperator) {
    ostringstream oss;
    oss << Opcode::NOTIFY();
    EXPECT_EQ(Opcode::NOTIFY().toText(), oss.str());
}
}