1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# Tests for the samba samdb api
#
# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from samba.tests import TestCaseInTempDir
from samba.samdb import SamDB
from ldb import LdbError, ERR_OPERATIONS_ERROR
import os
import errno
class SamDBApiTestCase(TestCaseInTempDir):
def setUp(self):
super(SamDBApiTestCase, self).setUp()
def tearDown(self):
self.rm_files("test.db", "existing.db", allow_missing=True)
super(SamDBApiTestCase, self).tearDown()
# Attempt to open and existing non tdb file as a tdb file.
# Don't create new db is set, the default
#
# Should fail to open
# And the existing file should be left intact.
#
def test_dont_create_db_existing_non_tdb_file(self):
existing_name = self.tempdir + "/existing.db"
existing = open(existing_name, "w")
existing.write("This is not a tdb file!!!!!!\n")
existing.close()
try:
SamDB(url="tdb://" + existing_name)
self.fail("Exception not thrown ")
except LdbError as e:
(err, _) = e.args
self.assertEqual(err, ERR_OPERATIONS_ERROR)
existing = open(existing_name, "r")
contents = existing.readline()
self.assertEqual("This is not a tdb file!!!!!!\n", contents)
# Attempt to open and existing non tdb file as a tdb file.
# Don't create new db is cleared
#
# Should open as a tdb file
# And the existing file should be over written
#
def test_create_db_existing_file_non_tdb_file(self):
existing_name = self.tempdir + "/existing.db"
existing = open(existing_name, "wb")
existing.write(b"This is not a tdb file!!!!!!")
existing.close()
SamDB(url="tdb://" + existing_name, flags=0)
existing = open(existing_name, "rb")
contents = existing.readline()
self.assertEqual(b"TDB file\n", contents)
#
# Attempt to open an existing tdb file as a tdb file.
# Don't create new db is set, the default
#
# Should open successfully
# And the existing file should be left intact.
#
def test_dont_create_db_existing_tdb_file(self):
existing_name = self.tempdir + "/existing.db"
initial = SamDB(url="tdb://" + existing_name, flags=0)
dn = "dn=,cn=test_dont_create_db_existing_tdb_file"
initial.add({
"dn": dn,
"cn": "test_dont_create_db_existing_tdb_file"
})
cn = initial.searchone("cn", dn)
self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
second = SamDB(url="tdb://" + existing_name)
cn = second.searchone("cn", dn)
self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
#
# Attempt to open an existing tdb file as a tdb file.
# Don't create new db is explicitly cleared
#
# Should open successfully
# And the existing file should be left intact.
#
def test_create_db_existing_file_tdb_file(self):
existing_name = self.tempdir + "/existing.db"
initial = SamDB(url="tdb://" + existing_name, flags=0)
dn = "dn=,cn=test_dont_create_db_existing_tdb_file"
initial.add({
"dn": dn,
"cn": "test_dont_create_db_existing_tdb_file"
})
cn = initial.searchone("cn", dn)
self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
second = SamDB(url="tdb://" + existing_name, flags=0)
cn = second.searchone("cn", dn)
self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
# Open a non existent TDB file.
# Don't create new db is set, the default
#
# Should fail
# and the database file should not be created
def test_dont_create_db_new_file(self):
try:
SamDB(url="tdb://" + self.tempdir + "/test.db")
self.fail("Exception not thrown ")
except LdbError as e1:
(err, _) = e1.args
self.assertEqual(err, ERR_OPERATIONS_ERROR)
try:
file = open(self.tempdir + "/test.db", "r")
self.fail("New database file created")
except IOError as e:
self.assertEqual(e.errno, errno.ENOENT)
# Open a SamDB with the don't create new DB flag cleared.
# The underlying database file does not exist.
#
# Should successful open the SamDB creating a new database file.
#
def test_create_db_new_file(self):
SamDB(url="tdb://" + self.tempdir + "/test.db", flags=0)
existing = open(self.tempdir + "/test.db", mode="rb")
contents = existing.readline()
self.assertEqual(b"TDB file\n", contents)
|