torture: test FSCTL_SET_SPARSE
[Samba.git] / source4 / torture / ldap / ldap_sort.c
blobee5855540d35858cc5e7e9bc7e40dfb6ea0fe1e5
1 /*
2 Unix SMB/CIFS implementation.
4 Test LDB attribute functions
6 Copyright (C) Andrew Bartlet <abartlet@samba.org> 2008-2009
7 Copyright (C) Matthieu Patou <mat@matws.net> 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include <ldb.h>
26 #include <ldb_errors.h>
27 #include "ldb_wrap.h"
28 #include "param/param.h"
29 #include "lib/cmdline/popt_common.h"
30 #include "libcli/ldap/ldap_client.h"
31 #include "torture/smbtorture.h"
32 #include "torture/ldap/proto.h"
33 #include <ctype.h>
35 bool torture_ldap_sort(struct torture_context *torture)
37 struct ldb_context *ldb;
39 bool ret = false;
40 const char *host = torture_setting_string(torture, "host", NULL);
41 char *url;
42 int i;
43 codepoint_t j;
44 struct ldb_message_element *elem;
45 struct ldb_message *msg;
47 struct ldb_server_sort_control **control;
48 struct ldb_request *req;
49 struct ldb_result *ctx;
50 struct ldb_val *prev = NULL;
51 const char *prev_txt = NULL;
52 int prev_len = 0;
53 struct ldb_val *cur = NULL;
54 const char *cur_txt = NULL;
55 int cur_len = 0;
56 struct ldb_dn *dn;
59 /* TALLOC_CTX* ctx;*/
61 url = talloc_asprintf(torture, "ldap://%s/", host);
63 ldb = ldb_wrap_connect(torture, torture->ev, torture->lp_ctx, url,
64 NULL,
65 cmdline_credentials,
66 0);
67 torture_assert(torture, ldb, "Failed to make LDB connection to target");
69 ctx = talloc_zero(ldb, struct ldb_result);
71 control = talloc_array(ctx, struct ldb_server_sort_control *, 2);
72 control[0] = talloc(control, struct ldb_server_sort_control);
73 control[0]->attributeName = talloc_strdup(control, "cn");
74 control[0]->orderingRule = NULL;
75 control[0]->reverse = 0;
76 control[1] = NULL;
78 dn = ldb_get_default_basedn(ldb);
79 ldb_dn_add_child_fmt(dn, "cn=users");
80 ret = ldb_build_search_req(&req, ldb, ctx,
81 dn,
82 LDB_SCOPE_SUBTREE,
83 "(objectClass=*)", NULL,
84 NULL,
85 ctx, ldb_search_default_callback, NULL);
86 torture_assert(torture, ret == LDB_SUCCESS, "Failed to build search request");
88 ret = ldb_request_add_control(req, LDB_CONTROL_SERVER_SORT_OID, true, control);
89 torture_assert(torture, ret == LDB_SUCCESS, "Failed to add control to search request");
91 ret = ldb_request(ldb, req);
92 torture_assert(torture, ret == LDB_SUCCESS, ldb_errstring(ldb));
94 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
95 torture_assert(torture, ret == LDB_SUCCESS, ldb_errstring(ldb));
97 ret = true;
98 if (ctx->count > 1) {
99 for (i=0;i<ctx->count;i++) {
100 msg = ctx->msgs[i];
101 elem = ldb_msg_find_element(msg,"cn");
102 cur = elem->values;
103 torture_comment(torture, "cn: %s\n",cur->data);
104 if (prev != NULL)
106 /* Do only the ascii case right now ... */
107 cur_txt = (const char *) cur->data;
108 cur_len = cur->length;
109 prev_txt = (const char *) prev->data;
110 prev_len = prev->length;
111 /* Remove leading whitespace as the sort function do so ... */
112 while ( cur_txt[0] == cur_txt[1] ) { cur_txt++; cur_len--;}
113 while ( prev_txt[0] == prev_txt[1] ) { prev_txt++; prev_len--;}
114 while( *(cur_txt) && *(prev_txt) && cur_len && prev_len ) {
115 j = toupper_m(*(prev_txt))-toupper_m(*(cur_txt));
116 if ( j > 0 ) {
117 /* Just check that is not due to trailling white space in prev_txt
118 * That is to say *cur_txt = 0 and prev_txt = 20 */
119 /* Remove trailling whitespace */
120 while ( *prev_txt == ' ' ) { prev_txt++; prev_len--;}
121 while ( *cur_txt == ' ' ) { cur_txt++; cur_len--;}
122 /* Now that potential whitespace are removed if we are at the end
123 * of the cur_txt then it means that in fact strings were identical
125 torture_assert(torture, *cur_txt && *prev_txt, "Data wrongly sorted");
126 break;
128 else
130 if ( j == 0 )
132 if ( *(cur_txt) == ' ') {
133 while ( cur_txt[0] == cur_txt[1] ) { cur_txt++; cur_len--;}
134 while ( prev_txt[0] == prev_txt[1] ) { prev_txt++; prev_len--;}
136 cur_txt++;
137 prev_txt++;
138 prev_len--;
139 cur_len--;
141 else
143 break;
147 if ( ret != 1 ) {
148 break;
151 prev = cur;
156 return ret;