1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/t_response_set.c - Test krb5_response_set */
3 /*
4 * Copyright 2012 Red Hat, Inc.
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 Red Hat 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 Red Hat software.
22 * Red Hat 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 #include <k5-int.h>
28
29 #include "int-proto.h"
30
31 #define TEST_STR1 "test1"
32 #define TEST_STR2 "test2"
33
34 static void
check_pred(int predicate)35 check_pred(int predicate)
36 {
37 if (!predicate)
38 abort();
39 }
40
41 static void
check(krb5_error_code code)42 check(krb5_error_code code)
43 {
44 if (code != 0) {
45 com_err("t_response_items", code, NULL);
46 abort();
47 }
48 }
49
50 static int
nstrcmp(const char * a,const char * b)51 nstrcmp(const char *a, const char *b)
52 {
53 if (a == NULL && b == NULL)
54 return 0;
55 else if (a == NULL)
56 return -1;
57 else if (b == NULL)
58 return 1;
59
60 return strcmp(a, b);
61 }
62
63 int
main(void)64 main(void)
65 {
66 k5_response_items *ri;
67
68 check(k5_response_items_new(&ri));
69 check_pred(k5_response_items_empty(ri));
70
71 check(k5_response_items_ask_question(ri, TEST_STR1, TEST_STR1));
72 check(k5_response_items_ask_question(ri, TEST_STR2, NULL));
73 check_pred(nstrcmp(k5_response_items_get_challenge(ri, TEST_STR1),
74 TEST_STR1) == 0);
75 check_pred(nstrcmp(k5_response_items_get_challenge(ri, TEST_STR2),
76 NULL) == 0);
77 check_pred(!k5_response_items_empty(ri));
78
79 k5_response_items_reset(ri);
80 check_pred(k5_response_items_empty(ri));
81 check_pred(k5_response_items_get_challenge(ri, TEST_STR1) == NULL);
82 check_pred(k5_response_items_get_challenge(ri, TEST_STR2) == NULL);
83
84 check(k5_response_items_ask_question(ri, TEST_STR1, TEST_STR1));
85 check_pred(nstrcmp(k5_response_items_get_challenge(ri, TEST_STR1),
86 TEST_STR1) == 0);
87 check(k5_response_items_set_answer(ri, TEST_STR1, TEST_STR1));
88 check_pred(nstrcmp(k5_response_items_get_answer(ri, TEST_STR1),
89 TEST_STR1) == 0);
90
91 k5_response_items_free(ri);
92
93 return 0;
94 }
95