--- /dev/null
+# This file is part of python-rwhoisd
+#
+# Copyright (C) 2008 David E. Blacka
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+
+
+import path
+import QueryProcessor, Session, MemDB, QueryParser
+import unittest, types, StringIO
+
+
+class TestHelperMethods(unittest.TestCase):
+
+ def testMatchCidr(self):
+ values = [ ("127.0.0.1/24", "127.0.0.0/24", True),
+ ("127.0.0.0/24**", "127.0.0.8", True),
+ ("127.0.0.0/24*", "127.0.0.0/23", True),
+ ("127.0.0.0/24*", "127.0.0.1/24", True),
+ ("127.0.0.10/24", "127.0.0.0/22", False) ]
+ for sv, tv, v in values:
+ self.assertEquals(QueryProcessor.match_cidr(sv, tv), v)
+
+ def testIsDomainName(self):
+ values = [ ("a.domain", True), ("not_a_domain", False),
+ ("www.foo.com.", True), ("glor!.com", False),
+ ("a_bad.domain", False) ]
+ for a, r in values:
+ assert(QueryProcessor.is_domainname(a) == r)
+
+ def testIsSubdomain(self):
+ values = [ ("sub.foo.domain", "domain", True),
+ ("sub.bar.domain", "foo.domain", False),
+ ("baz.domain", "a.baz.domain", False),
+ ("a.com", "a.com", True) ]
+ for sub, domain, value in values:
+ self.assertEquals(QueryProcessor.is_subdomain(domain, sub), value)
+
+ def testReduceDomain(self):
+ values = [ ("a.com", "com"), ("b.com.", "com."),
+ (".", ""), ("a.b.c.net", "b.c.net") ]
+ for d, v in values:
+ self.assertEquals(QueryProcessor.reduce_domain(d), v)
+
+
+class TestProcessing(unittest.TestCase):
+ def setUp(self):
+ db = MemDB.MemDB()
+ db.init_schema("test_schema")
+ db.load_data("test_data")
+ db.index_data()
+ QueryParser.db = db
+ self.processor = QueryProcessor.QueryProcessor(db)
+ self.session = Session.Context()
+ self.session.wfile = StringIO.StringIO()
+
+
+ def testParseKnown(self):
+ queries = [ ('domain a.com', """domain:ID:333.a.com\r
+domain:Auth-Area:a.com\r
+domain:Class-Name:domain\r
+domain:Guardian:444.a.com\r
+domain:Domain-Name:a.com\r
+domain:Primary-Server:5551.a.com\r
+domain:Secondary-Server:5552.a.com\r
+domain:Organization:777.a.com\r
+domain:Admin-Contact:222.a.com\r
+domain:Tech-Contact:222.a.com\r
+domain:Billing-Contact:222.a.com\r
+domain:Created:19961022\r
+domain:Updated:19961023\r
+domain:Updated-By:hostmaster@a.com\r
+\r
+%ok\r
+""") ]
+
+ for q, s in queries:
+ self.processor.process_query(self.session, q)
+ res = self.session.wfile.getvalue()
+ self.assertEquals(res, s.lower())
+
+if __name__ == "__main__":
+ unittest.main()