Blame SOURCES/0170-reported_to-add-a-function-formatting-reported_to-li.patch

28bab8
From 387e6fd8c974cf0c6b96b075baa968f86d6ed96f Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Mon, 19 Oct 2015 14:07:40 +0200
28bab8
Subject: [PATCH] reported_to: add a function formatting reported_to lines
28bab8
28bab8
Use this function to ensure consistent reported_to formatting.
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
28bab8
Conflicts:
28bab8
	po/POTFILES.in
28bab8
---
28bab8
 po/POTFILES.in         |   1 +
28bab8
 src/include/dump_dir.h |  49 ++++++++++++++--
28bab8
 src/lib/dump_dir.c     |  12 ++++
28bab8
 src/lib/reported_to.c  |  55 +++++++++++++++---
28bab8
 tests/reported_to.at   | 148 ++++++++++++++++++++++++++++++++++++++++++++++++-
28bab8
 5 files changed, 251 insertions(+), 14 deletions(-)
28bab8
28bab8
diff --git a/po/POTFILES.in b/po/POTFILES.in
28bab8
index e952711..1222c95 100644
28bab8
--- a/po/POTFILES.in
28bab8
+++ b/po/POTFILES.in
28bab8
@@ -25,6 +25,7 @@ src/lib/ureport.c
28bab8
 src/lib/make_descr.c
28bab8
 src/lib/parse_options.c
28bab8
 src/lib/problem_data.c
28bab8
+src/lib/reported_to.c
28bab8
 src/lib/run_event.c
28bab8
 src/plugins/abrt_rh_support.c
28bab8
 src/plugins/report_Bugzilla.xml.in
28bab8
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
28bab8
index 092ddeb..fff23f2 100644
28bab8
--- a/src/include/dump_dir.h
28bab8
+++ b/src/include/dump_dir.h
28bab8
@@ -121,20 +121,57 @@ int dd_chown(struct dump_dir *dd, uid_t new_uid);
28bab8
 
28bab8
 
28bab8
 /* reported_to handling */
28bab8
-#define add_reported_to_data libreport_add_reported_to_data
28bab8
-int add_reported_to_data(char **reported_to, const char *line);
28bab8
-#define add_reported_to libreport_add_reported_to
28bab8
-void add_reported_to(struct dump_dir *dd, const char *line);
28bab8
 struct report_result {
28bab8
     char *label;
28bab8
     char *url;
28bab8
     char *msg;
28bab8
     char *bthash;
28bab8
-    /* char *whole_line; */
28bab8
-    /* time_t timestamp; */
28bab8
+    time_t timestamp;
28bab8
     /* ^^^ if you add more fields, don't forget to update free_report_result() */
28bab8
 };
28bab8
 typedef struct report_result report_result_t;
28bab8
+
28bab8
+/* Appends a new unique line to the list of report results
28bab8
+ *
28bab8
+ * If the reported_to data already contains the given line, the line will not
28bab8
+ * be added again.
28bab8
+ *
28bab8
+ * @param reported_to The data
28bab8
+ * @param line The appended line
28bab8
+ * @return 1 if the line was added at the end of the reported_to; otherwise 0.
28bab8
+ */
28bab8
+#define add_reported_to_data libreport_add_reported_to_data
28bab8
+int add_reported_to_data(char **reported_to, const char *line);
28bab8
+
28bab8
+/* Appends a new unique entry to the list of report results
28bab8
+ *
28bab8
+ * result->label must be non-empty string which does not contain ':' character.
28bab8
+ *
28bab8
+ * The function converts the result to a valid reported_to line and calls
28bab8
+ * add_reported_to_data().
28bab8
+ *
28bab8
+ * @param reported_to The data
28bab8
+ * @param result The appended entry
28bab8
+ * @return -EINVAL if result->label is invalid; otherwise return value of
28bab8
+ * add_reported_to_data
28bab8
+ */
28bab8
+#define add_reported_to_entry_data libreport_add_reported_to_entry_data
28bab8
+int add_reported_to_entry_data(char **reported_to, struct report_result *result);
28bab8
+
28bab8
+/* This is a wrapper of add_reported_to_data which accepts 'struct dump_dir *'
28bab8
+ * in the first argument instead of 'char **'. The added line is stored in
28bab8
+ * 'reported_to' dump directory file.
28bab8
+ */
28bab8
+#define add_reported_to libreport_add_reported_to
28bab8
+void add_reported_to(struct dump_dir *dd, const char *line);
28bab8
+
28bab8
+/* This is a wrapper of add_reported_to_entry_data which accepts 'struct
28bab8
+ * dump_dir *' in the first argument instead of 'char **'. The added entry is
28bab8
+ * stored in 'reported_to' dump directory file.
28bab8
+ */
28bab8
+#define add_reported_to_entry libreport_add_reported_to_entry
28bab8
+void add_reported_to_entry(struct dump_dir *dd, struct report_result *result);
28bab8
+
28bab8
 #define free_report_result libreport_free_report_result
