From: Christian Babeux Date: Thu, 21 Feb 2013 21:29:47 +0000 (-0500) Subject: Tests: Move unit tests to tests/unit X-Git-Tag: v2.2.0-rc1~60^2~28 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=81d029da32ba3c502bdce6ea56b3603eed77e951 Tests: Move unit tests to tests/unit The regression/tools/{test_kernel_data_trace,test_sessions,test_ust_data_trace}.c and regression/tools/streaming/unit_tests.c tests files are really unit tests. In order to have a clear separation between unit tests and integration/system tests, move them to their own folder. Signed-off-by: Christian Babeux --- diff --git a/.gitignore b/.gitignore index aa794ae1f..5dc73aec9 100644 --- a/.gitignore +++ b/.gitignore @@ -49,9 +49,10 @@ extras/bindings/swig/python/lttng_wrap.c .checkpatch.conf # Tests -test_sessions -test_kernel_data_trace -test_ust_data_trace +tests/unit/test_kernel_data +tests/unit/test_session +tests/unit/test_uri +tests/unit/test_ust_data kernel_all_events_basic kernel_event_basic ust_global_event_wildcard diff --git a/configure.ac b/configure.ac index 5cb2e90b4..1809df7eb 100644 --- a/configure.ac +++ b/configure.ac @@ -323,6 +323,7 @@ AC_CONFIG_FILES([ tests/regression/ust/multi-session/Makefile tests/regression/ust/overlap/Makefile tests/regression/ust/overlap/demo/Makefile + tests/unit/Makefile ]) AC_OUTPUT diff --git a/tests/Makefile.am b/tests/Makefile.am index 2a4309e01..d8c85bda2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1 +1 @@ -SUBDIRS = . regression +SUBDIRS = regression unit diff --git a/tests/regression/tools/Makefile.am b/tests/regression/tools/Makefile.am index 40f3cc0c1..ccfbd34ac 100644 --- a/tests/regression/tools/Makefile.am +++ b/tests/regression/tools/Makefile.am @@ -4,38 +4,3 @@ AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(top_srcdir)/tests/ut AM_LDFLAGS = -lurcu EXTRA_DIST = runall.sh - -noinst_PROGRAMS = test_sessions test_kernel_data_trace - -UTILS=$(top_srcdir)/tests/utils/utils.h -SESSIONS=$(top_srcdir)/src/bin/lttng-sessiond/session.c \ - $(top_srcdir)/src/bin/lttng-sessiond/consumer.c \ - $(top_srcdir)/src/common/uri.c \ - $(top_srcdir)/src/common/utils.c \ - $(top_srcdir)/src/common/error.c -KERN_DATA_TRACE=$(top_srcdir)/src/bin/lttng-sessiond/trace-kernel.c \ - $(top_srcdir)/src/bin/lttng-sessiond/consumer.c \ - $(top_srcdir)/src/common/uri.c \ - $(top_srcdir)/src/common/utils.c -COMMON=$(top_builddir)/src/common/libcommon.la -HASHTABLE=$(top_builddir)/src/common/hashtable/libhashtable.la -SESSIOND_COMM=$(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la - -# Tracing sessions unit tests -test_sessions_SOURCES = test_sessions.c $(UTILS) $(SESSIONS) -test_sessions_LDADD = $(COMMON) $(HASHTABLE) $(SESSIOND_COMM) - -# Kernel trace data unit tests -test_kernel_data_trace_SOURCES = test_kernel_data_trace.c $(UTILS) $(KERN_DATA_TRACE) -test_kernel_data_trace_LDADD = $(COMMON) $(SESSIOND_COMM) $(HASHTABLE) - -if HAVE_LIBLTTNG_UST_CTL -noinst_PROGRAMS += test_ust_data_trace -UST_DATA_TRACE=$(top_srcdir)/src/bin/lttng-sessiond/trace-ust.c \ - $(top_srcdir)/src/bin/lttng-sessiond/consumer.c \ - $(top_srcdir)/src/common/uri.c \ - $(top_srcdir)/src/common/utils.c -# UST trace data unit tests -test_ust_data_trace_SOURCES = test_ust_data_trace.c $(UTILS) $(UST_DATA_TRACE) -test_ust_data_trace_LDADD = $(COMMON) $(HASHTABLE) $(SESSIOND_COMM) -endif diff --git a/tests/regression/tools/runall.sh b/tests/regression/tools/runall.sh index b2be91c6c..5d505d9b8 100755 --- a/tests/regression/tools/runall.sh +++ b/tests/regression/tools/runall.sh @@ -2,8 +2,7 @@ DIR=$(dirname $0) -tests=( $DIR/test_kernel_data_trace $DIR/test_sessions $DIR/test_ust_data_trace \ - $DIR/streaming/runall $DIR/health/runall $DIR/filtering/runall) +tests=( $DIR/streaming/runall $DIR/health/runall $DIR/filtering/runall ) exit_code=0 diff --git a/tests/regression/tools/streaming/Makefile.am b/tests/regression/tools/streaming/Makefile.am index 3fab87e9b..ddd092592 100644 --- a/tests/regression/tools/streaming/Makefile.am +++ b/tests/regression/tools/streaming/Makefile.am @@ -8,17 +8,8 @@ if LTTNG_TOOLS_BUILD_WITH_LIBC_DL AM_LDFLAGS += -lc endif -#UTILS=../../utils.h -UTILS= -LIBSESSIOND_COMM=$(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la -LIBCOMMON=$(top_builddir)/src/common/libcommon.la - -noinst_PROGRAMS = unit_tests -unit_tests_SOURCES = unit_tests.c $(UTILS) -unit_tests_LDADD = $(LIBCOMMON) - if HAVE_LIBLTTNG_UST_CTL -noinst_PROGRAMS += gen-ust-events +noinst_PROGRAMS = gen-ust-events gen_ust_events_SOURCES = gen-ust-events.c tp.c tp.h gen_ust_events_LDADD = -llttng-ust endif diff --git a/tests/regression/tools/streaming/unit_tests.c b/tests/regression/tools/streaming/unit_tests.c deleted file mode 100644 index d00e34d16..000000000 --- a/tests/regression/tools/streaming/unit_tests.c +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright (C) - 2012 David Goulet - * - * 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 as - * published by the Free Software Foundation; only version 2 of the License. - * - * 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. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "utils.h" - -/* This path will NEVER be created in this test */ -#define PATH1 "tmp/.test-junk-lttng" - -#define RANDOM_STRING_LEN 16 - -/* For lttngerr.h */ -int lttng_opt_quiet = 1; -int lttng_opt_verbose = 3; - -/* - * Test string URI and if uri_parse works well. - */ -void test_uri_parsing(void) -{ - ssize_t size; - const char *s_uri1; - struct lttng_uri *uri; - - fprintf(stdout, "Testing URIs...\n"); - - s_uri1 = "net://localhost"; - fprintf(stdout, " [+] URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == 2); - assert(uri[0].dtype == LTTNG_DST_IPV4); - assert(uri[0].utype == LTTNG_URI_DST); - assert(uri[0].stype == 0); - assert(uri[0].port == 0); - assert(strlen(uri[0].subdir) == 0); - assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); - - assert(uri[1].dtype == LTTNG_DST_IPV4); - assert(uri[1].utype == LTTNG_URI_DST); - assert(uri[1].stype == 0); - assert(uri[1].port == 0); - assert(strlen(uri[1].subdir) == 0); - assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); - PRINT_OK(); - uri_free(uri); - - s_uri1 = "net://localhost:8989:4242/my/test/path"; - fprintf(stdout, " [+] URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == 2); - assert(uri[0].dtype == LTTNG_DST_IPV4); - assert(uri[0].utype == LTTNG_URI_DST); - assert(uri[0].stype == 0); - assert(uri[0].port == 8989); - assert(strcmp(uri[0].subdir, "my/test/path") == 0); - assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); - - assert(uri[1].dtype == LTTNG_DST_IPV4); - assert(uri[1].utype == LTTNG_URI_DST); - assert(uri[1].stype == 0); - assert(uri[1].port == 4242); - assert(strcmp(uri[0].subdir, "my/test/path") == 0); - assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); - PRINT_OK(); - uri_free(uri); - - s_uri1 = "net://localhost:8989:4242"; - fprintf(stdout, " [+] URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == 2); - assert(uri[0].dtype == LTTNG_DST_IPV4); - assert(uri[0].utype == LTTNG_URI_DST); - assert(uri[0].stype == 0); - assert(uri[0].port == 8989); - assert(strlen(uri[1].subdir) == 0); - assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); - - assert(uri[1].dtype == LTTNG_DST_IPV4); - assert(uri[1].utype == LTTNG_URI_DST); - assert(uri[1].stype == 0); - assert(uri[1].port == 4242); - assert(strlen(uri[1].subdir) == 0); - assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); - PRINT_OK(); - uri_free(uri); - - s_uri1 = "net6://localhost:8989"; - fprintf(stdout, " [+] URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == 2); - assert(uri[0].dtype == LTTNG_DST_IPV6); - assert(uri[0].utype == LTTNG_URI_DST); - assert(uri[0].stype == 0); - assert(uri[0].port == 8989); - assert(strlen(uri[1].subdir) == 0); - assert(strcmp(uri[0].dst.ipv6, "::1") == 0); - - assert(uri[1].dtype == LTTNG_DST_IPV6); - assert(uri[1].utype == LTTNG_URI_DST); - assert(uri[1].stype == 0); - assert(uri[1].port == 0); - assert(strlen(uri[1].subdir) == 0); - assert(strcmp(uri[0].dst.ipv6, "::1") == 0); - PRINT_OK(); - uri_free(uri); - - s_uri1 = "tcp://42.42.42.42/my/test/path"; - fprintf(stdout, " [+] URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == 1); - assert(uri[0].dtype == LTTNG_DST_IPV4); - assert(uri[0].utype == LTTNG_URI_DST); - assert(uri[0].stype == 0); - assert(uri[0].port == 0); - assert(strcmp(uri[0].subdir, "my/test/path") == 0); - assert(strcmp(uri[0].dst.ipv4, "42.42.42.42") == 0); - PRINT_OK(); - uri_free(uri); - - s_uri1 = "tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path"; - fprintf(stdout, " [+] URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == 1); - assert(uri[0].dtype == LTTNG_DST_IPV6); - assert(uri[0].utype == LTTNG_URI_DST); - assert(uri[0].stype == 0); - assert(uri[0].port == 0); - assert(strcmp(uri[0].subdir, "my/test/path") == 0); - assert(strcmp(uri[0].dst.ipv6, "fe80::f66d:4ff:fe53:d220") == 0); - PRINT_OK(); - uri_free(uri); - - s_uri1 = "file:///my/test/path"; - fprintf(stdout, " [+] URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == 1); - assert(uri[0].dtype == LTTNG_DST_PATH); - assert(uri[0].utype == LTTNG_URI_DST); - assert(uri[0].stype == 0); - assert(uri[0].port == 0); - assert(strlen(uri[0].subdir) == 0); - assert(strcmp(uri[0].dst.path, "/my/test/path") == 0); - PRINT_OK(); - uri_free(uri); - - s_uri1 = "file/my/test/path"; - fprintf(stdout, " [+] Bad URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == -1); - PRINT_OK(); - - s_uri1 = "net://:8999"; - fprintf(stdout, " [+] Bad URI set to %s ", s_uri1); - size = uri_parse(s_uri1, &uri); - assert(size == -1); - PRINT_OK(); -} - -void test_uri_cmp() -{ - struct lttng_uri *uri1, *uri2; - const char *s_uri1 = "net://localhost"; - const char *s_uri2 = "net://localhost:8989:4242"; - ssize_t size1, size2; - int res; - - size1 = uri_parse(s_uri1, &uri1); - - /* Sanity checks */ - assert(size1 == 2); - assert(uri1[0].dtype == LTTNG_DST_IPV4); - assert(uri1[0].utype == LTTNG_URI_DST); - assert(uri1[0].stype == 0); - assert(uri1[0].port == 0); - assert(strlen(uri1[0].subdir) == 0); - assert(strcmp(uri1[0].dst.ipv4, "127.0.0.1") == 0); - assert(uri1[1].dtype == LTTNG_DST_IPV4); - assert(uri1[1].utype == LTTNG_URI_DST); - assert(uri1[1].stype == 0); - assert(uri1[1].port == 0); - assert(strlen(uri1[1].subdir) == 0); - assert(strcmp(uri1[1].dst.ipv4, "127.0.0.1") == 0); - - size2 = uri_parse(s_uri2, &uri2); - - assert(size2 == 2); - assert(uri2[0].dtype == LTTNG_DST_IPV4); - assert(uri2[0].utype == LTTNG_URI_DST); - assert(uri2[0].stype == 0); - assert(uri2[0].port == 8989); - assert(strlen(uri2[1].subdir) == 0); - assert(strcmp(uri2[0].dst.ipv4, "127.0.0.1") == 0); - assert(uri2[1].dtype == LTTNG_DST_IPV4); - assert(uri2[1].utype == LTTNG_URI_DST); - assert(uri2[1].stype == 0); - assert(uri2[1].port == 4242); - assert(strlen(uri2[1].subdir) == 0); - assert(strcmp(uri2[1].dst.ipv4, "127.0.0.1") == 0); - - - res = uri_compare(uri1, uri1); - fprintf(stdout, " [+] %s == %s ", s_uri1, s_uri1); - assert(res == 0); - PRINT_OK(); - - res = uri_compare(uri1, uri2); - fprintf(stdout, " [+] %s != %s ", s_uri1, s_uri2); - assert(res != 0); - PRINT_OK(); - - uri_free(uri1); - uri_free(uri2); -} - -int main(int argc, char **argv) -{ - srand(time(NULL)); - - printf("\nStreaming unit tests\n-----------\n"); - - /* URI tests */ - test_uri_parsing(); - test_uri_cmp(); - - return 0; -} diff --git a/tests/regression/tools/test_kernel_data_trace.c b/tests/regression/tools/test_kernel_data_trace.c deleted file mode 100644 index 70d71cadb..000000000 --- a/tests/regression/tools/test_kernel_data_trace.c +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright (c) 2011 David Goulet - * - * 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 - * as published by the Free Software Foundation; only version 2 - * of the License. - * - * 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. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "utils.h" - -/* This path will NEVER be created in this test */ -#define PATH1 "/tmp/.test-junk-lttng" - -#define RANDOM_STRING_LEN 11 - -/* For lttngerr.h */ -int lttng_opt_quiet = 1; -int lttng_opt_verbose; - -static const char alphanum[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - -static struct ltt_kernel_session *kern; -static char random_string[RANDOM_STRING_LEN]; - -/* - * Return random string of 10 characters. - * Not thread-safe. - */ -static char *get_random_string(void) -{ - int i; - - for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { - random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; - } - - random_string[RANDOM_STRING_LEN - 1] = '\0'; - - return random_string; -} - -static void create_one_kernel_session(void) -{ - printf("Create kernel session: "); - kern = trace_kernel_create_session(PATH1); - assert(kern != NULL); - PRINT_OK(); - - printf("Validating kernel session: "); - assert(kern->fd == -1); - assert(kern->metadata_stream_fd == -1); - assert(kern->consumer_fds_sent == 0); - assert(kern->channel_count == 0); - assert(kern->stream_count_global == 0); - assert(kern->metadata == NULL); - PRINT_OK(); - - /* Init list in order to avoid sefaults from cds_list_del */ - trace_kernel_destroy_session(kern); -} - -static void create_kernel_metadata(void) -{ - assert(kern != NULL); - - printf("Create kernel metadata: "); - kern->metadata = trace_kernel_create_metadata(); - assert(kern->metadata != NULL); - PRINT_OK(); - - printf("Validating kernel session metadata: "); - assert(kern->metadata->fd == -1); - assert(kern->metadata->conf != NULL); - assert(kern->metadata->conf->attr.overwrite - == DEFAULT_CHANNEL_OVERWRITE); - assert(kern->metadata->conf->attr.subbuf_size - == default_get_metadata_subbuf_size()); - assert(kern->metadata->conf->attr.num_subbuf - == DEFAULT_METADATA_SUBBUF_NUM); - assert(kern->metadata->conf->attr.switch_timer_interval - == DEFAULT_CHANNEL_SWITCH_TIMER); - assert(kern->metadata->conf->attr.read_timer_interval - == DEFAULT_CHANNEL_READ_TIMER); - assert(kern->metadata->conf->attr.output - == DEFAULT_KERNEL_CHANNEL_OUTPUT); - PRINT_OK(); - - trace_kernel_destroy_metadata(kern->metadata); -} - -static void create_kernel_channel(void) -{ - struct ltt_kernel_channel *chan; - struct lttng_channel attr; - - memset(&attr, 0, sizeof(attr)); - - printf("Creating kernel channel: "); - chan = trace_kernel_create_channel(&attr); - assert(chan != NULL); - PRINT_OK(); - - printf("Validating kernel channel: "); - assert(chan->fd == -1); - assert(chan->enabled == 1); - assert(chan->stream_count == 0); - assert(chan->ctx == NULL); - assert(chan->channel->attr.overwrite == attr.attr.overwrite); - PRINT_OK(); - - /* Init list in order to avoid sefaults from cds_list_del */ - CDS_INIT_LIST_HEAD(&chan->list); - trace_kernel_destroy_channel(chan); -} - -static void create_kernel_event(void) -{ - struct ltt_kernel_event *event; - struct lttng_event ev; - - memset(&ev, 0, sizeof(ev)); - strncpy(ev.name, get_random_string(), LTTNG_KERNEL_SYM_NAME_LEN); - ev.type = LTTNG_EVENT_TRACEPOINT; - ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; - - printf("Creating kernel event: "); - event = trace_kernel_create_event(&ev); - assert(event != NULL); - PRINT_OK(); - - printf("Validating kernel event: "); - assert(event->fd == -1); - assert(event->enabled == 1); - assert(event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT); - assert(strlen(event->event->name)); - PRINT_OK(); - - /* Init list in order to avoid sefaults from cds_list_del */ - CDS_INIT_LIST_HEAD(&event->list); - trace_kernel_destroy_event(event); -} - -static void create_kernel_stream(void) -{ - struct ltt_kernel_stream *stream; - - printf("Creating kernel stream: "); - stream = trace_kernel_create_stream("stream1", 0); - assert(stream != NULL); - PRINT_OK(); - - printf("Validating kernel stream: "); - assert(stream->fd == -1); - assert(stream->state == 0); - PRINT_OK(); - - /* Init list in order to avoid sefaults from cds_list_del */ - CDS_INIT_LIST_HEAD(&stream->list); - trace_kernel_destroy_stream(stream); -} - -int main(int argc, char **argv) -{ - printf("\nTesting kernel data structures:\n-----------\n"); - - create_one_kernel_session(); - - create_kernel_metadata(); - create_kernel_channel(); - - - create_kernel_event(); - - create_kernel_stream(); - - /* Success */ - return 0; -} diff --git a/tests/regression/tools/test_sessions.c b/tests/regression/tools/test_sessions.c deleted file mode 100644 index a6d76c73d..000000000 --- a/tests/regression/tools/test_sessions.c +++ /dev/null @@ -1,330 +0,0 @@ -/* - * Copyright (c) 2011 David Goulet - * - * 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 - * as published by the Free Software Foundation; only version 2 - * of the License. - * - * 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. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "utils.h" - -#define SESSION1 "test1" - -/* This path will NEVER be created in this test */ -#define PATH1 "/tmp/.test-junk-lttng" - -#define MAX_SESSIONS 10000 -#define RANDOM_STRING_LEN 11 - -/* - * String of 263 caracters. NAME_MAX + "OVERFLOW". If OVERFLOW appears in the - * session name, we have a problem. - * - * NAME_MAX = 255 - */ -#define OVERFLOW_SESSION_NAME \ - "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ - "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ - "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ - "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc" \ - "OVERFLOW" - -static struct ltt_session_list *session_list; - -/* For lttngerr.h */ -int lttng_opt_quiet = 1; -int lttng_opt_verbose = 0; - -static const char alphanum[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; -static char random_string[RANDOM_STRING_LEN]; - -/* - * Return random string of 10 characters. - * Not thread-safe. - */ -static char *get_random_string(void) -{ - int i; - - for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { - random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; - } - - random_string[RANDOM_STRING_LEN - 1] = '\0'; - - return random_string; -} - -/* - * Return 0 if session name is found, else -1 - */ -static int find_session_name(char *name) -{ - struct ltt_session *iter; - - cds_list_for_each_entry(iter, &session_list->head, list) { - if (strcmp(iter->name, name) == 0) { - return 0; - } - } - - return -1; -} - -static int session_list_count(void) -{ - int count = 0; - struct ltt_session *iter; - - cds_list_for_each_entry(iter, &session_list->head, list) { - count++; - } - return count; -} - -/* - * Empty session list manually. - */ -static void empty_session_list(void) -{ - struct ltt_session *iter, *tmp; - - cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { - cds_list_del(&iter->list); - free(iter); - } - - /* Session list must be 0 */ - assert(!session_list_count()); -} - -/* - * Test creation of 1 session - */ -static int create_one_session(char *name, char *path) -{ - int ret; - - ret = session_create(name, path, geteuid(), getegid()); - if (ret == LTTNG_OK) { - /* Validate */ - ret = find_session_name(name); - if (ret < 0) { - /* Session not found by name */ - printf("session not found after creation\n"); - return -1; - } else { - /* Success */ - return 0; - } - } else { - if (ret == LTTNG_ERR_EXIST_SESS) { - printf("(session already exists) "); - } - return -1; - } - - return 0; -} - -/* - * Test deletion of 1 session - */ -static int destroy_one_session(struct ltt_session *session) -{ - int ret; - - ret = session_destroy(session); - - if (ret == LTTNG_OK) { - /* Validate */ - if (session == NULL) { - return 0; - } - ret = find_session_name(session->name); - if (ret < 0) { - /* Success, -1 means that the sesion is NOT found */ - return 0; - } else { - /* Fail */ - return -1; - } - } - - return 0; -} - -static int fuzzing_create_args(void) -{ - int ret; - - ret = create_one_session(NULL, NULL); - if (ret > 0) { - printf("Session created with (null),(null)\n"); - return -1; - } - - ret = create_one_session(NULL, PATH1); - if (ret > 0) { - printf("Session created with (null), %s)\n", PATH1); - return -1; - } - - /* Session list must be 0 */ - assert(!session_list_count()); - - return 0; -} - -/* - * This test is supposed to fail at the second create call. If so, return 0 for - * test success, else -1. - */ -static int two_session_same_name(void) -{ - int ret; - struct ltt_session *sess; - - ret = create_one_session(SESSION1, PATH1); - if (ret < 0) { - /* Fail */ - return -1; - } - - sess = session_find_by_name(SESSION1); - if (sess) { - /* Success */ - return 0; - } - - /* Fail */ - return -1; -} - -int main(int argc, char **argv) -{ - int ret, i; - struct ltt_session *iter, *tmp; - - srand(time(NULL)); - - printf("\nTesting Sessions:\n-----------\n"); - - session_list = session_get_list(); - if (session_list == NULL) { - return -1; - } - - printf("Create 1 session %s: ", SESSION1); - fflush(stdout); - ret = create_one_session(SESSION1, PATH1); - if (ret < 0) { - return -1; - } - PRINT_OK(); - - printf("Validating created session %s: ", SESSION1); - fflush(stdout); - tmp = session_find_by_name(SESSION1); - if (tmp == NULL) { - return -1; - } - /* Basic init session values */ - assert(tmp->kernel_session == NULL); - assert(strlen(tmp->path)); - assert(strlen(tmp->name)); - session_lock(tmp); - session_unlock(tmp); - - PRINT_OK(); - - printf("Destroy 1 session %s: ", SESSION1); - fflush(stdout); - ret = destroy_one_session(tmp); - if (ret < 0) { - return -1; - } - PRINT_OK(); - - printf("Two session with same name: "); - fflush(stdout); - ret = two_session_same_name(); - if (ret < 0) { - return -1; - } - PRINT_OK(); - - empty_session_list(); - - printf("Fuzzing create_session arguments: "); - fflush(stdout); - ret = fuzzing_create_args(); - if (ret < 0) { - return -1; - } - PRINT_OK(); - - printf("Creating %d sessions: ", MAX_SESSIONS); - fflush(stdout); - for (i = 0; i < MAX_SESSIONS; i++) { - char *tmp_name = get_random_string(); - - ret = create_one_session(tmp_name, PATH1); - if (ret < 0) { - printf("session %d (name: %s) creation failed\n", i, tmp_name); - return -1; - } - - if ((i % 1000) == 0) { - fprintf(stdout, "%d..", i); - fflush(stdout); - } - } - PRINT_OK(); - - printf("Destroying %d sessions: ", MAX_SESSIONS); - fflush(stdout); - for (i = 0; i < MAX_SESSIONS; i++) { - cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { - ret = destroy_one_session(iter); - if (ret < 0) { - printf("session %d (name: %s) creation failed\n", i, iter->name); - return -1; - } - } - } - PRINT_OK(); - - /* Session list must be 0 */ - assert(!session_list_count()); - - /* Success */ - return 0; -} diff --git a/tests/regression/tools/test_ust_data_trace.c b/tests/regression/tools/test_ust_data_trace.c deleted file mode 100644 index 74f282bbb..000000000 --- a/tests/regression/tools/test_ust_data_trace.c +++ /dev/null @@ -1,204 +0,0 @@ -/* - * Copyright (c) 2011 David Goulet - * - * 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 - * as published by the Free Software Foundation; only version 2 - * of the License. - * - * 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. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "utils.h" - -/* This path will NEVER be created in this test */ -#define PATH1 "/tmp/.test-junk-lttng" - -#define RANDOM_STRING_LEN 11 - -/* For lttngerr.h */ -int lttng_opt_quiet = 1; -int lttng_opt_verbose; - -static const char alphanum[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; -static char random_string[RANDOM_STRING_LEN]; - -static struct ltt_ust_session *usess; -static struct lttng_domain dom; - -/* - * Return random string of 10 characters. - * Not thread-safe. - */ -static char *get_random_string(void) -{ - int i; - - for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { - random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; - } - - random_string[RANDOM_STRING_LEN - 1] = '\0'; - - return random_string; -} - -static void create_one_ust_session(void) -{ - printf("Create UST session: "); - - dom.type = LTTNG_DOMAIN_UST; - - usess = trace_ust_create_session(PATH1, 42); - assert(usess != NULL); - PRINT_OK(); - - printf("Validating UST session: "); - assert(usess->id == 42); - assert(usess->start_trace == 0); - assert(usess->domain_global.channels != NULL); - assert(usess->domain_pid != NULL); - assert(usess->domain_exec != NULL); - assert(usess->uid == 0); - assert(usess->gid == 0); - PRINT_OK(); - - trace_ust_destroy_session(usess); -} - -static void create_ust_metadata(void) -{ - struct ltt_ust_metadata *metadata; - - assert(usess != NULL); - - printf("Create UST metadata: "); - metadata = trace_ust_create_metadata(PATH1); - assert(metadata != NULL); - PRINT_OK(); - - printf("Validating UST session metadata: "); - assert(metadata->handle == -1); - assert(strlen(metadata->pathname)); - assert(metadata->attr.overwrite - == DEFAULT_CHANNEL_OVERWRITE); - assert(metadata->attr.subbuf_size - == default_get_metadata_subbuf_size()); - assert(metadata->attr.num_subbuf - == DEFAULT_METADATA_SUBBUF_NUM); - assert(metadata->attr.switch_timer_interval - == DEFAULT_CHANNEL_SWITCH_TIMER); - assert(metadata->attr.read_timer_interval - == DEFAULT_CHANNEL_READ_TIMER); - assert(metadata->attr.output == LTTNG_UST_MMAP); - PRINT_OK(); - - trace_ust_destroy_metadata(metadata); -} - -static void create_ust_channel(void) -{ - struct ltt_ust_channel *uchan; - struct lttng_channel attr; - - memset(&attr, 0, sizeof(attr)); - - strncpy(attr.name, "channel0", 8); - - printf("Creating UST channel: "); - uchan = trace_ust_create_channel(&attr, PATH1); - assert(uchan != NULL); - PRINT_OK(); - - printf("Validating UST channel: "); - assert(uchan->enabled == 0); - assert(strcmp(PATH1, uchan->pathname) == 0); - assert(strncmp(uchan->name, "channel0", 8) == 0); - assert(uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0'); - assert(uchan->ctx != NULL); - assert(uchan->events != NULL); - assert(uchan->attr.overwrite == attr.attr.overwrite); - PRINT_OK(); - - trace_ust_destroy_channel(uchan); -} - -static void create_ust_event(void) -{ - struct ltt_ust_event *event; - struct lttng_event ev; - - memset(&ev, 0, sizeof(ev)); - strncpy(ev.name, get_random_string(), LTTNG_SYMBOL_NAME_LEN); - ev.type = LTTNG_EVENT_TRACEPOINT; - ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; - - printf("Creating UST event: "); - event = trace_ust_create_event(&ev, NULL); - assert(event != NULL); - PRINT_OK(); - - printf("Validating UST event: "); - assert(event->enabled == 0); - assert(event->attr.instrumentation == LTTNG_UST_TRACEPOINT); - assert(strcmp(event->attr.name, ev.name) == 0); - assert(event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0'); - PRINT_OK(); - - trace_ust_destroy_event(event); -} - -static void create_ust_context(void) -{ - struct lttng_event_context ectx; - struct ltt_ust_context *uctx; - - ectx.ctx = LTTNG_EVENT_CONTEXT_VTID; - - printf("Creating UST context: "); - uctx = trace_ust_create_context(&ectx); - assert(uctx != NULL); - PRINT_OK(); - - printf("Validating UST context: "); - assert((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID); - PRINT_OK(); -} - -int main(int argc, char **argv) -{ - printf("\nTesting UST data structures:\n-----------\n"); - - create_one_ust_session(); - create_ust_metadata(); - create_ust_channel(); - create_ust_event(); - create_ust_context(); - - /* Success */ - return 0; -} diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am new file mode 100644 index 000000000..a277feaac --- /dev/null +++ b/tests/unit/Makefile.am @@ -0,0 +1,55 @@ +AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(top_srcdir)/tests/utils/ -I$(srcdir) +AM_LDFLAGS = + +if LTTNG_TOOLS_BUILD_WITH_LIBDL +AM_LDFLAGS += -ldl +endif +if LTTNG_TOOLS_BUILD_WITH_LIBC_DL +AM_LDFLAGS += -lc +endif + +LIBCOMMON=$(top_builddir)/src/common/libcommon.la +LIBSESSIOND_COMM=$(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la +LIBHASHTABLE=$(top_builddir)/src/common/hashtable/libhashtable.la + +EXTRA_DIST = run.sh + +if HAVE_LIBLTTNG_UST_CTL +noinst_PROGRAMS = test_uri test_session test_ust_data test_kernel_data +else +noinst_PROGRAMS = test_uri test_session test_kernel_data +endif + +# URI unit tests +test_uri_SOURCES = test_uri.c +test_uri_LDADD = $(LIBCOMMON) + +# Session unit test +SESSIONS=$(top_srcdir)/src/bin/lttng-sessiond/session.c \ + $(top_srcdir)/src/bin/lttng-sessiond/consumer.c \ + $(top_srcdir)/src/common/uri.c \ + $(top_srcdir)/src/common/utils.c \ + $(top_srcdir)/src/common/error.c + +test_session_SOURCES = test_session.c $(SESSIONS) +test_session_LDADD = $(LIBCOMMON) $(LIBSESSIOND_COMM) $(LIBHASHTABLE) + +# UST data structures unit test +if HAVE_LIBLTTNG_UST_CTL +UST_DATA_TRACE=$(top_srcdir)/src/bin/lttng-sessiond/trace-ust.c \ + $(top_srcdir)/src/bin/lttng-sessiond/consumer.c \ + $(top_srcdir)/src/common/uri.c \ + $(top_srcdir)/src/common/utils.c + +test_ust_data_SOURCES = test_ust_data.c $(UST_DATA_TRACE) +test_ust_data_LDADD = $(LIBCOMMON) $(LIBSESSIOND_COMM) $(LIBHASHTABLE) +endif + +# Kernel data structures unit test +KERN_DATA_TRACE=$(top_srcdir)/src/bin/lttng-sessiond/trace-kernel.c \ + $(top_srcdir)/src/bin/lttng-sessiond/consumer.c \ + $(top_srcdir)/src/common/uri.c \ + $(top_srcdir)/src/common/utils.c + +test_kernel_data_SOURCES = test_kernel_data.c $(KERN_DATA_TRACE) +test_kernel_data_LDADD = $(LIBCOMMON) $(LIBSESSIOND_COMM) $(LIBHASHTABLE) diff --git a/tests/unit/test_kernel_data.c b/tests/unit/test_kernel_data.c new file mode 100644 index 000000000..70d71cadb --- /dev/null +++ b/tests/unit/test_kernel_data.c @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2011 David Goulet + * + * 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 + * as published by the Free Software Foundation; only version 2 + * of the License. + * + * 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "utils.h" + +/* This path will NEVER be created in this test */ +#define PATH1 "/tmp/.test-junk-lttng" + +#define RANDOM_STRING_LEN 11 + +/* For lttngerr.h */ +int lttng_opt_quiet = 1; +int lttng_opt_verbose; + +static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + +static struct ltt_kernel_session *kern; +static char random_string[RANDOM_STRING_LEN]; + +/* + * Return random string of 10 characters. + * Not thread-safe. + */ +static char *get_random_string(void) +{ + int i; + + for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { + random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + random_string[RANDOM_STRING_LEN - 1] = '\0'; + + return random_string; +} + +static void create_one_kernel_session(void) +{ + printf("Create kernel session: "); + kern = trace_kernel_create_session(PATH1); + assert(kern != NULL); + PRINT_OK(); + + printf("Validating kernel session: "); + assert(kern->fd == -1); + assert(kern->metadata_stream_fd == -1); + assert(kern->consumer_fds_sent == 0); + assert(kern->channel_count == 0); + assert(kern->stream_count_global == 0); + assert(kern->metadata == NULL); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + trace_kernel_destroy_session(kern); +} + +static void create_kernel_metadata(void) +{ + assert(kern != NULL); + + printf("Create kernel metadata: "); + kern->metadata = trace_kernel_create_metadata(); + assert(kern->metadata != NULL); + PRINT_OK(); + + printf("Validating kernel session metadata: "); + assert(kern->metadata->fd == -1); + assert(kern->metadata->conf != NULL); + assert(kern->metadata->conf->attr.overwrite + == DEFAULT_CHANNEL_OVERWRITE); + assert(kern->metadata->conf->attr.subbuf_size + == default_get_metadata_subbuf_size()); + assert(kern->metadata->conf->attr.num_subbuf + == DEFAULT_METADATA_SUBBUF_NUM); + assert(kern->metadata->conf->attr.switch_timer_interval + == DEFAULT_CHANNEL_SWITCH_TIMER); + assert(kern->metadata->conf->attr.read_timer_interval + == DEFAULT_CHANNEL_READ_TIMER); + assert(kern->metadata->conf->attr.output + == DEFAULT_KERNEL_CHANNEL_OUTPUT); + PRINT_OK(); + + trace_kernel_destroy_metadata(kern->metadata); +} + +static void create_kernel_channel(void) +{ + struct ltt_kernel_channel *chan; + struct lttng_channel attr; + + memset(&attr, 0, sizeof(attr)); + + printf("Creating kernel channel: "); + chan = trace_kernel_create_channel(&attr); + assert(chan != NULL); + PRINT_OK(); + + printf("Validating kernel channel: "); + assert(chan->fd == -1); + assert(chan->enabled == 1); + assert(chan->stream_count == 0); + assert(chan->ctx == NULL); + assert(chan->channel->attr.overwrite == attr.attr.overwrite); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + CDS_INIT_LIST_HEAD(&chan->list); + trace_kernel_destroy_channel(chan); +} + +static void create_kernel_event(void) +{ + struct ltt_kernel_event *event; + struct lttng_event ev; + + memset(&ev, 0, sizeof(ev)); + strncpy(ev.name, get_random_string(), LTTNG_KERNEL_SYM_NAME_LEN); + ev.type = LTTNG_EVENT_TRACEPOINT; + ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; + + printf("Creating kernel event: "); + event = trace_kernel_create_event(&ev); + assert(event != NULL); + PRINT_OK(); + + printf("Validating kernel event: "); + assert(event->fd == -1); + assert(event->enabled == 1); + assert(event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT); + assert(strlen(event->event->name)); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + CDS_INIT_LIST_HEAD(&event->list); + trace_kernel_destroy_event(event); +} + +static void create_kernel_stream(void) +{ + struct ltt_kernel_stream *stream; + + printf("Creating kernel stream: "); + stream = trace_kernel_create_stream("stream1", 0); + assert(stream != NULL); + PRINT_OK(); + + printf("Validating kernel stream: "); + assert(stream->fd == -1); + assert(stream->state == 0); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + CDS_INIT_LIST_HEAD(&stream->list); + trace_kernel_destroy_stream(stream); +} + +int main(int argc, char **argv) +{ + printf("\nTesting kernel data structures:\n-----------\n"); + + create_one_kernel_session(); + + create_kernel_metadata(); + create_kernel_channel(); + + + create_kernel_event(); + + create_kernel_stream(); + + /* Success */ + return 0; +} diff --git a/tests/unit/test_session.c b/tests/unit/test_session.c new file mode 100644 index 000000000..a6d76c73d --- /dev/null +++ b/tests/unit/test_session.c @@ -0,0 +1,330 @@ +/* + * Copyright (c) 2011 David Goulet + * + * 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 + * as published by the Free Software Foundation; only version 2 + * of the License. + * + * 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "utils.h" + +#define SESSION1 "test1" + +/* This path will NEVER be created in this test */ +#define PATH1 "/tmp/.test-junk-lttng" + +#define MAX_SESSIONS 10000 +#define RANDOM_STRING_LEN 11 + +/* + * String of 263 caracters. NAME_MAX + "OVERFLOW". If OVERFLOW appears in the + * session name, we have a problem. + * + * NAME_MAX = 255 + */ +#define OVERFLOW_SESSION_NAME \ + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc" \ + "OVERFLOW" + +static struct ltt_session_list *session_list; + +/* For lttngerr.h */ +int lttng_opt_quiet = 1; +int lttng_opt_verbose = 0; + +static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; +static char random_string[RANDOM_STRING_LEN]; + +/* + * Return random string of 10 characters. + * Not thread-safe. + */ +static char *get_random_string(void) +{ + int i; + + for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { + random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + random_string[RANDOM_STRING_LEN - 1] = '\0'; + + return random_string; +} + +/* + * Return 0 if session name is found, else -1 + */ +static int find_session_name(char *name) +{ + struct ltt_session *iter; + + cds_list_for_each_entry(iter, &session_list->head, list) { + if (strcmp(iter->name, name) == 0) { + return 0; + } + } + + return -1; +} + +static int session_list_count(void) +{ + int count = 0; + struct ltt_session *iter; + + cds_list_for_each_entry(iter, &session_list->head, list) { + count++; + } + return count; +} + +/* + * Empty session list manually. + */ +static void empty_session_list(void) +{ + struct ltt_session *iter, *tmp; + + cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { + cds_list_del(&iter->list); + free(iter); + } + + /* Session list must be 0 */ + assert(!session_list_count()); +} + +/* + * Test creation of 1 session + */ +static int create_one_session(char *name, char *path) +{ + int ret; + + ret = session_create(name, path, geteuid(), getegid()); + if (ret == LTTNG_OK) { + /* Validate */ + ret = find_session_name(name); + if (ret < 0) { + /* Session not found by name */ + printf("session not found after creation\n"); + return -1; + } else { + /* Success */ + return 0; + } + } else { + if (ret == LTTNG_ERR_EXIST_SESS) { + printf("(session already exists) "); + } + return -1; + } + + return 0; +} + +/* + * Test deletion of 1 session + */ +static int destroy_one_session(struct ltt_session *session) +{ + int ret; + + ret = session_destroy(session); + + if (ret == LTTNG_OK) { + /* Validate */ + if (session == NULL) { + return 0; + } + ret = find_session_name(session->name); + if (ret < 0) { + /* Success, -1 means that the sesion is NOT found */ + return 0; + } else { + /* Fail */ + return -1; + } + } + + return 0; +} + +static int fuzzing_create_args(void) +{ + int ret; + + ret = create_one_session(NULL, NULL); + if (ret > 0) { + printf("Session created with (null),(null)\n"); + return -1; + } + + ret = create_one_session(NULL, PATH1); + if (ret > 0) { + printf("Session created with (null), %s)\n", PATH1); + return -1; + } + + /* Session list must be 0 */ + assert(!session_list_count()); + + return 0; +} + +/* + * This test is supposed to fail at the second create call. If so, return 0 for + * test success, else -1. + */ +static int two_session_same_name(void) +{ + int ret; + struct ltt_session *sess; + + ret = create_one_session(SESSION1, PATH1); + if (ret < 0) { + /* Fail */ + return -1; + } + + sess = session_find_by_name(SESSION1); + if (sess) { + /* Success */ + return 0; + } + + /* Fail */ + return -1; +} + +int main(int argc, char **argv) +{ + int ret, i; + struct ltt_session *iter, *tmp; + + srand(time(NULL)); + + printf("\nTesting Sessions:\n-----------\n"); + + session_list = session_get_list(); + if (session_list == NULL) { + return -1; + } + + printf("Create 1 session %s: ", SESSION1); + fflush(stdout); + ret = create_one_session(SESSION1, PATH1); + if (ret < 0) { + return -1; + } + PRINT_OK(); + + printf("Validating created session %s: ", SESSION1); + fflush(stdout); + tmp = session_find_by_name(SESSION1); + if (tmp == NULL) { + return -1; + } + /* Basic init session values */ + assert(tmp->kernel_session == NULL); + assert(strlen(tmp->path)); + assert(strlen(tmp->name)); + session_lock(tmp); + session_unlock(tmp); + + PRINT_OK(); + + printf("Destroy 1 session %s: ", SESSION1); + fflush(stdout); + ret = destroy_one_session(tmp); + if (ret < 0) { + return -1; + } + PRINT_OK(); + + printf("Two session with same name: "); + fflush(stdout); + ret = two_session_same_name(); + if (ret < 0) { + return -1; + } + PRINT_OK(); + + empty_session_list(); + + printf("Fuzzing create_session arguments: "); + fflush(stdout); + ret = fuzzing_create_args(); + if (ret < 0) { + return -1; + } + PRINT_OK(); + + printf("Creating %d sessions: ", MAX_SESSIONS); + fflush(stdout); + for (i = 0; i < MAX_SESSIONS; i++) { + char *tmp_name = get_random_string(); + + ret = create_one_session(tmp_name, PATH1); + if (ret < 0) { + printf("session %d (name: %s) creation failed\n", i, tmp_name); + return -1; + } + + if ((i % 1000) == 0) { + fprintf(stdout, "%d..", i); + fflush(stdout); + } + } + PRINT_OK(); + + printf("Destroying %d sessions: ", MAX_SESSIONS); + fflush(stdout); + for (i = 0; i < MAX_SESSIONS; i++) { + cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { + ret = destroy_one_session(iter); + if (ret < 0) { + printf("session %d (name: %s) creation failed\n", i, iter->name); + return -1; + } + } + } + PRINT_OK(); + + /* Session list must be 0 */ + assert(!session_list_count()); + + /* Success */ + return 0; +} diff --git a/tests/unit/test_uri.c b/tests/unit/test_uri.c new file mode 100644 index 000000000..d00e34d16 --- /dev/null +++ b/tests/unit/test_uri.c @@ -0,0 +1,252 @@ +/* + * Copyright (C) - 2012 David Goulet + * + * 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 as + * published by the Free Software Foundation; only version 2 of the License. + * + * 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "utils.h" + +/* This path will NEVER be created in this test */ +#define PATH1 "tmp/.test-junk-lttng" + +#define RANDOM_STRING_LEN 16 + +/* For lttngerr.h */ +int lttng_opt_quiet = 1; +int lttng_opt_verbose = 3; + +/* + * Test string URI and if uri_parse works well. + */ +void test_uri_parsing(void) +{ + ssize_t size; + const char *s_uri1; + struct lttng_uri *uri; + + fprintf(stdout, "Testing URIs...\n"); + + s_uri1 = "net://localhost"; + fprintf(stdout, " [+] URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == 2); + assert(uri[0].dtype == LTTNG_DST_IPV4); + assert(uri[0].utype == LTTNG_URI_DST); + assert(uri[0].stype == 0); + assert(uri[0].port == 0); + assert(strlen(uri[0].subdir) == 0); + assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); + + assert(uri[1].dtype == LTTNG_DST_IPV4); + assert(uri[1].utype == LTTNG_URI_DST); + assert(uri[1].stype == 0); + assert(uri[1].port == 0); + assert(strlen(uri[1].subdir) == 0); + assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); + PRINT_OK(); + uri_free(uri); + + s_uri1 = "net://localhost:8989:4242/my/test/path"; + fprintf(stdout, " [+] URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == 2); + assert(uri[0].dtype == LTTNG_DST_IPV4); + assert(uri[0].utype == LTTNG_URI_DST); + assert(uri[0].stype == 0); + assert(uri[0].port == 8989); + assert(strcmp(uri[0].subdir, "my/test/path") == 0); + assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); + + assert(uri[1].dtype == LTTNG_DST_IPV4); + assert(uri[1].utype == LTTNG_URI_DST); + assert(uri[1].stype == 0); + assert(uri[1].port == 4242); + assert(strcmp(uri[0].subdir, "my/test/path") == 0); + assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); + PRINT_OK(); + uri_free(uri); + + s_uri1 = "net://localhost:8989:4242"; + fprintf(stdout, " [+] URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == 2); + assert(uri[0].dtype == LTTNG_DST_IPV4); + assert(uri[0].utype == LTTNG_URI_DST); + assert(uri[0].stype == 0); + assert(uri[0].port == 8989); + assert(strlen(uri[1].subdir) == 0); + assert(strcmp(uri[0].dst.ipv4, "127.0.0.1") == 0); + + assert(uri[1].dtype == LTTNG_DST_IPV4); + assert(uri[1].utype == LTTNG_URI_DST); + assert(uri[1].stype == 0); + assert(uri[1].port == 4242); + assert(strlen(uri[1].subdir) == 0); + assert(strcmp(uri[1].dst.ipv4, "127.0.0.1") == 0); + PRINT_OK(); + uri_free(uri); + + s_uri1 = "net6://localhost:8989"; + fprintf(stdout, " [+] URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == 2); + assert(uri[0].dtype == LTTNG_DST_IPV6); + assert(uri[0].utype == LTTNG_URI_DST); + assert(uri[0].stype == 0); + assert(uri[0].port == 8989); + assert(strlen(uri[1].subdir) == 0); + assert(strcmp(uri[0].dst.ipv6, "::1") == 0); + + assert(uri[1].dtype == LTTNG_DST_IPV6); + assert(uri[1].utype == LTTNG_URI_DST); + assert(uri[1].stype == 0); + assert(uri[1].port == 0); + assert(strlen(uri[1].subdir) == 0); + assert(strcmp(uri[0].dst.ipv6, "::1") == 0); + PRINT_OK(); + uri_free(uri); + + s_uri1 = "tcp://42.42.42.42/my/test/path"; + fprintf(stdout, " [+] URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == 1); + assert(uri[0].dtype == LTTNG_DST_IPV4); + assert(uri[0].utype == LTTNG_URI_DST); + assert(uri[0].stype == 0); + assert(uri[0].port == 0); + assert(strcmp(uri[0].subdir, "my/test/path") == 0); + assert(strcmp(uri[0].dst.ipv4, "42.42.42.42") == 0); + PRINT_OK(); + uri_free(uri); + + s_uri1 = "tcp6://[fe80::f66d:4ff:fe53:d220]/my/test/path"; + fprintf(stdout, " [+] URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == 1); + assert(uri[0].dtype == LTTNG_DST_IPV6); + assert(uri[0].utype == LTTNG_URI_DST); + assert(uri[0].stype == 0); + assert(uri[0].port == 0); + assert(strcmp(uri[0].subdir, "my/test/path") == 0); + assert(strcmp(uri[0].dst.ipv6, "fe80::f66d:4ff:fe53:d220") == 0); + PRINT_OK(); + uri_free(uri); + + s_uri1 = "file:///my/test/path"; + fprintf(stdout, " [+] URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == 1); + assert(uri[0].dtype == LTTNG_DST_PATH); + assert(uri[0].utype == LTTNG_URI_DST); + assert(uri[0].stype == 0); + assert(uri[0].port == 0); + assert(strlen(uri[0].subdir) == 0); + assert(strcmp(uri[0].dst.path, "/my/test/path") == 0); + PRINT_OK(); + uri_free(uri); + + s_uri1 = "file/my/test/path"; + fprintf(stdout, " [+] Bad URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == -1); + PRINT_OK(); + + s_uri1 = "net://:8999"; + fprintf(stdout, " [+] Bad URI set to %s ", s_uri1); + size = uri_parse(s_uri1, &uri); + assert(size == -1); + PRINT_OK(); +} + +void test_uri_cmp() +{ + struct lttng_uri *uri1, *uri2; + const char *s_uri1 = "net://localhost"; + const char *s_uri2 = "net://localhost:8989:4242"; + ssize_t size1, size2; + int res; + + size1 = uri_parse(s_uri1, &uri1); + + /* Sanity checks */ + assert(size1 == 2); + assert(uri1[0].dtype == LTTNG_DST_IPV4); + assert(uri1[0].utype == LTTNG_URI_DST); + assert(uri1[0].stype == 0); + assert(uri1[0].port == 0); + assert(strlen(uri1[0].subdir) == 0); + assert(strcmp(uri1[0].dst.ipv4, "127.0.0.1") == 0); + assert(uri1[1].dtype == LTTNG_DST_IPV4); + assert(uri1[1].utype == LTTNG_URI_DST); + assert(uri1[1].stype == 0); + assert(uri1[1].port == 0); + assert(strlen(uri1[1].subdir) == 0); + assert(strcmp(uri1[1].dst.ipv4, "127.0.0.1") == 0); + + size2 = uri_parse(s_uri2, &uri2); + + assert(size2 == 2); + assert(uri2[0].dtype == LTTNG_DST_IPV4); + assert(uri2[0].utype == LTTNG_URI_DST); + assert(uri2[0].stype == 0); + assert(uri2[0].port == 8989); + assert(strlen(uri2[1].subdir) == 0); + assert(strcmp(uri2[0].dst.ipv4, "127.0.0.1") == 0); + assert(uri2[1].dtype == LTTNG_DST_IPV4); + assert(uri2[1].utype == LTTNG_URI_DST); + assert(uri2[1].stype == 0); + assert(uri2[1].port == 4242); + assert(strlen(uri2[1].subdir) == 0); + assert(strcmp(uri2[1].dst.ipv4, "127.0.0.1") == 0); + + + res = uri_compare(uri1, uri1); + fprintf(stdout, " [+] %s == %s ", s_uri1, s_uri1); + assert(res == 0); + PRINT_OK(); + + res = uri_compare(uri1, uri2); + fprintf(stdout, " [+] %s != %s ", s_uri1, s_uri2); + assert(res != 0); + PRINT_OK(); + + uri_free(uri1); + uri_free(uri2); +} + +int main(int argc, char **argv) +{ + srand(time(NULL)); + + printf("\nStreaming unit tests\n-----------\n"); + + /* URI tests */ + test_uri_parsing(); + test_uri_cmp(); + + return 0; +} diff --git a/tests/unit/test_ust_data.c b/tests/unit/test_ust_data.c new file mode 100644 index 000000000..74f282bbb --- /dev/null +++ b/tests/unit/test_ust_data.c @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2011 David Goulet + * + * 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 + * as published by the Free Software Foundation; only version 2 + * of the License. + * + * 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "utils.h" + +/* This path will NEVER be created in this test */ +#define PATH1 "/tmp/.test-junk-lttng" + +#define RANDOM_STRING_LEN 11 + +/* For lttngerr.h */ +int lttng_opt_quiet = 1; +int lttng_opt_verbose; + +static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; +static char random_string[RANDOM_STRING_LEN]; + +static struct ltt_ust_session *usess; +static struct lttng_domain dom; + +/* + * Return random string of 10 characters. + * Not thread-safe. + */ +static char *get_random_string(void) +{ + int i; + + for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { + random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + random_string[RANDOM_STRING_LEN - 1] = '\0'; + + return random_string; +} + +static void create_one_ust_session(void) +{ + printf("Create UST session: "); + + dom.type = LTTNG_DOMAIN_UST; + + usess = trace_ust_create_session(PATH1, 42); + assert(usess != NULL); + PRINT_OK(); + + printf("Validating UST session: "); + assert(usess->id == 42); + assert(usess->start_trace == 0); + assert(usess->domain_global.channels != NULL); + assert(usess->domain_pid != NULL); + assert(usess->domain_exec != NULL); + assert(usess->uid == 0); + assert(usess->gid == 0); + PRINT_OK(); + + trace_ust_destroy_session(usess); +} + +static void create_ust_metadata(void) +{ + struct ltt_ust_metadata *metadata; + + assert(usess != NULL); + + printf("Create UST metadata: "); + metadata = trace_ust_create_metadata(PATH1); + assert(metadata != NULL); + PRINT_OK(); + + printf("Validating UST session metadata: "); + assert(metadata->handle == -1); + assert(strlen(metadata->pathname)); + assert(metadata->attr.overwrite + == DEFAULT_CHANNEL_OVERWRITE); + assert(metadata->attr.subbuf_size + == default_get_metadata_subbuf_size()); + assert(metadata->attr.num_subbuf + == DEFAULT_METADATA_SUBBUF_NUM); + assert(metadata->attr.switch_timer_interval + == DEFAULT_CHANNEL_SWITCH_TIMER); + assert(metadata->attr.read_timer_interval + == DEFAULT_CHANNEL_READ_TIMER); + assert(metadata->attr.output == LTTNG_UST_MMAP); + PRINT_OK(); + + trace_ust_destroy_metadata(metadata); +} + +static void create_ust_channel(void) +{ + struct ltt_ust_channel *uchan; + struct lttng_channel attr; + + memset(&attr, 0, sizeof(attr)); + + strncpy(attr.name, "channel0", 8); + + printf("Creating UST channel: "); + uchan = trace_ust_create_channel(&attr, PATH1); + assert(uchan != NULL); + PRINT_OK(); + + printf("Validating UST channel: "); + assert(uchan->enabled == 0); + assert(strcmp(PATH1, uchan->pathname) == 0); + assert(strncmp(uchan->name, "channel0", 8) == 0); + assert(uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0'); + assert(uchan->ctx != NULL); + assert(uchan->events != NULL); + assert(uchan->attr.overwrite == attr.attr.overwrite); + PRINT_OK(); + + trace_ust_destroy_channel(uchan); +} + +static void create_ust_event(void) +{ + struct ltt_ust_event *event; + struct lttng_event ev; + + memset(&ev, 0, sizeof(ev)); + strncpy(ev.name, get_random_string(), LTTNG_SYMBOL_NAME_LEN); + ev.type = LTTNG_EVENT_TRACEPOINT; + ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; + + printf("Creating UST event: "); + event = trace_ust_create_event(&ev, NULL); + assert(event != NULL); + PRINT_OK(); + + printf("Validating UST event: "); + assert(event->enabled == 0); + assert(event->attr.instrumentation == LTTNG_UST_TRACEPOINT); + assert(strcmp(event->attr.name, ev.name) == 0); + assert(event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0'); + PRINT_OK(); + + trace_ust_destroy_event(event); +} + +static void create_ust_context(void) +{ + struct lttng_event_context ectx; + struct ltt_ust_context *uctx; + + ectx.ctx = LTTNG_EVENT_CONTEXT_VTID; + + printf("Creating UST context: "); + uctx = trace_ust_create_context(&ectx); + assert(uctx != NULL); + PRINT_OK(); + + printf("Validating UST context: "); + assert((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID); + PRINT_OK(); +} + +int main(int argc, char **argv) +{ + printf("\nTesting UST data structures:\n-----------\n"); + + create_one_ust_session(); + create_ust_metadata(); + create_ust_channel(); + create_ust_event(); + create_ust_context(); + + /* Success */ + return 0; +}