python tests Blackbox: add random_password
[samba.git] / python / samba / tests / samba_tool / provision_password_check.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst IT Ltd. 2017
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 from samba.tests.samba_tool.base import SambaToolCmdTest
19 import os
20 import shutil
21
22
23 class ProvisionPasswordTestCase(SambaToolCmdTest):
24     """Test for password validation in domain provision subcommand"""
25
26     def setUp(self):
27         super(SambaToolCmdTest, self).setUp()
28         self.tempsambadir = os.path.join(self.tempdir, "samba")
29         os.mkdir(self.tempsambadir)
30
31     def _provision_with_password(self, password):
32         return self.runsubcmd(
33             "domain", "provision", "--realm=foo.example.com", "--domain=FOO",
34             "--targetdir=%s" % self.tempsambadir, "--adminpass=%s" % password,
35             "--use-ntvfs")
36
37     def test_short_and_low_quality(self):
38         (result, out, err) = self._provision_with_password("foo")
39         self.assertCmdFail(result)
40
41     def test_short(self):
42         (result, out, err) = self._provision_with_password("Fo0!_9")
43         self.assertCmdFail(result)
44         self.assertRegexpMatches(err, r"minimum password length")
45
46     def test_low_quality(self):
47         (result, out, err) = self._provision_with_password("aaaaaaaaaaaaaaaaa")
48         self.assertCmdFail(result)
49         self.assertRegexpMatches(err, r"quality standards")
50
51     def test_good(self):
52         (result, out, err) = self._provision_with_password("Fo0!_9.")
53         self.assertCmdSuccess(result, out, err)
54
55     def tearDown(self):
56         super(SambaToolCmdTest, self).tearDown()
57         shutil.rmtree(self.tempsambadir)