Harald Welte | 21caf32 | 2022-07-16 14:06:46 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | from pySim.utils import h2b, b2h |
| 5 | from pySim.construct import filter_dict |
| 6 | from pySim.apdu import Apdu |
| 7 | from pySim.apdu.ts_31_102 import UsimAuthenticateEven |
| 8 | |
| 9 | class TestApdu(unittest.TestCase): |
| 10 | def test_successful(self): |
| 11 | apdu = Apdu('00a40400023f00', '9000') |
| 12 | self.assertEqual(apdu.successful, True) |
| 13 | apdu = Apdu('00a40400023f00', '6733') |
| 14 | self.assertEqual(apdu.successful, False) |
| 15 | |
| 16 | def test_successful_method(self): |
| 17 | """Test overloading of the success property with a custom method.""" |
| 18 | class SwApdu(Apdu): |
| 19 | def _is_success(self): |
| 20 | return False |
| 21 | apdu = SwApdu('00a40400023f00', '9000') |
| 22 | self.assertEqual(apdu.successful, False) |
| 23 | |
| 24 | # TODO: Tests for TS 102 221 / 31.102 ApduCommands |
| 25 | |
| 26 | class TestUsimAuth(unittest.TestCase): |
| 27 | """Test decoding of the rather complex USIM AUTHENTICATE command.""" |
| 28 | def test_2g(self): |
| 29 | apdu = ('80880080' + '09' + '080001020304050607', |
| 30 | '04a0a1a2a308b0b1b2b3b4b5b6b79000') |
| 31 | res = { |
| 32 | 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'gsm'}, |
| 33 | 'body': {'rand': '0001020304050607', 'autn': None}}, |
| 34 | 'rsp': {'body': {'sres': 'a0a1a2a3', 'kc': 'b0b1b2b3b4b5b6b7'}} |
| 35 | } |
| 36 | u = UsimAuthenticateEven(apdu[0], apdu[1]) |
| 37 | d = filter_dict(u.to_dict()) |
| 38 | self.assertEqual(d, res) |
| 39 | |
| 40 | def test_3g(self): |
| 41 | apdu = ('80880081' + '12' + '080001020304050607081011121314151617', |
| 42 | 'DB' + '08' + 'a0a1a2a3a4a5a6a7' + |
| 43 | '10' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + |
| 44 | '10' + 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + '9000') |
| 45 | res = { |
| 46 | 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'}, |
| 47 | 'body': {'rand': '0001020304050607', 'autn': '1011121314151617'}}, |
| 48 | 'rsp': {'body': {'tag': 219, |
| 49 | 'body': { |
| 50 | 'res': 'a0a1a2a3a4a5a6a7', |
| 51 | 'ck': 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf', |
| 52 | 'ik': 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 53 | 'kc': None |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | u = UsimAuthenticateEven(apdu[0], apdu[1]) |
| 59 | d = filter_dict(u.to_dict()) |
| 60 | self.assertEqual(d, res) |
| 61 | |
| 62 | def test_3g_sync(self): |
| 63 | apdu = ('80880081' + '12' + '080001020304050607081011121314151617', |
| 64 | 'DC' + '08' + 'a0a1a2a3a4a5a6a7' + '9000') |
| 65 | res = { |
| 66 | 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'}, |
| 67 | 'body': {'rand': '0001020304050607', 'autn': '1011121314151617'}}, |
| 68 | 'rsp': {'body': {'tag': 220, 'body': {'auts': 'a0a1a2a3a4a5a6a7' }}} |
| 69 | } |
| 70 | u = UsimAuthenticateEven(apdu[0], apdu[1]) |
| 71 | d = filter_dict(u.to_dict()) |
| 72 | self.assertEqual(d, res) |
| 73 | |
| 74 | def test_vgcs(self): |
| 75 | apdu = ('80880082' + '0E' + '04' + '00010203' + |
| 76 | '01' + '10' + |
| 77 | '08' + '2021222324252627', |
| 78 | 'DB' + '10' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + '9000') |
| 79 | res = { |
| 80 | 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'vgcs_vbs'}, |
| 81 | 'body': { 'vk_id': '10', 'vservice_id': '00010203', 'vstk_rand': '2021222324252627'}}, |
| 82 | 'rsp': {'body': {'vstk': 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf'}} |
| 83 | } |
| 84 | u = UsimAuthenticateEven(apdu[0], apdu[1]) |
| 85 | d = filter_dict(u.to_dict()) |
| 86 | self.assertEqual(d, res) |
| 87 | |
| 88 | |
| 89 | |
| 90 | if __name__ == "__main__": |
| 91 | unittest.main() |