1 # This file is part of python-rwhoisd
3 # Copyright (C) 2008 David E. Blacka
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.
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.
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
22 import QueryProcessor, Session, MemDB, QueryParser
23 import unittest, types, StringIO
26 class TestHelperMethods(unittest.TestCase):
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)
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) ]
42 assert(QueryProcessor.is_domainname(a) == r)
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)
52 def testReduceDomain(self):
53 values = [ ("a.com", "com"), ("b.com.", "com."),
54 (".", ""), ("a.b.c.net", "b.c.net") ]
56 self.assertEquals(QueryProcessor.reduce_domain(d), v)
59 class TestProcessing(unittest.TestCase):
62 db.init_schema("test_schema")
63 db.load_data("test_data")
66 self.processor = QueryProcessor.QueryProcessor(db)
67 self.session = Session.Context()
68 self.session.wfile = StringIO.StringIO()
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
91 self.processor.process_query(self.session, q)
92 res = self.session.wfile.getvalue()
93 self.assertEquals(res, s.lower())
95 if __name__ == "__main__":