Moved unit tests to a subpackage
This commit is contained in:
@@ -16,7 +16,7 @@
|
|||||||
# You should have received a copy of the GNU General Public License along with
|
# You should have received a copy of the GNU General Public License along with
|
||||||
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
|
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
import sys, binascii, random, logging, unittest
|
import sys, binascii, random, logging
|
||||||
from Crypto.Cipher import DES3, DES, AES, ARC4#@UnusedImport
|
from Crypto.Cipher import DES3, DES, AES, ARC4#@UnusedImport
|
||||||
from struct import pack
|
from struct import pack
|
||||||
from binascii import b2a_hex, a2b_hex
|
from binascii import b2a_hex, a2b_hex
|
||||||
@@ -604,24 +604,3 @@ def test_pbkdf2():
|
|||||||
if result != expected:
|
if result != expected:
|
||||||
raise RuntimeError("self-test failed")
|
raise RuntimeError("self-test failed")
|
||||||
print "PBKDF2 self test successfull"
|
print "PBKDF2 self test successfull"
|
||||||
|
|
||||||
class TestCryptoUtils(unittest.TestCase):
|
|
||||||
|
|
||||||
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/>.
|
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
import logging, unittest
|
import logging
|
||||||
from pickle import dumps, loads
|
from pickle import dumps, loads
|
||||||
from os import urandom
|
from os import urandom
|
||||||
|
|
||||||
@@ -355,62 +355,4 @@ class SAM(object):
|
|||||||
|
|
||||||
def manage_security_environment(self, p1, p2, data):
|
def manage_security_environment(self, p1, p2, data):
|
||||||
return self.current_SE.manage_security_environment(p1, p2, data)
|
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__":
|
|
||||||
unittest.main()
|
|
||||||
|
|
||||||
#SE = Security_Environment(None)
|
|
||||||
#testvektor = "foobar"
|
|
||||||
#print "Testvektor = %s" % testvektor
|
|
||||||
#sw, hash = SE.hash(0x90,0x80,testvektor)
|
|
||||||
#print "SW after hashing = %s" % sw
|
|
||||||
#print "Hash = %s" % hash
|
|
||||||
#sw, crypted = SE.encipher(0x00, 0x00, testvektor)
|
|
||||||
#print "SW after encryption = %s" % sw
|
|
||||||
#sw, plain = SE.decipher(0x00, 0x00, crypted)
|
|
||||||
#print "SW after encryption = %s" % sw
|
|
||||||
#print "Testvektor after en- and deciphering: %s" % plain
|
|
||||||
#sw, pk = SE.generate_public_key_pair(0x02, 0x00, "")
|
|
||||||
#print "SW after keygen = %s" % sw
|
|
||||||
#print "Public Key = %s" % pk
|
|
||||||
#CF = CryptoflexSE(None)
|
|
||||||
#print CF.generate_public_key_pair(0x00, 0x80, "\x01\x00\x01\x00")
|
|
||||||
#print MyCard._get_referenced_key(0x01)
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import unittest
|
||||||
|
from virtualsmartcard.CryptoUtils import *
|
||||||
|
|
||||||
|
class TestCryptoUtils(unittest.TestCase):
|
||||||
|
|
||||||
|
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()
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import unittest
|
||||||
|
from virtualsmartcard.SmartcardSAM import *
|
||||||
|
|
||||||
|
#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__":
|
||||||
|
unittest.main()
|
||||||
|
#SE = Security_Environment(None)
|
||||||
|
#testvektor = "foobar"
|
||||||
|
#print "Testvektor = %s" % testvektor
|
||||||
|
#sw, hash = SE.hash(0x90,0x80,testvektor)
|
||||||
|
#print "SW after hashing = %s" % sw
|
||||||
|
#print "Hash = %s" % hash
|
||||||
|
#sw, crypted = SE.encipher(0x00, 0x00, testvektor)
|
||||||
|
#print "SW after encryption = %s" % sw
|
||||||
|
#sw, plain = SE.decipher(0x00, 0x00, crypted)
|
||||||
|
#print "SW after encryption = %s" % sw
|
||||||
|
#print "Testvektor after en- and deciphering: %s" % plain
|
||||||
|
#sw, pk = SE.generate_public_key_pair(0x02, 0x00, "")
|
||||||
|
#print "SW after keygen = %s" % sw
|
||||||
|
#print "Public Key = %s" % pk
|
||||||
|
#CF = CryptoflexSE(None)
|
||||||
|
#print CF.generate_public_key_pair(0x00, 0x80, "\x01\x00\x01\x00")
|
||||||
|
#print MyCard._get_referenced_key(0x01)
|
||||||
Reference in New Issue
Block a user