From: Alan T. DeKok Date: Tue, 17 Jan 2017 17:50:16 +0000 (-0500) Subject: added schedule_test X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=27ecfc0b726acf766bb9105c2ab8321e54449205;p=thirdparty%2Ffreeradius-server.git added schedule_test --- diff --git a/src/tests/util/all.mk b/src/tests/util/all.mk index e4736b20365..dc20e74496d 100644 --- a/src/tests/util/all.mk +++ b/src/tests/util/all.mk @@ -1,8 +1,8 @@ -SUBMAKEFILES := ring_buffer_test.mk message_set_test.mk atomic_queue_test.mk control_test.mk +SUBMAKEFILES := ring_buffer_test.mk message_set_test.mk atomic_queue_test.mk control_test.mk # # These require pthread. # ifneq "$(findstring thread,${CFLAGS})" "" -SUBMAKEFILES += channel_test.mk worker_test.mk radius1_test.mk +SUBMAKEFILES += channel_test.mk worker_test.mk radius1_test.mk schedule_test.mk endif diff --git a/src/tests/util/schedule_test.c b/src/tests/util/schedule_test.c new file mode 100644 index 00000000000..d94151d4ae2 --- /dev/null +++ b/src/tests/util/schedule_test.c @@ -0,0 +1,161 @@ +/* + * schedule_test.c Tests for the scheduler + * + * Version: $Id$ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + * + * Copyright 2016 Alan DeKok + */ + +RCSID("$Id$") + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifdef HAVE_GETOPT_H +# include +#endif + +#ifdef HAVE_PTHREAD_H +#include +#endif + +#define MPRINT1 if (debug_lvl) printf + +typedef struct fr_packet_ctx_t { + uint8_t vector[16]; + uint8_t id; + + struct sockaddr_storage src; + socklen_t salen; +} fr_packet_ctx_t; + +static int debug_lvl = 0; +static char const *secret = "testing123"; + +static int test_decode(void const *packet_ctx, uint8_t *const data, size_t data_len, REQUEST *request) +{ + fr_packet_ctx_t const *pc = packet_ctx; + + request->number = pc->id; + + if (!debug_lvl) return 0; + + MPRINT1("\t\tDECODE <<< request %zd - %p data %p size %zd\n", request->number, packet_ctx, data, data_len); + + return 0; +} + +static ssize_t test_encode(void const *packet_ctx, REQUEST *request, uint8_t *buffer, size_t buffer_len) +{ + FR_MD5_CTX context; + fr_packet_ctx_t const *pc = packet_ctx; + + MPRINT1("\t\tENCODE >>> request %zd - data %p %p room %zd\n", request->number, packet_ctx, buffer, buffer_len); + + buffer[0] = PW_CODE_ACCESS_ACCEPT; + buffer[1] = pc->id; + buffer[2] = 0; + buffer[3] = 20; + + memcpy(buffer + 4, pc->vector, 16); + + fr_md5_init(&context); + fr_md5_update(&context, buffer, 20); + fr_md5_update(&context, (uint8_t const *) secret, strlen(secret)); + fr_md5_final(buffer + 4, &context); + + return 20; +} + +static size_t test_nak(void const *packet_ctx, uint8_t *const packet, size_t packet_len, UNUSED uint8_t *reply, UNUSED size_t reply_len) +{ + MPRINT1("\t\tNAK !!! request %d - data %p %p size %zd\n", packet[1], packet_ctx, packet, packet_len); + + return 10; +} + +static fr_transport_final_t test_process(REQUEST *request, fr_transport_action_t action) +{ + MPRINT1("\t\tPROCESS --- request %zd action %d\n", request->number, action); + return FR_TRANSPORT_REPLY; +} + +static fr_transport_t transport = { + .name = "schedule-test", + .id = 1, + .decode = test_decode, + .encode = test_encode, + .nak = test_nak, + .process = test_process, +}; + +static fr_transport_t *transports = &transport; + +static void NEVER_RETURNS usage(void) +{ + fprintf(stderr, "usage: schedule_test [OPTS]\n"); + fprintf(stderr, " -x Debugging mode.\n"); + + exit(1); +} + +int main(int argc, char *argv[]) +{ + int c; + TALLOC_CTX *autofree = talloc_init("main"); + fr_schedule_t *sched; + + fr_time_start(); + + while ((c = getopt(argc, argv, "hm:o:tx")) != EOF) switch (c) { + case 'x': + debug_lvl++; + break; + + case 'h': + default: + usage(); + } + +#if 0 + argc -= (optind - 1); + argv += (optind - 1); +#endif + + sched = fr_schedule_create(autofree, 1, 2, 1, &transports, NULL, NULL); + if (!sched) { + fprintf(stderr, "schedule_test: Failed to create scheduler\n"); + exit(1); + } + + sleep(1); + + (void) fr_schedule_destroy(sched); + + talloc_free(autofree); + + return 0; +} diff --git a/src/tests/util/schedule_test.mk b/src/tests/util/schedule_test.mk new file mode 100644 index 00000000000..f4225a5570f --- /dev/null +++ b/src/tests/util/schedule_test.mk @@ -0,0 +1,7 @@ +TARGET := schedule_test + +SOURCES := schedule_test.c + +TGT_PREREQS := libfreeradius-util.a libfreeradius-server.a libfreeradius-radius.a +TGT_LDLIBS := $(LIBS) +