1# Unix SMB/CIFS implementation. Tests for samba.kcc core.
2# Copyright (C) Andrew Bartlett 2015
3#
4# Written by Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
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 samba.kcc"""
21
22import samba
23import os
24import time
25from tempfile import mkdtemp
26
27import samba.tests
28from samba import kcc
29from samba import ldb
30from samba.dcerpc import misc
31
32
33from samba.param import LoadParm
34from samba.credentials import Credentials
35from samba.samdb import SamDB
36
37unix_now = int(time.time())
38unix_once_upon_a_time = 1000000000  # 2001-09-09
39
40ENV_DSAS = {
41    'ad_dc_ntvfs': ['CN=LOCALDC,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba,DC=example,DC=com'],
42    'fl2000dc': ['CN=DC5,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba2000,DC=example,DC=com'],
43    'fl2003dc': ['CN=DC6,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba2003,DC=example,DC=com'],
44    'fl2008r2dc': ['CN=DC7,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba2008r2,DC=example,DC=com'],
45    'promoted_dc': ['CN=PROMOTEDVDC,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba,DC=example,DC=com',
46                    'CN=LOCALDC,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba,DC=example,DC=com'],
47    'vampire_dc': ['CN=LOCALDC,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba,DC=example,DC=com',
48                   'CN=LOCALVAMPIREDC,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=samba,DC=example,DC=com'],
49}
50
51
52class KCCTests(samba.tests.TestCase):
53    def setUp(self):
54        super(KCCTests, self).setUp()
55        self.lp = LoadParm()
56        self.creds = Credentials()
57        self.creds.guess(self.lp)
58        self.creds.set_username(os.environ["USERNAME"])
59        self.creds.set_password(os.environ["PASSWORD"])
60
61    def test_list_dsas(self):
62        my_kcc = kcc.KCC(unix_now, False, False, False, False)
63        my_kcc.load_samdb("ldap://%s" % os.environ["SERVER"],
64                          self.lp, self.creds)
65        try:
66            dsas = my_kcc.list_dsas()
67        except kcc.KCCError as e:
68            self.fail("kcc.list_dsas failed with %s" % e)
69        env = os.environ['TEST_ENV']
70        for expected_dsa in ENV_DSAS[env]:
71            self.assertIn(expected_dsa, dsas)
72
73    def test_verify(self):
74        """check that the KCC generates graphs that pass its own verify
75        option. This is not a spectacular achievement when there are
76        only a couple of nodes to connect, but it shows something.
77        """
78        my_kcc = kcc.KCC(unix_now, readonly=True, verify=True,
79                         debug=False, dot_file_dir=None)
80
81        # As this is flapping with errors under python3, we catch
82        # exceptions and turn them into failures..
83        try:
84            my_kcc.run("ldap://%s" % os.environ["SERVER"],
85                       self.lp, self.creds,
86                       attempt_live_connections=False)
87        except (samba.kcc.graph_utils.GraphError, kcc.KCCError):
88            import traceback
89            traceback.print_exc()
90            self.fail()
91