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
23 import unittest, types
25 class InitSchemaTest(unittest.TestCase):
27 def testInitSchema(self):
31 db.init_schema("test_schema")
33 assert(db.is_attribute("id")) # built-in
34 assert(db.is_attribute("auth-area")) # built-in
35 assert(db.is_attribute("class-name")) # built-in
36 assert(db.is_attribute("updated")) # built-in
37 assert(db.is_attribute("referred-auth-area")) # built-in
38 assert(db.is_attribute("name"))
39 assert(db.is_attribute("ip-network"))
40 assert(db.is_indexed_attr("id"))
41 assert(db.is_indexed_attr("ip-network"))
42 assert(db.is_indexed_attr("ref-like"))
44 # At the moment, init_schema doesn't really fail on malformed
45 # schema files. Instead, it just silently fails to add an index
46 # for the mailformed attributes.
47 def testBadSchema(self):
51 db.init_schema("test_bad_schema")
53 assert(db.is_attribute("name"))
54 assert(not db.is_indexed_attr("name"))
55 assert(db.is_attribute("bad"))
56 assert(not db.is_indexed_attr("bad"))
57 assert(db.is_indexed_attr("questionable"))
60 class SchemaTest(unittest.TestCase):
63 self.db = MemDB.MemDB()
65 self.db.init_schema("test_schema")
66 self.db.load_data("test_data")
69 auth_areas = ["10.0.0.0/8", "a.com"]
71 def testAuthAreas(self):
74 for aa in self.auth_areas:
76 assert(self.db.is_autharea(aa))
77 for aa in self.db.get_authareas():
80 self.assertEquals(a, b)
82 classes = ['contact', 'domain', 'host', 'network',
83 'organization', 'referral']
85 def testClasses(self):
86 for c in self.classes:
87 assert(self.db.is_objectclass(c))
89 attrs = ['Admin-Contact', 'Auth-Area', 'Billing-Contact', 'City',
90 'Class-Name', 'Country-Code', 'Created', 'Domain-Name',
91 'Email', 'Fax', 'First-Name', 'Guardian', 'Host-Name',
92 'ID', 'IP-Address', 'IP-Network', 'Last-Name', 'Name',
93 'Network-Block', 'Network-Name', 'Organization', 'Org-Name',
94 'Phone', 'Postal-Code', 'Primary-Server', 'Referral',
95 'Referred-Auth-Area', 'Secondary-Server', 'See-Also',
96 'State', 'Street-Address', 'Tech-Contact', 'Type', 'Updated',
99 def testAttributes(self):
101 assert(self.db.is_attribute(a))
102 assert(self.db.is_indexed_attr("Host-Name"))
103 assert(not self.db.is_indexed_attr("Phone"))
104 assert(not self.db.is_indexed_attr("foobar"))
106 ids = ['local-block-1.a.com', '888.a.com', 'does-not-exist.a.com' ]
108 def testFetchObjs(self):
109 objs = self.db.fetch_objects(self.ids)
110 assert(len(objs) == 2)
112 assert(isinstance(o, Rwhois.rwhoisobject))
113 for i in range(len(objs)):
114 self.assertEquals(objs[i].getid().lower(), self.ids[i])
116 class SearchTest(unittest.TestCase):
119 self.db = MemDB.MemDB()
121 self.db.init_schema("test_schema")
122 self.db.load_data("test_data")
124 attr_search = [ ('domain-name', 'a.com', '333.a.com'),
125 ('ip-network', '10.131.0.0/16', '666.10.0.0.0/8'),
126 ('host-name', 'NS2.A.COM', '5552.a.com'),
127 ('domain-name', 'ns2.a.com', None) ]
129 def testAttrSearch(self):
130 for c, k, v in self.attr_search:
131 res = self.db.search_attr(c, k)
132 assert(isinstance(res, MemDB.IndexResult))
134 if v == None: length = 0
135 self.assertEquals(len(res), length)
137 self.assertEquals(res.list()[0], v)
139 nrml_search = [ ('a-net', '666.10.0.0.0/8', 1),
140 ('ns2.a.*', '5552.a.com', 1),
141 ('foo.a.com', None, 0),
142 ('a*', '666.10.0.0.0/8', 3) ]
144 def testNormalSearch(self):
145 for k, v, l in self.nrml_search:
146 res = self.db.search_normal(k)
147 assert(isinstance(res, MemDB.IndexResult))
148 self.assertEquals(len(res), l)
150 self.assertEquals(res.list()[0], v)
152 res = self.db.search_normal(k, 1)
153 self.assertEquals(len(res), 1)
156 cidr_search = [ ('10.0.0.2', '666.10.0.0.0/8', 2),
157 ('192.168.0.0/16**', 'local-block-1.a.com', 1),
158 ('127.0.0.1/26', None, 0) ]
160 def testCidrSearch(self):
161 for k, v, l in self.cidr_search:
162 res = self.db.search_cidr(k)
163 assert(isinstance(res, MemDB.IndexResult))
164 self.assertEquals(len(res), l)
166 self.assertEquals(res.list()[0], v)
168 res = self.db.search_cidr(k, 1)
169 self.assertEquals(len(res), 1)
171 def testReferralSearch(self):
172 res = self.db.search_referral("fddi.a.com")
173 self.assertEquals(len(res), 1)
174 self.assertEquals(res.list()[0], '888.a.com')
176 def testIterator(self):
177 it = self.db.object_iterator()
178 l = [ x for x in it ]
179 self.assertEquals(len(l), 9)
181 if __name__ == "__main__":