Path: blob/main/crypto/krb5/src/lib/krad/t_remote.c
39536 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/krad/t_remote.c - Protocol 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 63233static struct34{35int count;36struct event events[EVENT_COUNT];37} record;3839static krad_attrset *set;40static krad_remote *rr;41static verto_ctx *vctx;4243static void44callback(krb5_error_code retval, const krad_packet *request,45const krad_packet *response, void *data)46{47struct event *evt;4849evt = &record.events[record.count++];50evt->error = retval != 0;51if (evt->error)52evt->result.retval = retval;53else54evt->result.code = krad_packet_get_code(response);55verto_break(vctx);56}5758static void59remote_new(krb5_context kctx, krad_remote **remote)60{61struct addrinfo *ai = NULL, hints;6263memset(&hints, 0, sizeof(hints));64hints.ai_family = AF_INET;65hints.ai_socktype = SOCK_DGRAM;66noerror(gai_error_code(getaddrinfo("127.0.0.1", "radius", &hints, &ai)));6768noerror(kr_remote_new(kctx, vctx, ai, "foo", remote));69insist(kr_remote_equals(*remote, ai, "foo"));70freeaddrinfo(ai);71}7273static krb5_error_code74do_auth(const char *password, const krad_packet **pkt)75{76const krad_packet *tmppkt;77krb5_error_code retval;78krb5_data tmp = string2data((char *)password);7980retval = krad_attrset_add(set, KRAD_ATTR_USER_PASSWORD, &tmp);81if (retval != 0)82return retval;8384retval = kr_remote_send(rr, KRAD_CODE_ACCESS_REQUEST, set, callback, NULL,851000, 3, &tmppkt);86krad_attrset_del(set, KRAD_ATTR_USER_PASSWORD, 0);87if (retval != 0)88return retval;8990if (pkt != NULL)91*pkt = tmppkt;92return 0;93}9495static void96test_timeout(verto_ctx *ctx, verto_ev *ev)97{98static const krad_packet *pkt;99100noerror(do_auth("accept", &pkt));101kr_remote_cancel(rr, pkt);102}103104int105main(int argc, const char **argv)106{107krb5_context kctx = NULL;108krb5_data tmp;109110if (!daemon_start(argc, argv)) {111fprintf(stderr, "Unable to start pyrad daemon, skipping test...\n");112return 0;113}114115/* Initialize. */116noerror(krb5_init_context(&kctx));117vctx = verto_new(NULL, VERTO_EV_TYPE_IO | VERTO_EV_TYPE_TIMEOUT);118insist(vctx != NULL);119remote_new(kctx, &rr);120121/* Create attribute set. */122noerror(krad_attrset_new(kctx, &set));123tmp = string2data("testUser");124noerror(krad_attrset_add(set, KRAD_ATTR_USER_NAME, &tmp));125126/* Send accept packet. */127noerror(do_auth("accept", NULL));128verto_run(vctx);129130/* Send reject packet. */131noerror(do_auth("reject", NULL));132verto_run(vctx);133134/* Send canceled packet. */135insist(verto_add_timeout(vctx, VERTO_EV_FLAG_NONE, test_timeout, 0) !=136NULL);137verto_run(vctx);138139/* Test timeout. */140daemon_stop();141noerror(do_auth("accept", NULL));142verto_run(vctx);143144/* Test outstanding packet freeing. */145noerror(do_auth("accept", NULL));146kr_remote_free(rr);147krad_attrset_free(set);148149/* Verify the results. */150insist(record.count == EVENT_COUNT);151insist(record.events[0].error == FALSE);152insist(record.events[0].result.code == KRAD_CODE_ACCESS_ACCEPT);153insist(record.events[1].error == FALSE);154insist(record.events[1].result.code == KRAD_CODE_ACCESS_REJECT);155insist(record.events[2].error == TRUE);156insist(record.events[2].result.retval == ECANCELED);157insist(record.events[3].error == TRUE);158insist(record.events[3].result.retval == ETIMEDOUT);159insist(record.events[4].error == TRUE);160insist(record.events[4].result.retval == ECANCELED);161insist(record.events[5].error == TRUE);162insist(record.events[5].result.retval == ECANCELED);163164verto_free(vctx);165krb5_free_context(kctx);166return 0;167}168169170