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

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