python/samba/tests: PY2/PY3 port samba.tests.dcerpc.integer
[amitay/samba.git] / python / samba / tests / samdb_api.py
1 # Tests for the samba samdb api
2 #
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 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
19 from samba.tests import TestCaseInTempDir
20 from samba.samdb import SamDB
21 from ldb import LdbError, ERR_OPERATIONS_ERROR
22 import os
23 import errno
24
25
26 class SamDBApiTestCase(TestCaseInTempDir):
27
28     def setUp(self):
29         super(SamDBApiTestCase, self).setUp()
30
31     def tearDown(self):
32         try:
33             os.remove(self.tempdir + "/test.db")
34         except OSError as e:
35             self.assertEquals(e.errno, errno.ENOENT)
36
37         try:
38             os.remove(self.tempdir + "/existing.db")
39         except OSError as e:
40             self.assertEquals(e.errno, errno.ENOENT)
41
42         super(SamDBApiTestCase, self).tearDown()
43
44     # Attempt to open and existing non tdb file as a tdb file.
45     # Don't create new db is set, the default
46     #
47     # Should fail to open
48     # And the existing file should be left intact.
49     #
50     def test_dont_create_db_existing_non_tdb_file(self):
51         existing_name = self.tempdir + "/existing.db"
52         existing = open(existing_name, "w")
53         existing.write("This is not a tdb file!!!!!!\n")
54         existing.close()
55
56         try:
57             SamDB(url="tdb://" + existing_name)
58             self.fail("Exception not thrown ")
59         except LdbError as e:
60             (err, _) = e.args
61             self.assertEquals(err, ERR_OPERATIONS_ERROR)
62
63         existing = open(existing_name, "r")
64         contents = existing.readline()
65         self.assertEquals("This is not a tdb file!!!!!!\n", contents)
66
67     # Attempt to open and existing non tdb file as a tdb file.
68     # Don't create new db is cleared
69     #
70     # Should open as a tdb file
71     # And the existing file should be over written
72     #
73     def test_create_db_existing_file_non_tdb_file(self):
74         existing_name = self.tempdir + "/existing.db"
75         existing = open(existing_name, "wb")
76         existing.write(b"This is not a tdb file!!!!!!")
77         existing.close()
78
79         SamDB(url="tdb://" + existing_name, flags=0)
80
81         existing = open(existing_name, "rb")
82         contents = existing.readline()
83         self.assertEquals(b"TDB file\n", contents)
84
85     #
86     # Attempt to open an existing tdb file as a tdb file.
87     # Don't create new db is set, the default
88     #
89     # Should open successfully
90     # And the existing file should be left intact.
91     #
92     def test_dont_create_db_existing_tdb_file(self):
93         existing_name = self.tempdir + "/existing.db"
94         initial = SamDB(url="tdb://" + existing_name, flags=0)
95         dn = "dn=,cn=test_dont_create_db_existing_tdb_file"
96         initial.add({
97             "dn": dn,
98             "cn": "test_dont_create_db_existing_tdb_file"
99         })
100
101         cn = initial.searchone("cn", dn)
102         self.assertEquals(b"test_dont_create_db_existing_tdb_file", cn)
103
104         second = SamDB(url="tdb://" + existing_name)
105         cn = second.searchone("cn", dn)
106         self.assertEquals(b"test_dont_create_db_existing_tdb_file", cn)
107
108     #
109     # Attempt to open an existing tdb file as a tdb file.
110     # Don't create new db is explicitly cleared
111     #
112     # Should open successfully
113     # And the existing file should be left intact.
114     #
115     def test_create_db_existing_file_tdb_file(self):
116         existing_name = self.tempdir + "/existing.db"
117         initial = SamDB(url="tdb://" + existing_name, flags=0)
118         dn = "dn=,cn=test_dont_create_db_existing_tdb_file"
119         initial.add({
120             "dn": dn,
121             "cn": "test_dont_create_db_existing_tdb_file"
122         })
123
124         cn = initial.searchone("cn", dn)
125         self.assertEquals(b"test_dont_create_db_existing_tdb_file", cn)
126
127         second = SamDB(url="tdb://" + existing_name, flags=0)
128         cn = second.searchone("cn", dn)
129         self.assertEquals(b"test_dont_create_db_existing_tdb_file", cn)
130
131     # Open a non existent TDB file.
132     # Don't create new db is set, the default
133     #
134     # Should fail
135     # and the database file should not be created
136     def test_dont_create_db_new_file(self):
137         try:
138             SamDB(url="tdb://" + self.tempdir + "/test.db")
139             self.fail("Exception not thrown ")
140         except LdbError as e1:
141             (err, _) = e1.args
142             self.assertEquals(err, ERR_OPERATIONS_ERROR)
143
144         try:
145             file = open(self.tempdir + "/test.db", "r")
146             self.fail("New database file created")
147         except IOError as e:
148             self.assertEquals(e.errno, errno.ENOENT)
149
150     # Open a SamDB with the don't create new DB flag cleared.
151     # The underlying database file does not exist.
152     #
153     # Should successful open the SamDB creating a new database file.
154     #
155
156     def test_create_db_new_file(self):
157         SamDB(url="tdb://" + self.tempdir + "/test.db", flags=0)
158         existing = open(self.tempdir + "/test.db", mode="rb")
159         contents = existing.readline()
160         self.assertEquals(b"TDB file\n", contents)