From 63371d1e4ff3148287a04e14b468c4234e3d0182 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Mon, 25 Jul 2011 13:36:00 -0400 Subject: [PATCH] Add tracing session test Tests the tracing session functions found in ltt-sessiond/session.h/.c Creation, destruction and iteration is tested. Signed-off-by: David Goulet --- .gitignore | 2 + tests/Makefile.am | 12 +- tests/runall.sh | 39 +---- tests/test_sessions.c | 338 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 358 insertions(+), 33 deletions(-) create mode 100644 tests/test_sessions.c diff --git a/.gitignore b/.gitignore index 3802f6288..7fe9bf16a 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ tags ltt-sessiond/ltt-sessiond lttng/lttng ltt-kconsumerd/ltt-kconsumerd + +tests/test_sessions diff --git a/tests/Makefile.am b/tests/Makefile.am index 76ed2b545..761586e4c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,3 +1,11 @@ -SUBDIRS = . +AM_CFLAGS=-I$(top_srcdir)/include -I$(top_srcdir)/libkernelctl \ + -I$(top_srcdir)/liblttngctl -g -Wall -dist_noinst_SCRIPTS = runall.sh +noinst_PROGRAMS = test_sessions + +SESSIONS=$(top_srcdir)/ltt-sessiond/session.c + +test_sessions_SOURCES = test_sessions.c $(SESSIONS) + +check-am: + ./runall.sh diff --git a/tests/runall.sh b/tests/runall.sh index 866f45adf..e7f83ed11 100755 --- a/tests/runall.sh +++ b/tests/runall.sh @@ -4,8 +4,8 @@ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. +# 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 @@ -17,37 +17,14 @@ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # -TEST_DIR=$(dirname $0) - -failed=0 -num_test=1 - -function run() { - printf "%d) Running test $@\n" $num_test - echo "==================================" - - # Running test - ./$@ - if [ $? -ne 0 ]; then - let failed=$failed+1 - printf "\nTest $@ FAILED\n\n" - else - printf "\nTest $@ PASSED\n\n" - fi - - let num_test=$num_test+1 -} - #### ADD TESTS HERE #### -#### END TESTS HERE #### +for bin in test_sessions; +do + ./$bin +done -echo "--------------------------" -if [ $failed -eq 0 ]; then - echo "All passed!" -else - echo "$failed tests failed" -fi -echo "--------------------------" +#### END TESTS HERE #### +echo "" exit 0 diff --git a/tests/test_sessions.c b/tests/test_sessions.c new file mode 100644 index 000000000..f3ffe6a43 --- /dev/null +++ b/tests/test_sessions.c @@ -0,0 +1,338 @@ +/* + * 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 "ltt-sessiond/session.h" + +#define SESSION1 "test1" + +/* This path will NEVER be created in this test */ +#define PATH1 "/tmp/.test-junk-lttng" + +#define MAX_SESSIONS 10000 + +/* + * 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 opt_quiet = 1; +int opt_verbose = 0; + +static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + +/* + * Return random string of 10 characters. + */ +static char *get_random_string(void) +{ + int i; + char *str = malloc(11); + + for (i = 0; i < 10; i++) { + str[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + str[10] = '\0'; + + return str; +} + +/* + * 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; +} + +/* + * 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); + session_list->count--; + 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 = create_session(name, path); + if (ret >= 0) { + /* 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 < 0) { + if (ret == -EEXIST) { + printf("(session already exists) "); + } + return -1; + } + + return 0; +} + +/* + * Test deletion of 1 session + */ +static int destroy_one_session(char *name) +{ + int ret; + + ret = destroy_session(name); + if (ret == 1) { + /* Validate */ + ret = find_session_name(name); + if (ret < 0) { + /* Success, -1 means that the sesion is NOT found */ + return 0; + } else { + /* Fail */ + return -1; + } + } else if (ret < 0) { + if (ret == -EEXIST) { + printf("(session already exists) "); + } + 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; + } + + ret = create_one_session(SESSION1, NULL); + if (ret >= 0) { + printf("Session created with %s, (null)\n", SESSION1); + return -1; + } + + /* Session list must be 0 */ + assert(!session_list->count); + + return 0; +} + +static int fuzzing_destroy_args(void) +{ + int ret; + + ret = destroy_one_session(NULL); + if (ret >= 0) { + printf("Session destroyed with (null)\n"); + return -1; + } + + ret = destroy_one_session(OVERFLOW_SESSION_NAME); + if (ret >= 0) { + printf("Session destroyed with %s\n", OVERFLOW_SESSION_NAME); + 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; + + ret = create_one_session(SESSION1, PATH1); + if (ret < 0) { + /* Fail */ + return -1; + } + + ret = create_one_session(SESSION1, PATH1); + if (ret < 0) { + /* Success */ + return 0; + } + + /* Fail */ + return -1; +} + +int main(int argc, char **argv) +{ + int ret, i; + char *tmp_name; + struct ltt_session *iter, *tmp; + + srand(time(NULL)); + + printf("\nTesting Sessions:\n-----------\n"); + + session_list = get_session_list(); + if (session_list == NULL) { + return -1; + } + + printf("Create 1 session %s: ", SESSION1); + ret = create_one_session(SESSION1, PATH1); + if (ret < 0) { + return -1; + } + printf("Success\n"); + + printf("Validating created session %s: ", SESSION1); + tmp = find_session_by_name(SESSION1); + if (tmp == NULL) { + return -1; + } + /* Basic init session values */ + assert(tmp->kernel_session == NULL); + assert(tmp->ust_trace_count == 0); + assert(strlen(tmp->path)); + assert(strlen(tmp->name)); + lock_session(tmp); + unlock_session(tmp); + + printf("Success\n"); + + printf("Destroy 1 session %s: ", SESSION1); + ret = destroy_one_session(SESSION1); + if (ret < 0) { + return -1; + } + printf("Success\n"); + + printf("Two session with same name: "); + ret = two_session_same_name(); + if (ret < 0) { + return -1; + } + printf("Success\n"); + + empty_session_list(); + + printf("Fuzzing create_session arguments: "); + ret = fuzzing_create_args(); + if (ret < 0) { + return -1; + } + printf("Success\n"); + + printf("Fuzzing destroy_session argument: "); + ret = fuzzing_destroy_args(); + if (ret < 0) { + return -1; + } + printf("Success\n"); + + printf("Creating %d sessions: ", MAX_SESSIONS); + for (i = 0; i < MAX_SESSIONS; i++) { + 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; + } + free(tmp_name); + } + printf("Success\n"); + + printf("Destroying %d sessions: ", MAX_SESSIONS); + for (i = 0; i < MAX_SESSIONS; i++) { + cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { + ret = destroy_one_session(iter->name); + if (ret < 0) { + printf("session %d (name: %s) creation failed\n", i, iter->name); + return -1; + } + } + } + printf("Success\n"); + + /* Session list must be 0 */ + assert(!session_list->count); + + /* Success */ + return 0; +} -- 2.34.1