1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/kdb/t_strings.c - Test program for KDB string attribute functions */
3 /*
4 * Copyright (C) 2011 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
11 *
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
25 */
26
27 #include "k5-int.h"
28 #include <kdb.h>
29
30 /*
31 * This program exercises the KDB entry APIs for string attributes. It relies
32 * on some implementation knowledge:
33 *
34 * - The APIs will work on a DB entry initialized to all zeros (because it has
35 * an empty tl_data list).
36 *
37 * - Attribute order in krb5_dbe_get_strings matches attribute insertion order.
38 */
39
40 int
main(void)41 main(void)
42 {
43 krb5_db_entry *ent;
44 krb5_context context;
45 krb5_string_attr *strings;
46 char *val;
47 int count;
48
49 assert(krb5int_init_context_kdc(&context) == 0);
50
51 /* Start with an empty entry. */
52 ent = calloc(1, sizeof(*ent));
53 if (ent == NULL) {
54 fprintf(stderr, "Can't allocate memory for entry.\n");
55 return 1;
56 }
57
58 /* Check that the entry has no strings to start. */
59 assert(krb5_dbe_get_strings(context, ent, &strings, &count) == 0);
60 assert(strings == NULL && count == 0);
61 krb5_dbe_free_strings(context, strings, count);
62
63 /* Check that we get a null value querying a specific attribute. */
64 assert(krb5_dbe_get_string(context, ent, "foo", &val) == 0);
65 assert(val == NULL);
66
67 /* Set some attributes one at a time, including a deletion. */
68 assert(krb5_dbe_set_string(context, ent, "eggs", "dozen") == 0);
69 assert(krb5_dbe_set_string(context, ent, "price", "right") == 0);
70 assert(krb5_dbe_set_string(context, ent, "eggs", NULL) == 0);
71 assert(krb5_dbe_set_string(context, ent, "time", "flies") == 0);
72
73 /* Query each attribute. */
74 assert(krb5_dbe_get_string(context, ent, "price", &val) == 0);
75 assert(strcmp(val, "right") == 0);
76 krb5_dbe_free_string(context, val);
77 assert(krb5_dbe_get_string(context, ent, "time", &val) == 0);
78 assert(strcmp(val, "flies") == 0);
79 krb5_dbe_free_string(context, val);
80 assert(krb5_dbe_get_string(context, ent, "eggs", &val) == 0);
81 assert(val == NULL);
82
83 /* Query the list of attributes and verify it. */
84 assert(krb5_dbe_get_strings(context, ent, &strings, &count) == 0);
85 assert(count == 2);
86 assert(strcmp(strings[0].key, "price") == 0);
87 assert(strcmp(strings[0].value, "right") == 0);
88 assert(strcmp(strings[1].key, "time") == 0);
89 assert(strcmp(strings[1].value, "flies") == 0);
90 krb5_dbe_free_strings(context, strings, count);
91
92 krb5_db_free_principal(context, ent);
93 krb5_free_context(context);
94 return 0;
95 }
96