Skip to content

Commit

Permalink
Migrate to pytest features
Browse files Browse the repository at this point in the history
This PR introduces the following changes:

- Migrate the test suite to use pytest features
- Address a duplicated test name in test_Exceptions.py:
  test_ListReadersException.  The duplicate tests have been merged.

Most of this work was mechanical:

- Remove unittest.TestCase subclass definition lines
- De-dent the class methods
- Remove references to self in the function parameters
- Use regular expressions to search and replace assertions:
  - self.assertEqual(x, y) -> assert x == y
  - self.assertRaises -> pytest.raises
  - self.assertFalse(x) -> assert x is False
  - self.assertIn(x, y) -> assert x in y

Some long strings were wrapped to prepare for the possibility of using
code formatters in the future.

STDOUT capture code was replaced with the pytest capsys fixture, which
simplified the code in test_ATR.py.

This PR prepares for the possibility of consolidating the live tests in
src/smartcard/test/ together with the overall test suite, and using
pytest features to control whether the live tests run or not.

This is only a possibility, not a guarantee, but migrating from unittest
code to pytest code helps pave the way.
  • Loading branch information
kurtmckee authored and LudovicRousseau committed Sep 27, 2024
1 parent 51c268a commit 1e3c419
Show file tree
Hide file tree
Showing 7 changed files with 564 additions and 528 deletions.
Empty file removed test/__init__.py
Empty file.
209 changes: 102 additions & 107 deletions test/test_ATR.py
Original file line number Diff line number Diff line change
@@ -1,101 +1,97 @@
# -*- coding: utf-8 -*-
import pytest

import io
import sys
import unittest
from smartcard.ATR import ATR
from smartcard.Exceptions import SmartcardException
from smartcard.util import toBytes


class TestUtil(unittest.TestCase):

def setUp(self):
self.held, sys.stdout = sys.stdout, io.StringIO()

def test_ATR1(self):
atr = [0x3F, 0x65, 0x25, 0x00, 0x2C, 0x09, 0x69, 0x90, 0x00]
data_out = """TB1: 25
def test_atr1(capsys):
atr = [0x3F, 0x65, 0x25, 0x00, 0x2C, 0x09, 0x69, 0x90, 0x00]
data_out = """TB1: 25
TC1: 0
supported protocols T=0
T=0 supported: True
T=1 supported: False
clock rate conversion factor: 372
bit rate adjustment factor: 1
maximum programming current: 50
programming voltage: 30
guard time: 0
\tclock rate conversion factor: 372
\tbit rate adjustment factor: 1
\tmaximum programming current: 50
\tprogramming voltage: 30
\tguard time: 0
nb of interface bytes: 2
nb of historical bytes: 5
"""
a = ATR(atr)
a.dump()
output = sys.stdout.getvalue()
self.assertEqual(output, data_out)

def test_ATR2(self):
atr = [0x3F, 0x65, 0x25, 0x08, 0x93, 0x04, 0x6C, 0x90, 0x00]
data_out = """TB1: 25
a = ATR(atr)
a.dump()
stdout, _ = capsys.readouterr()
assert stdout == data_out


def test_atr2(capsys):
atr = [0x3F, 0x65, 0x25, 0x08, 0x93, 0x04, 0x6C, 0x90, 0x00]
data_out = """TB1: 25
TC1: 8
supported protocols T=0
T=0 supported: True
T=1 supported: False
clock rate conversion factor: 372
bit rate adjustment factor: 1
maximum programming current: 50
programming voltage: 30
guard time: 8
\tclock rate conversion factor: 372
\tbit rate adjustment factor: 1
\tmaximum programming current: 50
\tprogramming voltage: 30
\tguard time: 8
nb of interface bytes: 2
nb of historical bytes: 5
"""
a = ATR(atr)
a.dump()
output = sys.stdout.getvalue()
self.assertEqual(output, data_out)

def test_ATR3(self):
atr = [0x3B, 0x16, 0x94, 0x7C, 0x03, 0x01, 0x00, 0x00, 0x0D]
data_out = """TA1: 94
a = ATR(atr)
a.dump()

stdout, _ = capsys.readouterr()
assert stdout == data_out


def test_atr3(capsys):
atr = [0x3B, 0x16, 0x94, 0x7C, 0x03, 0x01, 0x00, 0x00, 0x0D]
data_out = """TA1: 94
supported protocols T=0
T=0 supported: True
T=1 supported: False
clock rate conversion factor: 512
bit rate adjustment factor: 8
maximum programming current: 50
programming voltage: 5
guard time: None
\tclock rate conversion factor: 512
\tbit rate adjustment factor: 8
\tmaximum programming current: 50
\tprogramming voltage: 5
\tguard time: None
nb of interface bytes: 1
nb of historical bytes: 6
"""
a = ATR(atr)
a.dump()
output = sys.stdout.getvalue()
self.assertEqual(output, data_out)

def test_ATR4(self):
atr = [0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03]
data_out = """TB1: 0
a = ATR(atr)
a.dump()
stdout, _ = capsys.readouterr()
assert stdout == data_out


