PEP8: fix E302: expected 2 blank lines, found 1
[garming/samba-autobuild/.git] / python / samba / tests / smb.py
1 # -*- coding: utf-8 -*-
2 # Unix SMB/CIFS implementation. Tests for smb manipulation
3 # Copyright (C) David Mulder <dmulder@suse.com> 2018
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 import samba, os, random, sys
19 from samba import smb
20
21 PY3 = sys.version_info[0] == 3
22 addom = 'addom.samba.example.com/'
23 test_contents = 'abcd' * 256
24 utf_contents = u'Süßigkeiten Äpfel ' * 128
25 test_literal_bytes_embed_nulls = b'\xff\xfe\x14\x61\x00\x00\x62\x63\x64' * 256
26 binary_contents = b'\xff\xfe'
27 binary_contents = binary_contents + "Hello cruel world of python3".encode('utf8') * 128
28 test_dir = os.path.join(addom, 'testing_%d' % random.randint(0, 0xFFFF))
29 test_file = os.path.join(test_dir, 'testing').replace('/', '\\')
30
31
32 class SMBTests(samba.tests.TestCase):
33     def setUp(self):
34         super(SMBTests, self).setUp()
35         self.server = os.environ["SERVER"]
36         creds = self.insta_creds(template=self.get_credentials())
37         self.conn = smb.SMB(self.server,
38                             "sysvol",
39                             lp=self.get_loadparm(),
40                             creds=creds)
41         self.conn.mkdir(test_dir)
42
43     def tearDown(self):
44         super(SMBTests, self).tearDown()
45         try:
46             self.conn.deltree(test_dir)
47         except:
48             pass
49
50     def test_list(self):
51         ls = [f['name'] for f in self.conn.list(addom)]
52         self.assertIn('scripts', ls,
53                       msg='"scripts" directory not found in sysvol')
54         self.assertIn('Policies', ls,
55                       msg='"Policies" directory not found in sysvol')
56
57     def test_unlink(self):
58         """
59         The smb.unlink API should delete file
60         """
61         self.conn.savefile(test_file, binary_contents);
62         self.conn.unlink(test_file)
63         self.assertFalse(self.conn.chkpath(test_file))
64
65     def test_save_load_text(self):
66
67         self.conn.savefile(test_file, test_contents.encode('utf8'))
68
69         contents = self.conn.loadfile(test_file)
70         self.assertEquals(contents.decode('utf8'), test_contents,
71                           msg='contents of test file did not match what was written')
72
73     # with python2 this will save/load str type (with embedded nulls)
74     # with python3 this will save/load bytes type
75     def test_save_load_string_bytes(self):
76         self.conn.savefile(test_file, test_literal_bytes_embed_nulls)
77
78         contents = self.conn.loadfile(test_file)
79         self.assertEquals(contents, test_literal_bytes_embed_nulls,
80                           msg='contents of test file did not match what was written')
81
82     # python3 only this will save/load unicode
83     def test_save_load_utfcontents(self):
84         if PY3:
85             self.conn.savefile(test_file, utf_contents.encode('utf8'))
86
87             contents = self.conn.loadfile(test_file)
88             self.assertEquals(contents.decode('utf8'), utf_contents,
89                               msg='contents of test file did not match what was written')
90
91     # with python2 this will save/load str type
92     # with python3 this will save/load bytes type
93     def test_save_binary_contents(self):
94         self.conn.savefile(test_file, binary_contents);
95
96         contents = self.conn.loadfile(test_file)
97         self.assertEquals(contents, binary_contents,
98                           msg='contents of test file did not match what was written')