Path: blob/main/crypto/krb5/src/util/profile/test_vtable.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/profile/test_vtable.c - Test program for vtable-backed profiles */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/*27* This test program exercises vtable profile functionality using two vtables,28* one which implements just the basic methods and one which implements all of29* the methods. The program doesn't attempt to create a working profile30* implementation; it just verifies the expected control flow into the vtable31* and back out to the caller.32*/3334#include <k5-platform.h>35#include "profile.h"3637static int basic_cbdata;38static int full_cbdata;39static const char *empty_names[] = { NULL };40static const char *name_string = "get_string";41static const char *name_int = "get_int";42static const char *name_bool = "get_bool";4344static long45basic_get_values(void *cbdata, const char *const *names, char ***ret_values)46{47assert(cbdata == &basic_cbdata);48assert(names == empty_names);49*ret_values = calloc(3, sizeof(*ret_values));50(*ret_values)[0] = strdup("one");51(*ret_values)[1] = strdup("two");52(*ret_values)[2] = NULL;53return 0;54}5556static void57free_values(void *cbdata, char **values)58{59char **v;6061for (v = values; *v; v++)62free(*v);63free(values);64}6566static long67full_get_values(void *cbdata, const char *const *names, char ***ret_values)68{69assert(cbdata == &full_cbdata);70*ret_values = calloc(2, sizeof(*ret_values));71if (names[0] == name_string)72(*ret_values)[0] = strdup("string result");73else if (names[0] == name_int)74(*ret_values)[0] = strdup("23");75else if (names[0] == name_bool)76(*ret_values)[0] = strdup("on");77else {78free(*ret_values);79return PROF_NO_RELATION;80}81(*ret_values)[1] = NULL;82return 0;83}8485static void86full_cleanup(void *cbdata)87{88assert(cbdata == &full_cbdata);89}9091static long92full_copy(void *cbdata, void **ret_cbdata)93{94assert(cbdata == &full_cbdata);95*ret_cbdata = &full_cbdata;96return 0;97}9899struct iterator {100int count;101};102103static long104full_iterator_create(void *cbdata, const char *const *names, int flags,105void **ret_iter)106{107struct iterator *iter;108109assert(cbdata == &full_cbdata);110assert(names == empty_names);111assert(flags == 126);112iter = malloc(sizeof(*iter));113iter->count = 0;114*ret_iter = iter;115return 0;116}117118static long119full_iterator(void *cbdata, void *iter_arg, char **ret_name, char **ret_value)120{121struct iterator *iter = iter_arg;122123assert(cbdata == &full_cbdata);124assert(iter->count >= 0 && iter->count <= 2);125if (iter->count == 0) {126*ret_name = strdup("name1");127*ret_value = strdup("value1");128} else if (iter->count == 1) {129*ret_name = strdup("name2");130*ret_value = NULL;131} else {132*ret_name = NULL;133*ret_value = NULL;134}135iter->count++;136return 0;137}138139static void140full_iterator_free(void *cbdata, void *iter_arg)141{142struct iterator *iter = iter_arg;143144assert(cbdata == &full_cbdata);145assert(iter->count == 3);146free(iter);147}148149static void150full_free_string(void *cbdata, char *string)151{152assert(cbdata == &full_cbdata);153free(string);154}155156static long157full_writable(void *cbdata, int *writable)158{159assert(cbdata == &full_cbdata);160*writable = 12;161return 0;162}163164static long165full_modified(void *cbdata, int *modified)166{167assert(cbdata == &full_cbdata);168*modified = 6;169return 0;170}171172static long173full_update_relation(void *cbdata, const char **names,174const char *old_value, const char *new_value)175{176assert(cbdata == &full_cbdata);177assert(names == empty_names);178assert(old_value == name_string || old_value == NULL);179assert(new_value == NULL);180return 0;181}182183static long184full_rename_section(void *cbdata, const char **names, const char *new_name)185{186assert(cbdata == &full_cbdata);187assert(names == empty_names);188assert(new_name == name_int);189return 0;190}191192static long193full_add_relation(void *cbdata, const char **names, const char *new_value)194{195assert(cbdata == &full_cbdata);196assert(names == empty_names);197assert(new_value == name_bool);198return 0;199}200201static long202full_flush(void *cbdata)203{204assert(cbdata == &full_cbdata);205return 0;206}207208struct profile_vtable basic_vtable = {2091,210basic_get_values,211free_values,212};213214struct profile_vtable full_vtable = {2151,216full_get_values,217free_values,218full_cleanup,219full_copy,220221full_iterator_create,222full_iterator,223full_iterator_free,224full_free_string,225226full_writable,227full_modified,228full_update_relation,229full_rename_section,230full_add_relation,231full_flush232};233234int235main(void)236{237profile_t profile;238char **values, *str, *name, *value;239void *iter;240int intval;241242assert(profile_init_vtable(&basic_vtable, &basic_cbdata, &profile) == 0);243assert(profile_get_values(profile, empty_names, &values) == 0);244assert(strcmp(values[0], "one") == 0);245assert(strcmp(values[1], "two") == 0);246assert(values[2] == NULL);247profile_free_list(values);248assert(profile_iterator_create(profile, NULL, 0, &iter) ==249PROF_UNSUPPORTED);250assert(profile_is_writable(profile, &intval) == 0);251assert(intval == 0);252assert(profile_is_modified(profile, &intval) == 0);253assert(intval == 0);254assert(profile_update_relation(profile, NULL, NULL, NULL) ==255PROF_UNSUPPORTED);256assert(profile_clear_relation(profile, NULL) == PROF_UNSUPPORTED);257assert(profile_rename_section(profile, NULL, NULL) == PROF_UNSUPPORTED);258assert(profile_add_relation(profile, NULL, NULL) == PROF_UNSUPPORTED);259profile_flush(profile);260profile_abandon(profile);261262assert(profile_init_vtable(&full_vtable, &full_cbdata, &profile) == 0);263assert(profile_get_string(profile, name_string, NULL, NULL, "wrong",264&str) == 0);265assert(strcmp(str, "string result") == 0);266profile_release_string(str);267assert(profile_get_integer(profile, name_int, NULL, NULL, 24,268&intval) == 0);269assert(intval == 23);270assert(profile_get_boolean(profile, name_bool, NULL, NULL, 0,271&intval) == 0);272assert(intval == 1);273assert(profile_get_integer(profile, "xxx", NULL, NULL, 62, &intval) == 0);274assert(intval == 62);275276assert(profile_iterator_create(profile, empty_names, 126, &iter) == 0);277assert(profile_iterator(&iter, &name, &value) == 0);278assert(strcmp(name, "name1") == 0);279assert(strcmp(value, "value1") == 0);280profile_release_string(name);281profile_release_string(value);282assert(profile_iterator(&iter, &name, &value) == 0);283assert(strcmp(name, "name2") == 0);284assert(value == NULL);285profile_release_string(name);286assert(profile_iterator(&iter, &name, &value) == 0);287assert(iter == NULL);288assert(name == NULL);289assert(value == NULL);290291assert(profile_is_writable(profile, &intval) == 0);292assert(intval == 12);293assert(profile_is_modified(profile, &intval) == 0);294assert(intval == 6);295assert(profile_update_relation(profile, empty_names, name_string,296NULL) == 0);297assert(profile_clear_relation(profile, empty_names) == 0);298assert(profile_rename_section(profile, empty_names, name_int) == 0);299assert(profile_add_relation(profile, empty_names, name_bool) == 0);300profile_release(profile);301302return 0;303}304305306