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_name2num("User-Name"), &tmp)); 78 79 /* Test accept. */ 80 tmp = string2data("accept"); 81 noerror(krad_attrset_add(attrs, krad_attr_name2num("User-Password"), 82 &tmp)); 83 noerror(krad_client_send(rc, krad_code_name2num("Access-Request"), attrs, 84 "localhost", "foo", 1000, 3, callback, NULL)); 85 verto_run(vctx); 86 87 /* Test reject. */ 88 tmp = string2data("reject"); 89 krad_attrset_del(attrs, krad_attr_name2num("User-Password"), 0); 90 noerror(krad_attrset_add(attrs, krad_attr_name2num("User-Password"), 91 &tmp)); 92 noerror(krad_client_send(rc, krad_code_name2num("Access-Request"), attrs, 93 "localhost", "foo", 1000, 3, callback, NULL)); 94 verto_run(vctx); 95 96 /* Test timeout. */ 97 daemon_stop(); 98 noerror(krad_client_send(rc, krad_code_name2num("Access-Request"), attrs, 99 "localhost", "foo", 1000, 3, callback, NULL)); 100 verto_run(vctx); 101 102 /* Test outstanding packet freeing. */ 103 noerror(krad_client_send(rc, krad_code_name2num("Access-Request"), attrs, 104 "localhost", "foo", 1000, 3, callback, NULL)); 105 krad_client_free(rc); 106 rc = NULL; 107 108 /* Verify the results. */ 109 insist(record.count == EVENT_COUNT); 110 insist(record.events[0].error == FALSE); 111 insist(record.events[0].result.code == 112 krad_code_name2num("Access-Accept")); 113 insist(record.events[1].error == FALSE); 114 insist(record.events[1].result.code == 115 krad_code_name2num("Access-Reject")); 116 insist(record.events[2].error == TRUE); 117 insist(record.events[2].result.retval == ETIMEDOUT); 118 insist(record.events[3].error == TRUE); 119 insist(record.events[3].result.retval == ECANCELED); 120 121 krad_attrset_free(attrs); 122 krad_client_free(rc); 123 verto_free(vctx); 124 krb5_free_context(kctx); 125 return 0; 126 } 127