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