1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* util/profile/proftest/test.c - Test dynamic profile module */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2011 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert * require a specific license from the United States Government.
9*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
24*7f2fe78bSCy Schubert * or implied warranty.
25*7f2fe78bSCy Schubert */
26*7f2fe78bSCy Schubert
27*7f2fe78bSCy Schubert /*
28*7f2fe78bSCy Schubert * This file implements a very simple profile module which just returns the
29*7f2fe78bSCy Schubert * residual string and the number of copies in response to any query. The full
30*7f2fe78bSCy Schubert * range of vtable profile operations is tested elsewhere.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert #include "k5-platform.h"
34*7f2fe78bSCy Schubert #include "profile.h"
35*7f2fe78bSCy Schubert
36*7f2fe78bSCy Schubert struct data {
37*7f2fe78bSCy Schubert char *residual;
38*7f2fe78bSCy Schubert int gen;
39*7f2fe78bSCy Schubert };
40*7f2fe78bSCy Schubert
41*7f2fe78bSCy Schubert static long
get_values(void * cbdata,const char * const * names,char *** ret_values)42*7f2fe78bSCy Schubert get_values(void *cbdata, const char *const *names, char ***ret_values)
43*7f2fe78bSCy Schubert {
44*7f2fe78bSCy Schubert struct data *d = cbdata;
45*7f2fe78bSCy Schubert
46*7f2fe78bSCy Schubert *ret_values = calloc(3, sizeof(*ret_values));
47*7f2fe78bSCy Schubert (*ret_values)[0] = strdup(d->residual);
48*7f2fe78bSCy Schubert asprintf(&(*ret_values)[1], "%d", d->gen);
49*7f2fe78bSCy Schubert (*ret_values)[2] = NULL;
50*7f2fe78bSCy Schubert return 0;
51*7f2fe78bSCy Schubert }
52*7f2fe78bSCy Schubert
53*7f2fe78bSCy Schubert static void
free_values(void * cbdata,char ** values)54*7f2fe78bSCy Schubert free_values(void *cbdata, char **values)
55*7f2fe78bSCy Schubert {
56*7f2fe78bSCy Schubert char **v;
57*7f2fe78bSCy Schubert
58*7f2fe78bSCy Schubert for (v = values; *v; v++)
59*7f2fe78bSCy Schubert free(*v);
60*7f2fe78bSCy Schubert free(values);
61*7f2fe78bSCy Schubert }
62*7f2fe78bSCy Schubert
63*7f2fe78bSCy Schubert static void
cleanup(void * cbdata)64*7f2fe78bSCy Schubert cleanup(void *cbdata)
65*7f2fe78bSCy Schubert {
66*7f2fe78bSCy Schubert struct data *d = cbdata;
67*7f2fe78bSCy Schubert
68*7f2fe78bSCy Schubert free(d->residual);
69*7f2fe78bSCy Schubert free(d);
70*7f2fe78bSCy Schubert }
71*7f2fe78bSCy Schubert
72*7f2fe78bSCy Schubert static long
copy(void * cbdata,void ** ret_cbdata)73*7f2fe78bSCy Schubert copy(void *cbdata, void **ret_cbdata)
74*7f2fe78bSCy Schubert {
75*7f2fe78bSCy Schubert struct data *old_data = cbdata, *new_data;
76*7f2fe78bSCy Schubert
77*7f2fe78bSCy Schubert new_data = malloc(sizeof(*new_data));
78*7f2fe78bSCy Schubert new_data->residual = strdup(old_data->residual);
79*7f2fe78bSCy Schubert new_data->gen = old_data->gen + 1;
80*7f2fe78bSCy Schubert *ret_cbdata = new_data;
81*7f2fe78bSCy Schubert return 0;
82*7f2fe78bSCy Schubert }
83*7f2fe78bSCy Schubert
84*7f2fe78bSCy Schubert long
85*7f2fe78bSCy Schubert profile_module_init(const char *residual, struct profile_vtable *vtable,
86*7f2fe78bSCy Schubert void **cb_ret);
87*7f2fe78bSCy Schubert
88*7f2fe78bSCy Schubert long
profile_module_init(const char * residual,struct profile_vtable * vtable,void ** cb_ret)89*7f2fe78bSCy Schubert profile_module_init(const char *residual, struct profile_vtable *vtable,
90*7f2fe78bSCy Schubert void **cb_ret)
91*7f2fe78bSCy Schubert {
92*7f2fe78bSCy Schubert struct data *d;
93*7f2fe78bSCy Schubert
94*7f2fe78bSCy Schubert d = malloc(sizeof(*d));
95*7f2fe78bSCy Schubert d->residual = strdup(residual);
96*7f2fe78bSCy Schubert d->gen = 0;
97*7f2fe78bSCy Schubert *cb_ret = d;
98*7f2fe78bSCy Schubert
99*7f2fe78bSCy Schubert vtable->get_values = get_values;
100*7f2fe78bSCy Schubert vtable->free_values = free_values;
101*7f2fe78bSCy Schubert vtable->cleanup = cleanup;
102*7f2fe78bSCy Schubert vtable->copy = copy;
103*7f2fe78bSCy Schubert return 0;
104*7f2fe78bSCy Schubert }
105