Path: blob/main/crypto/krb5/src/lib/krad/t_attrset.c
39536 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/krad/t_attrset.c - Attribute set 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_test.h"3031const static unsigned char auth[] = {320x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,330x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0034};3536const static unsigned char encpass[] = {370x58, 0x8d, 0xff, 0xda, 0x37, 0xf9, 0xe4, 0xca,380x19, 0xae, 0x49, 0xb7, 0x16, 0x6d, 0x58, 0x2739};4041int42main(void)43{44unsigned char buffer[KRAD_PACKET_SIZE_MAX], encoded[MAX_ATTRSETSIZE];45const char *username = "testUser", *password = "accept";46const krb5_data *tmpp;47krad_attrset *set;48krb5_context ctx;49size_t len = 0, encode_len;50krb5_data tmp;5152noerror(krb5_init_context(&ctx));53noerror(krad_attrset_new(ctx, &set));5455/* Add username. */56tmp = string2data((char *)username);57noerror(krad_attrset_add(set, KRAD_ATTR_USER_NAME, &tmp));5859/* Add password. */60tmp = string2data((char *)password);61noerror(krad_attrset_add(set, KRAD_ATTR_USER_PASSWORD, &tmp));6263/* Encode attrset. */64noerror(kr_attrset_encode(set, "foo", auth, FALSE, buffer, &encode_len));65krad_attrset_free(set);6667/* Manually encode User-Name. */68encoded[len + 0] = KRAD_ATTR_USER_NAME;69encoded[len + 1] = strlen(username) + 2;70memcpy(encoded + len + 2, username, strlen(username));71len += encoded[len + 1];7273/* Manually encode User-Password. */74encoded[len + 0] = KRAD_ATTR_USER_PASSWORD;75encoded[len + 1] = sizeof(encpass) + 2;76memcpy(encoded + len + 2, encpass, sizeof(encpass));77len += encoded[len + 1];7879/* Compare output. */80insist(len == encode_len);81insist(memcmp(encoded, buffer, len) == 0);8283/* Decode output. */84tmp = make_data(buffer, len);85noerror(kr_attrset_decode(ctx, &tmp, "foo", auth, &set));8687/* Test getting an attribute. */88tmp = string2data((char *)username);89tmpp = krad_attrset_get(set, KRAD_ATTR_USER_NAME, 0);90insist(tmpp != NULL);91insist(tmpp->length == tmp.length);92insist(strncmp(tmpp->data, tmp.data, tmp.length) == 0);9394krad_attrset_free(set);95krb5_free_context(ctx);96return 0;97}9899100