From e07d7c92d09427d8ec06a5e262663acb188e8154 Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Sun, 28 Dec 2014 19:20:24 +0100 Subject: [PATCH] Show warning if an XML parser probably parsed a bad type of medata (New XML parser warning type CR_XML_WARNING_BADMDTYPE) --- src/python/xml_dump-py.c | 3 ++- src/xml_parser.h | 1 + src/xml_parser_filelists.c | 11 +++++++++++ src/xml_parser_internal.h | 12 ++++++++++++ src/xml_parser_other.c | 11 +++++++++++ src/xml_parser_primary.c | 11 +++++++++++ src/xml_parser_repomd.c | 13 +++++++++++++ src/xml_parser_updateinfo.c | 13 +++++++++++++ tests/test_xml_parser_filelists.c | 22 ++++++++++++++++++++++ 9 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/python/xml_dump-py.c b/src/python/xml_dump-py.c index a74eaf9..ad2747d 100644 --- a/src/python/xml_dump-py.c +++ b/src/python/xml_dump-py.c @@ -138,7 +138,7 @@ PyObject * py_xml_dump_updaterecord(PyObject *self, PyObject *args) { PyObject *py_rec, *py_str; - char *xml; + char *xml = NULL; GError *err = NULL; CR_UNUSED(self); @@ -150,6 +150,7 @@ py_xml_dump_updaterecord(PyObject *self, PyObject *args) xml = cr_xml_dump_updaterecord(UpdateRecord_FromPyObject(py_rec), &err); if (err) { nice_exception(&err, NULL); + free(xml); return NULL; } diff --git a/src/xml_parser.h b/src/xml_parser.h index 8e5424c..75ab607 100644 --- a/src/xml_parser.h +++ b/src/xml_parser.h @@ -45,6 +45,7 @@ typedef enum { CR_XML_WARNING_UNKNOWNVAL, /*!< Unknown tag or attribute value */ CR_XML_WARNING_BADATTRVAL, /*!< Bad attribute value */ CR_XML_WARNING_MISSINGVAL, /*!< Missing tag value */ + CR_XML_WARNING_BADMDTYPE, /*!< Bad metadata type (expected mandatory tag was not found) */ CR_XML_WARNING_SENTINEL, } cr_XmlParserWarningType; diff --git a/src/xml_parser_filelists.c b/src/xml_parser_filelists.c index 32a5d2c..e24919e 100644 --- a/src/xml_parser_filelists.c +++ b/src/xml_parser_filelists.c @@ -102,7 +102,10 @@ cr_start_handler(void *pdata, const char *element, const char **attr) switch(pd->state) { case STATE_START: + break; + case STATE_FILELISTS: + pd->main_tag_found = TRUE; break; case STATE_PACKAGE: { @@ -329,6 +332,14 @@ cr_xml_parse_filelists(const char *path, if (tmp_err) g_propagate_error(err, tmp_err); + // Warning if file was probably a different type than expected + + if (!pd->main_tag_found && ret == CRE_OK) + cr_xml_parser_warning(pd, CR_XML_WARNING_BADMDTYPE, + "The file don't contain the expected element " + "\"\" - The file probably isn't " + "a valid filelists.xml"); + // Clean up if (ret != CRE_OK && newpkgcb == cr_newpkgcb) { diff --git a/src/xml_parser_internal.h b/src/xml_parser_internal.h index 3c5ad1c..6b400eb 100644 --- a/src/xml_parser_internal.h +++ b/src/xml_parser_internal.h @@ -82,6 +82,18 @@ typedef struct _cr_ParserData { cr_StatesSwitch **swtab; /*!< Pointers to statesswitches table */ unsigned int *sbtab; /*!< stab[to_state] = from_state */ + /* Common stuf */ + + gboolean main_tag_found; /*!< + Was the main tag present? E.g.: + For primary.xml + For filelists.xml + For other.xml + For repomd.xml + For updateinfo.xml + If the main tag is missing the most likely the input file + was a different then expected type */ + /* Package stuff */ void *newpkgcb_data; /*!< diff --git a/src/xml_parser_other.c b/src/xml_parser_other.c index 5a34bcd..ea64215 100644 --- a/src/xml_parser_other.c +++ b/src/xml_parser_other.c @@ -102,7 +102,10 @@ cr_start_handler(void *pdata, const char *element, const char **attr) switch(pd->state) { case STATE_START: + break; + case STATE_OTHERDATA: + pd->main_tag_found = TRUE; break; case STATE_PACKAGE: { @@ -328,6 +331,14 @@ cr_xml_parse_other(const char *path, if (tmp_err) g_propagate_error(err, tmp_err); + // Warning if file was probably a different type than expected + + if (!pd->main_tag_found && ret == CRE_OK) + cr_xml_parser_warning(pd, CR_XML_WARNING_BADMDTYPE, + "The file don't contain the expected element " + "\"\" - The file probably isn't " + "a valid other.xml"); + // Clean up if (ret != CRE_OK && newpkgcb == cr_newpkgcb) { diff --git a/src/xml_parser_primary.c b/src/xml_parser_primary.c index d33c5eb..019bf62 100644 --- a/src/xml_parser_primary.c +++ b/src/xml_parser_primary.c @@ -168,7 +168,10 @@ cr_start_handler(void *pdata, const char *element, const char **attr) switch(pd->state) { case STATE_START: + break; + case STATE_METADATA: + pd->main_tag_found = TRUE; break; case STATE_PACKAGE: @@ -705,6 +708,14 @@ cr_xml_parse_primary(const char *path, if (tmp_err) g_propagate_error(err, tmp_err); + // Warning if file was probably a different type than expected + + if (!pd->main_tag_found && ret == CRE_OK) + cr_xml_parser_warning(pd, CR_XML_WARNING_BADMDTYPE, + "The file don't contain the expected element " + "\"\" - The file probably isn't " + "a valid primary.xml"); + // Clean up if (ret != CRE_OK && newpkgcb == cr_newpkgcb) { diff --git a/src/xml_parser_repomd.c b/src/xml_parser_repomd.c index 0484779..d445a95 100644 --- a/src/xml_parser_repomd.c +++ b/src/xml_parser_repomd.c @@ -122,7 +122,12 @@ cr_start_handler(void *pdata, const char *element, const char **attr) switch(pd->state) { case STATE_START: + break; + case STATE_REPOMD: + pd->main_tag_found = TRUE; + break; + case STATE_REVISION: case STATE_TAGS: case STATE_REPO: @@ -424,6 +429,14 @@ cr_xml_parse_repomd(const char *path, if (tmp_err) g_propagate_error(err, tmp_err); + // Warning if file was probably a different type than expected + + if (!pd->main_tag_found && ret == CRE_OK) + cr_xml_parser_warning(pd, CR_XML_WARNING_BADMDTYPE, + "The file don't contain the expected element " + "\"\" - The file probably isn't " + "a valid repomd.xml"); + // Clean up cr_xml_parser_data_free(pd); diff --git a/src/xml_parser_updateinfo.c b/src/xml_parser_updateinfo.c index 489c0c7..9c0c33b 100644 --- a/src/xml_parser_updateinfo.c +++ b/src/xml_parser_updateinfo.c @@ -145,7 +145,12 @@ cr_start_handler(void *pdata, const char *element, const char **attr) switch(pd->state) { case STATE_START: + break; + case STATE_UPDATES: + pd->main_tag_found = TRUE; + break; + case STATE_ID: case STATE_TITLE: case STATE_RIGHTS: @@ -523,6 +528,14 @@ cr_xml_parse_updateinfo(const char *path, if (tmp_err) g_propagate_error(err, tmp_err); + // Warning if file was probably a different type than expected + + if (!pd->main_tag_found && ret == CRE_OK) + cr_xml_parser_warning(pd, CR_XML_WARNING_BADMDTYPE, + "The file don't contain the expected element " + "\"\" - The file probably isn't " + "a valid updates.xml"); + // Clean up cr_xml_parser_data_free(pd); diff --git a/tests/test_xml_parser_filelists.c b/tests/test_xml_parser_filelists.c index 166e178..d7379f5 100644 --- a/tests/test_xml_parser_filelists.c +++ b/tests/test_xml_parser_filelists.c @@ -316,6 +316,26 @@ test_cr_xml_parse_filelists_bad_file_type_01(void) g_free(warnmsgs); } +static void +test_cr_xml_parse_different_md_type(void) +{ + char *warnmsgs; + int parsed = 0; + GString *warn_strings = g_string_new(0); + GError *tmp_err = NULL; + int ret = cr_xml_parse_filelists(TEST_REPO_01_OTHER, NULL, NULL, + pkgcb, &parsed, warningcb, + warn_strings, &tmp_err); + g_assert(tmp_err == NULL); + g_assert_cmpint(ret, ==, CRE_OK); + g_assert_cmpint(parsed, ==, 0); + warnmsgs = g_string_free(warn_strings, FALSE); + g_assert_cmpstr(warnmsgs, ==, "Unknown element \"otherdata\";" + "The file don't contain the expected element \"\" - " + "The file probably isn't a valid filelists.xml;"); + g_free(warnmsgs); +} + int main(int argc, char *argv[]) { @@ -349,6 +369,8 @@ main(int argc, char *argv[]) test_cr_xml_parse_filelists_bad_file_type_00); g_test_add_func("/xml_parser_filelists/test_cr_xml_parse_filelists_bad_file_type_01", test_cr_xml_parse_filelists_bad_file_type_01); + g_test_add_func("/xml_parser_filelists/test_cr_xml_parse_different_md_type", + test_cr_xml_parse_different_md_type); return g_test_run(); } -- 2.7.4