[evolution-ews] Bug #708121 - Add functionalities to help the addition of the low level tests for EwsConnection



commit 00b21150fa118f528aea1f86d30f6fcc4572b1ce
Author: Fabiano Fidêncio <fidencio redhat com>
Date:   Wed Sep 11 13:50:28 2013 +0200

    Bug #708121 - Add functionalities to help the addition of the low level tests for EwsConnection
    
    - Add e_ews_folder_id_is_equal()
    - Add e_ews_debug_get_log_level()
    - Add EWS_DEBUG=3 debug level, which should only be used to log trace for the tests files
    - Don't use streaming-based messages when we are logging

 src/server/Makefile.am        |    3 +
 src/server/e-ews-connection.c |   86 ++++++++++++++++++++++++++++++++++++++---
 src/server/e-ews-debug.c      |   42 ++++++++++++++++++++
 src/server/e-ews-debug.h      |   32 +++++++++++++++
 src/server/e-ews-folder.c     |   22 ++++++++++
 src/server/e-ews-folder.h     |    2 +
 src/server/e-soap-message.c   |   19 +++++++--
 7 files changed, 195 insertions(+), 11 deletions(-)
---
diff --git a/src/server/Makefile.am b/src/server/Makefile.am
index 54425d7..c895be7 100644
--- a/src/server/Makefile.am
+++ b/src/server/Makefile.am
@@ -51,6 +51,8 @@ libeews_1_2_la_SOURCES = \
        $(KERBEROS_FILES) \
        e-ews-connection.c \
        e-ews-connection.h \
+       e-ews-debug.c \
+       e-ews-debug.h \
        e-ews-enumtypes.c \
        e-ews-folder.c \
        e-ews-item.c \
@@ -81,6 +83,7 @@ libeewsincludedir = $(privincludedir)/ews
 libeewsinclude_HEADERS = \
        ews-errors.h \
        e-ews-connection.h \
+       e-ews-debug.h \
        e-ews-enums.h \
        e-ews-enumtypes.h \
        e-ews-folder.h \
diff --git a/src/server/e-ews-connection.c b/src/server/e-ews-connection.c
index 6ee988e..4e2e71a 100644
--- a/src/server/e-ews-connection.c
+++ b/src/server/e-ews-connection.c
@@ -44,6 +44,7 @@
 #include "e-ews-connection.h"
 #include "e-ews-message.h"
 #include "e-ews-item-change.h"
+#include "e-ews-debug.h"
 
 #define d(x) x
 
@@ -438,7 +439,7 @@ ews_next_request (gpointer _cnc)
 
        node = (EwsNode *) l->data;
 
