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

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