2 # Unix SMB/CIFS implementation.
3 # Copyright (C) Stefan Metzmacher 2020
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 3 of the License, or
8 # (at your option) any later version.
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.
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/>.
22 sys.path.insert(0, 'bin/python')
23 os.environ['PYTHONUNBUFFERED'] = '1'
25 from samba.dcerpc import krb5pac
26 from samba.ndr import ndr_unpack
27 from samba.tests import DynamicTestCase
28 from samba.tests.krb5.kdc_base_test import KDCBaseTest
30 global_asn1_print = False
31 global_hexdump = False
35 class PacAlignTests(KDCBaseTest):
40 def setUpDynamicTestCases(cls):
41 for length in range(len(cls.base_name), 21):
42 cls.generate_dynamic_test('test_pac_align',
48 self.do_asn1_print = global_asn1_print
49 self.do_hexdump = global_hexdump
51 def _test_pac_align_with_args(self, length):
52 samdb = self.get_samdb()
54 account_name = self.base_name + 'a' * (length - len(self.base_name))
55 creds, _ = self.create_account(samdb, account_name)
57 tgt = self.get_tgt(creds, expect_pac=True)
59 pac_data = self.get_ticket_pac(tgt)
60 self.assertIsNotNone(pac_data)
62 self.assertEqual(0, len(pac_data) & 7)
64 pac = ndr_unpack(krb5pac.PAC_DATA_RAW, pac_data)
65 for pac_buffer in pac.buffers:
66 buffer_type = pac_buffer.type
67 buffer_size = pac_buffer.ndr_size
69 with self.subTest(buffer_type=buffer_type):
70 if buffer_type == krb5pac.PAC_TYPE_LOGON_NAME:
71 self.assertEqual(length * 2 + 10, buffer_size)
72 elif buffer_type == krb5pac.PAC_TYPE_REQUESTER_SID:
73 self.assertEqual(28, buffer_size)
74 elif buffer_type in {krb5pac.PAC_TYPE_SRV_CHECKSUM,
75 krb5pac.PAC_TYPE_KDC_CHECKSUM,
76 krb5pac.PAC_TYPE_TICKET_CHECKSUM}:
77 self.assertEqual(0, buffer_size & 3,
78 f'buffer type was: {buffer_type}, '
79 f'buffer size was: {buffer_size}')
81 self.assertEqual(0, buffer_size & 7,
82 f'buffer type was: {buffer_type}, '
83 f'buffer size was: {buffer_size}')
85 rounded_len = (buffer_size + 7) & ~7
86 self.assertEqual(rounded_len, len(pac_buffer.info.remaining))
89 if __name__ == '__main__':
90 global_asn1_print = False
91 global_hexdump = False