root/trunk/whisperlib/net/util/test/ipclassifier_test.cc

Revision 7, 4.2 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31
32 #include "common/base/types.h"
33 #include "common/base/log.h"
34 #include "common/base/system.h"
35 #include "common/base/gflags.h"
36
37 #include "net/base/address.h"
38 #include "net/util/ipclassifier.h"
39
40 //////////////////////////////////////////////////////////////////////
41
42 DEFINE_string(spec,
43               "",
44               "IP classifier specification");
45 DEFINE_string(ip,
46               "",
47               "IP to test");
48
49 DECLARE_string(ip2location_classifier_db);
50
51 //////////////////////////////////////////////////////////////////////
52
53 using namespace net;
54 void TestClassifier() {
55   {
56     net::IpClassifier* ipc = net::IpClassifier::CreateClassifier(
57       "IPFILTER(1.2.3.4, 1.2.4.0/24)");
58     CHECK(ipc->IsInClass(IpAddress("1.2.3.4")));
59     CHECK(ipc->IsInClass(IpAddress("1.2.4.0")));
60     CHECK(ipc->IsInClass(IpAddress("1.2.4.10")));
61     CHECK(ipc->IsInClass(IpAddress("1.2.4.255")));
62     CHECK(!ipc->IsInClass(IpAddress("1.2.3.5")));
63     CHECK(!ipc->IsInClass(IpAddress("1.2.5.0")));
64   }
65   {
66     net::IpClassifier* ipc = net::IpClassifier::CreateClassifier(
67       "OR(IPFILTER(1.2.3.4), IPFILTER(1.2.4.0/24))");
68     CHECK(ipc->IsInClass(IpAddress("1.2.3.4")));
69     CHECK(ipc->IsInClass(IpAddress("1.2.4.10")));
70     CHECK(!ipc->IsInClass(IpAddress("1.2.5.0")));
71   }
72   {
73     net::IpClassifier* ipc = net::IpClassifier::CreateClassifier(
74       "NOT(OR(IPFILTER(1.2.3.4), IPFILTER(1.2.4.0/24)))");
75     CHECK(!ipc->IsInClass(IpAddress("1.2.3.4")));
76     CHECK(!ipc->IsInClass(IpAddress("1.2.4.10")));
77     CHECK(ipc->IsInClass(IpAddress("1.2.5.0")));
78   }
79   {
80     net::IpClassifier* ipc = net::IpClassifier::CreateClassifier(
81       "AND(IPFILTER(1.2.4.0/28), IPFILTER(1.2.4.0/24))");
82     CHECK(!ipc->IsInClass(IpAddress("1.2.3.4")));
83     CHECK(ipc->IsInClass(IpAddress("1.2.4.0")));
84     CHECK(!ipc->IsInClass(IpAddress("1.2.4.240")));
85   }
86   if ( !FLAGS_ip2location_classifier_db.empty() ) {
87     net::IpClassifier* ipc = net::IpClassifier::CreateClassifier(
88       "IPLOC(C:ROMANIA,CITY:BUCHAREST)");
89     for ( int i = 0; i < 100; i++ ) {
90       CHECK(ipc->IsInClass(IpAddress("193.19.192.20")));
91       CHECK(ipc->IsInClass(IpAddress("194.102.93.160")));
92       CHECK(!ipc->IsInClass(IpAddress("209.85.129.104")));
93     }
94   } else {
95     LOG_ERROR << " You need to specify --ip2location_classifier_db "
96               << "for IpLocationClassifier to work!";
97   }
98 }
99
100 int main(int argc, char* argv[]) {
101   common::Init(argc, argv);
102
103   if ( !FLAGS_spec.empty() ) {
104     net::IpClassifier* ipc = net::IpClassifier::CreateClassifier(FLAGS_spec);
105     net::IpAddress ip(FLAGS_ip.c_str());
106     const bool matches = ipc->IsInClass(ip);
107     delete ipc;
108     printf(matches ? "TRUE\n" : "FALSE\n");
109     return !matches;
110   } else {
111     TestClassifier();
112   }
113 }
Note: See TracBrowser for help on using the browser.