def test_atr4(capsys):
atr = [0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03]
data_out = """TB1: 0
TC1: 0
supported protocols T=0
T=0 supported: True
T=1 supported: False
clock rate conversion factor: 372
bit rate adjustment factor: 1
maximum programming current: 25
programming voltage: 5
guard time: 0
\tclock rate conversion factor: 372
\tbit rate adjustment factor: 1
\tmaximum programming current: 25
\tprogramming voltage: 5
\tguard time: 0
nb of interface bytes: 2
nb of historical bytes: 5
"""
a = ATR(atr)
a.dump()
output = sys.stdout.getvalue()
self.assertEqual(output, data_out)

def test_ATR5(self):
atr = [0x3B, 0xE3, 0x00, 0xFF, 0x81, 0x31, 0x52, 0x45, 0xA1,
0xA2, 0xA3, 0x1B]
data_out = """TB1: 0
a = ATR(atr)
a.dump()
stdout, _ = capsys.readouterr()
assert stdout == data_out


def test_atr5(capsys):
atr = [0x3B, 0xE3, 0x00, 0xFF, 0x81, 0x31, 0x52, 0x45, 0xA1, 0xA2, 0xA3, 0x1B]
data_out = """TB1: 0
TC1: ff
TD1: 81
TD2: 31
Expand All @@ -105,23 +101,23 @@ def test_ATR5(self):
T=0 supported: False
T=1 supported: True
checksum: 27
clock rate conversion factor: 372
bit rate adjustment factor: 1
maximum programming current: 25
programming voltage: 5
guard time: 255
\tclock rate conversion factor: 372
\tbit rate adjustment factor: 1
\tmaximum programming current: 25
\tprogramming voltage: 5
\tguard time: 255
nb of interface bytes: 6
nb of historical bytes: 3
"""
a = ATR(atr)
a.dump()
output = sys.stdout.getvalue()
self.assertEqual(output, data_out)

def test_ATR6(self):
atr = [0x3B, 0xE5, 0x00, 0x00, 0x81, 0x21, 0x45, 0x9C, 0x10,
0x01, 0x00, 0x80, 0x0D]
data_out = """TB1: 0
a = ATR(atr)
a.dump()
stdout, _ = capsys.readouterr()
assert stdout == data_out


def test_atr6(capsys):
atr = [0x3B, 0xE5, 0x00, 0x00, 0x81, 0x21, 0x45, 0x9C, 0x10, 0x01, 0x00, 0x80, 0x0D]
data_out = """TB1: 0
TC1: 0
TD1: 81
TD2: 21
Expand All @@ -130,34 +126,33 @@ def test_ATR6(self):
T=0 supported: False
T=1 supported: True
checksum: 13
clock rate conversion factor: 372
bit rate adjustment factor: 1
maximum programming current: 25
programming voltage: 5
guard time: 0
\tclock rate conversion factor: 372
\tbit rate adjustment factor: 1
\tmaximum programming current: 25
\tprogramming voltage: 5
\tguard time: 0
nb of interface bytes: 5
nb of historical bytes: 5
"""
a = ATR(atr)
a.dump()
output = sys.stdout.getvalue()
self.assertEqual(output, data_out)

def test_ATR_TS(self):
atr = [0x42]
with self.assertRaises(SmartcardException):
ATR(atr)

def test_ATR_get(self):
atr = "3B F2 95 12 34 01 36 06"
a = ATR(toBytes(atr))
self.assertEqual(a.getTA1(), 0x95)
self.assertEqual(a.getTB1(), 0x12)
self.assertEqual(a.getTC1(), 0x34)
self.assertEqual(a.getTD1(), 0x01)
self.assertEqual(a.getHistoricalBytes(), [0x36, 0x06])
self.assertFalse(a.isT15Supported())
self.assertEqual(str(a), atr)

if __name__ == '__main__':
unittest.main(buffer=True)
a = ATR(atr)
a.dump()
stdout, _ = capsys.readouterr()
assert stdout == data_out


def test_atr_ts():
atr = [0x42]
with pytest.raises(SmartcardException):
ATR(atr)


def test_atr_get():
atr = "3B F2 95 12 34 01 36 06"
a = ATR(toBytes(atr))
assert a.getTA1() == 0x95
assert a.getTB1() == 0x12
assert a.getTC1() == 0x34
assert a.getTD1() == 0x01
assert a.getHistoricalBytes(), [0x36 == 0x06]
assert a.isT15Supported() is False
assert str(a) == atr
Loading

0 comments on commit 1e3c419

Please sign in to comment.