28bab8
 void free_report_result(struct report_result *result);
28bab8
 #define find_in_reported_to_data libreport_find_in_reported_to_data
28bab8
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
28bab8
index a5cd93e..9b5b1b5 100644
28bab8
--- a/src/lib/dump_dir.c
28bab8
+++ b/src/lib/dump_dir.c
28bab8
@@ -1306,6 +1306,18 @@ void add_reported_to(struct dump_dir *dd, const char *line)
28bab8
     free(reported_to);
28bab8
 }
28bab8
 
28bab8
+void add_reported_to_entry(struct dump_dir *dd, struct report_result *result)
28bab8
+{
28bab8
+    if (!dd->locked)
28bab8
+        error_msg_and_die("dump_dir is not opened"); /* bug */
28bab8
+
28bab8
+    char *reported_to = dd_load_text_ext(dd, FILENAME_REPORTED_TO, DD_FAIL_QUIETLY_ENOENT | DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE);
28bab8
+    if (add_reported_to_entry_data(&reported_to, result))
28bab8
+        dd_save_text(dd, FILENAME_REPORTED_TO, reported_to);
28bab8
+
28bab8
+    free(reported_to);
28bab8
+}
28bab8
+
28bab8
 report_result_t *find_in_reported_to(struct dump_dir *dd, const char *report_label)
28bab8
 {
28bab8
     char *reported_to = dd_load_text_ext(dd, FILENAME_REPORTED_TO,
28bab8
diff --git a/src/lib/reported_to.c b/src/lib/reported_to.c
28bab8
index bc0c2d2..3245ada 100644
28bab8
--- a/src/lib/reported_to.c
28bab8
+++ b/src/lib/reported_to.c
28bab8
@@ -45,6 +45,45 @@ int add_reported_to_data(char **reported_to, const char *line)
28bab8
     return 1;
28bab8
 }
28bab8
 
28bab8
+int add_reported_to_entry_data(char **reported_to, struct report_result *result)
28bab8
+{
28bab8
+    if (NULL == result->label || result->label[0] == '\0')
28bab8
+    {
28bab8
+        log_warning(_("Report result label mustn't be empty string."));
28bab8
+        return -EINVAL;
28bab8
+    }
28bab8
+
28bab8
+    if (strchr(result->label, ':') != NULL)
28bab8
+    {
28bab8
+        log_warning(_("Report result label mustn't contain ':' character."));
28bab8
+        return -EINVAL;
28bab8
+    }
28bab8
+
28bab8
+    struct strbuf *buf = strbuf_new();
28bab8
+    strbuf_append_strf(buf, "%s:", result->label);
28bab8
+
28bab8
+    if (result->timestamp != 0)
28bab8
+    {
28bab8
+        const char *const time = iso_date_string(&(result->timestamp));
28bab8
+        strbuf_append_strf(buf, " TIME=%s", time);
28bab8
+    }
28bab8
+
28bab8
+    if (result->url != NULL)
28bab8
+        strbuf_append_strf(buf, " URL=%s", result->url);
28bab8
+
28bab8
+    if (result->bthash != NULL)
28bab8
+        strbuf_append_strf(buf, " BTHASH=%s", result->bthash);
28bab8
+
28bab8
+    /* MSG must be last because the value is delimited by new line character */
28bab8
+    if (result->msg != NULL)
28bab8
+        strbuf_append_strf(buf, " MSG=%s", result->msg);
28bab8
+
28bab8
+    const int r = add_reported_to_data(reported_to, buf->buf);
28bab8
+    strbuf_free(buf);
28bab8
+
28bab8
+    return r;
28bab8
+}
28bab8
+
28bab8
 void free_report_result(struct report_result *result)
28bab8
 {
28bab8
     if (!result)
28bab8
@@ -94,13 +133,15 @@ static report_result_t *parse_reported_line(const char *line, size_t label_len)
28bab8
             free(result->bthash);
28bab8
             result->bthash = xstrndup(line + 7, end - (line + 7));
28bab8
         }
28bab8
-        //else
28bab8
-        //if (strncmp(line, "TIME=", 5) == 0)
28bab8
-        //{
28bab8
-        //    free(result->time);
28bab8
-        //    result->time = foo(line + 5, end - (line + 5));
28bab8
-        //}
28bab8
-        //...
28bab8
+        if (strncmp(line, "TIME=", 5) == 0)
28bab8
+        {
28bab8
+            char *datetime = xstrndup(line + 5, end - (line + 5));
28bab8
+            const int r = iso_date_string_parse(datetime, &result->timestamp);
28bab8
+            if (r != 0)
28bab8
+                log_warning(_("Ignored invalid ISO date of report result '%s'"), result->label);
28bab8
+
28bab8
+            free(datetime);
28bab8
+        }
28bab8
         line = end;
28bab8
         continue;
28bab8
     }
