Path: blob/main/crypto/krb5/src/lib/kdb/t_stringattr.c
39566 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/kdb/t_strings.c - Test program for KDB string attribute functions */2/*3* Copyright (C) 2011 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include "k5-int.h"27#include <kdb.h>2829/*30* This program exercises the KDB entry APIs for string attributes. It relies31* on some implementation knowledge:32*33* - The APIs will work on a DB entry initialized to all zeros (because it has34* an empty tl_data list).35*36* - Attribute order in krb5_dbe_get_strings matches attribute insertion order.37*/3839int40main(void)41{42krb5_db_entry *ent;43krb5_context context;44krb5_string_attr *strings;45char *val;46int count;4748assert(krb5int_init_context_kdc(&context) == 0);4950/* Start with an empty entry. */51ent = calloc(1, sizeof(*ent));52if (ent == NULL) {53fprintf(stderr, "Can't allocate memory for entry.\n");54return 1;55}5657/* Check that the entry has no strings to start. */58assert(krb5_dbe_get_strings(context, ent, &strings, &count) == 0);59assert(strings == NULL && count == 0);60krb5_dbe_free_strings(context, strings, count);6162/* Check that we get a null value querying a specific attribute. */63assert(krb5_dbe_get_string(context, ent, "foo", &val) == 0);64assert(val == NULL);6566/* Set some attributes one at a time, including a deletion. */67assert(krb5_dbe_set_string(context, ent, "eggs", "dozen") == 0);68assert(krb5_dbe_set_string(context, ent, "price", "right") == 0);69assert(krb5_dbe_set_string(context, ent, "eggs", NULL) == 0);70assert(krb5_dbe_set_string(context, ent, "time", "flies") == 0);7172/* Query each attribute. */73assert(krb5_dbe_get_string(context, ent, "price", &val) == 0);74assert(strcmp(val, "right") == 0);75krb5_dbe_free_string(context, val);76assert(krb5_dbe_get_string(context, ent, "time", &val) == 0);77assert(strcmp(val, "flies") == 0);78krb5_dbe_free_string(context, val);79assert(krb5_dbe_get_string(context, ent, "eggs", &val) == 0);80assert(val == NULL);8182/* Query the list of attributes and verify it. */83assert(krb5_dbe_get_strings(context, ent, &strings, &count) == 0);84assert(count == 2);85assert(strcmp(strings[0].key, "price") == 0);86assert(strcmp(strings[0].value, "right") == 0);87assert(strcmp(strings[1].key, "time") == 0);88assert(strcmp(strings[1].value, "flies") == 0);89krb5_dbe_free_strings(context, strings, count);9091krb5_db_free_principal(context, ent);92krb5_free_context(context);93return 0;94}959697