9d296dddf1c192bff0b04d733c4b7e7c9bc5a784
[sfrench/samba-autobuild/.git] / python / samba / tests / password_hash_fl2008.py
1 # Tests for Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
2 #
3 # Copyright (C) Catalyst IT Ltd. 2017
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 """
20 Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
21
22 These tests need to be run in an environment in which
23 io->ac->gpg_key_ids == NULL, so that the gpg supplemental credentials
24 are not generated. And also need to be in an environment with a
25 functional level of 2008 or greater to ensure the kerberos newer keys are
26 generated
27 """
28 from samba.tests.password_hash import (
29     PassWordHashTests,
30     get_package,
31     USER_PASS
32 )
33 from samba.ndr import ndr_unpack
34 from samba.dcerpc import drsblobs
35 import binascii
36
37 class PassWordHashFl2008Tests(PassWordHashTests):
38
39     def setUp(self):
40         super(PassWordHashFl2008Tests, self).setUp()
41
42
43     def test_default_supplementalCredentials(self):
44         self.add_user()
45
46         sc = self.get_supplemental_creds()
47
48         # Check that we got all the expected supplemental credentials
49         # And they are in the expected order.
50         size = len(sc.sub.packages)
51         self.assertEquals(4, size)
52         (pos, package) = get_package(sc, "Primary:Kerberos-Newer-Keys")
53         self.assertEquals(1, pos)
54         self.assertEquals("Primary:Kerberos-Newer-Keys", package.name)
55
56         (pos, package) = get_package(sc, "Primary:Kerberos")
57         self.assertEquals(2, pos)
58         self.assertEquals("Primary:Kerberos", package.name)
59
60         (pos, package) = get_package(sc, "Packages")
61         self.assertEquals(3, pos)
62         self.assertEquals("Packages", package.name)
63
64         (pos, package) = get_package(sc, "Primary:WDigest")
65         self.assertEquals(4, pos)
66         self.assertEquals("Primary:WDigest", package.name)
67
68         # Check that the WDigest values are correct.
69         #
70         digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
71                              binascii.a2b_hex(package.data))
72         self.check_wdigests(digests)
73
74     def test_supplementalCredentials_cleartext(self):
75         self.add_user(clear_text=True)
76
77         sc = self.get_supplemental_creds()
78
79         # Check that we got all the expected supplemental credentials
80         # And they are in the expected order.
81         size = len(sc.sub.packages)
82         self.assertEquals(5, size)
83         (pos, package) = get_package(sc, "Primary:Kerberos-Newer-Keys")
84         self.assertEquals(1, pos)
85         self.assertEquals("Primary:Kerberos-Newer-Keys", package.name)
86
87         (pos, package) = get_package(sc, "Primary:Kerberos")
88         self.assertEquals(2, pos)
89         self.assertEquals("Primary:Kerberos", package.name)
90
91         (pos, wd_package) = get_package(sc, "Primary:WDigest")
92         self.assertEquals(3, pos)
93         self.assertEquals("Primary:WDigest", wd_package.name)
94
95         (pos, package) = get_package(sc, "Packages")
96         self.assertEquals(4, pos)
97         self.assertEquals("Packages", package.name)
98
99         (pos, ct_package) = get_package(sc, "Primary:CLEARTEXT")
100         self.assertEquals(5, pos)
101         self.assertEquals("Primary:CLEARTEXT", ct_package.name)
102
103         # Check that the WDigest values are correct.
104         #
105         digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
106                              binascii.a2b_hex(wd_package.data))
107         self.check_wdigests(digests)
108
109         # Check the clear text  value is correct.
110         ct = ndr_unpack(drsblobs.package_PrimaryCLEARTEXTBlob,
111                         binascii.a2b_hex(ct_package.data))
112         self.assertEquals(USER_PASS.encode('utf-16-le'), ct.cleartext)