r26639: librpc: Pass iconv convenience on from RPC connection to NDR library, so...
[samba.git] / source4 / torture / ldap / uptodatevector.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    LDAP replUpToDateVector tests
4    
5    Copyright (C) Stefan Metzmacher 2007
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19    
20 */
21
22 #include "includes.h"
23 #include "libcli/ldap/ldap_client.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "ldb_wrap.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "lib/util/dlinklist.h"
30
31 #include "torture/torture.h"
32 #include "torture/ldap/proto.h"
33
34 #include "librpc/ndr/libndr.h"
35 #include "librpc/gen_ndr/ndr_drsblobs.h"
36
37 #include "param/param.h"
38
39 static bool test_check_uptodatevector(struct torture_context *torture,
40                                       struct ldb_context *ldb,
41                                       struct ldb_dn *partition_dn)
42 {
43         bool ok = true;
44         uint32_t i;
45         int ret;
46         enum ndr_err_code ndr_err;
47         struct ldb_result *r;
48         const struct ldb_val *utdv_val1;
49         struct replUpToDateVectorBlob utdv1;
50         static const char *attrs[] = {
51                 "uSNChanged",
52                 "replUpToDateVector",
53                 "description",
54                 NULL
55         };
56
57         torture_comment(torture, "Check replUpToDateVector on partition[%s]\n",
58                                  ldb_dn_get_linearized(partition_dn));
59
60         ret = ldb_search(ldb, partition_dn, LDB_SCOPE_BASE, 
61                          "(objectClass=*)", attrs, &r);
62         if (ret != LDB_SUCCESS) {
63                 return false;
64         } else if (r->count != 1) {
65                 talloc_free(r);
66                 return false;
67         }
68         talloc_steal(torture, r);
69
70         ZERO_STRUCT(utdv1);
71         utdv_val1 = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
72         if (utdv_val1) {
73                 ndr_err = ndr_pull_struct_blob_all(utdv_val1, torture, 
74                                                    lp_iconv_convenience(torture->lp_ctx), &utdv1,
75                                                    (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
76                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
77                         return false;
78                 }
79         }
80
81         for (i=0; i < 2; i++) {
82                 const struct ldb_val *utdv_val;
83                 struct replUpToDateVectorBlob utdv;
84                 struct ldb_message *msg;
85                 char *description;
86                 uint32_t j;
87                 bool no_match = false;
88
89                 /* make a 'modify' msg, and only for serverReference */
90                 msg = ldb_msg_new(torture);
91                 if (!msg) return false;
92                 msg->dn = partition_dn;
93
94                 description = talloc_asprintf(msg, "torture replUpToDateVector[%u]", i);
95                 if (!description) return false;
96
97                 ret = ldb_msg_add_string(msg, "description", description);
98                 if (ret != 0) return false;
99
100                 for (j=0;j<msg->num_elements;j++) {
101                         msg->elements[j].flags = LDB_FLAG_MOD_REPLACE;
102                 }
103
104                 ret = ldb_modify(ldb, msg);
105                 if (ret != LDB_SUCCESS) return false;
106
107                 ret = ldb_search(ldb, partition_dn, LDB_SCOPE_BASE, 
108                                  "(objectClass=*)", attrs, &r);
109                 if (ret != LDB_SUCCESS) {
110                         return false;
111                 } else if (r->count != 1) {
112                         talloc_free(r);
113                         return false;
114                 }
115                 talloc_steal(msg, r);
116
117                 ZERO_STRUCT(utdv);
118                 utdv_val = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
119                 if (utdv_val) {
120                         ndr_err = ndr_pull_struct_blob_all(utdv_val, torture, 
121                                                            lp_iconv_convenience(torture->lp_ctx), &utdv,
122                                                            (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
123                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
124                                 return false;
125                         }
126                 }
127
128                 if (!utdv_val1 && utdv_val) {
129                         no_match = true;
130                 } else if (utdv_val1 && !utdv_val) {
131                         no_match = true;
132                 } else if (utdv_val1->length != utdv_val->length) {
133                         no_match = true;
134                 } else if (utdv_val1->length && memcmp(utdv_val1->data, utdv_val->data, utdv_val->length) != 0) {
135                         no_match = true;
136                 }
137
138                 torture_comment(torture, "[%u]: uSNChanged[%llu] description[%s] replUpToDateVector[%s]\n", i,
139                                 (unsigned long long)samdb_result_uint64(r->msgs[0], "uSNChanged", 0),
140                                 samdb_result_string(r->msgs[0], "description", NULL),
141                                 (no_match ? "changed!: not ok" : "not changed: ok"));
142
143                 if (no_match) {
144                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, &utdv1);
145                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, &utdv);
146                         ok = false;
147                 }
148
149                 talloc_free(msg);
150         }
151
152         return ok;
153 }
154
155 bool torture_ldap_uptodatevector(struct torture_context *torture)
156 {
157         struct ldb_context *ldb;
158         bool ret = true;
159         const char *host = torture_setting_string(torture, "host", NULL);
160         char *url;
161
162         url = talloc_asprintf(torture, "ldap://%s/", host);
163         if (!url) goto failed;
164
165         ldb = ldb_wrap_connect(torture, torture->lp_ctx, url,
166                                NULL,
167                                cmdline_credentials,
168                                0, NULL);
169         if (!ldb) goto failed;
170
171         ret &= test_check_uptodatevector(torture, ldb, samdb_base_dn(ldb));
172         ret &= test_check_uptodatevector(torture, ldb, samdb_config_dn(ldb));
173         ret &= test_check_uptodatevector(torture, ldb, samdb_schema_dn(ldb));
174
175         return ret;
176 failed:
177         return false;
178 }