From 6b3c7041e3880fbdec8dff2de6d68806d123086d Mon Sep 17 00:00:00 2001 From: VilhoRaatikka Date: Tue, 5 Aug 2014 09:31:10 +0300 Subject: [PATCH 1/8] Bug #468, http://bugs.skysql.com/show_bug.cgi?id=468, Query classifier accessed freed thread context. If parsing fails thd doesn't need to be freed because it holds correct information about command type. session.c:session_setup_filters : fixed memory leak hintparser.c: added token_free for HINT_TOKENs and fixed a few memory leaks. mysql_client_server_protocol.h: added mysql_protocol_done which frees memory blocks pointed to by protocol members. Those can't be freed in dcb.c because dcb.c doesn't know about protocol's members. mysql_backend.c:gw_backend_close: fixed memory leak mysql_client.c: gw_client_close: fixed memory leak mysql_common.c: added implementation of mysql_protocol_done :protocol_archive_srv_command: tried to fix memory leak. Some memory is still leaking according to valgrind. Removed use of uninitialized local variable len. readwritesplit.c:execute_sescmd_in_backend: fixed a memory leak - visible only in DEBUG=Y build. --- query_classifier/query_classifier.cc | 16 +++++----- server/core/session.c | 1 + .../include/mysql_client_server_protocol.h | 1 + server/modules/protocol/mysql_backend.c | 2 ++ server/modules/protocol/mysql_client.c | 1 + server/modules/protocol/mysql_common.c | 31 ++++++++++++++++--- .../routing/readwritesplit/readwritesplit.c | 19 +++++++----- 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/query_classifier/query_classifier.cc b/query_classifier/query_classifier.cc index eccc35a31..889940e00 100644 --- a/query_classifier/query_classifier.cc +++ b/query_classifier/query_classifier.cc @@ -149,17 +149,17 @@ skygw_query_type_t skygw_query_classifier_get_type( thd = get_or_create_thd_for_parsing(mysql, query_str); if (thd == NULL) - { - skygw_query_classifier_free(mysql); - } - /** Create parse_tree inside thd */ - failp = create_parse_tree(thd); - - if (failp) { skygw_query_classifier_free(mysql); *p_mysql = NULL; + goto return_qtype; } + /** + * Create parse_tree inside thd. + * thd and even lex are readable even if parser failed so let it + * continue despite failure. + */ + failp = create_parse_tree(thd); qtype = resolve_query_type(thd); if (p_mysql == NULL) @@ -464,7 +464,7 @@ static skygw_query_type_t resolve_query_type( type |= QUERY_TYPE_DISABLE_AUTOCOMMIT; type |= QUERY_TYPE_BEGIN_TRX; } - /** + /** * REVOKE ALL, ASSIGN_TO_KEYCACHE, * PRELOAD_KEYS, FLUSH, RESET, CREATE|ALTER|DROP SERVER */ diff --git a/server/core/session.c b/server/core/session.c index 80767f9ff..62e1015e4 100644 --- a/server/core/session.c +++ b/server/core/session.c @@ -667,6 +667,7 @@ int i; session->filters[i].session = head->session; session->filters[i].instance = head->instance; session->head = *head; + free(head); } for (i = 0; i < service->n_filters; i++) diff --git a/server/modules/include/mysql_client_server_protocol.h b/server/modules/include/mysql_client_server_protocol.h index f7d6030c1..9e2147d05 100644 --- a/server/modules/include/mysql_client_server_protocol.h +++ b/server/modules/include/mysql_client_server_protocol.h @@ -299,6 +299,7 @@ typedef struct { void gw_mysql_close(MySQLProtocol **ptr); MySQLProtocol* mysql_protocol_init(DCB* dcb, int fd); +void mysql_protocol_done (DCB* dcb); MySQLProtocol *gw_mysql_init(MySQLProtocol *data); void gw_mysql_close(MySQLProtocol **ptr); int gw_receive_backend_auth(MySQLProtocol *protocol); diff --git a/server/modules/protocol/mysql_backend.c b/server/modules/protocol/mysql_backend.c index 43cd97bcd..5d8088d4e 100644 --- a/server/modules/protocol/mysql_backend.c +++ b/server/modules/protocol/mysql_backend.c @@ -996,6 +996,8 @@ gw_backend_close(DCB *dcb) /** Send COM_QUIT to the backend being closed */ mysql_send_com_quit(dcb, 0, quitbuf); + + mysql_protocol_done(dcb); if (session != NULL && session->state == SESSION_STATE_STOPPING) { diff --git a/server/modules/protocol/mysql_client.c b/server/modules/protocol/mysql_client.c index db61084eb..6cc9027de 100644 --- a/server/modules/protocol/mysql_client.c +++ b/server/modules/protocol/mysql_client.c @@ -1325,6 +1325,7 @@ gw_client_close(DCB *dcb) CHK_PROTOCOL(protocol); } #endif + mysql_protocol_done(dcb); session = dcb->session; /** diff --git a/server/modules/protocol/mysql_common.c b/server/modules/protocol/mysql_common.c index 4ce34452b..70050a13c 100644 --- a/server/modules/protocol/mysql_common.c +++ b/server/modules/protocol/mysql_common.c @@ -96,6 +96,31 @@ return_p: } +/** + * mysql_protocol_done + * + * free protocol allocations. + * + * @param dcb owner DCB + * + */ +void mysql_protocol_done ( + DCB* dcb) +{ + server_command_t* scmd = ((MySQLProtocol *)dcb->protocol)->protocol_cmd_history; + server_command_t* scmd2; + + while (scmd != NULL) + { + scmd2 = scmd->scom_next; + free(scmd); + scmd = scmd2; + } +} + + + + /** * gw_mysql_close * @@ -1601,7 +1626,7 @@ void protocol_archive_srv_command( { server_command_t* s1; server_command_t** s2; - int len; + int len = 0; spinlock_acquire(&p->protocol_lock); @@ -1617,9 +1642,7 @@ void protocol_archive_srv_command( s2 = &p->protocol_cmd_history; if (*s2 != NULL) - { - len = 0; - + { while ((*s2)->scom_next != NULL) { *s2 = (*s2)->scom_next; diff --git a/server/modules/routing/readwritesplit/readwritesplit.c b/server/modules/routing/readwritesplit/readwritesplit.c index 90132a9f9..7fcf22974 100644 --- a/server/modules/routing/readwritesplit/readwritesplit.c +++ b/server/modules/routing/readwritesplit/readwritesplit.c @@ -1132,6 +1132,8 @@ static int routeQuery( break; case MYSQL_COM_STMT_EXECUTE: + /** Parsing is not needed for this type of packet */ +#if defined(NOT_USED) plainsqlbuf = gwbuf_clone_transform(querybuf, GWBUF_TYPE_PLAINSQL); len = GWBUF_LENGTH(plainsqlbuf); @@ -1140,7 +1142,8 @@ static int routeQuery( memcpy(querystr, startpos, len); memset(&querystr[len], 0, 1); qtype = skygw_query_classifier_get_type(querystr, 0, &mysql); - qtype |= QUERY_TYPE_EXEC_STMT; +#endif + qtype = QUERY_TYPE_EXEC_STMT; break; case MYSQL_COM_SHUTDOWN: /**< 8 where should shutdown be routed ? */ @@ -1923,7 +1926,8 @@ static bool select_connect_backend_servers( } } } - } + } /*< log only */ + /** * Choose at least 1+min_nslaves (master and slave) and at most 1+max_nslaves * servers from the sorted list. First master found is selected. @@ -2666,8 +2670,9 @@ static bool execute_sescmd_in_backend( pthread_self(), dcb->fd, STRPACKETTYPE(cmd)))); + gwbuf_free(tmpbuf); } -#endif +#endif /*< SS_DEBUG */ switch (scur->scmd_cur_cmd->my_sescmd_packet_type) { case MYSQL_COM_CHANGE_USER: rc = dcb->func.auth( @@ -3065,7 +3070,7 @@ static bool router_option_configured( } return succp; } -#endif +#endif /*< NOT_USED */ static void rwsplit_process_router_options( ROUTER_INSTANCE* router, @@ -3346,7 +3351,7 @@ static void print_error_packet( { while ((buf = gwbuf_consume(buf, GWBUF_LENGTH(buf))) != NULL); } -#endif +#endif /*< SS_DEBUG */ } static int router_get_servercount( @@ -3568,10 +3573,10 @@ static prep_stmt_t* prep_stmt_init( if (pstmt != NULL) { - #if defined(SS_DEBUG) +#if defined(SS_DEBUG) pstmt->pstmt_chk_top = CHK_NUM_PREP_STMT; pstmt->pstmt_chk_tail = CHK_NUM_PREP_STMT; - #endif +#endif pstmt->pstmt_state = PREP_STMT_ALLOC; pstmt->pstmt_type = type; From dbfaa5a8eaa1d62def7dce5503ca24faefe46cd2 Mon Sep 17 00:00:00 2001 From: VilhoRaatikka Date: Tue, 5 Aug 2014 16:28:26 +0300 Subject: [PATCH 2/8] Fix to http://bugs.skysql.com/show_bug.cgi?id=469, connection counter leaks in master. Removed redundant counter increments. --- server/modules/routing/readwritesplit/readwritesplit.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/server/modules/routing/readwritesplit/readwritesplit.c b/server/modules/routing/readwritesplit/readwritesplit.c index 7fcf22974..218972ab4 100644 --- a/server/modules/routing/readwritesplit/readwritesplit.c +++ b/server/modules/routing/readwritesplit/readwritesplit.c @@ -381,7 +381,7 @@ static void refreshInstance( * used in slave selection. */ if (!rlag_enabled) - { + { if (rlag_limited) { LOGIF(LE, (skygw_log_write_flush( @@ -1985,7 +1985,7 @@ static bool select_connect_backend_servers( backend_ref[i].bref_state = 0; bref_set_state(&backend_ref[i], BREF_IN_USE); - /** + /** * Increase backend connection counter. * Server's stats are _increased_ in * dcb.c:dcb_alloc ! @@ -2023,7 +2023,7 @@ static bool select_connect_backend_servers( session, b->backend_server->protocol); - if (backend_ref[i].bref_dcb != NULL) + if (backend_ref[i].bref_dcb != NULL) { master_connected = true; /** @@ -2040,8 +2040,6 @@ static bool select_connect_backend_servers( bref_set_state(&backend_ref[i], BREF_IN_USE); /** Increase backend connection counters */ - atomic_add(&b->backend_server->stats.n_current, 1); - atomic_add(&b->backend_server->stats.n_connections, 1); atomic_add(&b->backend_conn_count, 1); } else From 28cc98d33ae0116f399131974d6d5ad88dcd1a01 Mon Sep 17 00:00:00 2001 From: Timofey Turenko Date: Thu, 14 Aug 2014 23:22:21 +0300 Subject: [PATCH 3/8] add gcov patch file --- gcov.diff | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 gcov.diff diff --git a/gcov.diff b/gcov.diff new file mode 100644 index 000000000..c310d1b08 --- /dev/null +++ b/gcov.diff @@ -0,0 +1,138 @@ +diff --git a/makefile.inc b/makefile.inc +index f2d93bf..c7dbffa 100644 +--- a/makefile.inc ++++ b/makefile.inc +@@ -24,8 +24,8 @@ endif + + # -O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -fPIC + +-CFLAGS := $(CFLAGS) -Wall +-LDLIBS := $(LDLIBS) -pthread ++CFLAGS := $(CFLAGS) -Wall -fprofile-arcs -ftest-coverage ++LDLIBS := $(LDLIBS) -pthread -lgcov + LDMYSQL := -lmysqld + CPP_LDLIBS := -lstdc++ + +diff --git a/server/core/Makefile b/server/core/Makefile +index 9bf650c..9df75a7 100644 +--- a/server/core/Makefile ++++ b/server/core/Makefile +@@ -75,7 +75,7 @@ POBJS=maxpasswd.o secrets.o utils.o + LIBS=-L$(EMBEDDED_LIB) \ + -lmysqld \ + -lz -lm -lcrypt -lcrypto -ldl -laio -lrt -pthread -llog_manager \ +- -L../inih/extra -linih -lssl -lstdc++ ++ -L../inih/extra -linih -lssl -lstdc++ -lgcov + + all: maxscale maxkeys maxpasswd + +diff --git a/server/modules/filter/Makefile b/server/modules/filter/Makefile +index 931c35a..d5dcca9 100644 +--- a/server/modules/filter/Makefile ++++ b/server/modules/filter/Makefile +@@ -25,7 +25,7 @@ UTILSPATH := $(ROOT_PATH)/utils + + CC=cc + CFLAGS=-c -fPIC -I/usr/include -I../include -I../../include -I$(LOGPATH) \ +- -I$(UTILSPATH) -Wall -g ++ -I$(UTILSPATH) -Wall -g -fprofile-arcs -ftest-coverage + + include ../../../makefile.inc + +@@ -44,7 +44,7 @@ TEESRCS=tee.c + TEEOBJ=$(TEESRCS:.c=.o) + SRCS=$(TESTSRCS) $(QLASRCS) $(REGEXSRCS) $(TOPNSRCS) $(TEESRCS) + OBJ=$(SRCS:.c=.o) +-LIBS=$(UTILSPATH)/skygw_utils.o -lssl -llog_manager ++LIBS=$(UTILSPATH)/skygw_utils.o -lssl -llog_manager -lgcov + MODULES= libtestfilter.so libqlafilter.so libregexfilter.so libtopfilter.so libtee.so + + +diff --git a/server/modules/monitor/Makefile b/server/modules/monitor/Makefile +index 7fdbc58..bca01de 100644 +--- a/server/modules/monitor/Makefile ++++ b/server/modules/monitor/Makefile +@@ -28,7 +28,7 @@ CFLAGS=-c -fPIC -I. -I/usr/include -I../include -I../../include -I$(LOGPATH) \ + + LDFLAGS=-shared -L$(LOGPATH) -Wl,-rpath,$(DEST)/lib \ + -Wl,-rpath,$(LOGPATH) -Wl,-rpath,$(UTILSPATH) \ +- -Wl,-rpath,$(EMBEDDED_LIB) ++ -Wl,-rpath,$(EMBEDDED_LIB) -fprofile-arcs -ftest-coverage + + + +@@ -39,7 +39,7 @@ GALERAOBJ=$(GALERASRCS:.c=.o) + SRCS=$(MYSQLSRCS) + OBJ=$(SRCS:.c=.o) + LIBS=$(UTILSPATH)/skygw_utils.o -llog_manager \ +- -L$(EMBEDDED_LIB) -lmysqld ++ -L$(EMBEDDED_LIB) -lmysqld -lgcov + MODULES=libmysqlmon.so libgaleramon.so + + +diff --git a/server/modules/protocol/Makefile b/server/modules/protocol/Makefile +index 54a8f8c..c8913ab 100644 +--- a/server/modules/protocol/Makefile ++++ b/server/modules/protocol/Makefile +@@ -31,7 +31,7 @@ UTILSPATH := $(ROOT_PATH)/utils + + CC=cc + CFLAGS=-c -fPIC -I/usr/include -I../include -I../../include -I$(LOGPATH) \ +- -I$(UTILSPATH) -Wall -g ++ -I$(UTILSPATH) -Wall -g -fprofile-arcs -ftest-coverage + + include ../../../makefile.inc + +@@ -51,7 +51,7 @@ MAXSCALEDOBJ=$(MAXSCALEDSRCS:.c=.o) + SRCS=$(MYSQLCLIENTSRCS) $(MYSQLBACKENDSRCS) $(TELNETDSRCS) $(HTTPDSRCS) \ + $(MAXSCALEDSRCS) + OBJ=$(SRCS:.c=.o) +-LIBS=$(UTILSPATH)/skygw_utils.o ++LIBS=$(UTILSPATH)/skygw_utils.o -lgcov + MODULES=libMySQLClient.so libMySQLBackend.so libtelnetd.so libHTTPD.so \ + libmaxscaled.so + +diff --git a/server/modules/routing/Makefile b/server/modules/routing/Makefile +index 4feac68..afd1da7 100644 +--- a/server/modules/routing/Makefile ++++ b/server/modules/routing/Makefile +@@ -29,7 +29,7 @@ UTILSPATH := $(ROOT_PATH)/utils + + CC=cc + CFLAGS=-c -fPIC -I/usr/include -I../include -I../../include -I$(LOGPATH) \ +- -I$(UTILSPATH) -Wall -g ++ -I$(UTILSPATH) -Wall -g -fprofile-arcs -ftest-coverage + + include ../../../makefile.inc + +@@ -46,7 +46,7 @@ CLISRCS=cli.c debugcmd.c + CLIOBJ=$(CLISRCS:.c=.o) + SRCS=$(TESTSRCS) $(READCONSRCS) $(DEBUGCLISRCS) cli.c + OBJ=$(SRCS:.c=.o) +-LIBS=$(UTILSPATH)/skygw_utils.o -lssl -llog_manager ++LIBS=$(UTILSPATH)/skygw_utils.o -lssl -llog_manager -lgcov + MODULES= libdebugcli.so libreadconnroute.so libtestroute.so libcli.so + + +diff --git a/server/modules/routing/readwritesplit/Makefile b/server/modules/routing/readwritesplit/Makefile +index c60f2ff..a3a643e 100644 +--- a/server/modules/routing/readwritesplit/Makefile ++++ b/server/modules/routing/readwritesplit/Makefile +@@ -27,7 +27,7 @@ QCLASSPATH := $(ROOT_PATH)/query_classifier + CC=cc + CFLAGS=-c -fPIC -I/usr/include -I../../include -I../../../include \ + -I$(LOGPATH) -I$(UTILSPATH) -I$(QCLASSPATH) \ +- $(MYSQL_HEADERS) -Wall -g ++ $(MYSQL_HEADERS) -Wall -g -fprofile-arcs -ftest-coverage + + include ../../../../makefile.inc + +@@ -38,7 +38,7 @@ LDFLAGS=-shared -L$(LOGPATH) -L$(QCLASSPATH) -L$(EMBEDDED_LIB) \ + + SRCS=readwritesplit.c + OBJ=$(SRCS:.c=.o) +-LIBS=-lssl -pthread -llog_manager -lquery_classifier -lmysqld ++LIBS=-lssl -pthread -llog_manager -lquery_classifier -lmysqld -lgcov + MODULES=libreadwritesplit.so + + all: $(MODULES) From 3dc09dfe434092e9b239f6058630650280b48ac4 Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Mon, 18 Aug 2014 18:26:14 +0100 Subject: [PATCH 4/8] Addition of spinlock unit test --- server/core/test/makefile | 23 +++++- server/core/test/testspinlock.c | 131 ++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 server/core/test/testspinlock.c diff --git a/server/core/test/makefile b/server/core/test/makefile index d3948575d..5f82b7fef 100644 --- a/server/core/test/makefile +++ b/server/core/test/makefile @@ -10,6 +10,8 @@ include ../../../test.inc CC=cc TESTLOG := $(shell pwd)/testhash.log +TESTS=testhash testspinlock + cleantests: - $(DEL) *.o - $(DEL) testhash @@ -20,11 +22,18 @@ testall: $(MAKE) DEBUG=Y buildtests $(MAKE) runtests -buildtests : +buildtests : $(TESTS) + +testhash: testhash.c $(CC) $(CFLAGS) \ -I$(ROOT_PATH)/server/include \ -I$(ROOT_PATH)/utils \ testhash.c ../hashtable.o ../atomic.o ../spinlock.o -o testhash +testspinlock: testspinlock.c + $(CC) $(CFLAGS) \ + -I$(ROOT_PATH)/server/include \ + -I$(ROOT_PATH)/utils \ + testspinlock.c ../spinlock.o ../atomic.o ../thread.o -o testspinlock runtests: @echo "" > $(TESTLOG) @@ -34,9 +43,15 @@ runtests: @echo "-------------------------------" >> $(TESTLOG) @ -./testhash 2>> $(TESTLOG) ifeq ($?,0) - @echo "MaxScale core PASSED" >> $(TESTLOG) + @echo "MaxScale hashtable PASSED" >> $(TESTLOG) else - @echo "MaxScale core FAILED" >> $(TESTLOG) + @echo "MaxScale hashtable FAILED" >> $(TESTLOG) +endif + @ -./testspinlock 2>> $(TESTLOG) +ifeq ($?,0) + @echo "MaxScale spinlock PASSED" >> $(TESTLOG) +else + @echo "MaxScale spinlock FAILED" >> $(TESTLOG) endif @echo "" >> $(TESTLOG) - @cat $(TESTLOG) >> $(TEST_MAXSCALE_LOG) \ No newline at end of file + @cat $(TESTLOG) >> $(TEST_MAXSCALE_LOG) diff --git a/server/core/test/testspinlock.c b/server/core/test/testspinlock.c new file mode 100644 index 000000000..5d03f0e47 --- /dev/null +++ b/server/core/test/testspinlock.c @@ -0,0 +1,131 @@ +/* + * This file is distributed as part of MaxScale. It 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, + * version 2. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright SkySQL Ab 2014 + */ + +/** + * + * @verbatim + * Revision History + * + * Date Who Description + * 18/08-2014 Mark Riddoch Initial implementation + * + * @endverbatim + */ + +#include +#include +#include + +#include +#include + + +/** + * test1 spinlock_acquire_nowait tests + * + * Test that spinlock_acquire_nowait returns false if the spinlock + * is already taken. + * + * Test that spinlock_acquire_nowait returns true if the spinlock + * is not taken. + * + * Test that spinlock_acquire_nowait does hold the spinlock. + */ +static int +test1() +{ +SPINLOCK lck; + + spinlock_init(&lck); + spinlock_acquire(&lck); + if (spinlock_acquire_nowait(&lck)) + { + fprintf(stderr, "spinlock_acquire_nowait: test 1 failed.\n"); + return 1; + } + spinlock_release(&lck); + if (!spinlock_acquire_nowait(&lck)) + { + fprintf(stderr, "spinlock_acquire_nowait: test 2 failed.\n"); + return 1; + } + if (spinlock_acquire_nowait(&lck)) + { + fprintf(stderr, "spinlock_acquire_nowait: test 3 failed.\n"); + return 1; + } + spinlock_release(&lck); + + return 0; +} + +static int acquire_time; + +static void +test2_helper(void *data) +{ +SPINLOCK *lck = (SPINLOCK *)data; +unsigned long t1 = time(0); + + spinlock_acquire(lck); + acquire_time = time(0) - t1; + spinlock_release(lck); + return; +} + +/** + * Check that spinlock correctly blocks another thread whilst the spinlock + * is held. + * + * Take out a lock. + * Start a second thread to take the same lock + * sleep for 10 seconds + * release lock + * verify that second thread took at least 8 seconds to obtain the lock + */ +static int +test2() +{ +SPINLOCK lck; +void *handle; + + acquire_time = 0; + spinlock_init(&lck); + spinlock_acquire(&lck); + handle = thread_start(test2_helper, (void *)&lck); + sleep(10); + spinlock_release(&lck); + thread_wait(handle); + + if (acquire_time < 8) + { + fprintf(stderr, "spinlock: test 1 failed.\n"); + return 1; + } + return 0; +} + +main(int argc, char **argv) +{ +int result = 0; + + result += test1(); + + exit(result); +} + From d6a2ef699631a5ae5c45d9cdc4ccf593e2deec24 Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Tue, 19 Aug 2014 13:22:40 +0100 Subject: [PATCH 5/8] Misc fixes to doxygen comments --- server/core/buffer.c | 8 +++++--- server/core/dcb.c | 15 ++++++++------- server/core/filter.c | 12 ++++++------ server/core/load_utils.c | 1 + server/core/maxpasswd.c | 2 +- server/core/monitor.c | 5 +++-- server/core/server.c | 3 +-- server/core/utils.c | 2 +- server/include/filter.h | 20 ++++++++++---------- server/modules/filter/qlafilter.c | 5 +++++ server/modules/filter/regexfilter.c | 5 ++++- server/modules/filter/tee.c | 3 +++ server/modules/filter/testfilter.c | 5 ++++- server/modules/filter/topfilter.c | 6 ++++++ 14 files changed, 58 insertions(+), 34 deletions(-) diff --git a/server/core/buffer.c b/server/core/buffer.c index d3278a505..26c2c1439 100644 --- a/server/core/buffer.c +++ b/server/core/buffer.c @@ -298,11 +298,13 @@ int rval = 0; } /** - * Trim bytes form the end of a GWBUF structure + * Trim bytes form the end of a GWBUF structure. If the + * buffer has n_bytes or less then it will be freed and + * NULL will be returned. * * @param buf The buffer to trim - * @param nbytes The number of bytes to trim off - * @return The buffer chain + * @param n_bytes The number of bytes to trim off + * @return The buffer chain or NULL if buffer has <= n_bytes */ GWBUF * gwbuf_trim(GWBUF *buf, unsigned int n_bytes) diff --git a/server/core/dcb.c b/server/core/dcb.c index 1dfde0b58..969702cf4 100644 --- a/server/core/dcb.c +++ b/server/core/dcb.c @@ -1703,14 +1703,15 @@ int gw_write( /** * Add a callback * - * Duplicate registrations are not allowed, therefore an error will be returned if - * the specific function, reason and userdata triple are already registered. + * Duplicate registrations are not allowed, therefore an error will be + * returned if the specific function, reason and userdata triple + * are already registered. * An error will also be returned if the is insufficient memeory available to * create the registration. * * @param dcb The DCB to add the callback to * @param reason The callback reason - * @param cb The callback function to call + * @param callback The callback function to call * @param userdata User data to send in the call * @return Non-zero (true) if the callback was added */ @@ -1766,7 +1767,7 @@ int rval = 1; * * @param dcb The DCB to add the callback to * @param reason The callback reason - * @param cb The callback function to call + * @param callback The callback function to call * @param userdata User data to send in the call * @return Non-zero (true) if the callback was removed */ @@ -1839,7 +1840,7 @@ DCB_CALLBACK *cb, *nextcb; /** * Check the passed DCB to ensure it is in the list of allDCBS * - * @param DCB The DCB to check + * @param dcb The DCB to check * @return 1 if the DCB is in the list, otherwise 0 */ int @@ -1936,8 +1937,8 @@ void dcb_call_foreach ( * Null protocol write routine used for cloned dcb's. It merely consumes * buffers written on the cloned DCB. * - * @params dcb The descriptor control block - * @params buf The buffer beign written + * @param dcb The descriptor control block + * @param buf The buffer being written * @return Always returns a good write operation result */ static int diff --git a/server/core/filter.c b/server/core/filter.c index baeea21d7..405a01470 100644 --- a/server/core/filter.c +++ b/server/core/filter.c @@ -39,8 +39,8 @@ extern int lm_enabled_logfiles_bitmask; -static SPINLOCK filter_spin = SPINLOCK_INIT; -static FILTER_DEF *allFilters = NULL; +static SPINLOCK filter_spin = SPINLOCK_INIT; /**< Protects the list of all filters */ +static FILTER_DEF *allFilters = NULL; /**< The list of all filters */ /** * Allocate a new filter within MaxScale @@ -79,7 +79,7 @@ FILTER_DEF *filter; /** * Deallocate the specified filter * - * @param server The service to deallocate + * @param filter The filter to deallocate * @return Returns true if the server was freed */ void @@ -243,8 +243,8 @@ int i; /** * Add a router option to a service * - * @param service The service to add the router option to - * @param option The option string + * @param filter The filter to add the option to + * @param option The option string */ void filterAddOption(FILTER_DEF *filter, char *option) @@ -273,7 +273,7 @@ int i; /** * Add a router parameter to a service * - * @param service The service to add the router option to + * @param filter The filter to add the parameter to * @param name The parameter name * @param value The parameter value */ diff --git a/server/core/load_utils.c b/server/core/load_utils.c index 93cdf95f6..cba0c6533 100644 --- a/server/core/load_utils.c +++ b/server/core/load_utils.c @@ -281,6 +281,7 @@ MODULES *mod = registered; * @param dlhandle The handle returned by dlopen * @param version The version string returned by the module * @param modobj The module object + * @param mod_info The module information */ static void register_module(const char *module, const char *type, void *dlhandle, char *version, void *modobj, MODULE_INFO *mod_info) diff --git a/server/core/maxpasswd.c b/server/core/maxpasswd.c index ac8b0a042..d11980ba3 100644 --- a/server/core/maxpasswd.c +++ b/server/core/maxpasswd.c @@ -34,7 +34,7 @@ * Encrypt a password for storing in the MaxScale.cnf file * * @param argc Argument count - * @param arv Argument vector + * @param argv Argument vector */ int main(int argc, char **argv) diff --git a/server/core/monitor.c b/server/core/monitor.c index d7b53f5b7..85ff878d7 100644 --- a/server/core/monitor.c +++ b/server/core/monitor.c @@ -207,7 +207,8 @@ MONITOR *ptr; /** * Show a single monitor * - * @param dcb DCB for printing output + * @param dcb DCB for printing output + * @param monitor The monitor to print information regarding */ void monitorShow(DCB *dcb, MONITOR *monitor) @@ -303,7 +304,7 @@ monitorSetInterval (MONITOR *mon, unsigned long interval) * Enable Replication Heartbeat support in monitor. * * @param mon The monitor instance - * @param interval The sampling interval in milliseconds + * @param replication_heartbeat The replication heartbeat */ void monitorSetReplicationHeartbeat(MONITOR *mon, int replication_heartbeat) diff --git a/server/core/server.c b/server/core/server.c index bfe5e4d04..5a75756ed 100644 --- a/server/core/server.c +++ b/server/core/server.c @@ -148,8 +148,7 @@ server_set_unique_name(SERVER *server, char *name) * Find an existing server using the unique section name in * configuration file * - * @param servname The Server name or address - * @param port The server port + * @param name The Server name defined in the header file * @return The server or NULL if not found */ SERVER * diff --git a/server/core/utils.c b/server/core/utils.c index c7e42dda1..1b8c6b5fe 100644 --- a/server/core/utils.c +++ b/server/core/utils.c @@ -204,7 +204,7 @@ void gw_sha1_2_str(const uint8_t *in, int in_len, const uint8_t *in2, int in2_le /** - * @node Gets errno corresponding to latest socket error + * node Gets errno corresponding to latest socket error * * Parameters: * @param fd - in, use diff --git a/server/include/filter.h b/server/include/filter.h index 568df291a..3076587e6 100644 --- a/server/include/filter.h +++ b/server/include/filter.h @@ -61,7 +61,7 @@ typedef struct { * filter pipline * routeQuery Called on each query that requires * routing - * clientReply + * clientReply Called for each reply packet * diagnostics Called to force the filter to print * diagnostic output * @@ -88,21 +88,21 @@ typedef struct filter_object { */ #define FILTER_VERSION {1, 1, 0} /** - * The definition of a filter form the configuration file. + * The definition of a filter from the configuration file. * This is basically the link between a plugin to load and the * optons to pass to that plugin. */ typedef struct filter_def { - char *name; /*< The Filter name */ - char *module; /*< The module to load */ - char **options; /*< The options set for this filter */ + char *name; /**< The Filter name */ + char *module; /**< The module to load */ + char **options; /**< The options set for this filter */ FILTER_PARAMETER - **parameters; /*< The filter parameters */ - FILTER filter; - FILTER_OBJECT *obj; - SPINLOCK spin; + **parameters; /**< The filter parameters */ + FILTER filter; /**< The runtime filter */ + FILTER_OBJECT *obj; /**< The "MODULE_OBJECT" for the filter */ + SPINLOCK spin; /**< Spinlock to protect the filter definition */ struct filter_def - *next; /*< Next filter in the chain of all filters */ + *next; /**< Next filter in the chain of all filters */ } FILTER_DEF; FILTER_DEF *filter_alloc(char *, char *); diff --git a/server/modules/filter/qlafilter.c b/server/modules/filter/qlafilter.c index da78713d2..b3accfbf8 100644 --- a/server/modules/filter/qlafilter.c +++ b/server/modules/filter/qlafilter.c @@ -17,6 +17,9 @@ */ /** + * @file qlafilter.c - Quary Log All Filter + * @verbatim + * * QLA Filter - Query Log All. A primitive query logging filter, simply * used to verify the filter mechanism for downstream filters. All queries * that are passed through the filter will be written to file. @@ -33,6 +36,7 @@ * 11/06/2014 Mark Riddoch Addition of source and match parameters * 19/06/2014 Mark Riddoch Addition of user parameter * + * @endverbatim */ #include #include @@ -154,6 +158,7 @@ GetModuleObject() * within MaxScale. * * @param options The options for this filter + * @param params The array of name/value pair parameters for the filter * * @return The instance data for this new instance */ diff --git a/server/modules/filter/regexfilter.c b/server/modules/filter/regexfilter.c index 5c4f1e7df..79cf70c09 100644 --- a/server/modules/filter/regexfilter.c +++ b/server/modules/filter/regexfilter.c @@ -27,7 +27,8 @@ extern int lm_enabled_logfiles_bitmask; /** - * regexfilter.c - a very simple regular expression rewrite filter. + * @file regexfilter.c - a very simple regular expression rewrite filter. + * @verbatim * * A simple regular expression query rewrite filter. * Two parameters should be defined in the filter configuration @@ -39,6 +40,7 @@ extern int lm_enabled_logfiles_bitmask; * * Date Who Description * 19/06/2014 Mark Riddoch Addition of source and user parameters + * @endverbatim */ MODULE_INFO info = { @@ -132,6 +134,7 @@ GetModuleObject() * within MaxScale. * * @param options The options for this filter + * @param params The array of name/value pair parameters for the filter * * @return The instance data for this new instance */ diff --git a/server/modules/filter/tee.c b/server/modules/filter/tee.c index 34efbb65b..f8e22c7eb 100644 --- a/server/modules/filter/tee.c +++ b/server/modules/filter/tee.c @@ -18,6 +18,7 @@ /** * @file tee.c A filter that splits the processing pipeline in two + * @verbatim * * Conditionally duplicate requests and send the duplicates to another service * within MaxScale. @@ -41,6 +42,7 @@ * 20/06/2014 Mark Riddoch Initial implementation * 24/06/2014 Mark Riddoch Addition of support for multi-packet queries * + * @endverbatim */ #include #include @@ -162,6 +164,7 @@ GetModuleObject() * within MaxScale. * * @param options The options for this filter + * @param params The array of name/value pair parameters for the filter * * @return The instance data for this new instance */ diff --git a/server/modules/filter/testfilter.c b/server/modules/filter/testfilter.c index f72471a36..289ccf4a2 100644 --- a/server/modules/filter/testfilter.c +++ b/server/modules/filter/testfilter.c @@ -21,13 +21,15 @@ #include /** - * testfilter.c - a very simple test filter. + * @file testfilter.c - a very simple test filter. + * @verbatim * * This filter is a very simple example used to test the filter API, * it merely counts the number of statements that flow through the * filter pipeline. * * Reporting is done via the diagnostics print routine. + * @endverbatim */ MODULE_INFO info = { @@ -114,6 +116,7 @@ GetModuleObject() * within MaxScale. * * @param options The options for this filter + * @param params The array of name/value pair parameters for the filter * * @return The instance data for this new instance */ diff --git a/server/modules/filter/topfilter.c b/server/modules/filter/topfilter.c index d4d594e6d..9ca281ef1 100644 --- a/server/modules/filter/topfilter.c +++ b/server/modules/filter/topfilter.c @@ -17,6 +17,9 @@ */ /** + * @file topfilter.c - Top N Longest Running Queries + * @verbatim + * * TOPN Filter - Query Log All. A primitive query logging filter, simply * used to verify the filter mechanism for downstream filters. All queries * that are passed through the filter will be written to file. @@ -30,6 +33,8 @@ * * Date Who Description * 18/06/2014 Mark Riddoch Addition of source and user filters + * + * @endverbatim */ #include #include @@ -172,6 +177,7 @@ GetModuleObject() * within MaxScale. * * @param options The options for this filter + * @param params The array of name/value pair parameters for the filter * * @return The instance data for this new instance */ From 24e16e97ed172909d30a6adf498e2c38dd7384dd Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Wed, 20 Aug 2014 11:07:28 +0100 Subject: [PATCH 6/8] Updates for unit tests --- server/core/test/makefile | 36 +++++--- server/core/test/runtest.sh | 10 +++ server/core/test/testfilter.c | 153 ++++++++++++++++++++++++++++++++ server/core/test/testspinlock.c | 1 + 4 files changed, 186 insertions(+), 14 deletions(-) create mode 100755 server/core/test/runtest.sh create mode 100644 server/core/test/testfilter.c diff --git a/server/core/test/makefile b/server/core/test/makefile index 5f82b7fef..446027948 100644 --- a/server/core/test/makefile +++ b/server/core/test/makefile @@ -10,7 +10,18 @@ include ../../../test.inc CC=cc TESTLOG := $(shell pwd)/testhash.log -TESTS=testhash testspinlock +LOGPATH := $(ROOT_PATH)/log_manager +UTILSPATH := $(ROOT_PATH)/utils + +LDFLAGS=-rdynamic -L$(LOGPATH) \ + -Wl,-rpath,$(DEST)/lib \ + -Wl,-rpath,$(LOGPATH) -Wl,-rpath,$(UTILSPATH) \ + -Wl,-rpath,$(EMBEDDED_LIB) + +LIBS= -lz -lm -lcrypt -lcrypto -ldl -laio -lrt -pthread -llog_manager \ + -L../../inih/extra -linih -lssl -lstdc++ + +TESTS=testhash testspinlock testfilter cleantests: - $(DEL) *.o @@ -34,24 +45,21 @@ testspinlock: testspinlock.c -I$(ROOT_PATH)/server/include \ -I$(ROOT_PATH)/utils \ testspinlock.c ../spinlock.o ../atomic.o ../thread.o -o testspinlock +testfilter: testfilter.c libcore.a + $(CC) $(CFLAGS) $(LDFLAGS) \ + -I$(ROOT_PATH)/server/include \ + -I$(ROOT_PATH)/utils \ + testfilter.c libcore.a $(UTILSPATH)/skygw_utils.o $(LIBS) -o testfilter -runtests: +libcore.a: ../*.o + ar rv libcore.a ../*.o + +runtests: $(TESTS) @echo "" > $(TESTLOG) @echo "-------------------------------" >> $(TESTLOG) @echo $(shell date) >> $(TESTLOG) @echo "Test MaxScale core" >> $(TESTLOG) @echo "-------------------------------" >> $(TESTLOG) - @ -./testhash 2>> $(TESTLOG) -ifeq ($?,0) - @echo "MaxScale hashtable PASSED" >> $(TESTLOG) -else - @echo "MaxScale hashtable FAILED" >> $(TESTLOG) -endif - @ -./testspinlock 2>> $(TESTLOG) -ifeq ($?,0) - @echo "MaxScale spinlock PASSED" >> $(TESTLOG) -else - @echo "MaxScale spinlock FAILED" >> $(TESTLOG) -endif + $(foreach var,$(TESTS),./runtest.sh $(var) $(TESTLOG);) @echo "" >> $(TESTLOG) @cat $(TESTLOG) >> $(TEST_MAXSCALE_LOG) diff --git a/server/core/test/runtest.sh b/server/core/test/runtest.sh new file mode 100755 index 000000000..434d45701 --- /dev/null +++ b/server/core/test/runtest.sh @@ -0,0 +1,10 @@ +#!/bin/bash +test=$1 +log=$2 +echo Running test $test >> $log +./$test 2>> $log +if [ $? -ne 0 ]; then + echo $test " " FAILED >> $log +else + echo $test " " PASSED >> $log +fi diff --git a/server/core/test/testfilter.c b/server/core/test/testfilter.c new file mode 100644 index 000000000..eefc8ecfa --- /dev/null +++ b/server/core/test/testfilter.c @@ -0,0 +1,153 @@ +/* + * This file is distributed as part of MaxScale. It 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, + * version 2. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright SkySQL Ab 2014 + */ + +/** + * + * @verbatim + * Revision History + * + * Date Who Description + * 19-08-2014 Mark Riddoch Initial implementation + * + * @endverbatim + */ + +#include +#include +#include + +#include + + +/** + * test1 Filter creation, finding and deletion + * + */ +static int +test1() +{ +FILTER_DEF *f1, *f2; + + if ((f1 = filter_alloc("test1", "module")) == NULL) + { + fprintf(stderr, "filter_alloc: test 1 failed.\n"); + return 1; + } + if ((f2 = filter_find("test1")) == NULL) + { + fprintf(stderr, "filter_find: test 2 failed.\n"); + return 1; + } + filter_free(f1); + if ((f2 = filter_find("test1")) != NULL) + { + fprintf(stderr, "filter_find: test 3 failed delete.\n"); + return 1; + } + + return 0; +} + + +/** + * Passive tests for filter_add_option and filter_add_parameter + * + * These tests add options and parameters to a filter, the only failure + * is related hard crashes, such as SIGSEGV etc. as there are no good hooks + * to check the creation of parameters and options currently. + */ +static int +test2() +{ +FILTER_DEF *f1; + + if ((f1 = filter_alloc("test1", "module")) == NULL) + { + fprintf(stderr, "filter_alloc: test 1 failed.\n"); + return 1; + } + filterAddOption(f1, "option1"); + filterAddOption(f1, "option2"); + filterAddOption(f1, "option3"); + filterAddParameter(f1, "name1", "value1"); + filterAddParameter(f1, "name2", "value2"); + filterAddParameter(f1, "name3", "value3"); + return 0; +} + + +/** + * test3 Filter creation, finding and deletion soak test + * + */ +static int +test3() +{ +FILTER_DEF *f1; +char name[40]; +int i, n_filters = 1000; + + for (i = 0; i < n_filters; i++) + { + sprintf(name, "filter%d", i); + if ((f1 = filter_alloc(name, "module")) == NULL) + { + fprintf(stderr, + "filter_alloc: test 3 failed with %s.\n", name); + return 1; + } + } + for (i = 0; i < n_filters; i++) + { + sprintf(name, "filter%d", i); + if ((f1 = filter_find(name)) == NULL) + { + fprintf(stderr, "filter_find: test 3 failed.\n"); + return 1; + } + } + for (i = 0; i < n_filters; i++) + { + sprintf(name, "filter%d", i); + if ((f1 = filter_find(name)) == NULL) + { + fprintf(stderr, "filter_find: test 3 failed.\n"); + return 1; + } + filter_free(f1); + if ((f1 = filter_find(name)) != NULL) + { + fprintf(stderr, + "filter_find: test 3 failed - found deleted filter.\n"); + return 1; + } + } + + return 0; +} +main(int argc, char **argv) +{ +int result = 0; + + result += test1(); + result += test2(); + result += test3(); + + exit(result); +} + diff --git a/server/core/test/testspinlock.c b/server/core/test/testspinlock.c index 5d03f0e47..bcbfa2a3f 100644 --- a/server/core/test/testspinlock.c +++ b/server/core/test/testspinlock.c @@ -125,6 +125,7 @@ main(int argc, char **argv) int result = 0; result += test1(); + result += test2(); exit(result); } From 65b25a825ad4449abe044c12133dea57dd52079d Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Wed, 20 Aug 2014 14:50:44 +0100 Subject: [PATCH 7/8] Addition of adminusers unit test Fix to filters unit test --- server/core/test/makefile | 12 +- server/core/test/testadminusers.c | 278 ++++++++++++++++++++++++++++++ server/core/test/testfilter.c | 2 + server/include/adminusers.h | 2 + 4 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 server/core/test/testadminusers.c diff --git a/server/core/test/makefile b/server/core/test/makefile index 446027948..7f6831082 100644 --- a/server/core/test/makefile +++ b/server/core/test/makefile @@ -8,7 +8,7 @@ include ../../../makefile.inc include ../../../test.inc CC=cc -TESTLOG := $(shell pwd)/testhash.log +TESTLOG := $(shell pwd)/testcore.log LOGPATH := $(ROOT_PATH)/log_manager UTILSPATH := $(ROOT_PATH)/utils @@ -21,7 +21,7 @@ LDFLAGS=-rdynamic -L$(LOGPATH) \ LIBS= -lz -lm -lcrypt -lcrypto -ldl -laio -lrt -pthread -llog_manager \ -L../../inih/extra -linih -lssl -lstdc++ -TESTS=testhash testspinlock testfilter +TESTS=testhash testspinlock testfilter testadminusers cleantests: - $(DEL) *.o @@ -40,17 +40,25 @@ testhash: testhash.c -I$(ROOT_PATH)/server/include \ -I$(ROOT_PATH)/utils \ testhash.c ../hashtable.o ../atomic.o ../spinlock.o -o testhash + testspinlock: testspinlock.c $(CC) $(CFLAGS) \ -I$(ROOT_PATH)/server/include \ -I$(ROOT_PATH)/utils \ testspinlock.c ../spinlock.o ../atomic.o ../thread.o -o testspinlock + testfilter: testfilter.c libcore.a $(CC) $(CFLAGS) $(LDFLAGS) \ -I$(ROOT_PATH)/server/include \ -I$(ROOT_PATH)/utils \ testfilter.c libcore.a $(UTILSPATH)/skygw_utils.o $(LIBS) -o testfilter +testadminusers: testadminusers.c libcore.a + $(CC) $(CFLAGS) $(LDFLAGS) \ + -I$(ROOT_PATH)/server/include \ + -I$(ROOT_PATH)/utils \ + testadminusers.c libcore.a $(UTILSPATH)/skygw_utils.o $(LIBS) -o testadminusers + libcore.a: ../*.o ar rv libcore.a ../*.o diff --git a/server/core/test/testadminusers.c b/server/core/test/testadminusers.c new file mode 100644 index 000000000..00ec3a452 --- /dev/null +++ b/server/core/test/testadminusers.c @@ -0,0 +1,278 @@ +/* + * This file is distributed as part of MaxScale. It 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, + * version 2. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright SkySQL Ab 2014 + */ + +/** + * + * @verbatim + * Revision History + * + * Date Who Description + * 20-08-2014 Mark Riddoch Initial implementation + * + * @endverbatim + */ + +#include +#include +#include + +#include + + +/** + * test1 default user + * + * Test that the username password admin/skysql is accepted if no users + * have been created and that no other users are accepted + * + * WARNING: $MAXSCALE_HOME/etc/passwd must be removed before this test is run + */ +static int +test1() +{ + if (admin_verify("admin", "skysql") == 0) + { + fprintf(stderr, "admin_verify: test 1.1 (default user) failed.\n"); + return 1; + } + if (admin_verify("bad", "user")) + { + fprintf(stderr, "admin_verify: test 1.2 (wrong user) failed.\n"); + return 1; + } + + return 0; +} + +/** + * test2 creating users + * + * Create a user + * Try to create a duplicate user - expects a failure + * Remove that user - expected to fail as one user must always remain + */ +static int +test2() +{ +char *err; + + if ((err = admin_add_user("user0", "passwd0")) != NULL) + { + fprintf(stderr, "admin_add_user: test 2.1 (add user) failed, %s.\n", err); + + return 1; + } + if (admin_add_user("user0", "passwd0") == NULL) + { + fprintf(stderr, "admin_add_user: test 2.2 (add user) failed, du;plicate.\n"); + + return 1; + } + + /* Deleting the last user is forbidden so we expect this to fail */ + if ((err = admin_remove_user("user0", "passwd0")) == NULL) + { + fprintf(stderr, "admin_remove_user: test 2.3 (add user) failed, %s.\n", err); + + return 1; + } + return 0; +} + +/** + * test3 search/verify users + * + * Create a user + * Search for that user + * Search for a non-existant user + * Remove the user + * Search for the user that was removed + */ +static int +test3() +{ +char *err; + + if ((err = admin_add_user("user1", "passwd1")) != NULL) + { + fprintf(stderr, "admin_add_user: test 3.1 (add user) failed, %s.\n", err); + + return 1; + } + + if (admin_search_user("user1") == 0) + { + fprintf(stderr, "admin_search_user: test 3.2 (search user) failed.\n"); + + return 1; + } + if (admin_search_user("user2") != 0) + { + fprintf(stderr, "admin_search_user: test 3.3 (search user) failed, unexpeted user found.\n"); + + return 1; + } + + if ((err = admin_remove_user("user1", "passwd1")) != NULL) + { + fprintf(stderr, "admin_remove_user: test 3.4 (add user) failed, %s.\n", err); + + return 1; + } + + if (admin_search_user("user1")) + { + fprintf(stderr, "admin_search_user: test 3.5 (search user) failed - user was deleted.\n"); + + return 1; + } + return 0; +} + +/** + * test4 verify users + * + * Create a numebr of users + * search for each user in turn + * verify each user in turn (password verification) + * Verify each user in turn with incorrect password + * Randomly verify each user + * Remove each user + */ +static int +test4() +{ +char *err, user[40], passwd[40]; +int i, n_users = 50; + + for (i = 1; i < n_users; i++) + { + sprintf(user, "user%d", i); + sprintf(passwd, "passwd%d", i); + if ((err = admin_add_user(user, passwd)) != NULL) + { + fprintf(stderr, "admin_add_user: test 4.1 (add user) failed, %s.\n", err); + + return 1; + } + } + + for (i = 1; i < n_users; i++) + { + sprintf(user, "user%d", i); + if (admin_search_user(user) == 0) + { + fprintf(stderr, "admin_search_user: test 4.2 (search user) failed.\n"); + + return 1; + } + } + for (i = 1; i < n_users; i++) + { + sprintf(user, "user%d", i); + sprintf(passwd, "passwd%d", i); + if (admin_verify(user, passwd) == 0) + { + fprintf(stderr, "admin_verify: test 4.3 (search user) failed.\n"); + + return 1; + } + } + + for (i = 1; i < n_users; i++) + { + sprintf(user, "user%d", i); + sprintf(passwd, "badpasswd%d", i); + if (admin_verify(user, passwd) != 0) + { + fprintf(stderr, "admin_verify: test 4.4 (search user) failed.\n"); + + return 1; + } + } + srand(time(0)); + for (i = 1; i < 1000; i++) + { + int j; + j = rand() % n_users; + if (j == 0) + j = 1; + sprintf(user, "user%d", j); + sprintf(passwd, "passwd%d", j); + if (admin_verify(user, passwd) == 0) + { + fprintf(stderr, "admin_verify: test 4.5 (random) failed.\n"); + + return 1; + } + } + + for (i = 1; i < n_users; i++) + { + sprintf(user, "user%d", i); + sprintf(passwd, "passwd%d", i); + if ((err = admin_remove_user(user, passwd)) != NULL) + { + fprintf(stderr, "admin_remove_user: test 4.6 (add user) failed, %s.\n", err); + + return 1; + } + } + return 0; +} + +/** + * test5 remove first user + * + * Create a user so that user0 may be removed + * Remove the first user created (user0) + */ +static int +test5() +{ +char *err; + + if ((err = admin_add_user("user", "passwd")) != NULL) + { + fprintf(stderr, "admin_add_user: test 5.1 (add user) failed, %s.\n", err); + + return 1; + } + if ((err = admin_remove_user("user0", "passwd0")) != NULL) + { + fprintf(stderr, "admin_remove_user: test 5.2 (add user) failed, %s.\n", err); + + return 1; + } + return 0; +} + +int +main(int argc, char **argv) +{ +int result = 0; + + result += test1(); + result += test2(); + result += test3(); + result += test4(); + result += test5(); + + exit(result); +} + diff --git a/server/core/test/testfilter.c b/server/core/test/testfilter.c index eefc8ecfa..55f7fadf3 100644 --- a/server/core/test/testfilter.c +++ b/server/core/test/testfilter.c @@ -140,6 +140,8 @@ int i, n_filters = 1000; return 0; } + +int main(int argc, char **argv) { int result = 0; diff --git a/server/include/adminusers.h b/server/include/adminusers.h index d46570e38..07afa5390 100644 --- a/server/include/adminusers.h +++ b/server/include/adminusers.h @@ -29,6 +29,8 @@ * * @endverbatim */ +#include + #define ADMIN_SALT "MS" extern int admin_verify(char *, char *); From a853b72baf2e12dcf21c88c724d83025e62484f5 Mon Sep 17 00:00:00 2001 From: counterpoint Date: Wed, 20 Aug 2014 17:22:32 +0100 Subject: [PATCH 8/8] Modify build_gateway.inc so that variables are used, thus avoiding a need for editing. Please review the file to see the variables that are used - they should be obvious. --- build_gateway.inc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/build_gateway.inc b/build_gateway.inc index 83ffaf762..0e1d6b75f 100644 --- a/build_gateway.inc +++ b/build_gateway.inc @@ -12,7 +12,7 @@ # # Set debug flags # -DEBUG := +DEBUG := ${MAXSCALE_DEBUG} # # Set build env @@ -22,7 +22,7 @@ UNIX := Y # # Set MaxScale branch directory # -ROOT_PATH := $(HOME)/src/bazaar/tmp/maxscale +ROOT_PATH := $(HOME)/${MAXSCALE_SOURCE} INC_PATH := $(HOME)/usr/include # @@ -38,7 +38,7 @@ MYSQL_HEADERS := -I$(INC_PATH) -I$(MYSQL_ROOT)/ -I$(MYSQL_ROOT)/private/ -I$(MYS # # Set DYNLIB=Y if you want to link MaxScale with dynamic embedded lib # -DYNLIB := +DYNLIB := ${MAXSCALE_DYNLIB} # # Set path to Embedded MySQL Server @@ -51,3 +51,4 @@ endif # Set path to MySQL errors file # ERRMSG := $(HOME)/usr/share/mysql +