1 // Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 #include <cc/server_tag.h>
9 #include <exceptions/exceptions.h>
10 #include <boost/scoped_ptr.hpp>
11 #include <gtest/gtest.h>
12 #include <string>
13 
14 using namespace isc;
15 using namespace isc::data;
16 
17 namespace {
18 
19 // This test verifies that the constructors of the ServerTag class
20 // work properly.
TEST(ServerTagTest,constructors)21 TEST(ServerTagTest, constructors) {
22     boost::scoped_ptr<ServerTag> tag;
23 
24     {
25         SCOPED_TRACE("default constructor for all servers");
26         ASSERT_NO_THROW(tag.reset(new ServerTag()));
27         EXPECT_EQ(ServerTag::ALL, tag->get());
28         EXPECT_TRUE(tag->amAll());
29     }
30 
31     {
32         SCOPED_TRACE("all servers");
33         ASSERT_NO_THROW(tag.reset(new ServerTag(ServerTag::ALL)));
34         EXPECT_EQ(ServerTag::ALL, tag->get());
35         EXPECT_TRUE(tag->amAll());
36     }
37 
38     {
39         SCOPED_TRACE("no whitespace");
40         ASSERT_NO_THROW(tag.reset(new ServerTag("xyz")));
41         EXPECT_EQ("xyz", tag->get());
42         EXPECT_FALSE(tag->amAll());
43     }
44 
45     {
46         SCOPED_TRACE("leading whitespace");
47         ASSERT_NO_THROW(tag.reset(new ServerTag("  left")));
48         EXPECT_EQ("left", tag->get());
49         EXPECT_FALSE(tag->amAll());
50     }
51 
52     {
53         SCOPED_TRACE("terminating whitespace");
54         ASSERT_NO_THROW(tag.reset(new ServerTag("right  ")));
55         EXPECT_EQ("right", tag->get());
56         EXPECT_FALSE(tag->amAll());
57     }
58 
59     {
60         SCOPED_TRACE("leading and terminating whitespace");
61         ASSERT_NO_THROW(tag.reset(new ServerTag("  both left-right  ")));
62         EXPECT_EQ("both left-right", tag->get());
63         EXPECT_FALSE(tag->amAll());
64     }
65 
66     {
67         SCOPED_TRACE("upper to lower case");
68         ASSERT_NO_THROW(tag.reset(new ServerTag("UPPER CASE TAG")));
69         EXPECT_EQ("upper case tag", tag->get());
70         EXPECT_FALSE(tag->amAll());
71     }
72 }
73 
74 // This test verifies that malformed server tags are rejected.
TEST(ServerTagTest,malformed)75 TEST(ServerTagTest, malformed) {
76     {
77         SCOPED_TRACE("empty tag");
78         EXPECT_THROW(ServerTag(""), BadValue);
79     }
80 
81     {
82         SCOPED_TRACE("only whitespaces");
83         EXPECT_THROW(ServerTag("   "), BadValue);
84     }
85 
86     {
87         SCOPED_TRACE("too long tag, max is 256");
88         EXPECT_THROW(ServerTag(std::string(257, 'c')), BadValue);
89     }
90 
91     {
92         SCOPED_TRACE("use reserved keyword any as a tag");
93         EXPECT_THROW(ServerTag("any"), BadValue);
94     }
95 }
96 
97 }
98