Add tracing session test
authorDavid Goulet <david.goulet@polymtl.ca>
Mon, 25 Jul 2011 17:36:00 +0000 (13:36 -0400)
committerDavid Goulet <david.goulet@polymtl.ca>
Mon, 25 Jul 2011 17:40:10 +0000 (13:40 -0400)
Tests the tracing session functions found in ltt-sessiond/session.h/.c

Creation, destruction and iteration is tested.

Signed-off-by: David Goulet <david.goulet@polymtl.ca>
.gitignore
tests/Makefile.am
tests/runall.sh
tests/test_sessions.c [new file with mode: 0644]

index 3802f628876f3b0501f2fa33e7edb16eac5ac856..7fe9bf16af2eebcceafd416c318283a20baee7b2 100644 (file)
@@ -27,3 +27,5 @@ tags
 ltt-sessiond/ltt-sessiond
 lttng/lttng
 ltt-kconsumerd/ltt-kconsumerd
+
+tests/test_sessions
index 76ed2b545dfe04decc58e919ffc60835c22b1087..761586e4c6bf18d81d28c463c1db666973f6ca00 100644 (file)
@@ -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
index 866f45adf057a3f91144657be0ee3a0336aad448..e7f83ed11e2a89e09bd7b2737a4b9d13d80776dd 100755 (executable)
@@ -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
 # 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 (file)
index 0000000..f3ffe6a
--- /dev/null
@@ -0,0 +1,338 @@
+/*
+ * Copyright (c)  2011 David Goulet <david.goulet@polymtl.ca>
+ *
+ * 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 <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+
+#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;
+}
This page took 0.042725 seconds and 4 git commands to generate.