CVE-2019-3870 tests: Extend smbd tests to check for umask being overwritten
[nivanova/samba-autobuild/.git] / python / samba / tests / smbd_base.py
1 # Unix SMB/CIFS implementation. Common code for smbd python bindings tests
2 # Copyright (C) Catalyst.Net Ltd 2019
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 from samba.tests import TestCaseInTempDir
18 import os
19
20 TEST_UMASK = 0o022
21
22 class SmbdBaseTests(TestCaseInTempDir):
23
24     def get_umask(self):
25         # we can only get the umask by setting it to something
26         curr_umask = os.umask(0)
27         # restore the old setting
28         os.umask(curr_umask)
29         return curr_umask
30
31     def setUp(self):
32         super(SmbdBaseTests, self).setUp()
33         self.orig_umask = self.get_umask()
34
35         # set an arbitrary umask - the underlying smbd code should override
36         # this, but it allows us to check if umask is left unset
37         os.umask(TEST_UMASK)
38
39     def tearDown(self):
40         # the current umask should be what we set it to earlier - if it's not,
41         # it indicates the code has changed it and not restored it
42         self.assertEqual(self.get_umask(), TEST_UMASK,
43                          "umask unexpectedly overridden by test")
44
45         # restore the original umask value (before we interferred with it)
46         os.umask(self.orig_umask)
47
48         super(SmbdBaseTests, self).tearDown()