Skip to content

Commit

Permalink
Merge 2d9e288 into bd1b2dc
Browse files Browse the repository at this point in the history
  • Loading branch information
btimby committed May 4, 2018
2 parents bd1b2dc + 2d9e288 commit b8d475e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
12 changes: 6 additions & 6 deletions radius.py
Expand Up @@ -389,9 +389,9 @@ def unpack(data):
"""
pos, attrs = 0, {}
while pos < len(data):
code, l = struct.unpack('BB', data[pos:pos + 2])
attrs[code] = data[pos + 2:pos + l]
pos += l
code, length = struct.unpack('BB', data[pos:pos + 2])
attrs[code] = data[pos + 2:pos + length]
pos += length
return Attributes(attrs)


Expand Down Expand Up @@ -446,10 +446,10 @@ def pack(self):
@staticmethod
def unpack(secret, data):
"""Unpack the data into it's fields."""
code, id, l, authenticator = struct.unpack('!BBH16s', data[:20])
if l != len(data):
code, id, length, authenticator = struct.unpack('!BBH16s', data[:20])
if length != len(data):
LOGGER.warning('Too much data!')
attrs = Attributes.unpack(data[20:l])
attrs = Attributes.unpack(data[20:length])
return Message(secret, code, id, authenticator, attrs)

def verify(self, data):
Expand Down
19 changes: 10 additions & 9 deletions tests.py
Expand Up @@ -3,6 +3,7 @@
import socket
import threading
import logging
import time

try:
from hashlib import md5
Expand Down Expand Up @@ -153,6 +154,12 @@ def setUp(self):
def tearDown(self):
self.sock.close()

def startServer(self, target):
t = threading.Thread(target=target)
t.daemon = True
t.start()
time.sleep(0.1)

def test_connect(self):
"""Test connecting."""
r = radius.Radius(TEST_SECRET, host='localhost', port=self.port)
Expand All @@ -170,9 +177,7 @@ def _reply_to_client():
m2 = create_reply(m1)
self.sock.sendto(m2.pack(), addr)

t = threading.Thread(target=_reply_to_client)
t.daemon = True
t.start()
self.startServer(_reply_to_client)

r = radius.Radius(TEST_SECRET, host='localhost', port=self.port)
self.assertFalse(r.authenticate('username', 'password'))
Expand All @@ -186,9 +191,7 @@ def _reply_to_client():
m2 = create_reply(m1, radius.CODE_ACCESS_ACCEPT)
self.sock.sendto(m2.pack(), addr)

t = threading.Thread(target=_reply_to_client)
t.daemon = True
t.start()
self.startServer(_reply_to_client)

r = radius.Radius(TEST_SECRET, host='localhost', port=self.port)
self.assertTrue(r.authenticate('username', 'password'))
Expand All @@ -205,9 +208,7 @@ def _reply_to_client():
})
self.sock.sendto(m2.pack(), addr)

t = threading.Thread(target=_reply_to_client)
t.daemon = True
t.start()
self.startServer(_reply_to_client)

r = radius.Radius(TEST_SECRET, host='localhost', port=self.port)
try:
Expand Down

0 comments on commit b8d475e

Please sign in to comment.