blob: 9e7afb2f74e9a811924e32287cd0534237011e56 [file] [log] [blame]
Harald Weltef2bcb442024-01-05 23:43:56 +01001#!/usr/bin/env python3
2
3# (C) 2023-2024 by Harald Welte <laforge@osmocom.org>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18import unittest
19import logging
20import copy
21
22from pySim.utils import h2b, b2h
23from pySim.esim.saip import *
24from pySim.esim.saip.personalization import *
25from pprint import pprint as pp
26
27
28class SaipTest(unittest.TestCase):
Harald Welte8a39d002024-01-30 21:01:01 +010029 with open('smdpp-data/upp/TS48v2_SAIP2.3_NoBERTLV.der', 'rb') as f:
Harald Weltef2bcb442024-01-05 23:43:56 +010030 per_input = f.read()
31 pes = ProfileElementSequence.from_der(per_input)
32 expected_pet_list = ['header', 'mf', 'pukCodes', 'pinCodes', 'telecom', 'pinCodes', 'genericFileManagement', 'usim', 'opt-usim', 'pinCodes', 'akaParameter', 'gsm-access', 'df-5gs', 'df-saip','csim', 'opt-csim', 'pinCodes', 'cdmaParameter', 'isim', 'opt-isim', 'pinCodes', 'akaParameter', 'genericFileManagement', 'genericFileManagement', 'securityDomain', 'rfm', 'rfm', 'rfm', 'rfm', 'end']
33
34 def test_reencode_sequence(self):
35 """Test that we can decode and re-encode the entire DER encoded UPP."""
36 reencoded_der = self.pes.to_der()
37 self.assertEqual(reencoded_der, self.per_input)
38
39 def test_reencode_pe(self):
40 """Test that we can decode and re-encode reach individual ProfileElement."""
41 remainder = self.per_input
42 while len(remainder):
43 first_tlv, remainder = bertlv_first_segment(remainder)
44 pe = ProfileElement.from_der(first_tlv)
45 with self.subTest(pe.type):
46 reenc_tlv = pe.to_der()
47 self.assertEqual(reenc_tlv, first_tlv)
48
49
50 def test_sequence_helpers(self):
51 """Verify that the convenience helpers worked as expected."""
52 self.assertEqual([x.type for x in self.pes.pe_list], self.expected_pet_list)
53 self.assertEqual(len(self.pes.pes_by_naa), 4)
54
55 def test_personalization(self):
56 """Test some of the personalization operations."""
57 pes = copy.deepcopy(self.pes)
58 params = [Puk1(value=b'01234567'), Puk2(value=b'98765432'), Pin1(b'1111'), Pin2(b'2222'), Adm1(b'11111111'),
59 K(h2b('000102030405060708090a0b0c0d0e0f')), Opc(h2b('101112131415161718191a1b1c1d1e1f'))]
60 for p in params:
61 p.apply(pes)
62 # TODO: we don't actually test the results here, but we just verify there is no exception
63 pes.to_der()
64
65
66if __name__ == "__main__":
67 unittest.main()