Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/krb5/src/lib/krad/t_client.c
39536 views
1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* lib/krad/t_client.c - Client request test program */
3
/*
4
* Copyright 2013 Red Hat, Inc. All rights reserved.
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are met:
8
*
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
*
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in
14
* the documentation and/or other materials provided with the
15
* distribution.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include "t_daemon.h"
31
32
#define EVENT_COUNT 4
33
34
static struct
35
{
36
int count;
37
struct event events[EVENT_COUNT];
38
} record;
39
40
static verto_ctx *vctx;
41
42
static void
43
callback(krb5_error_code retval, const krad_packet *request,
44
const krad_packet *response, void *data)
45
{
46
struct event *evt;
47
48
evt = &record.events[record.count++];
49
evt->error = retval != 0;
50
if (evt->error)
51
evt->result.retval = retval;
52
else
53
evt->result.code = krad_packet_get_code(response);
54
verto_break(vctx);
55
}
56
57
int
58
main(int argc, const char **argv)
59
{
60
krad_attrset *attrs;
61
krad_client *rc;
62
krb5_context kctx;
63
krb5_data tmp;
64
65
if (!daemon_start(argc, argv)) {
66
fprintf(stderr, "Unable to start pyrad daemon, skipping test...\n");
67
return 0;
68
}
69
70
noerror(krb5_init_context(&kctx));
71
vctx = verto_new(NULL, VERTO_EV_TYPE_IO | VERTO_EV_TYPE_TIMEOUT);
72
insist(vctx != NULL);
73
noerror(krad_client_new(kctx, vctx, &rc));
74
75
tmp = string2data("testUser");
76
noerror(krad_attrset_new(kctx, &attrs));
77
noerror(krad_attrset_add(attrs, KRAD_ATTR_USER_NAME, &tmp));
78
79
/* Test accept. */
80
tmp = string2data("accept");
81
noerror(krad_attrset_add(attrs, KRAD_ATTR_USER_PASSWORD, &tmp));
82
noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",
83
"foo", 1000, 3, callback, NULL));
84
verto_run(vctx);
85
86
/* Test reject. */
87
tmp = string2data("reject");
88
krad_attrset_del(attrs, KRAD_ATTR_USER_PASSWORD, 0);
89
noerror(krad_attrset_add(attrs, KRAD_ATTR_USER_PASSWORD, &tmp));
90
noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",
91
"foo", 1000, 3, callback, NULL));
92
verto_run(vctx);
93
94
/* Test timeout. */
95
daemon_stop();
96
noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",
97
"foo", 1000, 3, callback, NULL));
98
verto_run(vctx);
99
100
/* Test outstanding packet freeing. */
101
noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",
102
"foo", 1000, 3, callback, NULL));
103
krad_client_free(rc);
104
rc = NULL;
105
106
/* Verify the results. */
107
insist(record.count == EVENT_COUNT);
108
insist(record.events[0].error == FALSE);
109
insist(record.events[0].result.code == KRAD_CODE_ACCESS_ACCEPT);
110
insist(record.events[1].error == FALSE);
111
insist(record.events[1].result.code == KRAD_CODE_ACCESS_REJECT);
112
insist(record.events[2].error == TRUE);
113
insist(record.events[2].result.retval == ETIMEDOUT);
114
insist(record.events[3].error == TRUE);
115
insist(record.events[3].result.retval == ECANCELED);
116
117
krad_attrset_free(attrs);
118
krad_client_free(rc);
119
verto_free(vctx);
120
krb5_free_context(kctx);
121
return 0;
122
}
123
124