1 # Unix SMB/CIFS implementation.
2 # Copyright (C) David Mulder <dmulder@suse.com> 2019
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.
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.
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/>.
18 """Tests for cred option parsing.
24 from contextlib
import contextmanager
25 from samba
.getopt
import CredentialsOptions
, SambaOptions
30 password_opt
= '--password=super_secret_password'
31 clear_password_opt
= '--password '
34 def auth_fle_opt(auth_file_path
, long_opt
=True):
35 old_argv
= list(sys
.argv
)
38 sys
.argv
.append('--authentication-file=%s' % auth_file_path
)
41 sys
.argv
.append(auth_file_path
)
46 class CredentialsOptionsTests(samba
.tests
.TestCase
):
50 self
.old_proctitle
= setproctitle
.getproctitle()
52 # We must append two options to get the " " we look for in the
53 # test after the redacted password
54 sys
.argv
.extend([password_opt
, "--realm=samba.org"])
56 def test_clear_proctitle_password(self
):
57 parser
= optparse
.OptionParser()
59 # The password burning is on the SambaOptions __init__()
60 sambaopts
= SambaOptions(parser
)
61 parser
.add_option_group(sambaopts
)
62 credopts
= CredentialsOptions(parser
)
63 parser
.add_option_group(credopts
)
64 (opts
, args
) = parser
.parse_args()
65 self
.assertNotIn(password_opt
, setproctitle
.getproctitle())
66 self
.assertIn(clear_password_opt
, setproctitle
.getproctitle())
70 setproctitle
.setproctitle(self
.old_proctitle
)
73 class AuthenticationFileTests(samba
.tests
.TestCaseInTempDir
):
78 self
.parser
= optparse
.OptionParser()
79 self
.credopts
= CredentialsOptions(self
.parser
)
80 self
.parser
.add_option_group(self
.credopts
)
82 self
.auth_file_name
= os
.path
.join(self
.tempdir
, 'auth.txt')
84 self
.realm
= 'realm.example.com'
86 self
.password
= 'pass'
87 self
.username
= 'user'
89 auth_file_fd
= open(self
.auth_file_name
, 'x')
90 auth_file_fd
.write('realm=%s\n' % self
.realm
)
91 auth_file_fd
.write('domain=%s\n' % self
.domain
)
92 auth_file_fd
.write('username=%s\n' % self
.username
)
93 auth_file_fd
.write('password=%s\n' % self
.password
)
99 os
.unlink(self
.auth_file_name
)
101 def test_long_option_valid_path(self
):
102 with
auth_fle_opt(self
.auth_file_name
):
103 self
.parser
.parse_args()
104 credopts
= self
.credopts
105 creds
= credopts
.creds
107 self
.assertFalse(credopts
.ask_for_password
)
108 self
.assertFalse(credopts
.machine_pass
)
110 self
.assertEqual(self
.username
, creds
.get_username())
111 self
.assertEqual(self
.password
, creds
.get_password())
112 self
.assertEqual(self
.domain
.upper(), creds
.get_domain())
113 self
.assertEqual(self
.realm
.upper(), creds
.get_realm())
115 def test_long_option_invalid_path(self
):
116 with
auth_fle_opt(self
.auth_file_name
+ '.dontexist'):
117 self
.parser
.parse_args()
118 credopts
= self
.credopts
119 creds
= credopts
.creds
121 self
.assertTrue(credopts
.ask_for_password
)
122 self
.assertFalse(credopts
.machine_pass
)
124 self
.assertIsNone(creds
.get_username())
125 self
.assertIsNone(creds
.get_password())
126 self
.assertIsNone(creds
.get_domain())
127 self
.assertIsNone(creds
.get_realm())
129 def test_short_option_valid_path(self
):
130 with
auth_fle_opt(self
.auth_file_name
, long_opt
=False):
131 self
.parser
.parse_args()
132 credopts
= self
.credopts
133 creds
= credopts
.creds
135 self
.assertFalse(credopts
.ask_for_password
)
136 self
.assertFalse(credopts
.machine_pass
)
138 self
.assertEqual(self
.username
, creds
.get_username())
139 self
.assertEqual(self
.password
, creds
.get_password())
140 self
.assertEqual(self
.domain
.upper(), creds
.get_domain())
141 self
.assertEqual(self
.realm
.upper(), creds
.get_realm())
143 def test_short_option_invalid_path(self
):
144 with
auth_fle_opt(self
.auth_file_name
+ '.dontexist', long_opt
=False):
145 self
.parser
.parse_args()
146 credopts
= self
.credopts
147 creds
= credopts
.creds
149 self
.assertTrue(credopts
.ask_for_password
)
150 self
.assertFalse(credopts
.machine_pass
)
152 self
.assertIsNone(creds
.get_username())
153 self
.assertIsNone(creds
.get_password())
154 self
.assertIsNone(creds
.get_domain())
155 self
.assertIsNone(creds
.get_realm())