s4-gensec: Pass the auth context in during gensec test
[ddiss/samba.git] / source4 / scripting / python / samba / tests / gensec.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """Tests for GENSEC.
21
22 Note that this just tests the bindings work. It does not intend to test 
23 the functionality, that's already done in other tests.
24 """
25
26 from samba.credentials import Credentials
27 from samba import gensec, auth
28 import samba.tests
29
30 class GensecTests(samba.tests.TestCase):
31
32     def setUp(self):
33         super(GensecTests, self).setUp()
34         self.settings = {}
35         self.settings["lp_ctx"] = self.lp_ctx = samba.tests.env_loadparm()
36         self.settings["target_hostname"] = self.lp_ctx.get("netbios name")
37         """This is just for the API tests"""
38         self.gensec = gensec.Security.start_client(self.settings)
39
40     def test_start_mech_by_unknown_name(self):
41         self.assertRaises(RuntimeError, self.gensec.start_mech_by_name, "foo")
42
43     def test_start_mech_by_name(self):
44         self.gensec.start_mech_by_name("spnego")
45
46     def test_info_uninitialized(self):
47         self.assertRaises(RuntimeError, self.gensec.session_info)
48
49     def test_update(self):
50         """Test GENSEC by doing an exchange with ourselves using GSSAPI against a KDC"""
51
52         """Start up a client and server GENSEC instance to test things with"""
53
54         self.gensec_client = gensec.Security.start_client(self.settings)
55         self.gensec_client.set_credentials(self.get_credentials())
56         self.gensec_client.want_feature(gensec.FEATURE_SEAL)
57         self.gensec_client.start_mech_by_sasl_name("GSSAPI")
58
59         self.gensec_server = gensec.Security.start_server(settings=self.settings, 
60                                                           auth_context=auth.AuthContext(lp_ctx=self.lp_ctx))
61         creds = Credentials()
62         creds.guess(self.lp_ctx)
63         creds.set_machine_account(self.lp_ctx)
64         self.gensec_server.set_credentials(creds)
65
66         self.gensec_server.want_feature(gensec.FEATURE_SEAL)
67         self.gensec_server.start_mech_by_sasl_name("GSSAPI")
68
69         client_finished = False
70         server_finished = False
71         server_to_client = ""
72         
73         """Run the actual call loop"""
74         while client_finished == False and server_finished == False:
75             if not client_finished:
76                 print "running client gensec_update"
77                 (client_finished, client_to_server) = self.gensec_client.update(server_to_client)
78             if not server_finished:
79                 print "running server gensec_update"
80                 (server_finished, server_to_client) = self.gensec_server.update(client_to_server)
81         session_info = self.gensec_server.session_info()
82
83         test_string = "Hello Server"
84         test_wrapped = self.gensec_client.wrap(test_string)
85         test_unwrapped = self.gensec_server.unwrap(test_wrapped)
86         self.assertEqual(test_string, test_unwrapped)
87         test_string = "Hello Client"
88         test_wrapped = self.gensec_server.wrap(test_string)
89         test_unwrapped = self.gensec_client.unwrap(test_wrapped)
90         self.assertEqual(test_string, test_unwrapped)
91