From e6225c947b87d3afd457cc433fc272cc15cf8bf1 Mon Sep 17 00:00:00 2001 From: Markus Kauppila Date: Wed, 22 Jun 2011 21:56:23 +0300 Subject: [PATCH] Added escaping special characters to their corresponding entities in XML output. --- test/test-automation/logger.c | 6 +--- test/test-automation/xml.c | 56 +++++++++++++++++++++++++++++-- test/test-automation/xml_logger.c | 2 ++ 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/test/test-automation/logger.c b/test/test-automation/logger.c index 2060f2b07..5a6d548b8 100644 --- a/test/test-automation/logger.c +++ b/test/test-automation/logger.c @@ -31,7 +31,6 @@ LogGenericOutput(const char *message) fflush(stderr); } - /*! * Test app for logging functionality */ @@ -66,15 +65,12 @@ main(int argc, char *argv[]) Log = PlainLog; } - RunStarted(LogGenericOutput, "some_data_here", 0); + RunStarted(LogGenericOutput, "some_here&here", 0); SuiteStarted("Suite data here", 0); TestStarted("test1", "suite", "desc", 0); TestEnded("test1", "suite", 0, 0, 0, 0); - //XMLTestStarted("test2", "desc", 0); - //XMLTestEnded("test2", "desc", 0, 0, 0, 0); - SuiteEnded(0, 0, 0, 0.0f, 0); RunEnded(0, 0, 0, 0, 0, 0); diff --git a/test/test-automation/xml.c b/test/test-automation/xml.c index 28f07a46f..c222d4e66 100644 --- a/test/test-automation/xml.c +++ b/test/test-automation/xml.c @@ -104,6 +104,57 @@ PrintOpenTags() } } + +/*! + * Converts the special characters ', ", <, >, and & to + * corresponding entities: ' " < > and & + * + * \param string String to be escaped + * \return Escaped string + */ +const char *EscapeString(const char *string) { + const int bufferSize = 4096; + char buffer[bufferSize]; + memset(buffer, 0, bufferSize); + + // prevents the code doing a 'bus error' + char stringBuffer[bufferSize]; + strncpy(stringBuffer, string, bufferSize); + + // Ampersand (&) must be first, otherwise it'll mess up the other entities + char *characters[] = {"&", "'", "\"", "<", ">"}; + char *entities[] = {"&", "'", """, "<", ">"}; + int maxCount = 5; + + int counter = 0; + for(; counter < maxCount; ++counter) { + char *character = characters[counter]; + char *entity = entities[counter]; + + if(strstr(stringBuffer, character) == NULL) + continue; + + char *token = strtok(stringBuffer, character); + while(token) { + char *nextToken = strtok(NULL, character); + + //! \todo use strncat and count the bytes left in the buffer + strcat(buffer, token); + if(nextToken) + strcat(buffer, entity); + + token = nextToken; + } + + memcpy(stringBuffer, buffer, bufferSize); + memset(buffer, 0, bufferSize); + } + + return stringBuffer; +} + + + /* =================== @@ -131,7 +182,6 @@ XMLOpenDocument(const char *rootTag, LogOutputFp log) snprintf(buffer, bufferSize, "<%s>", rootTag); logger(buffer); - // add open tag AddOpenTag(rootTag); root = rootTag; // it's fine, as long as rootTag points to static memory? @@ -167,8 +217,10 @@ XMLOpenElementWithAttribute(const char *tag, Attribute *attribute) void XMLAddContent(const char *content) { + const char *escapedContent = EscapeString(content); + memset(buffer, 0, bufferSize); - snprintf(buffer, bufferSize, "%s", content); + snprintf(buffer, bufferSize, "%s", escapedContent); logger(buffer); } diff --git a/test/test-automation/xml_logger.c b/test/test-automation/xml_logger.c index db331dcbf..1522f2a65 100644 --- a/test/test-automation/xml_logger.c +++ b/test/test-automation/xml_logger.c @@ -26,6 +26,8 @@ void XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime) { + //! \todo Giving outputFn to the function is awful, fix it + //! Make the outputting differently XMLOpenDocument("testlog", outputFn); XMLOpenElement("parameters");