Finish removal of iconv_convenience in public API's.
[amitay/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 "dsdb/samdb/samdb.h"
27
28 #include "torture/torture.h"
29
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31
32 #include "param/param.h"
33
34 static bool test_check_uptodatevector(struct torture_context *torture,
35                                       struct ldb_context *ldb,
36                                       struct ldb_dn *partition_dn)
37 {
38         bool ok = true;
39         uint32_t i;
40         int ret;
41         enum ndr_err_code ndr_err;
42         struct ldb_result *r;
43         const struct ldb_val *utdv_val1;
44         struct replUpToDateVectorBlob utdv1;
45         static const char *attrs[] = {
46                 "uSNChanged",
47                 "replUpToDateVector",
48                 "description",
49                 NULL
50         };
51
52         torture_comment(torture, "Check replUpToDateVector on partition[%s]\n",
53                                  ldb_dn_get_linearized(partition_dn));
54
55         ret = ldb_search(ldb, torture, &r, partition_dn, LDB_SCOPE_BASE, attrs,
56                          "(objectClass=*)");
57         if (ret != LDB_SUCCESS) {
58                 return false;
59         } else if (r->count != 1) {
60                 talloc_free(r);
61                 return false;
62         }
63
64         ZERO_STRUCT(utdv1);
65         utdv_val1 = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
66         if (utdv_val1) {
67                 ndr_err = ndr_pull_struct_blob_all(utdv_val1, torture, 
68                                                    &utdv1,
69                                                    (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
70                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
71                         return false;
72                 }
73         }
74
75         for (i=0; i < 2; i++) {
76                 const struct ldb_val *utdv_val;
77                 struct replUpToDateVectorBlob utdv;
78                 struct ldb_message *msg;
79                 char *description;
80                 uint32_t j;
81                 bool no_match = false;
82
83                 /* make a 'modify' msg, and only for serverReference */
84                 msg = ldb_msg_new(torture);
85                 if (!msg) return false;
86                 msg->dn = partition_dn;
87
88                 description = talloc_asprintf(msg, "torture replUpToDateVector[%u]", i);
89                 if (!description) return false;
90
91                 ret = ldb_msg_add_string(msg, "description", description);
92                 if (ret != 0) return false;
93
94                 for (j=0;j<msg->num_elements;j++) {
95                         msg->elements[j].flags = LDB_FLAG_MOD_REPLACE;
96                 }
97
98                 ret = ldb_modify(ldb, msg);
99                 if (ret != LDB_SUCCESS) return false;
100
101                 ret = ldb_search(ldb, msg, &r, partition_dn, LDB_SCOPE_BASE,
102                                  attrs, "(objectClass=*)");
103                 if (ret != LDB_SUCCESS) {
104                         return false;
105                 } else if (r->count != 1) {
106                         talloc_free(r);
107                         return false;
108                 }
109
110                 ZERO_STRUCT(utdv);
111                 utdv_val = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
112                 if (utdv_val) {
113                         ndr_err = ndr_pull_struct_blob_all(utdv_val, torture, 
114                                                            &utdv,
115                                                            (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
116                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
117                                 return false;
118                         }
119                 }
120
121                 if (!utdv_val1 && utdv_val) {
122                         no_match = true;
123                 } else if (utdv_val1 && !utdv_val) {
124                         no_match = true;
125                 } else if (!utdv_val1 && !utdv_val) {
126                 } else if (utdv_val1->length != utdv_val->length) {
127                         no_match = true;
128                 } else if (utdv_val1->length && memcmp(utdv_val1->data, utdv_val->data, utdv_val->length) != 0) {
129                         no_match = true;
130                 }
131
132                 torture_comment(torture, "[%u]: uSNChanged[%llu] description[%s] replUpToDateVector[%s]\n", i,
133                                 (unsigned long long)samdb_result_uint64(r->msgs[0], "uSNChanged", 0),
134                                 samdb_result_string(r->msgs[0], "description", NULL),
135                                 (no_match ? "changed!: not ok" : "not changed: ok"));
136
137                 if (no_match) {
138                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, &utdv1);
139                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, &utdv);
140                         ok = false;
141                 }
142
143                 talloc_free(msg);
144         }
145
146         return ok;
147 }
148
149 bool torture_ldap_uptodatevector(struct torture_context *torture)
150 {
151         struct ldb_context *ldb;
152         bool ret = true;
153         const char *host = torture_setting_string(torture, "host", NULL);
154         char *url;
155
156         url = talloc_asprintf(torture, "ldap://%s/", host);
157         if (!url) goto failed;
158
159         ldb = ldb_wrap_connect(torture, torture->ev, torture->lp_ctx, url,
160                                NULL,
161                                cmdline_credentials,
162                                0);
163         if (!ldb) goto failed;
164
165         ret &= test_check_uptodatevector(torture, ldb, ldb_get_default_basedn(ldb));
166         ret &= test_check_uptodatevector(torture, ldb, ldb_get_config_basedn(ldb));
167         ret &= test_check_uptodatevector(torture, ldb, ldb_get_schema_basedn(ldb));
168
169         return ret;
170 failed:
171         return false;
172 }