summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/AclHost.cpp
blob: 5fa87711e5c4b9e9993a90864241459000fefd28 (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
115
116
117
118
119
/*
 *
 * Copyright (c) 2014 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


#include "unit_test.h"
#include "test_tools.h"
#include "qpid/AclHost.h"
#include <boost/assign.hpp>

using namespace std;
using namespace qpid;
using namespace boost::assign;

namespace qpid {
namespace tests {

QPID_AUTO_TEST_SUITE(AclHostTestSuite)

#define ACLURL_CHECK_INVALID(STR) BOOST_CHECK_THROW(AclHost(STR), AclHost::Invalid)

QPID_AUTO_TEST_CASE(TestParseTcpIPv4) {
  BOOST_CHECK_EQUAL(AclHost("1.1.1.1").str(),         "(1.1.1.1,1.1.1.1)");
  BOOST_CHECK_EQUAL(AclHost("1.1.1.1,2.2.2.2").str(), "(1.1.1.1,2.2.2.2)");
}

// QPID_AUTO_TEST_CASE(TestParseTcpIPv6) {
//   BOOST_CHECK_EQUAL(AclHost("[::1]").str(),         "([::1],[::1])");
//   BOOST_CHECK_EQUAL(AclHost("[::1],::5").str(),     "([::1],[::5])");
// }

QPID_AUTO_TEST_CASE(TestParseAll) {
  BOOST_CHECK_EQUAL(AclHost("").str(), "(all)");
}

// QPID_AUTO_TEST_CASE(TestInvalidMixedIpFamilies) {
//   ACLURL_CHECK_INVALID("1.1.1.1,[::1]");
//   ACLURL_CHECK_INVALID("[::1],1.1.1.1");
// }

QPID_AUTO_TEST_CASE(TestMalformedIPv4) {
  ACLURL_CHECK_INVALID("1.1.1.1.1");
  ACLURL_CHECK_INVALID("1.1.1.777");
  ACLURL_CHECK_INVALID("1.1.1.1abcd");
  ACLURL_CHECK_INVALID("1.1.1.*");
}

QPID_AUTO_TEST_CASE(TestRangeWithInvertedSizeOrder) {
  ACLURL_CHECK_INVALID("1.1.1.100,1.1.1.1");
//   ACLURL_CHECK_INVALID("[FF::1],[::1]");
}

QPID_AUTO_TEST_CASE(TestSingleHostResolvesMultipleAddresses) {
  AclHost XX("localhost");
}

QPID_AUTO_TEST_CASE(TestMatchSingleAddresses) {
    AclHost host1("1.1.1.1");
    BOOST_CHECK(host1.match("1.1.1.1") == true);
    BOOST_CHECK(host1.match("1.2.1.1") == false);

//     AclHost host2("FF::1");
//     BOOST_CHECK(host2.match("00FF:0000::1") == true);
}

QPID_AUTO_TEST_CASE(TestMatchMultipleAddresses) {
    AclHost host1("localhost");
    BOOST_CHECK(host1.match("127.0.0.1") == true);
//     BOOST_CHECK(host1.match("::1")       == true);
    BOOST_CHECK(host1.match("128.1.1.1") == false);
//     BOOST_CHECK(host1.match("::abcd")    == false);
}

QPID_AUTO_TEST_CASE(TestMatchIPv4Range) {
    AclHost host1("192.168.0.0,192.168.255.255");
    BOOST_CHECK(host1.match("128.1.1.1")       == false);
    BOOST_CHECK(host1.match("192.167.255.255") == false);
    BOOST_CHECK(host1.match("192.168.0.0")     == true);
    BOOST_CHECK(host1.match("192.168.0.1")     == true);
    BOOST_CHECK(host1.match("192.168.1.0")     == true);
    BOOST_CHECK(host1.match("192.168.255.254") == true);
    BOOST_CHECK(host1.match("192.168.255.255") == true);
    BOOST_CHECK(host1.match("192.169.0.0")     == false);
//     BOOST_CHECK(host1.match("::1")             == false);
}

// QPID_AUTO_TEST_CASE(TestMatchIPv6Range) {
//     AclHost host1("::10,::1:0");
//     BOOST_CHECK(host1.match("::1")     == false);
//     BOOST_CHECK(host1.match("::f")     == false);
//     BOOST_CHECK(host1.match("::10")    == true);
//     BOOST_CHECK(host1.match("::11")    == true);
//     BOOST_CHECK(host1.match("::ffff")  == true);
//     BOOST_CHECK(host1.match("::1:0")   == true);
//     BOOST_CHECK(host1.match("::1:1")   == false);
//     BOOST_CHECK(host1.match("192.169.0.0")     == false);
//     AclHost host2("[fc00::],[fc00::ff]");
//     BOOST_CHECK(host2.match("fc00::")     == true);
//     BOOST_CHECK(host2.match("fc00::1")    == true);
//     BOOST_CHECK(host2.match("fc00::ff")   == true);
//     BOOST_CHECK(host2.match("fc00::100")  == false);
// }
QPID_AUTO_TEST_SUITE_END()

}} // namespace qpid::tests