2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2017 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include "ac/stdlib.h"
28 #include "ac/socket.h"
29 #include "ac/string.h"
30 #include "ac/unistd.h"
36 #include "slapd-common.h"
39 get_add_entry( char *filename, LDAPMod ***mods );
42 do_addel( struct tester_conn_args *config,
43 char *dn, LDAPMod **attrs, int friendly );
46 usage( char *name, char opt )
49 fprintf( stderr, "%s: unable to handle option \'%c\'\n\n",
53 fprintf( stderr, "usage: %s " TESTER_COMMON_HELP
61 main( int argc, char **argv )
64 char *filename = NULL;
67 LDAPMod **attrs = NULL;
68 struct tester_conn_args *config;
70 config = tester_init( "slapd-addel", TESTER_ADDEL );
72 while ( ( i = getopt( argc, argv, TESTER_COMMON_OPTS "Ff:" ) ) != EOF )
80 /* ignored (!) by now */
83 case 'f': /* file with entry search request */
84 filename = strdup( optarg );
88 if ( tester_config_opt( config, i, optarg ) == LDAP_SUCCESS ) {
96 if ( filename == NULL )
99 entry = get_add_entry( filename, &attrs );
100 if (( entry == NULL ) || ( *entry == '\0' )) {
102 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
104 exit( EXIT_FAILURE );
108 if (( attrs == NULL ) || ( *attrs == '\0' )) {
110 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
112 exit( EXIT_FAILURE );
116 tester_config_finish( config );
118 for ( i = 0; i < config->outerloops; i++ ) {
119 do_addel( config, entry, attrs, friendly );
122 exit( EXIT_SUCCESS );
127 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
134 modop |= LDAP_MOD_BVALUES;
137 if ( pmods != NULL ) {
138 for ( ; pmods[ i ] != NULL; ++i ) {
139 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
140 pmods[ i ]->mod_op == modop ) {
146 if ( pmods == NULL || pmods[ i ] == NULL ) {
147 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
148 sizeof( LDAPMod * ))) == NULL ) {
149 tester_perror( "realloc", NULL );
150 exit( EXIT_FAILURE );
153 pmods[ i + 1 ] = NULL;
154 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
156 tester_perror( "calloc", NULL );
157 exit( EXIT_FAILURE );
159 pmods[ i ]->mod_op = modop;
160 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
161 tester_perror( "strdup", NULL );
162 exit( EXIT_FAILURE );
166 if ( value != NULL ) {
168 if ( pmods[ i ]->mod_bvalues != NULL ) {
169 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
173 if (( pmods[ i ]->mod_bvalues =
174 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
175 (j + 2) * sizeof( struct berval * ))) == NULL ) {
176 tester_perror( "ber_memrealloc", NULL );
177 exit( EXIT_FAILURE );
179 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
180 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
182 tester_perror( "ber_memalloc", NULL );
183 exit( EXIT_FAILURE );
185 pmods[ i ]->mod_bvalues[ j ] = bvp;
188 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
189 tester_perror( "malloc", NULL );
190 exit( EXIT_FAILURE );
192 AC_MEMCPY( bvp->bv_val, value, vlen );
193 bvp->bv_val[ vlen ] = '\0';
199 get_add_entry( char *filename, LDAPMod ***mods )
204 if ( (fp = fopen( filename, "r" )) != NULL ) {
207 if ( fgets( line, BUFSIZ, fp )) {
210 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
213 if ( !strncasecmp( nl, "dn: ", 4 ))
215 entry = strdup( nl );
219 while ( fgets( line, BUFSIZ, fp )) {
223 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
226 if ( *line == '\0' ) break;
227 if ( !( value = strchr( line, ':' ))) break;
230 while ( *value && isspace( (unsigned char) *value ))
233 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
245 struct tester_conn_args *config,
251 int i = 0, do_retry = config->retries;
252 int rc = LDAP_SUCCESS;
256 tester_init_ld( &ld, config, 0 );
259 if ( do_retry == config->retries ) {
260 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
261 (long) pid, config->loops, entry );
264 for ( ; i < config->loops; i++ ) {
267 rc = ldap_add_ext_s( ld, entry, attrs, NULL, NULL );
268 if ( rc != LDAP_SUCCESS ) {
269 tester_ldap_error( ld, "ldap_add_ext_s", NULL );
271 case LDAP_ALREADY_EXISTS:
272 /* NOTE: this likely means
274 * during the previous round... */
281 case LDAP_UNAVAILABLE:
282 if ( do_retry > 0 ) {
294 /* wait a second for the add to really complete */
295 /* This masks some race conditions though. */
299 /* now delete the entry again */
300 rc = ldap_delete_ext_s( ld, entry, NULL, NULL );
301 if ( rc != LDAP_SUCCESS ) {
302 tester_ldap_error( ld, "ldap_delete_ext_s", NULL );
304 case LDAP_NO_SUCH_OBJECT:
305 /* NOTE: this likely means
307 * during the previous round... */
314 case LDAP_UNAVAILABLE:
315 if ( do_retry > 0 ) {
328 fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );
330 ldap_unbind_ext( ld, NULL, NULL );