28bab8
diff --git a/tests/reported_to.at b/tests/reported_to.at
28bab8
index 42af44f..ecf323d 100644
28bab8
--- a/tests/reported_to.at
28bab8
+++ b/tests/reported_to.at
28bab8
@@ -53,6 +53,138 @@ int main(void)
28bab8
 }
28bab8
 ]])
28bab8
 
28bab8
+
28bab8
+## -------------------------- ##
28bab8
+## add_reported_to_entry_data ##
28bab8
+## -------------------------- ##
28bab8
+
28bab8
+AT_TESTFUN([add_reported_to_entry_data],
28bab8
+[[
28bab8
+#include "internal_libreport.h"
28bab8
+#include <assert.h>
28bab8
+
28bab8
+bool string_cmp(const char *orig, const char *other)
28bab8
+{
28bab8
+    if (strcmp(orig, other) == 0)
28bab8
+        return true;
28bab8
+
28bab8
+    printf("Exp: '%s'\nGot: '%s'\n", orig, other);
28bab8
+    return false;
28bab8
+}
28bab8
+
28bab8
+int main(void)
28bab8
+{
28bab8
+    g_verbose=3;
28bab8
+
28bab8
+    setenv("TZ", "", 1);
28bab8
+    setenv("LC_ALL", "C", 1);
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = NULL,
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) == -EINVAL || !"0 string");
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"",
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) == -EINVAL || !"Empty string");
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"Fo:",
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) == -EINVAL || !"Contains :");
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"Foo = blah",
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) || !"Label contains = and space");
28bab8
+        assert(reported_to != NULL);
28bab8
+        assert(string_cmp("Foo = blah:\n", reported_to));
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"OnlyURL",
28bab8
+            .url = (char *)"http://test1.com",
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) || !"Only URL");
28bab8
+        assert(reported_to != NULL);
28bab8
+        assert(string_cmp("OnlyURL: URL=http://test1.com\n", reported_to));
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"OnlyBTHASH",
28bab8
+            .bthash = (char *)"0123456789ABCDEF",
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) || !"Only BTHASH");
28bab8
+        assert(reported_to != NULL);
28bab8
+        assert(string_cmp("OnlyBTHASH: BTHASH=0123456789ABCDEF\n", reported_to));
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"OnlyMSG",
28bab8
+            .msg = (char *)"Message = foo : blah!",
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) || !"Only MSG");
28bab8
+        assert(reported_to != NULL);
28bab8
+        assert(string_cmp("OnlyMSG: MSG=Message = foo : blah!\n", reported_to));
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"OnlyTIME",
28bab8
+            /* 2000-01-01-00:00:00 */
28bab8
+            .timestamp = 946684800,
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) || !"Only MSG");
28bab8
+        assert(reported_to != NULL);
28bab8
+        assert(string_cmp("OnlyTIME: TIME=2000-01-01-00:00:00\n", reported_to));
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        char *reported_to = NULL;
28bab8
+        report_result_t result = {
28bab8
+            .label = (char *)"Everything",
28bab8
+            /* 2000-01-01-00:00:00 */
28bab8
+            .timestamp = 946684800,
28bab8
+            .url = (char *)"http://epic.win",
28bab8
+            .bthash = (char *)"0123456789ABCDEF",
28bab8
+            .msg = (char *)"Exhausting libreport test!",
28bab8
+        };
28bab8
+
28bab8
+        assert(add_reported_to_entry_data(&reported_to, &result) || !"Everything");
28bab8
+        assert(reported_to != NULL);
28bab8
+        assert(string_cmp("Everything: TIME=2000-01-01-00:00:00 URL=http://epic.win BTHASH=0123456789ABCDEF MSG=Exhausting libreport test!\n", reported_to));
28bab8
+    }
28bab8
+}
28bab8
+]])
28bab8
+
28bab8
+
28bab8
 ## ---------------------- ##
