Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added escaping special characters to their corresponding
Browse files Browse the repository at this point in the history
entities in XML output.
  • Loading branch information
mkauppila committed Jun 22, 2011
1 parent 9c36829 commit 8aeb539
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
6 changes: 1 addition & 5 deletions test/test-automation/logger.c
Expand Up @@ -31,7 +31,6 @@ LogGenericOutput(const char *message)
fflush(stderr);
}


/*!
* Test app for logging functionality
*/
Expand Down Expand Up @@ -66,15 +65,12 @@ main(int argc, char *argv[])
Log = PlainLog;
}

RunStarted(LogGenericOutput, "some_data_here", 0);
RunStarted(LogGenericOutput, "some_<data_>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);

Expand Down
56 changes: 54 additions & 2 deletions test/test-automation/xml.c
Expand Up @@ -104,6 +104,57 @@ PrintOpenTags()
}
}


/*!
* Converts the special characters ', ", <, >, and & to
* corresponding entities: &apos; &quot; &lt; &gt; and &amp;
*
* \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[] = {"&amp;", "&apos;", "&quot;", "&lt;", "&gt;"};
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;
}



/*
===================
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions test/test-automation/xml_logger.c
Expand Up @@ -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");
Expand Down

0 comments on commit 8aeb539

Please sign in to comment.