more unit test progress
[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 testIsDomainName(self):
38         values = [ ("a.domain", True), ("not_a_domain", False),
39                    ("www.foo.com.", True), ("glor!.com", False),
40                    ("a_bad.domain", False) ]
41         for a, r in values:
42             assert(QueryProcessor.is_domainname(a) == r)
43
44     def testIsSubdomain(self):
45         values = [ ("sub.foo.domain", "domain", True),
46                    ("sub.bar.domain", "foo.domain", False),
47                    ("baz.domain", "a.baz.domain", False),
48                    ("a.com", "a.com", True) ]
49         for sub, domain, value in values:
50             self.assertEquals(QueryProcessor.is_subdomain(domain, sub), value)
51
52     def testReduceDomain(self):
53         values = [ ("a.com", "com"), ("b.com.", "com."),
54                    (".", ""), ("a.b.c.net", "b.c.net") ]
55         for d, v in values:
56             self.assertEquals(QueryProcessor.reduce_domain(d), v)
57
58
59 class TestProcessing(unittest.TestCase):
60     def setUp(self):
61         db = MemDB.MemDB()
62         db.init_schema("test_schema")
63         db.load_data("test_data")
64         db.index_data()
65         QueryParser.db = db
66         self.processor = QueryProcessor.QueryProcessor(db)
67         self.session = Session.Context()
68         self.session.wfile = StringIO.StringIO()
69
70
71     def testParseKnown(self):
72         queries = [ ('domain a.com', """domain:ID:333.a.com\r
73 domain:Auth-Area:a.com\r
74 domain:Class-Name:domain\r
75 domain:Guardian:444.a.com\r
76 domain:Domain-Name:a.com\r
77 domain:Primary-Server:5551.a.com\r
78 domain:Secondary-Server:5552.a.com\r
79 domain:Organization:777.a.com\r
80 domain:Admin-Contact:222.a.com\r
81 domain:Tech-Contact:222.a.com\r
82 domain:Billing-Contact:222.a.com\r
83 domain:Created:19961022\r
84 domain:Updated:19961023\r
85 domain:Updated-By:hostmaster@a.com\r
86 \r
87 %ok\r
88 """) ]
89
90         for q, s in queries:
91             self.processor.process_query(self.session, q)
92             res = self.session.wfile.getvalue()
93             self.assertEquals(res, s.lower())
94
95 if __name__ == "__main__":
96     unittest.main()