Path: blob/main/crypto/krb5/src/lib/krad/t_client.c
39536 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/krad/t_client.c - Client request test program */2/*3* Copyright 2013 Red Hat, Inc. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions are met:7*8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in13* the documentation and/or other materials provided with the14* distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS17* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED18* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A19* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER20* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,21* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,22* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR23* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS26* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829#include "t_daemon.h"3031#define EVENT_COUNT 43233static struct34{35int count;36struct event events[EVENT_COUNT];37} record;3839static verto_ctx *vctx;4041static void42callback(krb5_error_code retval, const krad_packet *request,43const krad_packet *response, void *data)44{45struct event *evt;4647evt = &record.events[record.count++];48evt->error = retval != 0;49if (evt->error)50evt->result.retval = retval;51else52evt->result.code = krad_packet_get_code(response);53verto_break(vctx);54}5556int57main(int argc, const char **argv)58{59krad_attrset *attrs;60krad_client *rc;61krb5_context kctx;62krb5_data tmp;6364if (!daemon_start(argc, argv)) {65fprintf(stderr, "Unable to start pyrad daemon, skipping test...\n");66return 0;67}6869noerror(krb5_init_context(&kctx));70vctx = verto_new(NULL, VERTO_EV_TYPE_IO | VERTO_EV_TYPE_TIMEOUT);71insist(vctx != NULL);72noerror(krad_client_new(kctx, vctx, &rc));7374tmp = string2data("testUser");75noerror(krad_attrset_new(kctx, &attrs));76noerror(krad_attrset_add(attrs, KRAD_ATTR_USER_NAME, &tmp));7778/* Test accept. */79tmp = string2data("accept");80noerror(krad_attrset_add(attrs, KRAD_ATTR_USER_PASSWORD, &tmp));81noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",82"foo", 1000, 3, callback, NULL));83verto_run(vctx);8485/* Test reject. */86tmp = string2data("reject");87krad_attrset_del(attrs, KRAD_ATTR_USER_PASSWORD, 0);88noerror(krad_attrset_add(attrs, KRAD_ATTR_USER_PASSWORD, &tmp));89noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",90"foo", 1000, 3, callback, NULL));91verto_run(vctx);9293/* Test timeout. */94daemon_stop();95noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",96"foo", 1000, 3, callback, NULL));97verto_run(vctx);9899/* Test outstanding packet freeing. */100noerror(krad_client_send(rc, KRAD_CODE_ACCESS_REQUEST, attrs, "localhost",101"foo", 1000, 3, callback, NULL));102krad_client_free(rc);103rc = NULL;104105/* Verify the results. */106insist(record.count == EVENT_COUNT);107insist(record.events[0].error == FALSE);108insist(record.events[0].result.code == KRAD_CODE_ACCESS_ACCEPT);109insist(record.events[1].error == FALSE);110insist(record.events[1].result.code == KRAD_CODE_ACCESS_REJECT);111insist(record.events[2].error == TRUE);112insist(record.events[2].result.retval == ETIMEDOUT);113insist(record.events[3].error == TRUE);114insist(record.events[3].result.retval == ECANCELED);115116krad_attrset_free(attrs);117krad_client_free(rc);118verto_free(vctx);119krb5_free_context(kctx);120return 0;121}122123124