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