CVE-2022-37966 python:/tests/krb5: call sys.path.insert(0, "bin/python") before any...
[samba.git] / python / samba / tests / krb5 / pac_align_tests.py
1 #!/usr/bin/env python3
2 # Unix SMB/CIFS implementation.
3 # Copyright (C) Stefan Metzmacher 2020
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 3 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 #
18
19 import sys
20 import os
21
22 sys.path.insert(0, 'bin/python')
23 os.environ['PYTHONUNBUFFERED'] = '1'
24
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
29
30 global_asn1_print = False
31 global_hexdump = False
32
33
34 @DynamicTestCase
35 class PacAlignTests(KDCBaseTest):
36
37     base_name = 'krbpac'
38
39     @classmethod
40     def setUpDynamicTestCases(cls):
41         for length in range(len(cls.base_name), 21):
42             cls.generate_dynamic_test('test_pac_align',
43                                       f'{length}_chars',
44                                       length)
45
46     def setUp(self):
47         super().setUp()
48         self.do_asn1_print = global_asn1_print
49         self.do_hexdump = global_hexdump
50
51     def _test_pac_align_with_args(self, length):
52         samdb = self.get_samdb()
53
54         account_name = self.base_name + 'a' * (length - len(self.base_name))
55         creds, _ = self.create_account(samdb, account_name)
56
57         tgt = self.get_tgt(creds, expect_pac=True)
58
59         pac_data = self.get_ticket_pac(tgt)
60         self.assertIsNotNone(pac_data)
61
62         self.assertEqual(0, len(pac_data) & 7)
63
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
68
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}')
80                 else:
81                     self.assertEqual(0, buffer_size & 7,
82                                      f'buffer type was: {buffer_type}, '
83                                      f'buffer size was: {buffer_size}')
84
85                 rounded_len = (buffer_size + 7) & ~7
86                 self.assertEqual(rounded_len, len(pac_buffer.info.remaining))
87
88
89 if __name__ == '__main__':
90     global_asn1_print = False
91     global_hexdump = False
92     import unittest
93     unittest.main()