From 346b1ec1e45208b0a286bbad0036dcc961ee01a2 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Sun, 13 Apr 2008 16:10:28 +0000 Subject: [PATCH] Make sure all encoding utility methods raise a TypeError if a value of the wrong type is passed in. --- CHANGES.txt | 3 +++ LICENSE.txt | 4 ++-- README.txt | 6 +++--- pyrad/tests/testTools.py | 16 ++++++++++++++++ pyrad/tools.py | 6 ++++++ setup.py | 3 ++- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index af25e35..706de02 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 ========= diff --git a/LICENSE.txt b/LICENSE.txt index d6f85df..e01f563 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -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. diff --git a/README.txt b/README.txt index 5a6d1b5..5cf5752 100644 --- a/README.txt +++ b/README.txt @@ -43,9 +43,10 @@ Python modules:: Author, copyright, availability =============================== -pyrad was written by Wichert Akkerman +pyrad was written by Wichert Akkerman 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 @@ -53,4 +54,3 @@ http://www.simplon.biz/software/pyrad Bugs and wishes can be submitted in the pyrad issue tracker: https://code.wiggy.net/tracker/pyrad/ - diff --git a/pyrad/tests/testTools.py b/pyrad/tests/testTools.py index eb24171..5f423c9 100644 --- a/pyrad/tests/testTools.py +++ b/pyrad/tests/testTools.py @@ -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") diff --git a/pyrad/tools.py b/pyrad/tools.py index 0c040d4..109d0d9 100644 --- a/pyrad/tools.py +++ b/pyrad/tools.py @@ -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) diff --git a/setup.py b/setup.py index be13bee..6541fb8 100755 --- a/setup.py +++ b/setup.py @@ -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",