Added escaping special characters to their corresponding
authorMarkus Kauppila <markus.kauppila@gmail.com>
Wed, 22 Jun 2011 21:56:23 +0300
changeset 5709 2e5e43552878
parent 5708 41235b5cb4ea
child 5710 2f48255db3d8
Added escaping special characters to their corresponding entities in XML output.
test/test-automation/logger.c
test/test-automation/xml.c
test/test-automation/xml_logger.c
--- a/test/test-automation/logger.c	Wed Jun 22 18:13:06 2011 +0300
+++ b/test/test-automation/logger.c	Wed Jun 22 21:56:23 2011 +0300
@@ -31,7 +31,6 @@
 	fflush(stderr);
 }
 
-
 /*!
  * Test app for logging functionality
  */
@@ -66,15 +65,12 @@
 		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);
 
--- a/test/test-automation/xml.c	Wed Jun 22 18:13:06 2011 +0300
+++ b/test/test-automation/xml.c	Wed Jun 22 21:56:23 2011 +0300
@@ -104,6 +104,57 @@
 	}
 }
 
+
+/*!
+ * 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;
+}
+
+
+
 /*
 ===================
 
@@ -131,7 +182,6 @@
 	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 @@
 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);
 }
 
--- a/test/test-automation/xml_logger.c	Wed Jun 22 18:13:06 2011 +0300
+++ b/test/test-automation/xml_logger.c	Wed Jun 22 21:56:23 2011 +0300
@@ -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");