Convert existing unit tests to the python unittest framework
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import sys, binascii, random, logging
|
||||
import sys, binascii, random, logging, unittest
|
||||
from Crypto.Cipher import DES3, DES, AES, ARC4#@UnusedImport
|
||||
from struct import pack
|
||||
from binascii import b2a_hex, a2b_hex
|
||||
@@ -605,20 +605,23 @@ def test_pbkdf2():
|
||||
raise RuntimeError("self-test failed")
|
||||
print "PBKDF2 self test successfull"
|
||||
|
||||
if __name__ == "__main__":
|
||||
too_short = binascii.a2b_hex("".join("89 45 19 BF".split()))
|
||||
padded = append_padding(8, too_short)
|
||||
print "Padded data: " + hexdump(padded)
|
||||
unpadded = strip_padding(8, padded)
|
||||
print "Without padding: " + hexdump(unpadded)
|
||||
|
||||
teststring = "DEADBEEFistatsyksdvhwohfwoehcowc8hw8rogfq8whv75tsgohsav8wress"
|
||||
foo = append_padding(16, teststring)
|
||||
assert(strip_padding(16, foo) == teststring)
|
||||
class TestCryptoUtils(unittest.TestCase):
|
||||
|
||||
testpass = "SomeRandomPassphrase"
|
||||
protectedString = protect_string(teststring, testpass)
|
||||
unprotectedString = read_protected_string(protectedString, testpass)
|
||||
assert(teststring == unprotectedString)
|
||||
|
||||
def setUp(self):
|
||||
self.teststring = "DEADBEEFistatsyksdvhwohfwoehcowc8hw8rogfq8whv75tsgohsav8wress"
|
||||
self.testpass = "SomeRandomPassphrase"
|
||||
|
||||
def test_padding(self):
|
||||
padded = append_padding(16, self.teststring)
|
||||
unpadded = strip_padding(16, padded)
|
||||
self.assertEqual(unpadded, self.teststring)
|
||||
|
||||
def test_protect_string(self):
|
||||
protectedString = protect_string(self.teststring, self.testpass)
|
||||
unprotectedString = read_protected_string(protectedString, self.testpass)
|
||||
self.assertEqual(self.teststring, unprotectedString)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
#test_pbkdf2()
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
import logging
|
||||
import logging, unittest
|
||||
from pickle import dumps, loads
|
||||
from os import urandom
|
||||
|
||||
@@ -357,32 +357,45 @@ class SAM(object):
|
||||
return self.current_SE.manage_security_environment(p1, p2, data)
|
||||
|
||||
#Unit Tests
|
||||
class TestSmartcardSAM(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.password = "DUMMYKEYDUMMYKEY"
|
||||
self.myCard = SAM("1234", "1234567890")
|
||||
|
||||
def test_incorrect_pin(self):
|
||||
with self.assertRaises(SwError):
|
||||
self.myCard.verify(0x00, 0x00, "5678")
|
||||
|
||||
def test_counter_decrement(self):
|
||||
ctr1 = self.myCard.counter
|
||||
try:
|
||||
self.myCard.verify(0x00, 0x00, "3456")
|
||||
except SwError as e:
|
||||
pass
|
||||
self.assertEquals(self.myCard.counter, ctr1 - 1)
|
||||
|
||||
def test_internal_authenticate(self):
|
||||
sw, challenge = self.myCard.get_challenge(0x00, 0x00, "")
|
||||
print("Before encryption: " + challenge)
|
||||
blocklen = vsCrypto.get_cipher_blocklen("DES3-ECB")
|
||||
padded = vsCrypto.append_padding(blocklen, challenge)
|
||||
sw, result_data = self.myCard.internal_authenticate(0x00, 0x00, padded)
|
||||
print("Internal Authenticate status code: %x" % sw)
|
||||
self.assertEquals(sw, SW["NORMAL"])
|
||||
|
||||
def test_external_authenticate(self):
|
||||
sw, challenge = self.myCard.get_challenge(0x00, 0x00, "")
|
||||
print("Before encryption: " + challenge)
|
||||
blocklen = vsCrypto.get_cipher_blocklen("DES3-ECB")
|
||||
padded = vsCrypto.append_padding(blocklen, challenge)
|
||||
sw, result_data = self.myCard.internal_authenticate(0x00, 0x00, padded)
|
||||
sw, result_data = self.myCard.external_authenticate(0x00, 0x00, result_data)
|
||||
print ("After external authenticate: " + result_data)
|
||||
self.assertEquals(sw, SW["NORMAL"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
password = "DUMMYKEYDUMMYKEY"
|
||||
|
||||
MyCard = SAM("1234", "1234567890")
|
||||
try:
|
||||
print(MyCard.verify(0x00, 0x00, "5678"))
|
||||
except SwError as e:
|
||||
print(e.message)
|
||||
|
||||
print("Counter = " + str(MyCard.counter))
|
||||
print(MyCard.verify(0x00, 0x00, "1234"))
|
||||
print("Counter = " + str(MyCard.counter))
|
||||
sw, challenge = MyCard.get_challenge(0x00, 0x00, "")
|
||||
print("Before encryption: " + challenge)
|
||||
blocklen = vsCrypto.get_cipher_blocklen("DES3-ECB")
|
||||
padded = vsCrypto.append_padding(blocklen, challenge)
|
||||
sw, result_data = MyCard.internal_authenticate(0x00, 0x00, padded)
|
||||
print("Internal Authenticate status code: %x" % sw)
|
||||
|
||||
try:
|
||||
sw, res = MyCard.external_authenticate(0x00, 0x00, result_data)
|
||||
except SwError as e:
|
||||
print(e.message)
|
||||
sw = e.sw
|
||||
print("Decryption Status code: %x" % sw)
|
||||
unittest.main()
|
||||
|
||||
#SE = Security_Environment(None)
|
||||
#testvektor = "foobar"
|
||||
|
||||
Reference in New Issue
Block a user