pyrad 2.3__py3-none-any.whl → 2.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docs/Makefile +20 -0
- docs/make.bat +36 -0
- docs/source/_static/logo.png +0 -0
- docs/source/api/client.rst +10 -0
- docs/source/api/dictionary.rst +10 -0
- docs/source/api/host.rst +7 -0
- docs/source/api/packet.rst +48 -0
- docs/source/api/proxy.rst +7 -0
- docs/source/api/server.rst +13 -0
- docs/source/conf.py +158 -0
- docs/source/index.rst +75 -0
- example/acct.py +41 -0
- example/auth.py +37 -0
- example/auth_async.py +164 -0
- example/client-coa.py +61 -0
- example/coa.py +40 -0
- example/dictionary +405 -0
- example/dictionary.freeradius +91 -0
- example/pyrad.log +0 -0
- example/server.py +68 -0
- example/server_async.py +117 -0
- example/status.py +26 -0
- pyrad/__init__.py +3 -3
- pyrad/client.py +54 -9
- pyrad/client_async.py +22 -14
- pyrad/dictfile.py +2 -5
- pyrad/dictionary.py +12 -1
- pyrad/host.py +1 -1
- pyrad/packet.py +208 -133
- pyrad/proxy.py +2 -2
- pyrad/server.py +3 -7
- pyrad/server_async.py +4 -5
- pyrad/tests/__init__.py +2 -2
- pyrad/tests/mock.py +5 -1
- pyrad/tests/{testBidict.py → test_bidict.py} +2 -2
- pyrad/tests/{testClient.py → test_client.py} +28 -30
- pyrad/tests/{testDictionary.py → test_dictionary.py} +38 -21
- pyrad/tests/{testHost.py → test_host.py} +10 -10
- pyrad/tests/test_packet.py +679 -0
- pyrad/tests/{testProxy.py → test_proxy.py} +11 -11
- pyrad/tests/{testServer.py → test_server.py} +35 -33
- pyrad/tests/test_tools.py +126 -0
- pyrad/tools.py +254 -158
- {pyrad-2.3.dist-info → pyrad-2.5.0.dist-info}/METADATA +44 -20
- pyrad-2.5.0.dist-info/RECORD +51 -0
- {pyrad-2.3.dist-info → pyrad-2.5.0.dist-info}/WHEEL +1 -1
- {pyrad-2.3.dist-info → pyrad-2.5.0.dist-info/licenses}/LICENSE.txt +2 -1
- pyrad-2.5.0.dist-info/top_level.txt +3 -0
- pyrad/tests/testPacket.py +0 -530
- pyrad/tests/testTools.py +0 -122
- pyrad-2.3.dist-info/RECORD +0 -29
- pyrad-2.3.dist-info/top_level.txt +0 -1
- {pyrad-2.3.dist-info → pyrad-2.5.0.dist-info}/zip-safe +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
Copyright
|
|
1
|
+
Copyright 2020 Istvan Ruzman. All rights reserved.
|
|
2
|
+
Copyright 2017-2026 Christian Giese. All rights reserved.
|
|
2
3
|
Copyright 2007-2008 Simplon. All rights reserved.
|
|
3
4
|
Copyright 2002-2008 Wichert Akkerman. All rights reserved.
|
|
4
5
|
|
pyrad/tests/testPacket.py
DELETED
|
@@ -1,530 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import unittest
|
|
3
|
-
import six
|
|
4
|
-
|
|
5
|
-
from collections import OrderedDict
|
|
6
|
-
from pyrad import packet
|
|
7
|
-
from pyrad.client import Client
|
|
8
|
-
from pyrad.tests import home
|
|
9
|
-
from pyrad.dictionary import Dictionary
|
|
10
|
-
try:
|
|
11
|
-
import hashlib
|
|
12
|
-
md5_constructor = hashlib.md5
|
|
13
|
-
except ImportError:
|
|
14
|
-
# BBB for python 2.4
|
|
15
|
-
import md5
|
|
16
|
-
md5_constructor = md5.new
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class UtilityTests(unittest.TestCase):
|
|
20
|
-
def testGenerateID(self):
|
|
21
|
-
id = packet.CreateID()
|
|
22
|
-
self.assertTrue(isinstance(id, int))
|
|
23
|
-
newid = packet.CreateID()
|
|
24
|
-
self.assertNotEqual(id, newid)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class PacketConstructionTests(unittest.TestCase):
|
|
28
|
-
klass = packet.Packet
|
|
29
|
-
|
|
30
|
-
def setUp(self):
|
|
31
|
-
self.path = os.path.join(home, 'tests', 'data')
|
|
32
|
-
self.dict = Dictionary(os.path.join(self.path, 'simple'))
|
|
33
|
-
|
|
34
|
-
def testBasicConstructor(self):
|
|
35
|
-
pkt = self.klass()
|
|
36
|
-
self.assertTrue(isinstance(pkt.code, int))
|
|
37
|
-
self.assertTrue(isinstance(pkt.id, int))
|
|
38
|
-
self.assertTrue(isinstance(pkt.secret, six.binary_type))
|
|
39
|
-
|
|
40
|
-
def testNamedConstructor(self):
|
|
41
|
-
pkt = self.klass(code=26, id=38, secret=six.b('secret'),
|
|
42
|
-
authenticator=six.b('authenticator'),
|
|
43
|
-
dict='fakedict')
|
|
44
|
-
self.assertEqual(pkt.code, 26)
|
|
45
|
-
self.assertEqual(pkt.id, 38)
|
|
46
|
-
self.assertEqual(pkt.secret, six.b('secret'))
|
|
47
|
-
self.assertEqual(pkt.authenticator, six.b('authenticator'))
|
|
48
|
-
self.assertEqual(pkt.dict, 'fakedict')
|
|
49
|
-
|
|
50
|
-
def testConstructWithDictionary(self):
|
|
51
|
-
pkt = self.klass(dict=self.dict)
|
|
52
|
-
self.assertTrue(pkt.dict is self.dict)
|
|
53
|
-
|
|
54
|
-
def testConstructorIgnoredParameters(self):
|
|
55
|
-
marker = []
|
|
56
|
-
pkt = self.klass(fd=marker)
|
|
57
|
-
self.assertFalse(getattr(pkt, 'fd', None) is marker)
|
|
58
|
-
|
|
59
|
-
def testSecretMustBeBytestring(self):
|
|
60
|
-
self.assertRaises(TypeError, self.klass, secret=six.u('secret'))
|
|
61
|
-
|
|
62
|
-
def testConstructorWithAttributes(self):
|
|
63
|
-
pkt = self.klass(**{'Test-String' :'this works', 'dict' : self.dict})
|
|
64
|
-
self.assertEqual(pkt['Test-String'], ['this works'])
|
|
65
|
-
|
|
66
|
-
def testConstructorWithTlvAttribute(self):
|
|
67
|
-
pkt = self.klass(**{
|
|
68
|
-
'Test-Tlv-Str': 'this works',
|
|
69
|
-
'Test-Tlv-Int': 10,
|
|
70
|
-
'dict': self.dict
|
|
71
|
-
})
|
|
72
|
-
self.assertEqual(
|
|
73
|
-
pkt['Test-Tlv'],
|
|
74
|
-
{'Test-Tlv-Str': ['this works'], 'Test-Tlv-Int' : [10]}
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
class PacketTests(unittest.TestCase):
|
|
79
|
-
def setUp(self):
|
|
80
|
-
self.path = os.path.join(home, 'tests', 'data')
|
|
81
|
-
self.dict = Dictionary(os.path.join(self.path, 'full'))
|
|
82
|
-
self.packet = packet.Packet(
|
|
83
|
-
id=0, secret=six.b('secret'),
|
|
84
|
-
authenticator=six.b('01234567890ABCDEF'), dict=self.dict)
|
|
85
|
-
|
|
86
|
-
def testCreateReply(self):
|
|
87
|
-
reply = self.packet.CreateReply(**{'Test-Integer' : 10})
|
|
88
|
-
self.assertEqual(reply.id, self.packet.id)
|
|
89
|
-
self.assertEqual(reply.secret, self.packet.secret)
|
|
90
|
-
self.assertEqual(reply.authenticator, self.packet.authenticator)
|
|
91
|
-
self.assertEqual(reply['Test-Integer'], [10])
|
|
92
|
-
|
|
93
|
-
def testAttributeAccess(self):
|
|
94
|
-
self.packet['Test-Integer'] = 10
|
|
95
|
-
self.assertEqual(self.packet['Test-Integer'], [10])
|
|
96
|
-
self.assertEqual(self.packet[3], [six.b('\x00\x00\x00\x0a')])
|
|
97
|
-
|
|
98
|
-
self.packet['Test-String'] = 'dummy'
|
|
99
|
-
self.assertEqual(self.packet['Test-String'], ['dummy'])
|
|
100
|
-
self.assertEqual(self.packet[1], [six.b('dummy')])
|
|
101
|
-
|
|
102
|
-
def testAttributeValueAccess(self):
|
|
103
|
-
self.packet['Test-Integer'] = 'Three'
|
|
104
|
-
self.assertEqual(self.packet['Test-Integer'], ['Three'])
|
|
105
|
-
self.assertEqual(self.packet[3], [six.b('\x00\x00\x00\x03')])
|
|
106
|
-
|
|
107
|
-
def testVendorAttributeAccess(self):
|
|
108
|
-
self.packet['Simplon-Number'] = 10
|
|
109
|
-
self.assertEqual(self.packet['Simplon-Number'], [10])
|
|
110
|
-
self.assertEqual(self.packet[(16, 1)], [six.b('\x00\x00\x00\x0a')])
|
|
111
|
-
|
|
112
|
-
self.packet['Simplon-Number'] = 'Four'
|
|
113
|
-
self.assertEqual(self.packet['Simplon-Number'], ['Four'])
|
|
114
|
-
self.assertEqual(self.packet[(16, 1)], [six.b('\x00\x00\x00\x04')])
|
|
115
|
-
|
|
116
|
-
def testRawAttributeAccess(self):
|
|
117
|
-
marker = [six.b('')]
|
|
118
|
-
self.packet[1] = marker
|
|
119
|
-
self.assertTrue(self.packet[1] is marker)
|
|
120
|
-
self.packet[(16, 1)] = marker
|
|
121
|
-
self.assertTrue(self.packet[(16, 1)] is marker)
|
|
122
|
-
|
|
123
|
-
def testHasKey(self):
|
|
124
|
-
self.assertEqual(self.packet.has_key('Test-String'), False)
|
|
125
|
-
self.assertEqual('Test-String' in self.packet, False)
|
|
126
|
-
self.packet['Test-String'] = 'dummy'
|
|
127
|
-
self.assertEqual(self.packet.has_key('Test-String'), True)
|
|
128
|
-
self.assertEqual(self.packet.has_key(1), True)
|
|
129
|
-
self.assertEqual(1 in self.packet, True)
|
|
130
|
-
|
|
131
|
-
def testHasKeyWithUnknownKey(self):
|
|
132
|
-
self.assertEqual(self.packet.has_key('Unknown-Attribute'), False)
|
|
133
|
-
self.assertEqual('Unknown-Attribute' in self.packet, False)
|
|
134
|
-
|
|
135
|
-
def testDelItem(self):
|
|
136
|
-
self.packet['Test-String'] = 'dummy'
|
|
137
|
-
del self.packet['Test-String']
|
|
138
|
-
self.assertEqual(self.packet.has_key('Test-String'), False)
|
|
139
|
-
self.packet['Test-String'] = 'dummy'
|
|
140
|
-
del self.packet[1]
|
|
141
|
-
self.assertEqual(self.packet.has_key('Test-String'), False)
|
|
142
|
-
|
|
143
|
-
def testKeys(self):
|
|
144
|
-
self.assertEqual(self.packet.keys(), [])
|
|
145
|
-
self.packet['Test-String'] = 'dummy'
|
|
146
|
-
self.assertEqual(self.packet.keys(), ['Test-String'])
|
|
147
|
-
self.packet['Test-Integer'] = 10
|
|
148
|
-
self.assertEqual(self.packet.keys(), ['Test-String', 'Test-Integer'])
|
|
149
|
-
OrderedDict.__setitem__(self.packet, 12345, None)
|
|
150
|
-
self.assertEqual(self.packet.keys(),
|
|
151
|
-
['Test-String', 'Test-Integer', 12345])
|
|
152
|
-
|
|
153
|
-
def testCreateAuthenticator(self):
|
|
154
|
-
a = packet.Packet.CreateAuthenticator()
|
|
155
|
-
self.assertTrue(isinstance(a, six.binary_type))
|
|
156
|
-
self.assertEqual(len(a), 16)
|
|
157
|
-
|
|
158
|
-
b = packet.Packet.CreateAuthenticator()
|
|
159
|
-
self.assertNotEqual(a, b)
|
|
160
|
-
|
|
161
|
-
def testGenerateID(self):
|
|
162
|
-
id = self.packet.CreateID()
|
|
163
|
-
self.assertTrue(isinstance(id, int))
|
|
164
|
-
newid = self.packet.CreateID()
|
|
165
|
-
self.assertNotEqual(id, newid)
|
|
166
|
-
|
|
167
|
-
def testReplyPacket(self):
|
|
168
|
-
reply = self.packet.ReplyPacket()
|
|
169
|
-
self.assertEqual(reply,
|
|
170
|
-
six.b('\x00\x00\x00\x14\xb0\x5e\x4b\xfb\xcc\x1c'
|
|
171
|
-
'\x8c\x8e\xc4\x72\xac\xea\x87\x45\x63\xa7'))
|
|
172
|
-
|
|
173
|
-
def testVerifyReply(self):
|
|
174
|
-
reply = self.packet.CreateReply()
|
|
175
|
-
self.assertEqual(self.packet.VerifyReply(reply), True)
|
|
176
|
-
|
|
177
|
-
reply.id += 1
|
|
178
|
-
self.assertEqual(self.packet.VerifyReply(reply), False)
|
|
179
|
-
reply.id = self.packet.id
|
|
180
|
-
|
|
181
|
-
reply.secret = six.b('different')
|
|
182
|
-
self.assertEqual(self.packet.VerifyReply(reply), False)
|
|
183
|
-
reply.secret = self.packet.secret
|
|
184
|
-
|
|
185
|
-
reply.authenticator = six.b('X') * 16
|
|
186
|
-
self.assertEqual(self.packet.VerifyReply(reply), False)
|
|
187
|
-
reply.authenticator = self.packet.authenticator
|
|
188
|
-
|
|
189
|
-
def testPktEncodeAttribute(self):
|
|
190
|
-
encode = self.packet._PktEncodeAttribute
|
|
191
|
-
|
|
192
|
-
# Encode a normal attribute
|
|
193
|
-
self.assertEqual(
|
|
194
|
-
encode(1, six.b('value')),
|
|
195
|
-
six.b('\x01\x07value'))
|
|
196
|
-
# Encode a vendor attribute
|
|
197
|
-
self.assertEqual(
|
|
198
|
-
encode((1, 2), six.b('value')),
|
|
199
|
-
six.b('\x1a\x0d\x00\x00\x00\x01\x02\x07value'))
|
|
200
|
-
|
|
201
|
-
def testPktEncodeTlvAttribute(self):
|
|
202
|
-
encode = self.packet._PktEncodeTlv
|
|
203
|
-
|
|
204
|
-
# Encode a normal tlv attribute
|
|
205
|
-
self.assertEqual(
|
|
206
|
-
encode(4, {1:[six.b('value')], 2:[six.b('\x00\x00\x00\x02')]}),
|
|
207
|
-
six.b('\x04\x0f\x01\x07value\x02\x06\x00\x00\x00\x02'))
|
|
208
|
-
|
|
209
|
-
# Encode a normal tlv attribute with several sub attribute instances
|
|
210
|
-
self.assertEqual(
|
|
211
|
-
encode(4, {1:[six.b('value'), six.b('other')], 2:[six.b('\x00\x00\x00\x02')]}),
|
|
212
|
-
six.b('\x04\x16\x01\x07value\x02\x06\x00\x00\x00\x02\x01\x07other'))
|
|
213
|
-
# Encode a vendor tlv attribute
|
|
214
|
-
self.assertEqual(
|
|
215
|
-
encode((16, 3), {1:[six.b('value')], 2:[six.b('\x00\x00\x00\x02')]}),
|
|
216
|
-
six.b('\x1a\x15\x00\x00\x00\x10\x03\x0f\x01\x07value\x02\x06\x00\x00\x00\x02'))
|
|
217
|
-
|
|
218
|
-
def testPktEncodeLongTlvAttribute(self):
|
|
219
|
-
encode = self.packet._PktEncodeTlv
|
|
220
|
-
|
|
221
|
-
long_str = 'a' * 245
|
|
222
|
-
# Encode a long tlv attribute - check it is split between AVPs
|
|
223
|
-
self.assertEqual(
|
|
224
|
-
encode(4, {1:[six.b('value'), six.b(long_str)], 2:[six.b('\x00\x00\x00\x02')]}),
|
|
225
|
-
six.b('\x04\x0f\x01\x07value\x02\x06\x00\x00\x00\x02\x04\xf9\x01\xf7' + long_str))
|
|
226
|
-
|
|
227
|
-
# Encode a long vendor tlv attribute
|
|
228
|
-
first_avp = '\x1a\x15\x00\x00\x00\x10\x03\x0f\x01\x07value\x02\x06\x00\x00\x00\x02'
|
|
229
|
-
second_avp = '\x1a\xff\x00\x00\x00\x10\x03\xf9\x01\xf7' + long_str
|
|
230
|
-
self.assertEqual(
|
|
231
|
-
encode((16, 3), {1:[six.b('value'), six.b(long_str)], 2:[six.b('\x00\x00\x00\x02')]}),
|
|
232
|
-
six.b(first_avp + second_avp))
|
|
233
|
-
|
|
234
|
-
def testPktEncodeAttributes(self):
|
|
235
|
-
self.packet[1] = [six.b('value')]
|
|
236
|
-
self.assertEqual(self.packet._PktEncodeAttributes(),
|
|
237
|
-
six.b('\x01\x07value'))
|
|
238
|
-
|
|
239
|
-
self.packet.clear()
|
|
240
|
-
self.packet[(16, 2)] = [six.b('value')]
|
|
241
|
-
self.assertEqual(self.packet._PktEncodeAttributes(),
|
|
242
|
-
six.b('\x1a\x0d\x00\x00\x00\x10\x02\x07value'))
|
|
243
|
-
|
|
244
|
-
self.packet.clear()
|
|
245
|
-
self.packet[1] = [six.b('one'), six.b('two'), six.b('three')]
|
|
246
|
-
self.assertEqual(self.packet._PktEncodeAttributes(),
|
|
247
|
-
six.b('\x01\x05one\x01\x05two\x01\x07three'))
|
|
248
|
-
|
|
249
|
-
self.packet.clear()
|
|
250
|
-
self.packet[1] = [six.b('value')]
|
|
251
|
-
self.packet[(16, 2)] = [six.b('value')]
|
|
252
|
-
self.assertEqual(
|
|
253
|
-
self.packet._PktEncodeAttributes(),
|
|
254
|
-
six.b('\x01\x07value\x1a\x0d\x00\x00\x00\x10\x02\x07value'))
|
|
255
|
-
|
|
256
|
-
def testPktDecodeVendorAttribute(self):
|
|
257
|
-
decode = self.packet._PktDecodeVendorAttribute
|
|
258
|
-
|
|
259
|
-
# Non-RFC2865 recommended form
|
|
260
|
-
self.assertEqual(decode(six.b('')), [(26, six.b(''))])
|
|
261
|
-
self.assertEqual(decode(six.b('12345')), [(26, six.b('12345'))])
|
|
262
|
-
|
|
263
|
-
# Almost RFC2865 recommended form: bad length value
|
|
264
|
-
self.assertEqual(
|
|
265
|
-
decode(six.b('\x00\x00\x00\x01\x02\x06value')),
|
|
266
|
-
[(26, six.b('\x00\x00\x00\x01\x02\x06value'))])
|
|
267
|
-
|
|
268
|
-
# Proper RFC2865 recommended form
|
|
269
|
-
self.assertEqual(
|
|
270
|
-
decode(six.b('\x00\x00\x00\x10\x02\x07value')),
|
|
271
|
-
[((16, 2), six.b('value'))])
|
|
272
|
-
|
|
273
|
-
def testPktDecodeTlvAttribute(self):
|
|
274
|
-
decode = self.packet._PktDecodeTlvAttribute
|
|
275
|
-
|
|
276
|
-
decode(4,six.b('\x01\x07value'))
|
|
277
|
-
self.assertEqual(self.packet[4], {1: [six.b('value')]})
|
|
278
|
-
|
|
279
|
-
#add another instance of the same sub attribute
|
|
280
|
-
decode(4,six.b('\x01\x07other'))
|
|
281
|
-
self.assertEqual(self.packet[4], {1: [six.b('value'), six.b('other')]})
|
|
282
|
-
|
|
283
|
-
#add a different sub attribute
|
|
284
|
-
decode(4,six.b('\x02\x07\x00\x00\x00\x01'))
|
|
285
|
-
self.assertEqual(self.packet[4], {
|
|
286
|
-
1: [six.b('value'), six.b('other')],
|
|
287
|
-
2: [six.b('\x00\x00\x00\x01')]
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
def testDecodePacketWithEmptyPacket(self):
|
|
291
|
-
try:
|
|
292
|
-
self.packet.DecodePacket(six.b(''))
|
|
293
|
-
except packet.PacketError as e:
|
|
294
|
-
self.assertTrue('header is corrupt' in str(e))
|
|
295
|
-
else:
|
|
296
|
-
self.fail()
|
|
297
|
-
|
|
298
|
-
def testDecodePacketWithInvalidLength(self):
|
|
299
|
-
try:
|
|
300
|
-
self.packet.DecodePacket(six.b('\x00\x00\x00\x001234567890123456'))
|
|
301
|
-
except packet.PacketError as e:
|
|
302
|
-
self.assertTrue('invalid length' in str(e))
|
|
303
|
-
else:
|
|
304
|
-
self.fail()
|
|
305
|
-
|
|
306
|
-
def testDecodePacketWithTooBigPacket(self):
|
|
307
|
-
try:
|
|
308
|
-
self.packet.DecodePacket(six.b('\x00\x00\x24\x00') + (0x2400 - 4) * six.b('X'))
|
|
309
|
-
except packet.PacketError as e:
|
|
310
|
-
self.assertTrue('too long' in str(e))
|
|
311
|
-
else:
|
|
312
|
-
self.fail()
|
|
313
|
-
|
|
314
|
-
def testDecodePacketWithPartialAttributes(self):
|
|
315
|
-
try:
|
|
316
|
-
self.packet.DecodePacket(
|
|
317
|
-
six.b('\x01\x02\x00\x151234567890123456\x00'))
|
|
318
|
-
except packet.PacketError as e:
|
|
319
|
-
self.assertTrue('header is corrupt' in str(e))
|
|
320
|
-
else:
|
|
321
|
-
self.fail()
|
|
322
|
-
|
|
323
|
-
def testDecodePacketWithoutAttributes(self):
|
|
324
|
-
self.packet.DecodePacket(six.b('\x01\x02\x00\x141234567890123456'))
|
|
325
|
-
self.assertEqual(self.packet.code, 1)
|
|
326
|
-
self.assertEqual(self.packet.id, 2)
|
|
327
|
-
self.assertEqual(self.packet.authenticator, six.b('1234567890123456'))
|
|
328
|
-
self.assertEqual(self.packet.keys(), [])
|
|
329
|
-
|
|
330
|
-
def testDecodePacketWithBadAttribute(self):
|
|
331
|
-
try:
|
|
332
|
-
self.packet.DecodePacket(
|
|
333
|
-
six.b('\x01\x02\x00\x161234567890123456\x00\x01'))
|
|
334
|
-
except packet.PacketError as e:
|
|
335
|
-
self.assertTrue('too small' in str(e))
|
|
336
|
-
else:
|
|
337
|
-
self.fail()
|
|
338
|
-
|
|
339
|
-
def testDecodePacketWithEmptyAttribute(self):
|
|
340
|
-
self.packet.DecodePacket(
|
|
341
|
-
six.b('\x01\x02\x00\x161234567890123456\x01\x02'))
|
|
342
|
-
self.assertEqual(self.packet[1], [six.b('')])
|
|
343
|
-
|
|
344
|
-
def testDecodePacketWithAttribute(self):
|
|
345
|
-
self.packet.DecodePacket(
|
|
346
|
-
six.b('\x01\x02\x00\x1b1234567890123456\x01\x07value'))
|
|
347
|
-
self.assertEqual(self.packet[1], [six.b('value')])
|
|
348
|
-
|
|
349
|
-
def testDecodePacketWithTlvAttribute(self):
|
|
350
|
-
self.packet.DecodePacket(
|
|
351
|
-
six.b('\x01\x02\x00\x1d1234567890123456\x04\x09\x01\x07value'))
|
|
352
|
-
self.assertEqual(self.packet[4], {1:[six.b('value')]})
|
|
353
|
-
|
|
354
|
-
def testDecodePacketWithVendorTlvAttribute(self):
|
|
355
|
-
self.packet.DecodePacket(
|
|
356
|
-
six.b('\x01\x02\x00\x231234567890123456\x1a\x0f\x00\x00\x00\x10\x03\x09\x01\x07value'))
|
|
357
|
-
self.assertEqual(self.packet[(16,3)], {1:[six.b('value')]})
|
|
358
|
-
|
|
359
|
-
def testDecodePacketWithTlvAttributeWith2SubAttributes(self):
|
|
360
|
-
self.packet.DecodePacket(
|
|
361
|
-
six.b('\x01\x02\x00\x231234567890123456\x04\x0f\x01\x07value\x02\x06\x00\x00\x00\x09'))
|
|
362
|
-
self.assertEqual(self.packet[4], {1:[six.b('value')], 2:[six.b('\x00\x00\x00\x09')]})
|
|
363
|
-
|
|
364
|
-
def testDecodePacketWithSplitTlvAttribute(self):
|
|
365
|
-
self.packet.DecodePacket(
|
|
366
|
-
six.b('\x01\x02\x00\x251234567890123456\x04\x09\x01\x07value\x04\x09\x02\x06\x00\x00\x00\x09'))
|
|
367
|
-
self.assertEqual(self.packet[4], {1:[six.b('value')], 2:[six.b('\x00\x00\x00\x09')]})
|
|
368
|
-
|
|
369
|
-
def testDecodePacketWithMultiValuedAttribute(self):
|
|
370
|
-
self.packet.DecodePacket(
|
|
371
|
-
six.b('\x01\x02\x00\x1e1234567890123456\x01\x05one\x01\x05two'))
|
|
372
|
-
self.assertEqual(self.packet[1], [six.b('one'), six.b('two')])
|
|
373
|
-
|
|
374
|
-
def testDecodePacketWithTwoAttributes(self):
|
|
375
|
-
self.packet.DecodePacket(
|
|
376
|
-
six.b('\x01\x02\x00\x1e1234567890123456\x01\x05one\x01\x05two'))
|
|
377
|
-
self.assertEqual(self.packet[1], [six.b('one'), six.b('two')])
|
|
378
|
-
|
|
379
|
-
def testDecodePacketWithVendorAttribute(self):
|
|
380
|
-
self.packet.DecodePacket(
|
|
381
|
-
six.b('\x01\x02\x00\x1b1234567890123456\x1a\x07value'))
|
|
382
|
-
self.assertEqual(self.packet[26], [six.b('value')])
|
|
383
|
-
|
|
384
|
-
def testEncodeKeyValues(self):
|
|
385
|
-
self.assertEqual(self.packet._EncodeKeyValues(1, '1234'), (1, '1234'))
|
|
386
|
-
|
|
387
|
-
def testEncodeKey(self):
|
|
388
|
-
self.assertEqual(self.packet._EncodeKey(1), 1)
|
|
389
|
-
|
|
390
|
-
def testAddAttribute(self):
|
|
391
|
-
self.packet.AddAttribute('Test-String', '1')
|
|
392
|
-
self.assertEqual(self.packet['Test-String'], ['1'])
|
|
393
|
-
self.packet.AddAttribute('Test-String', '1')
|
|
394
|
-
self.assertEqual(self.packet['Test-String'], ['1', '1'])
|
|
395
|
-
self.packet.AddAttribute('Test-String', ['2', '3'])
|
|
396
|
-
self.assertEqual(self.packet['Test-String'], ['1', '1', '2', '3'])
|
|
397
|
-
|
|
398
|
-
class AuthPacketConstructionTests(PacketConstructionTests):
|
|
399
|
-
klass = packet.AuthPacket
|
|
400
|
-
|
|
401
|
-
def testConstructorDefaults(self):
|
|
402
|
-
pkt = self.klass()
|
|
403
|
-
self.assertEqual(pkt.code, packet.AccessRequest)
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
class AuthPacketTests(unittest.TestCase):
|
|
407
|
-
def setUp(self):
|
|
408
|
-
self.path = os.path.join(home, 'tests', 'data')
|
|
409
|
-
self.dict = Dictionary(os.path.join(self.path, 'full'))
|
|
410
|
-
self.packet = packet.AuthPacket(id=0, secret=six.b('secret'),
|
|
411
|
-
authenticator=six.b('01234567890ABCDEF'), dict=self.dict)
|
|
412
|
-
|
|
413
|
-
def testCreateReply(self):
|
|
414
|
-
reply = self.packet.CreateReply(**{'Test-Integer' : 10})
|
|
415
|
-
self.assertEqual(reply.code, packet.AccessAccept)
|
|
416
|
-
self.assertEqual(reply.id, self.packet.id)
|
|
417
|
-
self.assertEqual(reply.secret, self.packet.secret)
|
|
418
|
-
self.assertEqual(reply.authenticator, self.packet.authenticator)
|
|
419
|
-
self.assertEqual(reply['Test-Integer'], [10])
|
|
420
|
-
|
|
421
|
-
def testRequestPacket(self):
|
|
422
|
-
self.assertEqual(self.packet.RequestPacket(),
|
|
423
|
-
six.b('\x01\x00\x00\x1401234567890ABCDE'))
|
|
424
|
-
|
|
425
|
-
def testRequestPacketCreatesAuthenticator(self):
|
|
426
|
-
self.packet.authenticator = None
|
|
427
|
-
self.packet.RequestPacket()
|
|
428
|
-
self.assertTrue(self.packet.authenticator is not None)
|
|
429
|
-
|
|
430
|
-
def testRequestPacketCreatesID(self):
|
|
431
|
-
self.packet.id = None
|
|
432
|
-
self.packet.RequestPacket()
|
|
433
|
-
self.assertTrue(self.packet.id is not None)
|
|
434
|
-
|
|
435
|
-
def testPwCryptEmptyPassword(self):
|
|
436
|
-
self.assertEqual(self.packet.PwCrypt(''), six.b(''))
|
|
437
|
-
|
|
438
|
-
def testPwCryptPassword(self):
|
|
439
|
-
self.assertEqual(self.packet.PwCrypt('Simplon'),
|
|
440
|
-
six.b('\xd3U;\xb23\r\x11\xba\x07\xe3\xa8*\xa8x\x14\x01'))
|
|
441
|
-
|
|
442
|
-
def testPwCryptSetsAuthenticator(self):
|
|
443
|
-
self.packet.authenticator = None
|
|
444
|
-
self.packet.PwCrypt(six.u(''))
|
|
445
|
-
self.assertTrue(self.packet.authenticator is not None)
|
|
446
|
-
|
|
447
|
-
def testPwDecryptEmptyPassword(self):
|
|
448
|
-
self.assertEqual(self.packet.PwDecrypt(six.b('')), six.u(''))
|
|
449
|
-
|
|
450
|
-
def testPwDecryptPassword(self):
|
|
451
|
-
self.assertEqual(self.packet.PwDecrypt(
|
|
452
|
-
six.b('\xd3U;\xb23\r\x11\xba\x07\xe3\xa8*\xa8x\x14\x01')),
|
|
453
|
-
six.u('Simplon'))
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
class AuthPacketChapTests(unittest.TestCase):
|
|
457
|
-
def setUp(self):
|
|
458
|
-
self.path = os.path.join(home, 'tests', 'data')
|
|
459
|
-
self.dict = Dictionary(os.path.join(self.path, 'chap'))
|
|
460
|
-
# self.packet = packet.Packet(id=0, secret=six.b('secret'),
|
|
461
|
-
# dict=self.dict)
|
|
462
|
-
self.client = Client(server='localhost', secret=six.b('secret'),
|
|
463
|
-
dict=self.dict)
|
|
464
|
-
|
|
465
|
-
def testVerifyChapPasswd(self):
|
|
466
|
-
chap_id = b'9'
|
|
467
|
-
chap_challenge = b'987654321'
|
|
468
|
-
chap_password = chap_id + md5_constructor(
|
|
469
|
-
chap_id + b'test_password' + chap_challenge).digest()
|
|
470
|
-
pkt = self.client.CreateAuthPacket(
|
|
471
|
-
code=packet.AccessChallenge,
|
|
472
|
-
authenticator=b'ABCDEFG',
|
|
473
|
-
User_Name='test_name',
|
|
474
|
-
CHAP_Challenge=chap_challenge,
|
|
475
|
-
CHAP_Password=chap_password
|
|
476
|
-
)
|
|
477
|
-
self.assertEqual(pkt['CHAP-Challenge'][0], chap_challenge)
|
|
478
|
-
self.assertEqual(pkt['CHAP-Password'][0], chap_password)
|
|
479
|
-
self.assertEqual(pkt.VerifyChapPasswd('test_password'), True)
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
class AcctPacketConstructionTests(PacketConstructionTests):
|
|
483
|
-
klass = packet.AcctPacket
|
|
484
|
-
|
|
485
|
-
def testConstructorDefaults(self):
|
|
486
|
-
pkt = self.klass()
|
|
487
|
-
self.assertEqual(pkt.code, packet.AccountingRequest)
|
|
488
|
-
|
|
489
|
-
def testConstructorRawPacket(self):
|
|
490
|
-
raw = six.b('\x00\x00\x00\x14\xb0\x5e\x4b\xfb\xcc\x1c' \
|
|
491
|
-
'\x8c\x8e\xc4\x72\xac\xea\x87\x45\x63\xa7')
|
|
492
|
-
pkt = self.klass(packet=raw)
|
|
493
|
-
self.assertEqual(pkt.raw_packet, raw)
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
class AcctPacketTests(unittest.TestCase):
|
|
497
|
-
def setUp(self):
|
|
498
|
-
self.path = os.path.join(home, 'tests', 'data')
|
|
499
|
-
self.dict = Dictionary(os.path.join(self.path, 'full'))
|
|
500
|
-
self.packet = packet.AcctPacket(id=0, secret=six.b('secret'),
|
|
501
|
-
authenticator=six.b('01234567890ABCDEF'), dict=self.dict)
|
|
502
|
-
|
|
503
|
-
def testCreateReply(self):
|
|
504
|
-
reply = self.packet.CreateReply(**{'Test-Integer' : 10})
|
|
505
|
-
self.assertEqual(reply.code, packet.AccountingResponse)
|
|
506
|
-
self.assertEqual(reply.id, self.packet.id)
|
|
507
|
-
self.assertEqual(reply.secret, self.packet.secret)
|
|
508
|
-
self.assertEqual(reply.authenticator, self.packet.authenticator)
|
|
509
|
-
self.assertEqual(reply['Test-Integer'], [10])
|
|
510
|
-
|
|
511
|
-
def testVerifyAcctRequest(self):
|
|
512
|
-
rawpacket = self.packet.RequestPacket()
|
|
513
|
-
pkt = packet.AcctPacket(secret=six.b('secret'), packet=rawpacket)
|
|
514
|
-
self.assertEqual(pkt.VerifyAcctRequest(), True)
|
|
515
|
-
|
|
516
|
-
pkt.secret = six.b('different')
|
|
517
|
-
self.assertEqual(pkt.VerifyAcctRequest(), False)
|
|
518
|
-
pkt.secret = six.b('secret')
|
|
519
|
-
|
|
520
|
-
pkt.raw_packet = six.b('X') + pkt.raw_packet[1:]
|
|
521
|
-
self.assertEqual(pkt.VerifyAcctRequest(), False)
|
|
522
|
-
|
|
523
|
-
def testRequestPacket(self):
|
|
524
|
-
self.assertEqual(self.packet.RequestPacket(),
|
|
525
|
-
six.b('\x04\x00\x00\x14\x95\xdf\x90\xccbn\xfb\x15G!\x13\xea\xfa>6\x0f'))
|
|
526
|
-
|
|
527
|
-
def testRequestPacketSetsId(self):
|
|
528
|
-
self.packet.id = None
|
|
529
|
-
self.packet.RequestPacket()
|
|
530
|
-
self.assertTrue(self.packet.id is not None)
|
pyrad/tests/testTools.py
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
from netaddr import AddrFormatError
|
|
2
|
-
from pyrad import tools
|
|
3
|
-
import unittest
|
|
4
|
-
import six
|
|
5
|
-
import sys
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class EncodingTests(unittest.TestCase):
|
|
10
|
-
def testStringEncoding(self):
|
|
11
|
-
self.assertRaises(ValueError, tools.EncodeString, 'x' * 254)
|
|
12
|
-
self.assertEqual(
|
|
13
|
-
tools.EncodeString('1234567890'),
|
|
14
|
-
six.b('1234567890'))
|
|
15
|
-
|
|
16
|
-
def testInvalidStringEncodingRaisesTypeError(self):
|
|
17
|
-
self.assertRaises(TypeError, tools.EncodeString, 1)
|
|
18
|
-
|
|
19
|
-
def testAddressEncoding(self):
|
|
20
|
-
self.assertRaises(AddrFormatError, tools.EncodeAddress, 'TEST123')
|
|
21
|
-
self.assertEqual(
|
|
22
|
-
tools.EncodeAddress('192.168.0.255'),
|
|
23
|
-
six.b('\xc0\xa8\x00\xff'))
|
|
24
|
-
|
|
25
|
-
def testInvalidAddressEncodingRaisesTypeError(self):
|
|
26
|
-
self.assertRaises(TypeError, tools.EncodeAddress, 1)
|
|
27
|
-
|
|
28
|
-
def testIntegerEncoding(self):
|
|
29
|
-
self.assertEqual(tools.EncodeInteger(0x01020304), six.b('\x01\x02\x03\x04'))
|
|
30
|
-
|
|
31
|
-
def testInteger64Encoding(self):
|
|
32
|
-
self.assertEqual(
|
|
33
|
-
tools.EncodeInteger64(0xFFFFFFFFFFFFFFFF), six.b('\xff' * 8)
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
def testUnsignedIntegerEncoding(self):
|
|
37
|
-
self.assertEqual(tools.EncodeInteger(0xFFFFFFFF), six.b('\xff\xff\xff\xff'))
|
|
38
|
-
|
|
39
|
-
def testInvalidIntegerEncodingRaisesTypeError(self):
|
|
40
|
-
self.assertRaises(TypeError, tools.EncodeInteger, 'ONE')
|
|
41
|
-
|
|
42
|
-
def testDateEncoding(self):
|
|
43
|
-
self.assertEqual(tools.EncodeDate(0x01020304), six.b('\x01\x02\x03\x04'))
|
|
44
|
-
|
|
45
|
-
def testInvalidDataEncodingRaisesTypeError(self):
|
|
46
|
-
self.assertRaises(TypeError, tools.EncodeDate, '1')
|
|
47
|
-
|
|
48
|
-
def testEncodeAscendBinary(self):
|
|
49
|
-
self.assertEqual(
|
|
50
|
-
tools.EncodeAscendBinary('family=ipv4 action=discard direction=in dst=10.10.255.254/32'),
|
|
51
|
-
six.b('\x01\x00\x01\x00\x00\x00\x00\x00\n\n\xff\xfe\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'))
|
|
52
|
-
|
|
53
|
-
def testStringDecoding(self):
|
|
54
|
-
self.assertEqual(
|
|
55
|
-
tools.DecodeString(six.b('1234567890')),
|
|
56
|
-
'1234567890')
|
|
57
|
-
|
|
58
|
-
def testAddressDecoding(self):
|
|
59
|
-
self.assertEqual(
|
|
60
|
-
tools.DecodeAddress(six.b('\xc0\xa8\x00\xff')),
|
|
61
|
-
'192.168.0.255')
|
|
62
|
-
|
|
63
|
-
def testIntegerDecoding(self):
|
|
64
|
-
self.assertEqual(
|
|
65
|
-
tools.DecodeInteger(six.b('\x01\x02\x03\x04')),
|
|
66
|
-
0x01020304)
|
|
67
|
-
|
|
68
|
-
def testInteger64Decoding(self):
|
|
69
|
-
self.assertEqual(
|
|
70
|
-
tools.DecodeInteger64(six.b('\xff' * 8)), 0xFFFFFFFFFFFFFFFF
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
def testDateDecoding(self):
|
|
74
|
-
self.assertEqual(
|
|
75
|
-
tools.DecodeDate(six.b('\x01\x02\x03\x04')),
|
|
76
|
-
0x01020304)
|
|
77
|
-
|
|
78
|
-
def testUnknownTypeEncoding(self):
|
|
79
|
-
self.assertRaises(ValueError, tools.EncodeAttr, 'unknown', None)
|
|
80
|
-
|
|
81
|
-
def testUnknownTypeDecoding(self):
|
|
82
|
-
self.assertRaises(ValueError, tools.DecodeAttr, 'unknown', None)
|
|
83
|
-
|
|
84
|
-
def testEncodeFunction(self):
|
|
85
|
-
self.assertEqual(
|
|
86
|
-
tools.EncodeAttr('string', six.u('string')),
|
|
87
|
-
six.b('string'))
|
|
88
|
-
self.assertEqual(
|
|
89
|
-
tools.EncodeAttr('octets', six.b('string')),
|
|
90
|
-
six.b('string'))
|
|
91
|
-
self.assertEqual(
|
|
92
|
-
tools.EncodeAttr('ipaddr', '192.168.0.255'),
|
|
93
|
-
six.b('\xc0\xa8\x00\xff'))
|
|
94
|
-
self.assertEqual(
|
|
95
|
-
tools.EncodeAttr('integer', 0x01020304),
|
|
96
|
-
six.b('\x01\x02\x03\x04'))
|
|
97
|
-
self.assertEqual(
|
|
98
|
-
tools.EncodeAttr('date', 0x01020304),
|
|
99
|
-
six.b('\x01\x02\x03\x04'))
|
|
100
|
-
self.assertEqual(
|
|
101
|
-
tools.EncodeAttr('integer64', 0xFFFFFFFFFFFFFFFF),
|
|
102
|
-
six.b('\xff'*8))
|
|
103
|
-
|
|
104
|
-
def testDecodeFunction(self):
|
|
105
|
-
self.assertEqual(
|
|
106
|
-
tools.DecodeAttr('string', six.b('string')),
|
|
107
|
-
six.u('string'))
|
|
108
|
-
self.assertEqual(
|
|
109
|
-
tools.EncodeAttr('octets', six.b('string')),
|
|
110
|
-
six.b('string'))
|
|
111
|
-
self.assertEqual(
|
|
112
|
-
tools.DecodeAttr('ipaddr', six.b('\xc0\xa8\x00\xff')),
|
|
113
|
-
'192.168.0.255')
|
|
114
|
-
self.assertEqual(
|
|
115
|
-
tools.DecodeAttr('integer', six.b('\x01\x02\x03\x04')),
|
|
116
|
-
0x01020304)
|
|
117
|
-
self.assertEqual(
|
|
118
|
-
tools.DecodeAttr('integer64', six.b('\xff'*8)),
|
|
119
|
-
0xFFFFFFFFFFFFFFFF)
|
|
120
|
-
self.assertEqual(
|
|
121
|
-
tools.DecodeAttr('date', six.b('\x01\x02\x03\x04')),
|
|
122
|
-
0x01020304)
|