python: Add samba.auth.session_info_fill_unix()
[samba.git] / python / samba / tests / auth.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 """Tests for the Auth Python bindings.
19
20 Note that this just tests the bindings work. It does not intend to test
21 the functionality, that's already done in other tests.
22 """
23
24 from samba import auth
25 import samba.tests
26
27 class AuthSystemSessionTests(samba.tests.TestCase):
28
29     def setUp(self):
30         super(AuthSystemSessionTests, self).setUp()
31         self.system_session = auth.system_session()
32         self.lp = samba.tests.env_loadparm()
33
34     def test_system_session_attrs(self):
35         self.assertTrue(hasattr(self.system_session, 'credentials'))
36         self.assertTrue(hasattr(self.system_session, 'info'))
37         self.assertTrue(hasattr(self.system_session, 'security_token'))
38         self.assertTrue(hasattr(self.system_session, 'session_key'))
39         self.assertTrue(hasattr(self.system_session, 'torture'))
40
41     def test_system_session_credentials(self):
42         self.assertIsNone(self.system_session.credentials.get_bind_dn())
43         self.assertIsNotNone(self.system_session.credentials.get_password())
44         self.assertEqual(self.system_session.credentials.get_username(),
45                          self.lp.get('netbios name').upper() + "$")
46
47     def test_system_session_info(self):
48         self.assertEqual(self.system_session.info.full_name, 'System')
49         self.assertEqual(self.system_session.info.domain_name, 'NT AUTHORITY')
50         self.assertEqual(self.system_session.info.account_name, 'SYSTEM')
51
52     def test_system_session_session_key(self):
53         expected = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
54         self.assertEqual(self.system_session.session_key, expected)
55
56     def test_system_session_security_token(self):
57         self.assertTrue(self.system_session.security_token.is_system())
58         self.assertFalse(self.system_session.security_token.is_anonymous())
59
60 class AuthAdminSessionTests(samba.tests.TestCase):
61
62     def setUp(self):
63         super(AuthAdminSessionTests, self).setUp()
64         self.lp = samba.tests.env_loadparm()
65         self.admin_session = auth.admin_session(self.lp,
66                                                 "S-1-5-21-2212615479-2695158682-2101375467")
67
68     def test_admin_session_attrs(self):
69         self.assertTrue(hasattr(self.admin_session, 'credentials'))
70         self.assertTrue(hasattr(self.admin_session, 'info'))
71         self.assertTrue(hasattr(self.admin_session, 'security_token'))
72         self.assertTrue(hasattr(self.admin_session, 'session_key'))
73         self.assertTrue(hasattr(self.admin_session, 'torture'))
74
75     def test_admin_session_credentials(self):
76         self.assertIsNone(self.admin_session.credentials)
77
78     def test_session_info_details(self):
79         self.assertEqual(self.admin_session.info.full_name,
80                          'Administrator')
81         self.assertEqual(self.admin_session.info.domain_name,
82                          self.lp.get('workgroup'))
83         self.assertEqual(self.admin_session.info.account_name,
84                          'Administrator')
85
86     def test_security_token(self):
87         self.assertFalse(self.admin_session.security_token.is_system())
88         self.assertFalse(self.admin_session.security_token.is_anonymous())
89         self.assertTrue(self.admin_session.security_token.has_builtin_administrators())
90
91     def test_session_info_unix_details(self):
92         samba.auth.session_info_fill_unix(session_info = self.admin_session,
93                                           lp_ctx=self.lp,
94                                           user_name="Administrator")
95         self.assertEqual(self.admin_session.unix_info.sanitized_username,
96                          'Administrator')
97         self.assertEqual(self.admin_session.unix_info.unix_name,
98                          self.lp.get('workgroup').upper() +
99                          self.lp.get('winbind separator') + 'Administrator')
100         self.assertIsNotNone(self.admin_session.unix_token)