-       if (g_getenv ("EWS_DEBUG") && (atoi (g_getenv ("EWS_DEBUG")) == 1)) {
+       if (e_ews_debug_get_log_level () == 1) {
                soup_buffer_free (soup_message_body_flatten (SOUP_MESSAGE (node->msg)->request_body));
                /* print request's body */
                printf ("\n The request headers");
@@ -595,6 +596,7 @@ ews_response_cb (SoupSession *session,
 {
        EwsNode *enode = (EwsNode *) data;
        ESoapResponse *response;
+       gint log_level;
 
        if (g_cancellable_is_cancelled (enode->cancellable))
                goto exit;
@@ -621,7 +623,9 @@ ews_response_cb (SoupSession *session,
 
        /* TODO: The stdout can be replaced with Evolution's
         * Logging framework also */
-       if (g_getenv ("EWS_DEBUG") && (atoi (g_getenv ("EWS_DEBUG")) >= 1))
+
+       log_level = e_ews_debug_get_log_level ();
+       if (log_level >= 1 && log_level < 3)
                e_soap_response_dump_response (response, stdout);
 
        if (enode->cb != NULL)
@@ -1529,9 +1533,56 @@ e_ews_soup_thread (gpointer user_data)
        return NULL;
 }
 
+/*
+ * e_ews_debug_handler:
+ *
+ * GLib debug message handler, which is passed all messages from g_debug() calls,
+ * and decides whether to print them.
+ */
+static void
+e_ews_debug_handler (const gchar *log_domain,
+                    GLogLevelFlags log_level,
+                    const gchar *message,
+                    gpointer user_data)
+{
+       if (e_ews_debug_get_log_level () >= 3)
+               g_log_default_handler (log_domain, log_level, message, NULL);
+}
+
+/*
+ * e_ews_soup_log_print:
+ *
+ * Log printer for the libsoup logging functionality, which just marshal all soup log
+ * output to the standard GLib logging framework (and thus to debug_handler(), above).
+ */
+static void
+e_ews_soup_log_printer (SoupLogger *logger,
+                       SoupLoggerLogLevel level,
+                       char direction,
+                       const gchar *data,
+                       gpointer user_data)
+{
+       const gchar *filtered_data = NULL;
+
+       if (e_ews_debug_get_log_level () >= 3) {
+               if (direction == '>' && g_ascii_strncasecmp (data, "Host:", 5) == 0)
+                       filtered_data = "Host: <redacted>";
+               else if (direction == '>' && g_ascii_strncasecmp (data, "Authorization:", 14) == 0)
+                       filtered_data = "Authorization: <redacted>";
+               else if (direction == '<' && g_ascii_strncasecmp (data, "Set-Cookie:", 11) == 0)
+                       filtered_data = "Set-Cookie: <redacted>";
+               else
+                       filtered_data = data;
+       }
+
+       g_debug ("%c %s", direction, filtered_data ? filtered_data : data);
+}
+
 static void
 e_ews_connection_init (EEwsConnection *cnc)
 {
+       gint log_level;
+
        cnc->priv = E_EWS_CONNECTION_GET_PRIVATE (cnc);
 
        cnc->priv->soup_context = g_main_context_new ();
@@ -1552,12 +1603,25 @@ e_ews_connection_init (EEwsConnection *cnc)
        e_proxy_setup_proxy (cnc->priv->proxy);
        g_signal_connect (cnc->priv->proxy, "changed", G_CALLBACK (proxy_settings_changed), cnc);
 
-       if (g_getenv ("EWS_DEBUG") && (atoi (g_getenv ("EWS_DEBUG")) >= 2)) {
+       log_level = e_ews_debug_get_log_level ();
+
+       if (log_level >= 2) {
                SoupLogger *logger;
                logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
+
+               if (log_level >= 3) {
+                       soup_logger_set_printer (logger, e_ews_soup_log_printer, NULL, NULL);
+                       g_log_set_handler (
+                               G_LOG_DOMAIN,
+                               G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
+                               G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO,
+                               e_ews_debug_handler, cnc);
+               }
+
                soup_session_add_feature (
                        cnc->priv->soup_session,
                        SOUP_SESSION_FEATURE (logger));
+               g_object_unref (logger);
        }
 
        g_mutex_init (&cnc->priv->password_lock);
@@ -2070,7 +2134,10 @@ autodiscover_data_free (struct _autodiscover_data *ad)
 static void
 ews_dump_raw_soup_response (SoupMessage *msg)
 {
-       if (g_getenv ("EWS_DEBUG") && (atoi (g_getenv ("EWS_DEBUG")) >= 1)) {
+       gint log_level;
+
+       log_level = e_ews_debug_get_log_level ();
+       if (log_level >= 1 && log_level < 3) {
                soup_buffer_free (soup_message_body_flatten (SOUP_MESSAGE (msg)->response_body));
                /* print response body */
                printf ("\n The response headers");
@@ -2260,6 +2327,7 @@ e_ews_get_msg_for_url (const gchar *url,
                        GError **error)
 {
        SoupMessage *msg;
+       gint log_level;
 
        if (url == NULL) {
                g_set_error_literal (
@@ -2295,7 +2363,8 @@ e_ews_get_msg_for_url (const gchar *url,
                        G_CALLBACK (post_restarted), buf);
        }
 
-       if (g_getenv ("EWS_DEBUG") && (atoi (g_getenv ("EWS_DEBUG")) >= 1)) {
+       log_level = e_ews_debug_get_log_level ();
+       if (log_level >= 1 && log_level < 3) {
                soup_buffer_free (
                        soup_message_body_flatten (
                        SOUP_MESSAGE (msg)->request_body));
@@ -3158,7 +3227,12 @@ e_ews_connection_download_oal_file (EEwsConnection *cnc,
        g_simple_async_result_set_op_res_gpointer (
                simple, data, (GDestroyNotify) oal_req_data_free);
 
-       soup_message_body_set_accumulate (soup_message->response_body, FALSE);
+       /*
+        * Don't use streaming-based messages when we are loggin the traffic
+        * to generate trace files for tests
+        */
+       if (e_ews_debug_get_log_level () <= 2)
+               soup_message_body_set_accumulate (soup_message->response_body, FALSE);
 
        g_signal_connect (
                soup_message, "got-headers",
diff --git a/src/server/e-ews-debug.c b/src/server/e-ews-debug.c
new file mode 100644
index 0000000..5d6a62e
--- /dev/null
+++ b/src/server/e-ews-debug.c
@@ -0,0 +1,42 @@
+/*
+ * e-ews-debug.c
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "e-ews-debug.h"
+
+gint
+e_ews_debug_get_log_level (void)
+{
+       static gint level = -1;
+
+       if (level < 0) {
+               const gchar *envvar = g_getenv ("EWS_DEBUG");
+               if (envvar != NULL)
+                       /*
+                        * We don't care about error here.
+                        * If something goes wrong, level is set to 0
+                        */
+                       level = g_ascii_strtoll (envvar, NULL, 0);
+               level = MAX (level, 0);
+       }
+
+       return level;
+}
diff --git a/src/server/e-ews-debug.h b/src/server/e-ews-debug.h
new file mode 100644
index 0000000..95f1f09
--- /dev/null
+++ b/src/server/e-ews-debug.h
@@ -0,0 +1,32 @@
+/*
+ * e-ews-debug.h
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#ifndef E_EWS_DEBUG_H
+#define E_EWS_DEBUG_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gint           e_ews_debug_get_log_level               (void);
+
+G_END_DECLS
+
+#endif /* E_EWS_DEBUG_H */
+
+
diff --git a/src/server/e-ews-folder.c b/src/server/e-ews-folder.c
index 296ac35..cb2b005 100644
--- a/src/server/e-ews-folder.c
+++ b/src/server/e-ews-folder.c
@@ -349,6 +349,28 @@ e_ews_folder_id_free (EwsFolderId *fid)
        }
 }
 
+gboolean
+e_ews_folder_id_is_equal (const EwsFolderId *a,
+                         const EwsFolderId *b)
+{
+       if (a == NULL && b == NULL)
+               return TRUE;
+
+       if (a == NULL || b == NULL)
+               return FALSE;
+
+       if ((a->is_distinguished_id ? 1 : 0) != (b->is_distinguished_id ? 1 : 0))
+               return FALSE;
+
+       if (g_strcmp0 (a->id, b->id) != 0)
+               return FALSE;
+
+       if (g_strcmp0 (a->change_key, b->change_key) != 0)
+               return FALSE;
+
+       return TRUE;
+}
+
 const gchar *
 e_ews_folder_get_name (EEwsFolder *folder)
 {
diff --git a/src/server/e-ews-folder.h b/src/server/e-ews-folder.h
index 44a7065..b440eb0 100644
--- a/src/server/e-ews-folder.h
+++ b/src/server/e-ews-folder.h
@@ -83,6 +83,8 @@ EwsFolderId * e_ews_folder_id_new (const gchar *id,
                                     const gchar *change_key,
                                     gboolean is_distinguished_id);
 void           e_ews_folder_id_free (EwsFolderId *fid);
+gboolean       e_ews_folder_id_is_equal (const EwsFolderId *a,
+                                         const EwsFolderId *b);
 
 typedef enum {
        E_EWS_ESOURCE_FLAG_NONE                 = 0,
diff --git a/src/server/e-soap-message.c b/src/server/e-soap-message.c
index ca7e39c..c737b1b 100644
--- a/src/server/e-soap-message.c
+++ b/src/server/e-soap-message.c
@@ -16,7 +16,9 @@
 #ifdef G_OS_WIN32
 #include <io.h>
 #endif
+
 #include "e-soap-message.h"
+#include "e-ews-debug.h"
 
 #define E_SOAP_MESSAGE_GET_PRIVATE(obj) \
        (G_TYPE_INSTANCE_GET_PRIVATE \
@@ -344,11 +346,18 @@ e_soap_message_new (const gchar *method,
 
        soup_uri_free (uri);
 
-       /* Don't accumulate body data into a huge buffer.
-        * Instead, parse it as it arrives. */
-       soup_message_body_set_accumulate (
-               SOUP_MESSAGE (msg)->response_body,
-               FALSE);
+       /*
+        * Don't use streaming-based messages when we are loggin the traffic
+        * to generate trace files for tests
+        */
+       if (e_ews_debug_get_log_level () <= 2) {
+               /* Don't accumulate body data into a huge buffer.
+                * Instead, parse it as it arrives. */
+               soup_message_body_set_accumulate (
+                               SOUP_MESSAGE (msg)->response_body,
+                               FALSE);
+       }
+
        g_signal_connect (msg, "got-headers", G_CALLBACK (soap_got_headers), NULL);
        g_signal_connect (msg, "got-chunk", G_CALLBACK (soap_got_chunk), NULL);
        g_signal_connect (msg, "restarted", G_CALLBACK (soap_restarted), NULL);


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