s4:python tests __init__.py - do not depend on "subprocess.check_call()"
[samba.git] / source4 / scripting / python / samba / tests / dsdb.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation. Tests for dsdb
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2010
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """Tests for samba.dsdb."""
21
22 from samba.credentials import Credentials
23 from samba.samdb import SamDB
24 from samba.auth import system_session
25 from samba.tests import TestCase
26 from samba.ndr import ndr_unpack, ndr_pack
27 from samba.dcerpc import drsblobs
28 import ldb
29 import os
30 import samba
31
32
33 class DsdbTests(TestCase):
34
35     def setUp(self):
36         super(DsdbTests, self).setUp()
37         self.lp = samba.param.LoadParm()
38         self.lp.load(os.path.join(os.path.join(self.baseprovpath(), "etc"), "smb.conf"))
39         self.creds = Credentials()
40         self.creds.guess(self.lp)
41         self.session = system_session()
42         self.samdb = SamDB(os.path.join(self.baseprovpath(), "private", "sam.ldb"),
43             session_info=self.session, credentials=self.creds,lp=self.lp)
44
45     def baseprovpath(self):
46         return os.path.join(os.environ['SELFTEST_PREFIX'], "dc")
47
48     def test_get_oid_from_attrid(self):
49         oid = self.samdb.get_oid_from_attid(591614)
50         self.assertEquals(oid, "1.2.840.113556.1.4.1790")
51
52     def test_error_replpropertymetadata(self):
53         res = self.samdb.search(expression="cn=Administrator",
54                             scope=ldb.SCOPE_SUBTREE,
55                             attrs=["replPropertyMetaData"])
56         repl = ndr_unpack(drsblobs.replPropertyMetaDataBlob,
57                             str(res[0]["replPropertyMetaData"]))
58         ctr = repl.ctr
59         for o in ctr.array:
60             # Search for Description
61             if o.attid == 13:
62                 old_version = o.version
63                 o.version = o.version + 1
64         replBlob = ndr_pack(repl)
65         msg = ldb.Message()
66         msg.dn = res[0].dn
67         msg["replPropertyMetaData"] = ldb.MessageElement(replBlob, ldb.FLAG_MOD_REPLACE, "replPropertyMetaData")
68         self.assertRaises(ldb.LdbError, self.samdb.modify, msg, ["local_oid:1.3.6.1.4.1.7165.4.3.14:0"])
69
70     def test_twoatt_replpropertymetadata(self):
71         res = self.samdb.search(expression="cn=Administrator",
72                             scope=ldb.SCOPE_SUBTREE,
73                             attrs=["replPropertyMetaData", "uSNChanged"])
74         repl = ndr_unpack(drsblobs.replPropertyMetaDataBlob,
75                             str(res[0]["replPropertyMetaData"]))
76         ctr = repl.ctr
77         for o in ctr.array:
78             # Search for Description
79             if o.attid == 13:
80                 old_version = o.version
81                 o.version = o.version + 1
82                 o.local_usn = long(str(res[0]["uSNChanged"])) + 1
83         replBlob = ndr_pack(repl)
84         msg = ldb.Message()
85         msg.dn = res[0].dn
86         msg["replPropertyMetaData"] = ldb.MessageElement(replBlob, ldb.FLAG_MOD_REPLACE, "replPropertyMetaData")
87         msg["description"] = ldb.MessageElement("new val", ldb.FLAG_MOD_REPLACE, "description")
88         self.assertRaises(ldb.LdbError, self.samdb.modify, msg, ["local_oid:1.3.6.1.4.1.7165.4.3.14:0"])
89
90     def test_set_replpropertymetadata(self):
91         res = self.samdb.search(expression="cn=Administrator",
92                             scope=ldb.SCOPE_SUBTREE,
93                             attrs=["replPropertyMetaData", "uSNChanged"])
94         repl = ndr_unpack(drsblobs.replPropertyMetaDataBlob,
95                             str(res[0]["replPropertyMetaData"]))
96         ctr = repl.ctr
97         for o in ctr.array:
98             # Search for Description
99             if o.attid == 13:
100                 old_version = o.version
101                 o.version = o.version + 1
102                 o.local_usn = long(str(res[0]["uSNChanged"])) + 1
103                 o.originating_usn = long(str(res[0]["uSNChanged"])) + 1
104         replBlob = ndr_pack(repl)
105         msg = ldb.Message()
106         msg.dn = res[0].dn
107         msg["replPropertyMetaData"] = ldb.MessageElement(replBlob, ldb.FLAG_MOD_REPLACE, "replPropertyMetaData")
108         self.samdb.modify(msg, ["local_oid:1.3.6.1.4.1.7165.4.3.14:0"])
109
110     def test_ok_get_attribute_from_attid(self):
111         self.assertEquals(self.samdb.get_attribute_from_attid(13), "description")
112
113     def test_ko_get_attribute_from_attid(self):
114         self.assertEquals(self.samdb.get_attribute_from_attid(11979), None)
115
116     def test_get_attribute_replmetadata_version(self):
117         res = self.samdb.search(expression="cn=Administrator",
118                             scope=ldb.SCOPE_SUBTREE,
119                             attrs=["dn"])
120         self.assertEquals(len(res), 1)
121         dn = str(res[0].dn)
122         self.assertEqual(self.samdb.get_attribute_replmetadata_version(dn, "unicodePwd"), 1)
123
124     def test_set_attribute_replmetadata_version(self):
125         res = self.samdb.search(expression="cn=Administrator",
126                             scope=ldb.SCOPE_SUBTREE,
127                             attrs=["dn"])
128         self.assertEquals(len(res), 1)
129         dn = str(res[0].dn)
130         version = self.samdb.get_attribute_replmetadata_version(dn, "description")
131         self.samdb.set_attribute_replmetadata_version(dn, "description", version + 2)
132         self.assertEqual(self.samdb.get_attribute_replmetadata_version(dn, "description"), version + 2)