28bab8
 ## parse_reported_to_data ##
28bab8
 ## ---------------------- ##
28bab8
@@ -87,6 +219,12 @@ bool parse_and_check(const char *reported_to, GList *expected)
28bab8
             goto finish;
28bab8
         }
28bab8
 
28bab8
+        if(e->timestamp != c->timestamp)
28bab8
+        {
28bab8
+            printf("Timestamps: '%lld' != '%lld'\n", e->timestamp, c->timestamp);
28bab8
+            goto finish;
28bab8
+        }
28bab8
+
28bab8
         if(!((e->url == NULL && c->url == NULL) || strcmp(e->url, c->url) == 0))
28bab8
         {
28bab8
             printf("'%s' != '%s'\n", e->url, c->url);
28bab8
@@ -116,20 +254,26 @@ int main(void)
28bab8
 {
28bab8
     g_verbose=3;
28bab8
 
28bab8
+    setenv("TZ", "", 1);
28bab8
+    setenv("LC_ALL", "C", 1);
28bab8
+
28bab8
 #define FIRST_LINE "Bugzilla: URL=https://goodluck.org"
28bab8
+#define FOURTH_LINE "Bugzilla: TIME=invalid URL=https://goodluck.org"
28bab8
     report_result_t first_result = {
28bab8
         .label = (char *)"Bugzilla",
28bab8
         .url   = (char *)"https://goodluck.org"
28bab8
     };
28bab8
 
28bab8
 #define SECOND_LINE "ABRT Server: BTHASH=81680083BIGBOOBS"
28bab8
+#define FIFTH_LINE "ABRT Server: TIME=invalid BTHASH=81680083BIGBOOBS"
28bab8
     report_result_t second_result = {
28bab8
         .label = (char *)"ABRT Server",
28bab8
         .bthash = (char *)"81680083BIGBOOBS"
28bab8
     };
28bab8
 
28bab8
-#define THIRD_LINE "RHTSupport: TIME=12345678 URL=https://access.redhat.com/home MSG=The world's best IT support"
28bab8
+#define THIRD_LINE "RHTSupport: TIME=2000-01-01-00:00:00 URL=https://access.redhat.com/home MSG=The world's best IT support"
28bab8
     report_result_t third_result = {
28bab8
+        .timestamp = 946684800,
28bab8
         .label = (char *)"RHTSupport",
28bab8
         .url = (char *)"https://access.redhat.com/home",
28bab8
         .msg = (char *)"The world's best IT support",
28bab8
@@ -137,9 +281,11 @@ int main(void)
28bab8
 
28bab8
     GList *expected = g_list_append(NULL, &first_result);
28bab8
     assert(parse_and_check(FIRST_LINE, expected));
28bab8
+    assert(parse_and_check(FOURTH_LINE, expected));
28bab8
 
28bab8
     expected = g_list_append(expected, &second_result);
28bab8
     assert(parse_and_check(FIRST_LINE"\n"SECOND_LINE, expected));
28bab8
+    assert(parse_and_check(FIRST_LINE"\n"FIFTH_LINE, expected));
28bab8
 
28bab8
     expected = g_list_append(expected, &third_result);
28bab8
     assert(parse_and_check(FIRST_LINE"\n"SECOND_LINE"\n"THIRD_LINE, expected));
28bab8
-- 
28bab8
1.8.3.1
28bab8