[evolution-ews/wip/tests: 1/2] Add tests



commit 65e003ab90ae8640d54e864f8e9520ecab71e6a9
Author: Fabiano Fidêncio <fidencio redhat com>
Date:   Fri Sep 6 11:04:52 2013 +0200

    Add tests
    
    -FIXME:
    - compare support
    - clean up
    - run using valgrind
    
    -README:
    to write traces, please run:
    ./camel --write-traces \
        --config number=2 \ /* in how many servers you're running the tests */
        --config hostname=your_hostname0 \
        --config server-uri=your_server_uri0 \
        --config username=your_username0 \
        --config password=your_password0 \
        --config hostname=your_hostname1 \
        --config server-uri=your_server_uri1 \
        --config username=your_username1 \
        --config password=your_password1

 configure.ac             |   28 +++
 gtester.mk               |   60 ++++++
 src/Makefile.am          |   24 ++-
 src/server/e-ews-debug.c |   26 +++
 src/server/e-ews-debug.h |    4 +
 src/tests/Makefile.am    |   36 ++++
 src/tests/camel.c        |  516 ++++++++++++++++++++++++++++++++++++++++++++++
 src/tests/common.c       |  303 +++++++++++++++++++++++++++
 src/tests/common.h       |   28 +++
 9 files changed, 1023 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index b4482b3..fe110b7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -284,6 +284,32 @@ LIBS=$save_libs
 AC_MSG_RESULT([$ac_cv_have_smi])
 
 dnl ****************************
+dnl Check for uhttpmock
+dnl ****************************
+AC_ARG_ENABLE([tests],
+       [AS_HELP_STRING([--enable-tests],
+       [Enable low level tests for EwsConnection @<:@default=no@:>@])],
+       [enable_tests="$enableval"], [enable_tests=no])
+
+if test "x$enable_tests" = "xyes" ; then
+       PKG_CHECK_MODULES(UHTTPMOCK, [libuhttpmock-0.0], have_uhttpmock=yes,
+                         have_uhttpmock=no)
+       AC_SUBST(UHTTPMOCK_CFLAGS)
+       AC_SUBST(UHTTPMOCK_LIBS)
+
+       if test "x$have_uhttpmock" = "xyes" ; then
+               AC_DEFINE(ENABLE_TESTS, 1, [Define to add support for low level tests])
+       else
+               AC_ERROR([
+
+               libuhttpmock >= 0.0 not found.
+
+               ])
+       fi
+fi
+AM_CONDITIONAL([ENABLE_TESTS], [test "x$have_uhttpmock" = "xyes"])
+
+dnl ****************************
 dnl Expose version information
 dnl ****************************
 API_VERSION=1.2
@@ -352,6 +378,7 @@ src/calendar/Makefile
 src/camel/Makefile
 src/collection/Makefile
 src/configuration/Makefile
+src/tests/Makefile
 po/Makefile.in
 ])
 AC_OUTPUT
@@ -360,5 +387,6 @@ AC_MSG_NOTICE([
        Kerberos 5      : $msg_krb5
        Strict building : $enable_strict
        Use libmspack   : $have_mspack
+       Enable tests    : $enable_tests
 ])
 
