From 9e8331c95c94003458ec0a9f9a750bee9f25fdbd Mon Sep 17 00:00:00 2001 From: Karl Ramm Date: Thu, 31 Jan 2013 08:02:34 -0500 Subject: Test the low-level code in uloc.c Also, tweak the debian build infrastructure so that we can pass in arbitrary CFLAGS. New program test_server that links with the non-main.c parts of the server. Currently only (as above) tests the low-level bits of uloc.c. --- debian/rules | 3 +- server/Makefile.in | 18 ++++-- server/test_server.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++ server/uloc.c | 44 ++++---------- server/zserver.h | 18 ++++++ 5 files changed, 212 insertions(+), 37 deletions(-) create mode 100644 server/test_server.c diff --git a/debian/rules b/debian/rules index 42e0bb3..eba0709 100755 --- a/debian/rules +++ b/debian/rules @@ -22,6 +22,7 @@ CONFIGURE_krb5=--with-krb5=/usr CONFIGURE_krb45=--with-krb4=/usr --with-krb5=/usr CONFIGURE_krb=--with-krb4=/usr CONFIGURE_no-krb= +CFLAGS=-g -O CHECK=check # see /usr/share/doc/autotools-dev/README.Debian.gz @@ -66,7 +67,7 @@ configure-stamp: automake -a || true # we only want this for install-sh autoreconf mkdir -p $(BUILD_VARIETALS) - $(foreach VARIETY,$(BUILD_VARIETALS),(cd $(VARIETY) && CFLAGS="-g -O" ../configure $(CONFIGURE_$(VARIETY)) $(CONFIGURE_ROOT));) + $(foreach VARIETY,$(BUILD_VARIETALS),(cd $(VARIETY) && CFLAGS="$(CFLAGS)" ../configure $(CONFIGURE_$(VARIETY)) $(CONFIGURE_ROOT));) touch configure-stamp build-arch: build-stamp diff --git a/server/Makefile.in b/server/Makefile.in index b2a0600..a23b509 100644 --- a/server/Makefile.in +++ b/server/Makefile.in @@ -36,15 +36,22 @@ LDFLAGS=@LDFLAGS@ LIBS=${LIBZEPHYR} @LIBS@ -lcom_err HESIOD_LIBS=@HESIOD_LIBS@ -OBJS= zsrv_err.o access.o acl_files.o bdump.o class.o client.o common.o \ - dispatch.o kstuff.o main.o server.o subscr.o timer.o uloc.o \ +NMOBJS= zsrv_err.o access.o acl_files.o bdump.o class.o client.o common.o \ + dispatch.o kstuff.o global.o server.o subscr.o timer.o uloc.o \ zstring.o realm.o version.o utf8proc.o -all: zephyrd zephyrd.8 +OBJS= main.o $(NMOBJS) + +TESTOBJS = test_server.o $(NMOBJS) + +all: zephyrd zephyrd.8 test_server zephyrd: ${OBJS} ${LIBZEPHYR} ${LIBTOOL} --mode=link ${CC} ${LDFLAGS} -o $@ ${OBJS} ${LIBS} ${HESIOD_LIBS} +test_server: $(TESTOBJS) + ${LIBTOOL} --mode=link ${CC} ${LDFLAGS} -o $@ ${TESTOBJS} ${LIBS} ${HESIOD_LIBS} + zsrv_err.h: zsrv_err.c zsrv_err.c: zsrv_err.et compile_et ${srcdir}/zsrv_err.et @@ -56,7 +63,8 @@ zephyrd.8: ${srcdir}/zephyrd.8.in Makefile ${editman} ${srcdir}/$@.in > $@.tmp mv $@.tmp $@ -check: +check: test_server + ./test_server # No dependency on zephyrd, to avoid rebuilding version.o. install: zephyrd.8 zephyrd @@ -67,7 +75,7 @@ install: zephyrd.8 zephyrd ${DESTDIR}${sysconfdir}/zephyr clean: - ${LIBTOOL} --mode=clean rm -f zephyrd + ${LIBTOOL} --mode=clean rm -f zephyrd test_server rm -f ${OBJS} zsrv_err.[ch] rm -f zephyrd.8 diff --git a/server/test_server.c b/server/test_server.c new file mode 100644 index 0000000..e9ff71e --- /dev/null +++ b/server/test_server.c @@ -0,0 +1,166 @@ +/* This file is part of the Project Athena Zephyr Notification System. + * It contains the server unit tests. + * + * Created by: Karl Ramm + * + * Copyright (c) 1987,1988,1991 by the Massachusetts Institute of Technology. + * For copying and distribution information, see the file + * "mit-copyright.h". + */ + +#include +#include + +#include "zserver.h" + +int ulogin_add_user(ZNotice_t *notice, Exposure_type exposure, + struct sockaddr_in *who); +Exposure_type ulogin_remove_user(ZNotice_t *notice, + struct sockaddr_in *who, + int *err_return); +Location *ulogin_find(char *user, struct in_addr *host, + unsigned int port); +Location *ulogin_find_user(char *user); +void ulogin_flush_user(ZNotice_t *notice); + +#define TEST(EXP) \ + do { \ + printf("%s:%d: %s: ", __FILE__, __LINE__, #EXP); \ + fflush(stdout); \ + if (EXP) { \ + puts("PASS"); \ + } else { \ + puts("FAIL"); \ + failures++; \ + } \ + fflush(stdout); \ + } while (0) + +#define V(EXP) \ + do { \ + printf("%s:%d: %s\n", __FILE__, __LINE__, #EXP); \ + fflush(stdout); \ + EXP; \ + } while (0) + + +int failures = 0; + +void test_uloc(); + +main() { + puts("Zephyr server testing"); + puts(""); + + test_uloc(); + + exit(!(failures == 0)); +} + +void +test_uloc() { + ZNotice_t z1, z2, z0, z4; + String *s1, *s2, *s0, *s4; + struct sockaddr_in who1, who2, who3, who0, who4; + int ret; + + puts("uloc storage routines"); + + TEST(ulogin_find_user("nonexistent") == NULL); + + /* fake up just enough */ + who1.sin_family = AF_INET; + who1.sin_port = 1; + who1.sin_addr.s_addr = INADDR_LOOPBACK; + + z1.z_class_inst = "user1"; + z1.z_port = 1; + z1.z_message = "here\0now\0this\0"; + z1.z_message_len = 14; + + s1 = make_string(z1.z_class_inst, 0); + + TEST(ulogin_add_user(&z1, NET_ANN, &who1) == 0); + TEST(ulogin_find_user("user1") != NULL); + + who2.sin_family = AF_INET; + who2.sin_port = 2; + who2.sin_addr.s_addr = INADDR_LOOPBACK; + + z2.z_class_inst = "user2"; + z2.z_port = 2; + z2.z_message = "here\0now\0this\0"; + z2.z_message_len = 14; + + s2 = make_string(z2.z_class_inst, 0); + + TEST(ulogin_add_user(&z2, NET_ANN, &who2) == 0); + TEST(ulogin_find_user("user2") != NULL); + TEST(ulogin_find_user("user1")->user == s1); + TEST(ulogin_find_user("user2")->user == s2); + TEST(ulogin_add_user(&z1, NET_ANN, &who1) == 0); + TEST(ulogin_find_user("user1")->user == s1); + TEST(ulogin_find_user("user2")->user == s2); + + who3.sin_family = AF_INET; + who3.sin_port = 3; + who3.sin_addr.s_addr = INADDR_LOOPBACK; + + TEST(ulogin_find("user1", &who3.sin_addr, 3) == NULL); + + who0.sin_family = AF_INET; + who0.sin_port = 3; + who0.sin_addr.s_addr = INADDR_LOOPBACK; + + z0.z_class_inst = "user0"; + z0.z_port = 3; + z0.z_message = "here\0now\0this\0"; + z0.z_message_len = 14; + + s0 = make_string(z0.z_class_inst, 0); + + TEST(ulogin_add_user(&z0, NET_ANN, &who0) == 0); + TEST(ulogin_find_user("user0") != NULL); + TEST(ulogin_find_user("user1")->user == s1); + TEST(ulogin_find_user("user2")->user == s2); + + TEST(ulogin_remove_user(&z0, &who0, &ret) == NET_ANN && ret == 0); + /* 1 = NOLOC */ + TEST(ulogin_remove_user(&z0, &who0, &ret) == NONE && ret == 1); + + TEST(ulogin_add_user(&z0, NET_ANN, &who0) == 0); + TEST(ulogin_remove_user(&z1, &who0, &ret) == NET_ANN && ret == 0); + + V(ulogin_flush_user(&z0)); + TEST(ulogin_find_user("user0") == NULL); + + TEST(ulogin_add_user(&z0, NET_ANN, &who0) == 0); + TEST(ulogin_add_user(&z1, NET_ANN, &who1) == 0); + V(ulogin_flush_user(&z1)); + TEST(ulogin_find_user("user1") == NULL); + + who4.sin_family = AF_INET; + who4.sin_port = 4; + who4.sin_addr.s_addr = INADDR_ANY; + + z4.z_class_inst = "user4"; + z4.z_port = 4; + z4.z_message = "here\0now\0this\0"; + z4.z_message_len = 14; + + s4 = make_string(z4.z_class_inst, 0); + + TEST(ulogin_add_user(&z4, NET_ANN, &who4) == 0); + + V(uloc_flush_client(&who2)); + TEST(ulogin_find_user("user0")->user == s0); + TEST(ulogin_find_user("user1") == NULL); + TEST(ulogin_find_user("user2") == NULL); + TEST(ulogin_find_user("user4")->user == s4); + + V(uloc_hflush(&who0.sin_addr)); + TEST(ulogin_find_user("user0") == NULL); + TEST(ulogin_find_user("user1") == NULL); + TEST(ulogin_find_user("user2") == NULL); + TEST(ulogin_find_user("user4")->user == s4); +} diff --git a/server/uloc.c b/server/uloc.c index 6b26894..0e45bfb 100644 --- a/server/uloc.c +++ b/server/uloc.c @@ -61,42 +61,24 @@ static const char rcsid_uloc_c[] = /* else you will lose. See ulogin_locate() and uloc_send_locations() */ #define NUM_FIELDS 3 -typedef enum _Exposure_type { - NONE, - OPSTAFF_VIS, - REALM_VIS, - REALM_ANN, - NET_VIS, - NET_ANN -} Exposure_type; - -typedef struct _Location { - String *user; - String *machine; - char *time; /* in ctime format */ - String *tty; - struct sockaddr_in addr; /* IP address and port of location */ - Exposure_type exposure; -} Location; - #define NOLOC 1 #define QUIET -1 #define UNAUTH -2 static void ulogin_locate(ZNotice_t *notice, struct sockaddr_in *who, int auth); -static void ulogin_flush_user(ZNotice_t *notice); -static Location *ulogin_find(char *user, struct in_addr *host, - unsigned int port); -static Location *ulogin_find_user(char *user); +void ulogin_flush_user(ZNotice_t *notice); +Location *ulogin_find(char *user, struct in_addr *host, + unsigned int port); +Location *ulogin_find_user(char *user); static int ulogin_setup(ZNotice_t *notice, Location *locs, Exposure_type exposure, struct sockaddr_in *who); -static int ulogin_add_user(ZNotice_t *notice, Exposure_type exposure, +int ulogin_add_user(ZNotice_t *notice, Exposure_type exposure, struct sockaddr_in *who); static int ulogin_parse(ZNotice_t *notice, Location *locs); -static Exposure_type ulogin_remove_user(ZNotice_t *notice, - struct sockaddr_in *who, - int *err_return); +Exposure_type ulogin_remove_user(ZNotice_t *notice, + struct sockaddr_in *who, + int *err_return); static void login_sendit(ZNotice_t *notice, int auth, struct sockaddr_in *who, int external); static char **ulogin_marshal_locs(ZNotice_t *notice, int *found, int auth); @@ -457,7 +439,7 @@ uloc_send_locations(void) * Add the user to the internal table of locations. */ -static int +int ulogin_add_user(ZNotice_t *notice, Exposure_type exposure, struct sockaddr_in *who) @@ -593,7 +575,7 @@ ulogin_parse(ZNotice_t *notice, } -static Location * +Location * ulogin_find(char *user, struct in_addr *host, unsigned int port) @@ -626,7 +608,7 @@ ulogin_find(char *user, * table. */ -static Location * +Location * ulogin_find_user(char *user) { int i, rlo, rhi; @@ -668,7 +650,7 @@ ulogin_find_user(char *user) * remove the user specified in notice from the internal table */ -static Exposure_type +Exposure_type ulogin_remove_user(ZNotice_t *notice, struct sockaddr_in *who, int *err_return) @@ -727,7 +709,7 @@ ulogin_remove_user(ZNotice_t *notice, * remove all locs of the user specified in notice from the internal table */ -static void +void ulogin_flush_user(ZNotice_t *notice) { Location *loc, *loc2; diff --git a/server/zserver.h b/server/zserver.h index c694489..6ded4a4 100644 --- a/server/zserver.h +++ b/server/zserver.h @@ -211,6 +211,24 @@ struct _Statistic { char *str; }; +typedef enum _Exposure_type { + NONE, + OPSTAFF_VIS, + REALM_VIS, + REALM_ANN, + NET_VIS, + NET_ANN +} Exposure_type; + +typedef struct _Location { + String *user; + String *machine; + char *time; /* in ctime format */ + String *tty; + struct sockaddr_in addr; /* IP address and port of location */ + Exposure_type exposure; +} Location; + /* Function declarations */ /* These macros instantiate inline functions that do the work of the formder -- cgit v1.2.3