xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/apitest.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* apitest.c -- OpenLDAP API Test Program */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/apitest.c,v 1.25.2.3 2008/02/11 23:26:41 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2008 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This program was orignally developed by Kurt D. Zeilenga for inclusion in
19  * OpenLDAP Software.
20  */
21 #include "portable.h"
22 
23 #include <ac/stdlib.h>
24 
25 #include <stdio.h>
26 
27 #include <ldap.h>
28 
29 int
30 main(int argc, char **argv)
31 {
32 	LDAPAPIInfo api;
33 	int ival;
34 	char *sval;
35 
36 	printf("Compile time API Information\n");
37 
38 #ifdef LDAP_API_INFO_VERSION
39 	api.ldapai_info_version = LDAP_API_INFO_VERSION;
40 	printf("  API Info version:  %d\n", (int) api.ldapai_info_version);
41 #else
42 	api.ldapai_info_version = 1;
43 	printf("  API Info version:  unknown\n");
44 #endif
45 
46 #ifdef LDAP_FEATURE_INFO_VERSION
47 	printf("  Feature Info version:  %d\n", (int) LDAP_FEATURE_INFO_VERSION);
48 #else
49 	printf("  Feature Info version:  unknown\n");
50 	api.ldapai_info_version = 1;
51 #endif
52 
53 #ifdef LDAP_API_VERSION
54 	printf("  API version:       %d\n", (int) LDAP_API_VERSION);
55 #else
56 	printf("  API version:       unknown\n");
57 #endif
58 
59 #ifdef LDAP_VERSION
60 	printf("  Protocol Version:  %d\n", (int) LDAP_VERSION);
61 #else
62 	printf("  Protocol Version:  unknown\n");
63 #endif
64 #ifdef LDAP_VERSION_MIN
65 	printf("  Protocol Min:      %d\n", (int) LDAP_VERSION_MIN);
66 #else
67 	printf("  Protocol Min:      unknown\n");
68 #endif
69 #ifdef LDAP_VERSION_MAX
70 	printf("  Protocol Max:      %d\n", (int) LDAP_VERSION_MAX);
71 #else
72 	printf("  Protocol Max:      unknown\n");
73 #endif
74 #ifdef LDAP_VENDOR_NAME
75 	printf("  Vendor Name:       %s\n", LDAP_VENDOR_NAME);
76 #else
77 	printf("  Vendor Name:       unknown\n");
78 #endif
79 #ifdef LDAP_VENDOR_VERSION
80 	printf("  Vendor Version:    %d\n", (int) LDAP_VENDOR_VERSION);
81 #else
82 	printf("  Vendor Version:    unknown\n");
83 #endif
84 
85 	if(ldap_get_option(NULL, LDAP_OPT_API_INFO, &api) != LDAP_SUCCESS) {
86 		fprintf(stderr, "%s: ldap_get_option(API_INFO) failed\n", argv[0]);
87 		return EXIT_FAILURE;
88 	}
89 
90 	printf("\nExecution time API Information\n");
91 	printf("  API Info version:  %d\n", api.ldapai_info_version);
92 
93 	if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
94 		printf(" API INFO version mismatch: got %d, expected %d\n",
95 			api.ldapai_info_version, LDAP_API_INFO_VERSION);
96 		return EXIT_FAILURE;
97 	}
98 
99 	printf("  API Version:       %d\n", api.ldapai_api_version);
100 	printf("  Protocol Max:      %d\n", api.ldapai_protocol_version);
101 
102 	if(api.ldapai_extensions == NULL) {
103 		printf("  Extensions:        none\n");
104 
105 	} else {
106 		int i;
107 		for(i=0; api.ldapai_extensions[i] != NULL; i++) /* empty */;
108 		printf("  Extensions:        %d\n", i);
109 		for(i=0; api.ldapai_extensions[i] != NULL; i++) {
110 #ifdef LDAP_OPT_API_FEATURE_INFO
111 			LDAPAPIFeatureInfo fi;
112 			fi.ldapaif_info_version = LDAP_FEATURE_INFO_VERSION;
113 			fi.ldapaif_name = api.ldapai_extensions[i];
114 			fi.ldapaif_version = 0;
115 
116 			if( ldap_get_option(NULL, LDAP_OPT_API_FEATURE_INFO, &fi) == LDAP_SUCCESS ) {
117 				if(fi.ldapaif_info_version != LDAP_FEATURE_INFO_VERSION) {
118 					printf("                     %s feature info mismatch: got %d, expected %d\n",
119 						api.ldapai_extensions[i],
120 						LDAP_FEATURE_INFO_VERSION,
121 						fi.ldapaif_info_version);
122 
123 				} else {
124 					printf("                     %s: version %d\n",
125 						fi.ldapaif_name,
126 						fi.ldapaif_version);
127 				}
128 
129 			} else {
130 				printf("                     %s (NO FEATURE INFO)\n",
131 					api.ldapai_extensions[i]);
132 			}
133 
134 #else
135 			printf("                     %s\n",
136 				api.ldapai_extensions[i]);
137 #endif
138 
139 			ldap_memfree(api.ldapai_extensions[i]);
140 		}
141 		ldap_memfree(api.ldapai_extensions);
142 	}
143 
144 	printf("  Vendor Name:       %s\n", api.ldapai_vendor_name);
145 	ldap_memfree(api.ldapai_vendor_name);
146 
147 	printf("  Vendor Version:    %d\n", api.ldapai_vendor_version);
148 
149 	printf("\nExecution time Default Options\n");
150 
151 	if(ldap_get_option(NULL, LDAP_OPT_DEREF, &ival) != LDAP_SUCCESS) {
152 		fprintf(stderr, "%s: ldap_get_option(api) failed\n", argv[0]);
153 		return EXIT_FAILURE;
154 	}
155 	printf("  DEREF:             %d\n", ival);
156 
157 	if(ldap_get_option(NULL, LDAP_OPT_SIZELIMIT, &ival) != LDAP_SUCCESS) {
158 		fprintf(stderr, "%s: ldap_get_option(sizelimit) failed\n", argv[0]);
159 		return EXIT_FAILURE;
160 	}
161 	printf("  SIZELIMIT:         %d\n", ival);
162 
163 	if(ldap_get_option(NULL, LDAP_OPT_TIMELIMIT, &ival) != LDAP_SUCCESS) {
164 		fprintf(stderr, "%s: ldap_get_option(timelimit) failed\n", argv[0]);
165 		return EXIT_FAILURE;
166 	}
167 	printf("  TIMELIMIT:         %d\n", ival);
168 
169 	if(ldap_get_option(NULL, LDAP_OPT_REFERRALS, &ival) != LDAP_SUCCESS) {
170 		fprintf(stderr, "%s: ldap_get_option(referrals) failed\n", argv[0]);
171 		return EXIT_FAILURE;
172 	}
173 	printf("  REFERRALS:         %s\n", ival ? "on" : "off");
174 
175 	if(ldap_get_option(NULL, LDAP_OPT_RESTART, &ival) != LDAP_SUCCESS) {
176 		fprintf(stderr, "%s: ldap_get_option(restart) failed\n", argv[0]);
177 		return EXIT_FAILURE;
178 	}
179 	printf("  RESTART:           %s\n", ival ? "on" : "off");
180 
181 	if(ldap_get_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ival) != LDAP_SUCCESS) {
182 		fprintf(stderr, "%s: ldap_get_option(protocol version) failed\n", argv[0]);
183 		return EXIT_FAILURE;
184 	}
185 	printf("  PROTOCOL VERSION:  %d\n", ival);
186 
187 	if(ldap_get_option(NULL, LDAP_OPT_HOST_NAME, &sval) != LDAP_SUCCESS) {
188 		fprintf(stderr, "%s: ldap_get_option(host name) failed\n", argv[0]);
189 		return EXIT_FAILURE;
190 	}
191 	if( sval != NULL ) {
192 		printf("  HOST NAME:         %s\n", sval);
193 		ldap_memfree(sval);
194 	} else {
195 		puts("  HOST NAME:         <not set>");
196 	}
197 
198 #if 0
199 	/* API tests */
200 	{	/* bindless unbind */
201 		LDAP *ld;
202 		int rc;
203 
204 		ld = ldap_init( "localhost", 389 );
205 		if( ld == NULL ) {
206 			perror("ldap_init");
207 			return EXIT_FAILURE;
208 		}
209 
210 		rc = ldap_unbind( ld );
211 		if( rc != LDAP_SUCCESS ) {
212 			perror("ldap_unbind");
213 			return EXIT_FAILURE;
214 		}
215 	}
216 	{	/* bindless unbind */
217 		LDAP *ld;
218 		int rc;
219 
220 		ld = ldap_init( "localhost", 389 );
221 		if( ld == NULL ) {
222 			perror("ldap_init");
223 			return EXIT_FAILURE;
224 		}
225 
226 		rc = ldap_abandon_ext( ld, 0, NULL, NULL );
227 		if( rc != LDAP_SERVER_DOWN ) {
228 			ldap_perror( ld, "ldap_abandon");
229 			return EXIT_FAILURE;
230 		}
231 
232 		rc = ldap_unbind( ld );
233 		if( rc != LDAP_SUCCESS ) {
234 			perror("ldap_unbind");
235 			return EXIT_FAILURE;
236 		}
237 	}
238 #endif
239 
240 	return EXIT_SUCCESS;
241 }
242