1 # Tests for the samba samdb api
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
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.
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.
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/>.
19 from samba.tests import TestCaseInTempDir
20 from samba.samdb import SamDB
21 from ldb import LdbError, ERR_OPERATIONS_ERROR
26 class SamDBApiTestCase(TestCaseInTempDir):
29 super(SamDBApiTestCase, self).setUp()
33 os.remove(self.tempdir + "/test.db")
35 self.assertEquals(e.errno, errno.ENOENT)
38 os.remove(self.tempdir + "/existing.db")
40 self.assertEquals(e.errno, errno.ENOENT)
42 super(SamDBApiTestCase, self).tearDown()
44 # Attempt to open and existing non tdb file as a tdb file.
45 # Don't create new db is set, the default
48 # And the existing file should be left intact.
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")
57 SamDB(url="tdb://" + existing_name)
58 self.fail("Exception not thrown ")
61 self.assertEquals(err, ERR_OPERATIONS_ERROR)
63 existing = open(existing_name, "r")
64 contents = existing.readline()
65 self.assertEquals("This is not a tdb file!!!!!!\n", contents)
67 # Attempt to open and existing non tdb file as a tdb file.
68 # Don't create new db is cleared
70 # Should open as a tdb file
71 # And the existing file should be over written
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!!!!!!")
79 SamDB(url="tdb://" + existing_name, flags=0)
81 existing = open(existing_name, "rb")
82 contents = existing.readline()
83 self.assertEquals(b"TDB file\n", contents)
86 # Attempt to open an existing tdb file as a tdb file.
87 # Don't create new db is set, the default
89 # Should open successfully
90 # And the existing file should be left intact.
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"
98 "cn": "test_dont_create_db_existing_tdb_file"
101 cn = initial.searchone("cn", dn)
102 self.assertEquals(b"test_dont_create_db_existing_tdb_file", cn)
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)
109 # Attempt to open an existing tdb file as a tdb file.
110 # Don't create new db is explicitly cleared
112 # Should open successfully
113 # And the existing file should be left intact.
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"
121 "cn": "test_dont_create_db_existing_tdb_file"
124 cn = initial.searchone("cn", dn)
125 self.assertEquals(b"test_dont_create_db_existing_tdb_file", cn)
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)
131 # Open a non existent TDB file.
132 # Don't create new db is set, the default
135 # and the database file should not be created
136 def test_dont_create_db_new_file(self):
138 SamDB(url="tdb://" + self.tempdir + "/test.db")
139 self.fail("Exception not thrown ")
140 except LdbError as e1:
142 self.assertEquals(err, ERR_OPERATIONS_ERROR)
145 file = open(self.tempdir + "/test.db", "r")
146 self.fail("New database file created")
148 self.assertEquals(e.errno, errno.ENOENT)
150 # Open a SamDB with the don't create new DB flag cleared.
151 # The underlying database file does not exist.
153 # Should successful open the SamDB creating a new database file.
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)