1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* util/profile/proftest/test.c - Test dynamic profile module */
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 /*
28 * This file implements a very simple profile module which just returns the
29 * residual string and the number of copies in response to any query. The full
30 * range of vtable profile operations is tested elsewhere.
31 */
32
33 #include "k5-platform.h"
34 #include "profile.h"
35
36 struct data {
37 char *residual;
38 int gen;
39 };
40
41 static long
get_values(void * cbdata,const char * const * names,char *** ret_values)42 get_values(void *cbdata, const char *const *names, char ***ret_values)
43 {
44 struct data *d = cbdata;
45
46 *ret_values = calloc(3, sizeof(*ret_values));
47 (*ret_values)[0] = strdup(d->residual);
48 asprintf(&(*ret_values)[1], "%d", d->gen);
49 (*ret_values)[2] = NULL;
50 return 0;
51 }
52
53 static void
free_values(void * cbdata,char ** values)54 free_values(void *cbdata, char **values)
55 {
56 char **v;
57
58 for (v = values; *v; v++)
59 free(*v);
60 free(values);
61 }
62
63 static void
cleanup(void * cbdata)64 cleanup(void *cbdata)
65 {
66 struct data *d = cbdata;
67
68 free(d->residual);
69 free(d);
70 }
71
72 static long
copy(void * cbdata,void ** ret_cbdata)73 copy(void *cbdata, void **ret_cbdata)
74 {
75 struct data *old_data = cbdata, *new_data;
76
77 new_data = malloc(sizeof(*new_data));
78 new_data->residual = strdup(old_data->residual);
79 new_data->gen = old_data->gen + 1;
80 *ret_cbdata = new_data;
81 return 0;
82 }
83
84 long
85 profile_module_init(const char *residual, struct profile_vtable *vtable,
86 void **cb_ret);
87
88 long
profile_module_init(const char * residual,struct profile_vtable * vtable,void ** cb_ret)89 profile_module_init(const char *residual, struct profile_vtable *vtable,
90 void **cb_ret)
91 {
92 struct data *d;
93
94 d = malloc(sizeof(*d));
95 d->residual = strdup(residual);
96 d->gen = 0;
97 *cb_ret = d;
98
99 vtable->get_values = get_values;
100 vtable->free_values = free_values;
101 vtable->cleanup = cleanup;
102 vtable->copy = copy;
103 return 0;
104 }
105