tests:samba_tool: pass stdout and stderr to assertCmdSuccess()
[nivanova/samba-autobuild/.git] / python / samba / tests / samba_tool / user_check_password_script.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Sean Dague <sdague@linux.vnet.ibm.com> 2011
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2016
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 import os
20 import time
21 import ldb
22 from samba.tests.samba_tool.base import SambaToolCmdTest
23 from samba import (
24         nttime2unix,
25         dsdb
26         )
27
28 class UserCheckPwdTestCase(SambaToolCmdTest):
29     """Tests for samba-tool user subcommands"""
30     users = []
31     samdb = None
32
33     def setUp(self):
34         super(UserCheckPwdTestCase, self).setUp()
35         self.samdb = self.getSamDB("-H", "ldap://%s" % os.environ["DC_SERVER"],
36             "-U%s%%%s" % (os.environ["DC_USERNAME"], os.environ["DC_PASSWORD"]))
37         self.old_min_pwd_age = self.samdb.get_minPwdAge()
38         self.samdb.set_minPwdAge("0")
39
40     def tearDown(self):
41         super(UserCheckPwdTestCase, self).tearDown()
42         self.samdb.set_minPwdAge(self.old_min_pwd_age)
43
44     def test_checkpassword(self):
45         # Add
46         user = self._randomUser()
47         bad_password = os.environ["UNACCEPTABLE_PASSWORD"]
48         good_password = bad_password[:-1]
49
50         (result, out, err) = self.runsubcmd("user", "add", user["name"], bad_password,
51                                             "-H", "ldap://%s" % os.environ["DC_SERVER"],
52                                             "-U%s%%%s" % (os.environ["DC_USERNAME"], os.environ["DC_PASSWORD"]))
53         self.assertCmdFail(result, "Should fail adding a user with bad password.")
54         (result, out, err) = self.runsubcmd("user", "delete", user["name"],
55                                             "-H", "ldap://%s" % os.environ["DC_SERVER"],
56                                             "-U%s%%%s" % (os.environ["DC_USERNAME"], os.environ["DC_PASSWORD"]))
57         self.assertCmdSuccess(result, out, err, "Should delete user with bad password.")
58
59         (result, out, err) = self.runsubcmd("user", "add", user["name"], good_password,
60                                             "-H", "ldap://%s" % os.environ["DC_SERVER"],
61                                             "-U%s%%%s" % (os.environ["DC_USERNAME"], os.environ["DC_PASSWORD"]))
62         self.assertCmdSuccess(result, out, err, "Should succeed adding a user with good password.")
63
64         # Set password
65         (result, out, err) = self.runsubcmd("user", "setpassword", user["name"],
66                                             "--newpassword=%s" % bad_password,
67                                             "-H", "ldap://%s" % os.environ["DC_SERVER"],
68                                             "-U%s%%%s" % (os.environ["DC_USERNAME"], os.environ["DC_PASSWORD"]))
69         self.assertCmdFail(result, "Should fail setting a user's password to a bad one.")
70
71         (result, out, err) = self.runsubcmd("user", "setpassword", user["name"],
72                                             "--newpassword=%s" % good_password,
73                                             "-H", "ldap://%s" % os.environ["DC_SERVER"],
74                                             "-U%s%%%s" % (os.environ["DC_USERNAME"], os.environ["DC_PASSWORD"]))
75         self.assertCmdSuccess(result, out, err, "Should succeed setting a user's password to a good one.")
76
77         # Password=
78
79         (result, out, err) = self.runsubcmd("user", "password",
80                                             "--newpassword=%s" % bad_password,
81                                             "--ipaddress", os.environ["DC_SERVER_IP"],
82                                             "-U%s%%%s" % (user["name"], good_password))
83         self.assertCmdFail(result, "A user setting their own password to a bad one should fail.")
84
85         (result, out, err) = self.runsubcmd("user", "password",
86                                             "--newpassword=%s" % good_password + 'XYZ',
87                                             "--ipaddress", os.environ["DC_SERVER_IP"],
88                                             "-U%s%%%s" % (user["name"], good_password))
89         self.assertCmdSuccess(result, out, err, "A user setting their own password to a good one should succeed.")
90
91     def _randomUser(self, base={}):
92         """create a user with random attribute values, you can specify base attributes"""
93         user = {
94             "name": self.randomName(),
95         }
96         user.update(base)
97         return user