Skip to content

Commit

Permalink
Make sure all encoding utility methods raise a TypeError if a value o…
Browse files Browse the repository at this point in the history
…f the wrong type is passed in.
  • Loading branch information
wichert committed Apr 13, 2008
1 parent 8708582 commit 346b1ec
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Pyrad 1.2 (unreleased)
======================

* Make sure all encoding utility methods raise a TypeError if a value of
the wrong type is passed in.


Pyrad 1.1
=========
Expand Down
4 changes: 2 additions & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Copyright 2002-2007 Wichert Akkerman. All rights reserved.
Copyright 2007 Simplon. All rights reserved.
Copyright 2002-2008 Wichert Akkerman. All rights reserved.
Copyright 2007-2008 Simplon. All rights reserved.

All rights reserved.

Expand Down
6 changes: 3 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ Python modules::
Author, copyright, availability
===============================

pyrad was written by Wichert Akkerman <wichert@wiggy.net>
pyrad was written by Wichert Akkerman <wichert@wiggy.net> and is licensed
under a BSD license.

Copyright and license information can be found in LICENSE.txt
Copyright and license information can be found in the LICENSE.txt file.

The current version and documentation can be found at its homepage:
http://www.simplon.biz/software/pyrad

Bugs and wishes can be submitted in the pyrad issue tracker:
https://code.wiggy.net/tracker/pyrad/


16 changes: 16 additions & 0 deletions pyrad/tests/testTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,38 @@ def testStringEncoding(self):
self.assertEqual(tools.EncodeString("1234567890"), "1234567890")


def testInvalidStringEncodingRaisesTypeError(self):
self.assertRaises(TypeError, tools.EncodeString, 1)


def testAddressEncoding(self):
self.assertRaises(ValueError, tools.EncodeAddress, "123")
self.assertEqual(tools.EncodeAddress("192.168.0.255"),
"\xc0\xa8\x00\xff")


def testInvalidAddressEncodingRaisesTypeError(self):
self.assertRaises(TypeError, tools.EncodeAddress, 1)


def testIntegerEncoding(self):
self.assertEqual(tools.EncodeInteger(0x01020304),
"\x01\x02\x03\x04")


def testInvalidIntegerEncodingRaisesTypeError(self):
self.assertRaises(TypeError, tools.EncodeInteger, "1")


def testDateEncoding(self):
self.assertEqual(tools.EncodeDate(0x01020304),
"\x01\x02\x03\x04")


def testInvalidDataEncodingRaisesTypeError(self):
self.assertRaises(TypeError, tools.EncodeDate, "1")


def testStringDecoding(self):
self.assertEqual(tools.DecodeString("1234567890"), "1234567890")

Expand Down
6 changes: 6 additions & 0 deletions pyrad/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ def EncodeString(str):


def EncodeAddress(addr):
if not isinstance(addr, basestring):
raise TypeError, "Address has to be a string"
(a,b,c,d)=map(int, addr.split("."))
return struct.pack("BBBB", a, b, c, d)


def EncodeInteger(num):
if not isinstance(num, int):
raise TypeError, "Can not encode non-integer as integer"
return struct.pack("!I", num)


def EncodeDate(num):
if not isinstance(num, int):
raise TypeError, "Can not encode non-integer as date"
return struct.pack("!I", num)


Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
url = "http://www.wiggy.net/code/pyrad/",
license = "BSD",
description = "RADIUS client tools",
long_description= open("README.txt").read()+open("CHANGES.txt").read(),
long_description= open("README.txt").read() + "\n" +
open("CHANGES.txt").read(),
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
Expand Down

0 comments on commit 346b1ec

Please sign in to comment.