diff --git a/gtester.mk b/gtester.mk
new file mode 100644
index 0000000..1306193
--- /dev/null
+++ b/gtester.mk
@@ -0,0 +1,60 @@
+# GLIB - Library of useful C routines
+# From http://git.gnome.org/browse/glib/tree/Makefile.decl
+
+GTESTER = gtester
+GTESTER_REPORT = gtester-report
+
+# initialize variables for unconditional += appending
+EXTRA_DIST =
+TEST_PROGS =
+
+### testing rules
+
+# test: run all tests in cwd and subdirs
+test:  ${TEST_PROGS}
+       @test -z "${TEST_PROGS}" || ${GTESTER} --verbose ${TEST_PROGS}
+       @ for subdir in $(SUBDIRS) . ; do \
+           test "$$subdir" = "." -o "$$subdir" = "po" || \
+           ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \
+         done
+
+# test-report: run tests in subdirs and generate report
+# perf-report: run tests in subdirs with -m perf and generate report
+# full-report: like test-report: with -m perf and -m slow
+test-report perf-report full-report:   ${TEST_PROGS}
+       @test -z "${TEST_PROGS}" || { \
+         case $@ in \
+         test-report) test_options="-k";; \
+         perf-report) test_options="-k -m=perf";; \
+         full-report) test_options="-k -m=perf -m=slow";; \
+         esac ; \
+         if test -z "$$GTESTER_LOGDIR" ; then  \
+           ${GTESTER} --verbose $$test_options -o test-report.xml ${TEST_PROGS} ; \
+         elif test -n "${TEST_PROGS}" ; then \
+           ${GTESTER} --verbose $$test_options -o `mktemp "$$GTESTER_LOGDIR/log-XXXXXX"` ${TEST_PROGS} ; \
+         fi ; \
+       }
+       @ ignore_logdir=true ; \
+         if test -z "$$GTESTER_LOGDIR" ; then \
+           GTESTER_LOGDIR=`mktemp -d "\`pwd\`/.testlogs-XXXXXX"`; export GTESTER_LOGDIR ; \
+           ignore_logdir=false ; \
+         fi ; \
+         for subdir in $(SUBDIRS) . ; do \
+           test "$$subdir" = "." -o "$$subdir" = "po" || \
+           ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \
+         done ; \
+         $$ignore_logdir || { \
+           echo '<?xml version="1.0"?>' > $  xml ; \
+           echo '<report-collection>'  >> $  xml ; \
+           for lf in `ls -L "$$GTESTER_LOGDIR"/.` ; do \
+             sed '1,1s/^<?xml\b[^>?]*?>//' <"$$GTESTER_LOGDIR"/"$$lf" >> $  xml ; \
+           done ; \
+           echo >> $  xml ; \
+           echo '</report-collection>' >> $  xml ; \
+           rm -rf "$$GTESTER_LOGDIR"/ ; \
+           ${GTESTER_REPORT} --version 2>/dev/null 1>&2 ; test "$$?" != 0 || ${GTESTER_REPORT} $  xml >$  
html ; \
+         }
+.PHONY: test test-report perf-report full-report
+# run make test as part of make check
+check-local: test
+
diff --git a/src/Makefile.am b/src/Makefile.am
index 7a1e4e3..8fcc15e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,4 +1,24 @@
-SUBDIRS = server utils camel calendar addressbook collection configuration
-DIST_SUBDIRS = server utils camel calendar addressbook collection configuration
+if ENABLE_TESTS
+TESTS_DIR = tests
+endif
+
+SUBDIRS = \
+       server \
+       utils \
+       camel \
+       calendar \
+       addressbook \
+       collection \
+       configuration \
+       $(TESTS_DIR)
+
+DIST_SUBDIRS = \
+       server \
+       utils \
+       camel \
+       calendar \
+       addressbook \
+       collection \
+       configuration
 
 -include $(top_srcdir)/git.mk
diff --git a/src/server/e-ews-debug.c b/src/server/e-ews-debug.c
index 5d6a62e..8a443e9 100644
--- a/src/server/e-ews-debug.c
+++ b/src/server/e-ews-debug.c
@@ -21,6 +21,7 @@
 #endif
 
 #include "e-ews-debug.h"
+#include "e-ews-message.h"
 
 gint
 e_ews_debug_get_log_level (void)
@@ -40,3 +41,28 @@ e_ews_debug_get_log_level (void)
 
        return level;
 }
+
+const gchar *
+e_ews_connection_get_server_version_string (EEwsConnection *cnc)
+{
+       switch (e_ews_connection_get_server_version(cnc)) {
+               case E_EWS_EXCHANGE_2007:
+                       return "2007";
+               case E_EWS_EXCHANGE_2007_SP1:
+                       return "2007_SP1";
+               case E_EWS_EXCHANGE_2010:
+                       return "2010";
+               case E_EWS_EXCHANGE_2010_SP1:
+                       return "2010_SP1";
+               case E_EWS_EXCHANGE_2010_SP2:
+               case E_EWS_EXCHANGE_FUTURE:
+                       return "2010_SP2";
+               case E_EWS_EXCHANGE_UNKNOWN:
+                       return "Unknown";
+               default:
+                       g_assert_not_reached ();
+                       return NULL;
+       }
+}
+
+
diff --git a/src/server/e-ews-debug.h b/src/server/e-ews-debug.h
index 95f1f09..b7782a8 100644
--- a/src/server/e-ews-debug.h
+++ b/src/server/e-ews-debug.h
@@ -20,10 +20,14 @@
 #define E_EWS_DEBUG_H
 
 #include <glib.h>
+#include <server/e-ews-connection.h>
 
 G_BEGIN_DECLS
 
 gint           e_ews_debug_get_log_level               (void);
+const gchar *  e_ews_connection_get_server_version_string
+                                                       (EEwsConnection *cnc);
+
 
 G_END_DECLS
 
diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am
new file mode 100644
index 0000000..13260c0
--- /dev/null
+++ b/src/tests/Makefile.am
@@ -0,0 +1,36 @@
+include $(top_srcdir)/gtester.mk
+
+AM_CPPFLAGS = \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/src \
+       -I$(top_builddir) \
+       -I$(top_builddir)/src \
+       -DTEST_FILE_DIR="\"$(top_srcdir)/src/tests/\"" \
+       -DG_LOG_DOMAIN=\"libeews\" \
+       -DLOCALEDIR="\"$(datadir)/locale\"" \
+       $(SOUP_CFLAGS) \
+       $(LIBECAL_CFLAGS) \
+       $(LIBICAL_CFLAGS) \
+       $(LIBEDATASERVER_CFLAGS) \
+       $(UHTTPMOCK_CFLAGS) \
+       $(NULL)
+
+
+noinst_PROGRAMS = $(TEST_PROGS)
+
+LIBS = \
+       $(top_builddir)/src/server/libeews-1.2.la \
+       $(top_builddir)/src/utils/libewsutils.la \
+       $(UHTTPMOCK_LIBS)
+       $(NULL)
+
+TEST_SRCS = \
+       common.c \
+       common.h\
+       e-ews-connection.h \
+       $(NULL)
+
+TEST_PROGS                     += camel
+camel_SOURCES                   = camel.c $(TEST_SRCS)
+
+-include $(top_srcdir)/git.mk
diff --git a/src/tests/camel.c b/src/tests/camel.c
new file mode 100644
index 0000000..bac7d95
--- /dev/null
+++ b/src/tests/camel.c
@@ -0,0 +1,516 @@
+#include "common.h"
+#include <server/e-ews-connection.h>
+#include <server/e-ews-debug.h>
+#include <server/e-ews-folder.h>
+
+static UhmServer *server = NULL;
+
+static EwsFolderId *distinguished_folder_subfolder_id = NULL;
+static EwsFolderId *non_distinguished_folder_subfolder_id = NULL;
+
+static const gchar *invalid_folder_id =
+"AAMkADQyYzVlYmU0LWNhNTUtNDNkYy04ZGYxLTk5ZTk5ZGY4NmJlMwAuAAAAAAB9G7pDgpwKQKc31aq6C3GTAQAi6qqMgDmPQrslhHoZnZkhAABOpnTeSTE=";
+static const gchar *invalid_change_key = "AQAAABYAAAAi6qqMgDmPQrslhHoZnZkhAAB7est3";
+
+static void
+server_notify_resolver_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
+{
+       UhmServer *server;
+       UhmResolver *resolver;
+       const gchar *hostname;
+
+       server = UHM_SERVER (object);
+       hostname = user_data;
+
+       /* Set up the expected domain names here. This should technically be split up between
+        * the different unit test suites, but that's too much effort. */
+       resolver = uhm_server_get_resolver (server);
+
+       if (resolver != NULL) {
+               const gchar *ip_address = uhm_server_get_address (server);
+
+               uhm_resolver_add_A (resolver, hostname, ip_address);
+       }
+}
+
+static gboolean
+is_folder_on_server (EEwsConnection *connection,
+                    EwsFolderId *fid,
+                    gboolean should_have)
+{
+       gboolean found = !should_have;
+       GSList *folders_ids = NULL;
+       GSList *folders = NULL, *f;
+       GError *error = NULL;
+
+       folders_ids = g_slist_prepend (folders_ids, fid);
+       e_ews_connection_get_folder_sync (
+               connection, EWS_PRIORITY_MEDIUM, "IdOnly",
+               NULL, folders_ids, &folders,
+               NULL, &error);
+
+       if (error != NULL) {
+               if (g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_ITEMNOTFOUND))
+                       goto exit;
+
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       for (f = folders; f != NULL; f = f->next) {
+               EEwsFolder *folder = f->data;
+               const EwsFolderId *result_fid;
+
+               if (e_ews_folder_is_error (folder)) {
+                       const GError *folder_error;
+
+                       found = FALSE;
+
+                       folder_error = e_ews_folder_get_error (folder);
+                       if (g_error_matches (folder_error, EWS_CONNECTION_ERROR, 
EWS_CONNECTION_ERROR_ITEMNOTFOUND))
+                               break;
+                       else
+                               continue;
+               }
+
+               result_fid = e_ews_folder_get_id (folder);
+               if (e_ews_folder_id_is_equal (fid, result_fid)) {
+                       found = TRUE;
+                       break;
+               }
+       }
+
+ exit:
+       g_slist_free_full (folders, g_object_unref);
+       g_slist_free (folders_ids);
+       g_clear_error (&error);
+       return found;
+}
+
+static void
+test_create_existing_folder (gconstpointer user_data)
+{
+       gboolean retval;
+       gboolean folder_exists;
+       GError *error = NULL;
+       EwsFolderId *fid;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "create_existing_folder", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_create_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               "msgfolderroot", TRUE,
+               "inbox", E_EWS_FOLDER_TYPE_MAILBOX, &fid,
+               NULL, &error);
+
+       if (error != NULL) {
+               folder_exists = g_error_matches (
+                       error,
+                       EWS_CONNECTION_ERROR,
+                       EWS_CONNECTION_ERROR_FOLDEREXISTS);
+
+               if (!folder_exists)
+                       g_printerr ("\n%s\n", error->message);
+       }
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+
+       g_assert (retval == FALSE && folder_exists == TRUE);
+}
+
+static void
+test_create_folder_with_invalid_distinguished_parent_id (gconstpointer user_data)
+{
+       gboolean retval;
+       gboolean parent_folder_not_found;
+       GError *error = NULL;
+       EwsFolderId *fid;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "create_folder_with_invalid_distinguished_parent_id", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_create_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               "invalidparentid", TRUE,
+               "folder", E_EWS_FOLDER_TYPE_MAILBOX, &fid,
+               NULL, &error);
+
+       if (error != NULL) {
+               parent_folder_not_found = g_error_matches (
+                       error,
+                       SOUP_HTTP_ERROR,
+                       SOUP_STATUS_IO_ERROR);
+
+               if (!parent_folder_not_found)
+                       g_printerr ("\n%s\n", error->message);
+       }
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+
+       g_assert (retval == FALSE && parent_folder_not_found == TRUE);
+}
+
+
+static void
+test_create_folder_with_invalid_non_distinguished_parent_id (gconstpointer user_data)
+{
+       gboolean retval;
+       gboolean parent_folder_not_found = FALSE;
+       GError *error = NULL;
+       EwsFolderId *fid;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "create_folder_with_invalid_non_distinguished_parent_id", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_create_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               invalid_folder_id, FALSE,
+               "folder", E_EWS_FOLDER_TYPE_MAILBOX, &fid,
+               NULL, &error);
+
+       if (error != NULL) {
+               EEwsServerVersion server_version;
+
+               server_version = e_ews_connection_get_server_version (etd->connection);
+               switch (server_version) {
+                       case E_EWS_EXCHANGE_2007:
+                       case E_EWS_EXCHANGE_2007_SP1:
+                               parent_folder_not_found = g_error_matches (
+                                       error,
+                                       EWS_CONNECTION_ERROR,
+                                       EWS_CONNECTION_ERROR_NONEXISTENTMAILBOX);
+                               break;
+                       case E_EWS_EXCHANGE_2010:
+                       case E_EWS_EXCHANGE_2010_SP1:
+                       case E_EWS_EXCHANGE_2010_SP2:
+                       case E_EWS_EXCHANGE_FUTURE:
+                               parent_folder_not_found = g_error_matches (
+                                       error,
+                                       EWS_CONNECTION_ERROR,
+                                       EWS_CONNECTION_ERROR_PARENTFOLDERNOTFOUND);
+                               break;
+                       case E_EWS_EXCHANGE_UNKNOWN:
+                               break;
+                       default:
+                               g_assert_not_reached ();
+                               break;
+               }
+
+               if (!parent_folder_not_found)
+                       g_printerr ("\n%s\n", error->message);
+       }
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+
+       g_assert (retval == FALSE && parent_folder_not_found == TRUE);
+}
+
+static void
+test_create_folder_with_malformed_parent_id (gconstpointer user_data)
+{
+       gboolean retval;
+       gboolean malformed_parent_id;
+       GError *error = NULL;
+       EwsFolderId *fid;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "create_folder_with_malformed_parent_id", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_create_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               "malformedparentid", FALSE,
+               "folder", E_EWS_FOLDER_TYPE_MAILBOX, &fid,
+               NULL, &error);
+
+       if (error != NULL) {
+               malformed_parent_id = g_error_matches (
+                       error,
+                       EWS_CONNECTION_ERROR,
+                       EWS_CONNECTION_ERROR_INVALIDIDMALFORMED);
+
+               if (!malformed_parent_id)
+                       g_printerr ("\n%s\n", error->message);
+       }
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+
+       g_assert (retval == FALSE && malformed_parent_id == TRUE);
+}
+
+static void
+test_create_distinguished_folder_subfolder (gconstpointer user_data)
+{
+       gboolean retval = FALSE;
+       gboolean on_server = FALSE;
+       GError *error = NULL;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "create_distinguished_folder_subfolder", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_create_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               "inbox", TRUE,
+               "distinguished-folder-subfolder", E_EWS_FOLDER_TYPE_MAILBOX,
+               &distinguished_folder_subfolder_id, NULL, &error);
+
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       on_server = is_folder_on_server (etd->connection, distinguished_folder_subfolder_id, TRUE);
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+
+       g_assert (retval == TRUE && on_server == TRUE);
+
+}
+
+static void
+test_create_non_distinguished_folder_subfolder (gconstpointer user_data)
+{
+       gboolean retval = FALSE;
+       gboolean on_server = FALSE;
+       GError *error = NULL;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "create_non_distinguished_folder_subfolder", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_create_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               distinguished_folder_subfolder_id->id, FALSE,
+               "non-distinguished-folder-subfolder", E_EWS_FOLDER_TYPE_MAILBOX,
+               &non_distinguished_folder_subfolder_id,
+               NULL, &error);
+
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       on_server = is_folder_on_server (etd->connection, non_distinguished_folder_subfolder_id, TRUE);
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+
+       g_assert (retval == TRUE && on_server == TRUE);
+}
+
+static void
+test_hard_delete_distinguished_folder (gconstpointer user_data)
+{
+       gboolean retval = FALSE;
+       gboolean distinguished_folder = FALSE;
+       GError *error = NULL;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "hard_delete_root_subfolder", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_delete_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               "inbox", TRUE, "HardDelete",
+               NULL, &error);
+
+       if (error != NULL) {
+               distinguished_folder = g_error_matches (
+                       error,
+                       EWS_CONNECTION_ERROR,
+                       EWS_CONNECTION_ERROR_DELETEDISTINGUISHEDFOLDER);
+
+               if (!distinguished_folder)
+                       g_printerr ("\n%s\n", error->message);
+       }
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+
+       g_assert (retval == FALSE && distinguished_folder == TRUE);
+}
+
+static void
+test_hard_delete_non_distinguished_folder_subfolder (gconstpointer user_data)
+{
+       gboolean retval = FALSE;
+       gboolean on_server = TRUE;
+       GError *error = NULL;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "hard_delete_non_distinguished_folder_subfolder", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_delete_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               non_distinguished_folder_subfolder_id->id, FALSE, "HardDelete",
+               NULL, &error);
+
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       on_server = is_folder_on_server (etd->connection, non_distinguished_folder_subfolder_id, FALSE);
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+       e_ews_folder_id_free (non_distinguished_folder_subfolder_id);
+       non_distinguished_folder_subfolder_id = NULL;
+
+       g_assert (retval == TRUE && on_server == FALSE);
+
+}
+
+static void
+test_hard_delete_distinguished_folder_subfolder (gconstpointer user_data)
+{
+       gboolean retval = FALSE;
+       gboolean on_server = TRUE;
+       GError *error = NULL;
+       EwsTestData *etd = (gpointer) user_data;
+
+       ews_test_server_start_trace (etd, "hard_delete_distinguished_folder_subfolder", &error);
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       retval = e_ews_connection_delete_folder_sync (
+               etd->connection, EWS_PRIORITY_MEDIUM,
+               distinguished_folder_subfolder_id->id, FALSE, "HardDelete",
+               NULL, &error);
+
+       if (error != NULL) {
+               g_printerr ("\n%s\n", error->message);
+               goto exit;
+       }
+
+       on_server = is_folder_on_server (etd->connection, distinguished_folder_subfolder_id, FALSE);
+
+ exit:
+       uhm_server_end_trace (server);
+       g_clear_error (&error);
+       e_ews_folder_id_free (distinguished_folder_subfolder_id);
+       distinguished_folder_subfolder_id = NULL;
+
+       g_assert (retval == TRUE && on_server == FALSE);
+}
+
+int main (int argc,
+         char **argv)
+{
+       gint retval;
+       GFile *trace_directory;
+       gchar *path;
+       GList *etds, *l;
+
+       retval = ews_test_init (argc, argv);
+
+       if (retval < 0)
+               goto exit;
+
+       server = ews_test_get_mock_server ();
+       etds = ews_test_get_test_data_list ();
+
+       for (l = etds; l != NULL; l = l->next) {
+               EwsTestData *etd = l->data;
+               gchar *message;
+
+               g_signal_connect (server, "notify::resolver", (GCallback) server_notify_resolver_cb, etd);
+
+               path = g_strdup_printf (TEST_FILE_DIR "/traces/%s/camel/folder", etd->version);
+               trace_directory = g_file_new_for_path (path);
+               g_free (path);
+               uhm_server_set_trace_directory (server, trace_directory);
+               g_object_unref (trace_directory);
+
+               /* Create folder */
+               message = g_strdup_printf ("/%s/camel/folder/create_existing_folder", etd->version);
+               g_test_add_data_func (message, etd, test_create_existing_folder);
+               g_free (message);
+
+               message = g_strdup_printf (
+                       "/%s/camel/folder/create_folder_invalid_distinguished_parent_id", etd->version);
+               g_test_add_data_func (message, etd, test_create_folder_with_invalid_distinguished_parent_id);
+               g_free (message);
+
+               message = g_strdup_printf (
+                       "/%s/camel/folder/create_folder_invalid_non_distinguished_parent_id", etd->version);
+               g_test_add_data_func (message, etd, 
test_create_folder_with_invalid_non_distinguished_parent_id);
+               g_free (message);
+
+               message = g_strdup_printf ("/%s/camel/folder/create_folder_with_malformed_parent_id", 
etd->version);
+               g_test_add_data_func (message, etd, test_create_folder_with_malformed_parent_id);
+               g_free (message);
+
+               message = g_strdup_printf ("/%s/camel/folder/create_distinguished_folder_subfolder", 
etd->version);
+               g_test_add_data_func (message, etd, test_create_distinguished_folder_subfolder);
+               g_free (message);
+
+               message = g_strdup_printf ("/%s/camel/folder/create_non_distinguished_folder_subfolder", 
etd->version);
+               g_test_add_data_func (message, etd, test_create_non_distinguished_folder_subfolder);
+               g_free (message);
+
+               /* Delete fdata_older */
+               message = g_strdup_printf ("/%s/camel/folder/hard_delete_distinguished_folder", etd->version);
+               g_test_add_data_func (message, etd, test_hard_delete_distinguished_folder);
+               g_free (message);
+
+               message = g_strdup_printf ("/%s/camel/folder/hard_delete_non_distinguished_folder_subfolder", 
etd->version);
+               g_test_add_data_func (message, etd, test_hard_delete_non_distinguished_folder_subfolder);
+               g_free (message);
+
+               message = g_strdup_printf ("/%s/camel/folder/hard_delete_distinguished_folder_subfolder", 
etd->version);
+               g_test_add_data_func (message, etd, test_hard_delete_distinguished_folder_subfolder);
+               g_free (message);
+
+       }
+               retval = g_test_run ();
+
+ exit:
+       ews_test_cleanup ();
+       return retval;
+}
diff --git a/src/tests/common.c b/src/tests/common.c
new file mode 100644
index 0000000..b1ae662
--- /dev/null
+++ b/src/tests/common.c
@@ -0,0 +1,303 @@
+#include <config.h>
+
+#include <locale.h>
+#include <glib/gi18n.h>
+
+#include "common.h"
+
+/* declaration of debug handler */
+static void ews_test_debug_handler (const gchar *log_domain,
+                                   GLogLevelFlags log_level,
+                                   const gchar *message,
+                                   gpointer user_data);
+
+#if 0
+/* declaration of printerr handler */
+static void ews_test_assert_handler (const gchar *message);
+#endif
+
+/* global list of debugging messages */
+static GSList *message_list = NULL;
+
+/* Directory to output network trace files to, if trace output is enabled. (NULL otherwise.) */
+static const GFile *trace_dir = NULL;
+
+/*
+ * if (
+ * TRUE if tests should be run and a trace file written for each;
+ * FALSE if tests should run offline against existing trace files.
+ */
+static gboolean write_traces = FALSE;
+
+/*
+ * TRUE if tests should be run online and the server's response compared to the existing trace file for each;
+ * FALSE if tests should run offline without comparison.
+ */
+static gboolean compare_traces = FALSE;
+
+static gint number = 0;
+
+/* Global mock server instance used by all tests */
+static UhmServer *server = NULL;
+
+static GList *etds = NULL;
+
+static GList *versions = NULL;
+static GList *hostnames = NULL;
+static GList *server_uris = NULL;
+static GList *usernames = NULL;
+static GList *passwords = NULL;
+
+static GList *fake_versions = NULL;
+static GList *fake_hostnames = NULL;
+static GList *fake_server_uris = NULL;
+static const gchar *fake_username = "foo";
+static const gchar *fake_password = "bar";
+
+static void
+prepare_fake_data (void)
+{
+       guint i;
+       const gchar *supported_versions[] = {
+               "2007_SP1",
+               "2010_SP2",
+               NULL
+       };
+
+       for (i = 0; supported_versions[i] != NULL; i++) {
+               fake_versions = g_list_append (
+                       fake_versions,
+                       g_strdup_printf ("Exchange%s", supported_versions[i]));
+               fake_hostnames = g_list_append (
+                       fake_hostnames,
+                       g_strdup_printf ("exchange%s.foo.bar/EWS/Exchange.asm", supported_versions[i]));
+               fake_server_uris = g_list_append (
+                       fake_server_uris,
+                       g_strdup_printf ("https://exchange%s.foo.bar/EWS/Exchange.asm";, 
supported_versions[i]));
+       }
+}
+
+static gboolean
+handle_config (const gchar *option_name G_GNUC_UNUSED,
+              const gchar *value,
+              gpointer data G_GNUC_UNUSED,
+              GError **error)
+{
+       const gchar *val;
+       gchar *key;
+       gsize len;
+       gboolean ret = FALSE;
+
+       if (!(val = strchr (value, '='))) {
+               g_set_error (error, 0, 0, _("Expected configuration key=value"));
+               goto exit;
+       }
+
+       len = val - value;
+       val++;
+       key = g_strndup (value, len);
+
+       if (g_strcmp0 (key, "number") == 0) {
+               number = g_ascii_strtoll (val, NULL, 0);
+               ret = TRUE;
+       } else if (g_strcmp0 (key, "version") == 0) {
+               versions = g_list_append (versions, g_strdup (val));
+               ret = TRUE;
+       } else if (g_strcmp0 (key, "hostname") == 0) {
+               hostnames = g_list_append (hostnames, g_strdup (val));
+               ret = TRUE;
+       } else if (g_strcmp0 (key, "server-uri") == 0) {
+               server_uris = g_list_append (server_uris, g_strdup (val));
+               ret = TRUE;
+       } else if (g_strcmp0 (key, "username") == 0) {
+               usernames = g_list_append (usernames, g_strdup (val));
+               ret = TRUE;
+       } else if (g_strcmp0 (key, "password") == 0) {
+               passwords = g_list_append (passwords, g_strdup (val));
+               ret = TRUE;
+       }
+
+       g_free (key);
+
+ exit:
+       return ret;
+}
+
+static GOptionEntry entries[] =
+{
+       { "config", 'c', 0, G_OPTION_ARG_CALLBACK, handle_config,
+         "Set configuration parameters", "key=value" },
+       { "verbose", 'v', 0, G_OPTION_ARG_NONE, NULL,
+        "Run tests verbosely", NULL, },
+       { "quiet", 'q', 0, G_OPTION_ARG_NONE, NULL,
+        "Run tests quietly", NULL, },
+       { "trace-dir", 't', 0, G_OPTION_ARG_FILENAME, (void *)&trace_dir,
+         "Read/Write traces files in the specified directory", NULL, },
+       { "write-traces", 'w', 0, G_OPTION_ARG_NONE, (void *)&write_traces,
+         "Work online and write traces files to --trace-dir\n", NULL, },
+       { NULL }
+};
+
+gint
+ews_test_init (int argc,
+              char **argv)
+{
+       GOptionContext *context;
+       GError *error = NULL;
+       guint ret = 0;
+       guint i;
+
+#if !GLIB_CHECK_VERSION (2, 35, 0)
+       g_type_init ();
+#endif
+
+       setlocale (LC_ALL, "");
+       textdomain (GETTEXT_PACKAGE);
+       bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+       context = g_option_context_new (_("- EWS Tests"));
+       g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
+       if (!g_option_context_parse (context, &argc, &argv, &error)) {
+               g_printerr (_("Error while parsing options: %s\n"), error->message);
+               g_printerr ("%s\n", g_option_context_get_help (context, FALSE, NULL));
+
+               ret = -1;
+               goto exit;
+       }
+
+       if (write_traces) {
+               if (number == 0 ||
+                   versions == NULL ||
+                   server_uris == NULL ||
+                   hostnames == NULL ||
+                   usernames == NULL ||
+                   passwords == NULL ||
+                   number != g_list_length (versions) ||
+                   number != g_list_length (server_uris) ||
+                   number != g_list_length (hostnames) ||
+                   number != g_list_length (usernames) ||
+                   number != g_list_length (passwords)) {
+                       g_printerr (_("Error while parsing options\n"));
+                       g_printerr ("%s\n", g_option_context_get_help (context, FALSE, NULL));
+
+                       ret = -2;
+                       goto exit;
+               }
+       } else {
+               prepare_fake_data ();
+       }
+
+       g_test_init (&argc, &argv, NULL);
+       g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=";);
+
+       /* Set handler of debug information */
+       g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, (GLogFunc) ews_test_debug_handler, message_list);
+
+       g_setenv ("EWS_DEBUG", "3", TRUE);
+
+       server = uhm_server_new ();
+       uhm_server_set_enable_logging (server, write_traces);
+       uhm_server_set_enable_online (server, write_traces || compare_traces);
+
+       for (i = 0; i < number; i++) {
+               EwsTestData *etd;
+               const gchar *username;
+               const gchar *password;
+               const gchar *server_uri;
+               gchar *hostname;
+               gchar *version;
+               CamelEwsSettings *settings = NULL;
+
+               if (write_traces) {
+                       username = g_list_nth_data (usernames, i);
+                       password = g_list_nth_data (passwords, i);
+                       server_uri = g_list_nth_data (server_uris, i);
+                       hostname = g_list_nth_data (hostnames, i);
+                       version = g_list_nth_data (versions, i);
+               } else {
+                       username = fake_username;
+                       password = fake_password;
+                       server_uri = g_list_nth_data (fake_server_uris, i);
+                       hostname = g_list_nth_data (fake_hostnames, i);
+                       version = g_list_nth_data (fake_versions, i);
+               }
+
+               etd = g_new0 (EwsTestData, 1);
+               etd->hostname = hostname;
+               etd->version = version;
+
+               settings = g_object_new (
+                       CAMEL_TYPE_EWS_SETTINGS,
+                       "user", username,
+                       NULL);
+
+               etd->connection = e_ews_connection_new (server_uri, settings);
+               e_ews_connection_set_password (etd->connection, password);
+               e_ews_connection_set_server_version (etd->connection, version);
+
+               etds = g_list_append (etds, etd);
+
+               g_object_unref (settings);
+       }
+
+ exit:
+       g_option_context_free (context);
+
+       return ret;
+}
+
+UhmServer *
+ews_test_get_mock_server (void)
+{
+       return server;
+}
+
+GList * /* EwsTestData */
+ews_test_get_test_data_list (void)
+{
+       return etds;
+}
+
+void
+ews_test_cleanup (void)
+{
+//     g_list_free_full (etds, (GDestroyNotify) ews_test_data_free);
+}
+
+static void
+ews_test_set_https_port (EwsTestData *etd)
+{
+       CamelEwsSettings *ews_settings;
+       guint16 port;
+
+       port = uhm_server_get_port (server);
+
+       ews_settings = e_ews_connection_ref_settings (etd->connection);
+       camel_network_settings_set_port (CAMEL_NETWORK_SETTINGS (ews_settings), port);
+       g_object_unref (ews_settings);
+}
+
+void
+ews_test_server_start_trace (EwsTestData *etd,
+                            const gchar *trace_filename,
+                            GError **error)
+{
+       uhm_server_start_trace (server, trace_filename, error);
+       ews_test_set_https_port (etd);
+}
+
+static void
+ews_test_debug_handler (const gchar *log_domain,
+                       GLogLevelFlags log_level,
+                       const gchar *message,
+                       gpointer user_data)
+{
+       message_list = g_slist_append (message_list, g_strdup (message));
+
+       if (message != NULL &&
+           (*message == '<' || *message == '>' || *message == ' ') &&
+           *(message + 1) == ' ') {
+               uhm_server_received_message_chunk (server, message, strlen (message), NULL);
+       }
+}
diff --git a/src/tests/common.h b/src/tests/common.h
new file mode 100644
index 0000000..0af99bd
--- /dev/null
+++ b/src/tests/common.h
@@ -0,0 +1,28 @@
+#include <uhttpmock/uhm.h>
+
+#include "server/camel-ews-settings.h"
+#include "server/e-ews-connection.h"
+
+#ifndef EWS_TEST_COMMON_H
+#define EWS_TEST_COMMON_H
+
+G_BEGIN_DECLS
+
+typedef struct _EwsTestData {
+       EEwsConnection *connection;
+       gchar *hostname;
+       gchar *version;
+} EwsTestData;
+
+gint                   ews_test_init                                   (int argc,
+                                                                        char **argv);
+UhmServer *            ews_test_get_mock_server                        (void);
+GList *                        ews_test_get_test_data_list                     (void);
+void                   ews_test_cleanup                                (void);
+void                   ews_test_server_start_trace                     (EwsTestData *etd,
+                                                                        const gchar *trace_filename,
+                                                                        GError **error);
+
+G_END_DECLS
+
+#endif /* EWS_TEST_COMMON_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]