Fix build error.
[openais.git] / test / testconfdb.c
blob64e194ab8d320aebba4431f6f427057991f32006
1 /*
2 * Copyright (c) 2008 Red Hat Inc
4 * All rights reserved.
6 * Author: Christine Caulfield <ccaulfie@redhat.com>
8 * This software licensed under BSD license, the text of which follows:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the MontaVista Software, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/un.h>
44 #include "saAis.h"
45 #include "confdb.h"
48 /* Callbacks are not supported yet */
49 confdb_callbacks_t callbacks = {
50 .confdb_change_notify_fn = NULL,
53 /* Recursively dump the object tree */
54 static void print_config_tree(confdb_handle_t handle, unsigned int parent_object_handle, int depth)
56 unsigned int object_handle;
57 char object_name[1024];
58 int object_name_len;
59 char key_name[1024];
60 int key_name_len;
61 char key_value[1024];
62 int key_value_len;
63 int res;
64 int i;
66 /* Show the keys */
67 res = confdb_key_iter_start(handle, parent_object_handle);
68 if (res != SA_AIS_OK) {
69 printf( "error resetting key iterator for object %d: %d\n", parent_object_handle, res);
70 return;
73 while ( (res = confdb_key_iter(handle, parent_object_handle, key_name, &key_name_len,
74 key_value, &key_value_len)) == SA_AIS_OK) {
75 key_name[key_name_len] = '\0';
76 key_value[key_value_len] = '\0';
77 for (i=0; i<depth; i++) printf(" ");
78 printf(" KEY %s=%s\n", key_name, key_value);
81 /* Show sub-objects */
82 res = confdb_object_iter_start(handle, parent_object_handle);
83 if (res != SA_AIS_OK) {
84 printf( "error resetting object iterator for object %d: %d\n", parent_object_handle, res);
85 return;
88 while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == SA_AIS_OK) {
89 unsigned int parent;
91 res = confdb_object_parent_get(handle, object_handle, &parent);
92 if (res != SA_AIS_OK) {
93 printf( "error getting parent for object %d: %d\n", object_handle, res);
94 return;
97 for (i=0; i<depth; i++) printf(" ");
99 object_name[object_name_len] = '\0';
100 printf("OBJECT: %s (%u, parent: %u)\n", object_name, object_handle, parent);
102 /* Down we go ... */
103 print_config_tree(handle, object_handle, depth+1);
107 static void do_write_tests(confdb_handle_t handle)
109 int res;
110 unsigned int object_handle;
112 /* Add a scratch object and put some keys into it */
113 res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, (void *)"testconfdb", strlen("testconfdb"), &object_handle);
114 if (res != SA_AIS_OK) {
115 printf( "error creating 'testconfdb' object: %d\n", res);
116 return;
119 res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "one", strlen("one"));
120 if (res != SA_AIS_OK) {
121 printf( "error creating 'testconfdb' key 1: %d\n", res);
122 return;
125 res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"));
126 if (res != SA_AIS_OK) {
127 printf( "error creating 'testconfdb' key 2: %d\n", res);
128 return;
131 res = confdb_key_create(handle, object_handle, "grot", strlen("grot"), "perrins", strlen("perrins"));
132 if (res != SA_AIS_OK) {
133 printf( "error creating 'testconfdb' key 3: %d\n", res);
134 return;
137 res = confdb_key_replace(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"),
138 "newtwo", strlen("newtwo"));
140 if (res != SA_AIS_OK) {
141 printf( "error replace 'testconfdb' key 2: %d\n", res);
142 return;
145 /* Print it for verification */
146 print_config_tree(handle, object_handle, 0);
147 printf("-------------------------\n");
149 /* Remove it.
150 Check that it doesn't exist when the full tree dump runs next */
151 res = confdb_object_destroy(handle, object_handle);
152 if (res != SA_AIS_OK) {
153 printf( "error destroying 'testconfdb' object: %d\n", res);
154 return;
159 int main (int argc, char *argv[]) {
160 confdb_handle_t handle;
161 int result;
162 unsigned int totem_handle;
163 char key_value[256];
164 int value_len;
166 result = confdb_initialize (&handle, &callbacks);
167 if (result != SA_AIS_OK) {
168 printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result);
169 exit (1);
172 if (argv[1] && strcmp(argv[1], "write")==0)
173 do_write_tests(handle);
175 /* Test iterators */
176 print_config_tree(handle, OBJECT_PARENT_HANDLE, 0);
178 /* Find "totem" and dump bits of it again, to test the direct APIs */
179 result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
180 if (result != SA_AIS_OK) {
181 printf ("Could not start object_find %d\n", result);
182 exit (1);
185 result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
186 if (result != SA_AIS_OK) {
187 printf ("Could not object_find \"totem\": %d\n", result);
188 exit (1);
191 result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
192 if (result != SA_AIS_OK) {
193 printf ("Could not get \"version\" key: %d\n", result);
194 exit (1);
196 key_value[value_len] = '\0';
197 printf("totem/version = '%s'\n", key_value);
199 result = confdb_key_get(handle, totem_handle, "secauth", strlen("secauth"), key_value, &value_len);
200 if (result != SA_AIS_OK) {
201 printf ("Could not get \"secauth\" key: %d\n", result);
202 exit (1);
204 key_value[value_len] = '\0';
205 printf("totem/secauth = '%s'\n", key_value);
207 /* Try a call that fails */
208 result = confdb_key_get(handle, totem_handle, "grot", strlen("grot"), key_value, &value_len);
209 printf ("Get \"grot\" key returned: %d (should fail)\n", result);
211 result = confdb_finalize (handle);
212 printf ("Finalize result is %d (should be 1)\n", result);
213 return (0);