summaryrefslogtreecommitdiffstats
path: root/src/lib/kdb/t_stringattr.c
blob: a153f7c2d16344d1d1c606961f322139c3940d71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/kdb/t_strings.c - Test program for KDB string attribute functions */
/*
 * Copyright (C) 2011 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "k5-int.h"
#include <kdb.h>

/*
 * This program exercises the KDB entry APIs for string attributes.  It relies
 * on some implementation knowledge:
 *
 * - The APIs will work on a DB entry initialized to all zeros (because it has
 *   an empty tl_data list).
 *
 * - Attribute order in krb5_dbe_get_strings matches attribute insertion order.
 */

int
main()
{
    krb5_db_entry *ent;
    krb5_context context;
    krb5_string_attr *strings;
    char *val;
    int count;

    assert(krb5int_init_context_kdc(&context) == 0);

    /* Start with an empty entry. */
    ent = krb5_db_alloc(context, NULL, sizeof(*ent));
    if (ent == NULL) {
        fprintf(stderr, "Can't allocate memory for entry.\n");
        return 1;
    }
    memset(ent, 0, sizeof(*ent));

    /* Check that the entry has no strings to start. */
    assert(krb5_dbe_get_strings(context, ent, &strings, &count) == 0);
    assert(strings == NULL && count == 0);
    krb5_dbe_free_strings(context, strings, count);

    /* Check that we get a null value querying a specific attribute. */
    assert(krb5_dbe_get_string(context, ent, "foo", &val) == 0);
    assert(val == NULL);

    /* Set some attributes one at a time, including a deletion. */
    assert(krb5_dbe_set_string(context, ent, "eggs", "dozen") == 0);
    assert(krb5_dbe_set_string(context, ent, "price", "right") == 0);
    assert(krb5_dbe_set_string(context, ent, "eggs", NULL) == 0);
    assert(krb5_dbe_set_string(context, ent, "time", "flies") == 0);

    /* Query each attribute. */
    assert(krb5_dbe_get_string(context, ent, "price", &val) == 0);
    assert(strcmp(val, "right") == 0);
    krb5_dbe_free_string(context, val);
    assert(krb5_dbe_get_string(context, ent, "time", &val) == 0);
    assert(strcmp(val, "flies") == 0);
    krb5_dbe_free_string(context, val);
    assert(krb5_dbe_get_string(context, ent, "eggs", &val) == 0);
    assert(val == NULL);

    /* Query the list of attributes and verify it. */
    assert(krb5_dbe_get_strings(context, ent, &strings, &count) == 0);
    assert(count == 2);
    assert(strcmp(strings[0].key, "price") == 0);
    assert(strcmp(strings[0].value, "right") == 0);
    assert(strcmp(strings[1].key, "time") == 0);
    assert(strcmp(strings[1].value, "flies") == 0);
    krb5_dbe_free_strings(context, strings, count);

    krb5_db_free_principal(context, ent);
    krb5_free_context(context);
    return 0;
}