3 Copyright (c) 2008, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/ip_filter.hpp"
34 #include <boost/utility.hpp>
40 Currently this test only tests that the filter can handle
41 IPv4 addresses. Maybe it should be extended to IPv6 as well,
42 but the actual code is just a template, so it is probably
43 pretty safe to assume that as long as it works for IPv4 it
48 using namespace libtorrent
;
51 bool compare(ip_range
<Addr
> const& lhs
52 , ip_range
<Addr
> const& rhs
)
54 return lhs
.first
== rhs
.first
55 && lhs
.last
== rhs
.last
56 && lhs
.flags
== rhs
.flags
;
59 void test_rules_invariant(std::vector
<ip_range
<address_v4
> > const& r
, ip_filter
const& f
)
61 typedef std::vector
<ip_range
<address_v4
> >::const_iterator iterator
;
62 TEST_CHECK(!r
.empty());
63 if (r
.empty()) return;
65 TEST_CHECK(r
.front().first
== address::from_string("0.0.0.0"));
66 TEST_CHECK(r
.back().last
== address::from_string("255.255.255.255"));
68 iterator i
= r
.begin();
69 iterator j
= boost::next(i
);
70 for (iterator
i(r
.begin()), j(boost::next(r
.begin()))
71 , end(r
.end()); j
!= end
; ++j
, ++i
)
73 TEST_CHECK(f
.access(i
->last
) == i
->flags
);
74 TEST_CHECK(f
.access(j
->first
) == j
->flags
);
75 TEST_CHECK(i
->last
.to_ulong() + 1 == j
->first
.to_ulong());
81 using namespace libtorrent
;
83 std::vector
<ip_range
<address_v4
> > range
;
85 // **** test joining of ranges at the end ****
86 ip_range
<address_v4
> expected1
[] =
88 {address_v4::from_string("0.0.0.0"), address_v4::from_string("0.255.255.255"), 0}
89 , {address_v4::from_string("1.0.0.0"), address_v4::from_string("3.0.0.0"), ip_filter::blocked
}
90 , {address_v4::from_string("3.0.0.1"), address_v4::from_string("255.255.255.255"), 0}
95 f
.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked
);
96 f
.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked
);
98 range
= boost::get
<0>(f
.export_filter());
99 test_rules_invariant(range
, f
);
101 TEST_CHECK(range
.size() == 3);
102 TEST_CHECK(std::equal(range
.begin(), range
.end(), expected1
, &compare
<address_v4
>));
106 // **** test joining of ranges at the start ****
110 f
.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked
);
111 f
.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked
);
113 range
= boost::get
<0>(f
.export_filter());
114 test_rules_invariant(range
, f
);
116 TEST_CHECK(range
.size() == 3);
117 TEST_CHECK(std::equal(range
.begin(), range
.end(), expected1
, &compare
<address_v4
>));
122 // **** test joining of overlapping ranges at the start ****
126 f
.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked
);
127 f
.add_rule(address::from_string("1.0.0.0"), address::from_string("2.4.0.0"), ip_filter::blocked
);
129 range
= boost::get
<0>(f
.export_filter());
130 test_rules_invariant(range
, f
);
132 TEST_CHECK(range
.size() == 3);
133 TEST_CHECK(std::equal(range
.begin(), range
.end(), expected1
, &compare
<address_v4
>));
138 // **** test joining of overlapping ranges at the end ****
142 f
.add_rule(address::from_string("1.0.0.0"), address::from_string("2.4.0.0"), ip_filter::blocked
);
143 f
.add_rule(address::from_string("2.0.0.1"), address::from_string("3.0.0.0"), ip_filter::blocked
);
145 range
= boost::get
<0>(f
.export_filter());
146 test_rules_invariant(range
, f
);
148 TEST_CHECK(range
.size() == 3);
149 TEST_CHECK(std::equal(range
.begin(), range
.end(), expected1
, &compare
<address_v4
>));
154 // **** test joining of multiple overlapping ranges 1 ****
158 f
.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked
);
159 f
.add_rule(address::from_string("3.0.0.0"), address::from_string("4.0.0.0"), ip_filter::blocked
);
160 f
.add_rule(address::from_string("5.0.0.0"), address::from_string("6.0.0.0"), ip_filter::blocked
);
161 f
.add_rule(address::from_string("7.0.0.0"), address::from_string("8.0.0.0"), ip_filter::blocked
);
163 f
.add_rule(address::from_string("1.0.1.0"), address::from_string("9.0.0.0"), ip_filter::blocked
);
165 range
= boost::get
<0>(f
.export_filter());
166 test_rules_invariant(range
, f
);
168 TEST_CHECK(range
.size() == 3);
169 ip_range
<address_v4
> expected
[] =
171 {address_v4::from_string("0.0.0.0"), address_v4::from_string("0.255.255.255"), 0}
172 , {address_v4::from_string("1.0.0.0"), address_v4::from_string("9.0.0.0"), ip_filter::blocked
}
173 , {address_v4::from_string("9.0.0.1"), address_v4::from_string("255.255.255.255"), 0}
176 TEST_CHECK(std::equal(range
.begin(), range
.end(), expected
, &compare
<address_v4
>));
180 // **** test joining of multiple overlapping ranges 2 ****
184 f
.add_rule(address::from_string("1.0.0.0"), address::from_string("2.0.0.0"), ip_filter::blocked
);
185 f
.add_rule(address::from_string("3.0.0.0"), address::from_string("4.0.0.0"), ip_filter::blocked
);
186 f
.add_rule(address::from_string("5.0.0.0"), address::from_string("6.0.0.0"), ip_filter::blocked
);
187 f
.add_rule(address::from_string("7.0.0.0"), address::from_string("8.0.0.0"), ip_filter::blocked
);
189 f
.add_rule(address::from_string("0.0.1.0"), address::from_string("7.0.4.0"), ip_filter::blocked
);
191 range
= boost::get
<0>(f
.export_filter());
192 test_rules_invariant(range
, f
);
194 TEST_CHECK(range
.size() == 3);
195 ip_range
<address_v4
> expected
[] =
197 {address_v4::from_string("0.0.0.0"), address_v4::from_string("0.0.0.255"), 0}
198 , {address_v4::from_string("0.0.1.0"), address_v4::from_string("8.0.0.0"), ip_filter::blocked
}
199 , {address_v4::from_string("8.0.0.1"), address_v4::from_string("255.255.255.255"), 0}
202 TEST_CHECK(std::equal(range
.begin(), range
.end(), expected
, &compare
<address_v4
>));
208 // default contructed port filter should allow any port
209 TEST_CHECK(pf
.access(0) == 0);
210 TEST_CHECK(pf
.access(65535) == 0);
211 TEST_CHECK(pf
.access(6881) == 0);
213 // block port 100 - 300
214 pf
.add_rule(100, 300, port_filter::blocked
);
216 TEST_CHECK(pf
.access(0) == 0);
217 TEST_CHECK(pf
.access(99) == 0);
218 TEST_CHECK(pf
.access(100) == port_filter::blocked
);
219 TEST_CHECK(pf
.access(150) == port_filter::blocked
);
220 TEST_CHECK(pf
.access(300) == port_filter::blocked
);
221 TEST_CHECK(pf
.access(301) == 0);
222 TEST_CHECK(pf
.access(6881) == 0);
223 TEST_CHECK(pf
.access(65535) == 0);