File: unittests.py

package info (click to toggle)
pyzor 1%3A0.4.0%2Bcvs20030201-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 356 kB
  • ctags: 394
  • sloc: python: 1,590; sh: 617; makefile: 16
file content (196 lines) | stat: -rw-r--r-- 6,577 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import sys
import unittest
import StringIO

sys.path.insert(0, './lib')
from pyzor import *
from pyzor.server import *
from pyzor.client import *

__revision__ = "$Id: unittests.py,v 1.8 2002/09/07 22:45:03 ftobin Exp $"


class ACLTest(unittest.TestCase):
    def setUp(self):
        access_file = AccessFile(StringIO.StringIO("""check : alice : allow
        # comment
        check : bob : deny
        report check : all : allow
        ping : all : deny
        all : charlie : allow
        all : all : allow
        all : all : deny
        """))

        self.acl = ACL()
        access_file.feed_into(self.acl)

    def test_basic_allow(self):
        self.assert_(self.acl.allows(Username('alice'), Opname('check')))

    def test_basic_deny(self):
        self.assert_(not self.acl.allows(Username('bob'), Opname('check')))

    def test_all_user_allow(self):
        self.assert_(self.acl.allows(Username('dennis'), Opname('report')))
        self.assert_(self.acl.allows(Username('bob'), Opname('report')))

    def test_all_user_deny(self):
        self.assert_(not self.acl.allows(Username('alice'), Opname('ping')))
        self.assert_(not self.acl.allows(Username('frank'), Opname('ping')))

    def test_allow_user_all_ops(self):
        self.assert_(self.acl.allows(Username('charlie'), Opname('check')))
        self.assert_(self.acl.allows(Username('charlie'), Opname('foobar')))
        
    def test_all_allowed_all(self):
        self.assert_(self.acl.allows(Username('giggles'), Opname('report')))
        self.assert_(self.acl.allows(Username('zoe'), Opname('foobar')))



class PasswdTest(unittest.TestCase):
    def setUp(self):
        passwd_file = PasswdFile(StringIO.StringIO("""alice:5
        bob:b
        charlie:cc
        """))

        self.passwd = Passwd()
        for u,k in passwd_file:
            self.passwd[u] = k
        
    def test_keys(self):
        self.assertEquals(self.passwd[Username('alice')], 5L)
        self.assertEquals(self.passwd[Username('bob')], 11L)
        self.assertEquals(self.passwd[Username('charlie')], 204L)

    def test_no_user(self):
        self.assert_(not self.passwd.has_key(Username('foobar')))


class AcountInfoTest(unittest.TestCase):
    def setUp(self):
        account_file = AccountsFile(StringIO.StringIO("""127.0.0.0 : 3333 : alice : 5,a
        # comment
        127.0.0.1 : 4444 : bob : ,18
        """))
        
##        # For testing in the future
##        127.0.0.1 : 4445 : charlie : c,
##        127.0.0.1 : 4446 : david : ,

        self.accounts = AccountsDict()

        for addr, acc in account_file:
            self.accounts[addr] = acc

    def test_full_key(self):
        self.assertEquals(self.accounts[Address(('127.0.0.0', 3333))],
                          Account((Username('alice'),
                                   Keystuff((5L, 10L)))))

    def test_only_key(self):
        self.assertEquals(self.accounts[Address(('127.0.0.1', 4444))],
                          Account((Username('bob'),
                                   Keystuff((None, 24L)))))

##    def test_only_salt(self):
##       self.assertEquals(self.accounts[Address(('127.0.0.1', 4445))],
##                         Account((Username('charlie'),
##                                  Keystuff((None, 12)))))
        

##    def test_neither(self):
##        self.assertEquals(self.accounts[Address(('127.0.0.1', 4446))],
##                          Account((Username('david'),
##                                   Keystuff((None, 24)))))



class KeystuffTest(unittest.TestCase):
    def test_full_stuff(self):
        self.assertEquals(Keystuff.from_hexstr("10,ab"),
                          Keystuff((16L, 171L)))

    def test_only_key(self):
        self.assertEquals(Keystuff.from_hexstr(",ab"),
                          Keystuff((None, 171L)))



class ServerListTest(unittest.TestCase):
    def setUp(self):
        self.sl = ServerList()

        self.sl.read(StringIO.StringIO("""127.0.0.1:4444
        # comment
        127.0.0.2:1234
        """))

    def test_sl_length(self):
        self.assertEquals(len(self.sl), 2)

    def test_entries(self):
        self.assert_(Address(('127.0.0.1', 4444)) in self.sl)
        self.assert_(Address(('127.0.0.2', 1234)) in self.sl)



class DataDigestTest(unittest.TestCase):
    def test_ptrns(self):
        norm = DataDigester.normalize
        self.assertEqual(norm('aaa me@example.com bbb'), 'aaabbb')
        self.assertEqual(norm('aaa http://www.example.com/ bbb'), 'aaabbb')
        self.assertEqual(norm('aaa Supercalifragilisticexpialidocious bbb'),
                         'aaabbb')
        self.assertEqual(norm('aaa  bbb  ccc\n'), 'aaabbbccc')
        self.assertEqual(norm('aaa <! random tag > bbb'), 'aaabbb')

    def test_should_handle_line(self):
        min_len = int(DataDigester.min_line_length)
        self.assert_(DataDigester.should_handle_line('a' * min_len))
        self.assert_(not DataDigester.should_handle_line('a' * (min_len-1)))


    def test_atomicness(self):
        self.assert_(DataDigester(StringIO.StringIO("""
        It is contrary to reasoning to say that there is a vacuum or space in
        which there is absolutely nothing.
                -- Descartes
        """),
                                  ExecCall.digest_spec,
                                  seekable=True).is_atomic())

    def test_non_atomicness(self):
        self.assert_(not DataDigester(StringIO.StringIO("""
        If builders built buildings the way programmers write programs,
        Jolt Cola would be a Fortune-500 company.

        If builders built buildings the way programmers write programs,
        you'd be able to buy a nice little colonial split-level at
        Babbages for $34.95.

        If programmers wrote programs the way builders build buildings,
        we'd still be using autocoder and running compile decks.

        ---

        Love is always open arms.  With arms open you allow love to come and
        go as it wills, freely, for it will do so anyway.  If you close your
        arms about love you'll find you are left only holding yourself.
        """),
                                      ExecCall.digest_spec,
                                      seekable=True).is_atomic())


class rfc822BodyCleanerTest(unittest.TestCase):
    def test_cleaning(self):
        expected = open('t/multipart.expected')
        for line in rfc822BodyCleaner(open('t/multipart')):
            self.assertEqual(line, expected.readline())



if __name__ == "__main__":
    unittest.main()