more query processor tests. Ignore coverage data
[python-rwhoisd.git] / test / TestQueryProcessor.py
1 # This file is part of python-rwhoisd
2 #
3 # Copyright (C) 2008 David E. Blacka
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 # USA
19
20
21 import path
22 import QueryProcessor, Session, MemDB, QueryParser
23 import unittest, types, StringIO
24
25
26 class TestHelperMethods(unittest.TestCase):
27
28     def testMatchCidr(self):
29         values = [ ("127.0.0.1/24", "127.0.0.0/24", True),
30                    ("127.0.0.0/24**", "127.0.0.8", True),
31                    ("127.0.0.0/24*", "127.0.0.0/23", True),
32                    ("127.0.0.0/24*", "127.0.0.1/24", True),
33                    ("127.0.0.10/24", "127.0.0.0/22", False) ]
34         for sv, tv, v in values:
35             self.assertEquals(QueryProcessor.match_cidr(sv, tv), v)
36
37     def testMatchValue(self):
38         values = [ ("foo", "foobar", False), ("foo*", "foobar", True), 
39                    ("*bar", "foobar", True), ("*bar", "foobara", False),
40                    ("127.0.0.0/24*", "127.0/8", True) ]
41         for sv, tv, v in values:
42             self.assertEquals(QueryProcessor.match_value(sv, tv), v)
43
44     def testMatchValues(self):
45         values = [ "bar", "127.0.0.1/32", "foobar", "fbar" ]
46         assert(QueryProcessor.match_values("foo*", values)) 
47         assert(QueryProcessor.match_values("127.0.0.0/24**", values))
48         assert(not QueryProcessor.match_values("*bart", values))
49
50     def testIsDomainName(self):
51         values = [ ("a.domain", True), ("not_a_domain", False),
52                    ("www.foo.com.", True), ("glor!.com", False),
53                    ("a_bad.domain", False) ]
54         for a, r in values:
55             assert(QueryProcessor.is_domainname(a) == r)
56
57     def testIsSubdomain(self):
58         values = [ ("sub.foo.domain", "domain", True),
59                    ("sub.bar.domain", "foo.domain", False),
60                    ("baz.domain", "a.baz.domain", False),
61                    ("a.com", "a.com", True) ]
62         for sub, domain, value in values:
63             self.assertEquals(QueryProcessor.is_subdomain(domain, sub), value)
64
65     def testReduceDomain(self):
66         values = [ ("a.com", "com"), ("b.com.", "com."),
67                    (".", ""), ("a.b.c.net", "b.c.net") ]
68         for d, v in values:
69             self.assertEquals(QueryProcessor.reduce_domain(d), v)
70
71
72 class TestProcessing(unittest.TestCase):
73     def setUp(self):
74         db = MemDB.MemDB()
75         db.init_schema("test_schema")
76         db.load_data("test_data")
77         db.index_data()
78         QueryParser.db = db
79         self.processor = QueryProcessor.QueryProcessor(db)
80         self.session = Session.Context()
81         self.session.wfile = StringIO.StringIO()
82
83
84     def testParseKnown(self):
85         queries = [ ('domain a.com', """domain:ID:333.a.com\r
86 domain:Auth-Area:a.com\r
87 domain:Class-Name:domain\r
88 domain:Guardian:444.a.com\r
89 domain:Domain-Name:a.com\r
90 domain:Primary-Server:5551.a.com\r
91 domain:Secondary-Server:5552.a.com\r
92 domain:Organization:777.a.com\r
93 domain:Admin-Contact:222.a.com\r
94 domain:Tech-Contact:222.a.com\r
95 domain:Billing-Contact:222.a.com\r
96 domain:Created:19961022\r
97 domain:Updated:19961023\r
98 domain:Updated-By:hostmaster@a.com\r
99 \r
100 %ok\r
101 """) ]
102
103         for q, s in queries:
104             self.processor.process_query(self.session, q)
105             res = self.session.wfile.getvalue()
106             self.assertEquals(res, s.lower())
107
108 if __name__ == "__main__":
109     unittest.main()