Skip to content

Instantly share code, notes, and snippets.

@dylanahsmith
Created October 17, 2011 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanahsmith/1291927 to your computer and use it in GitHub Desktop.
Save dylanahsmith/1291927 to your computer and use it in GitHub Desktop.
houdini_escape_xml tests
#include <houdini.h>
#include <buffer.h>
#include <string.h>
#include <stdio.h>
static unsigned int failures = 0;
static void test_input(const char *input, const char *expected_output)
{
struct buf *encoded = bufnew(128);
houdini_escape_xml(encoded, (uint8_t *)input, strlen(input));
if (strcmp(expected_output, (char *)encoded->data)) {
failures++;
fprintf(stderr, "encoded: input '%s' expected '%s' got '%s'\n",
input, expected_output, encoded->data);
}
}
int main(int argc, char **argv)
{
static const struct {
const char *input;
const char *expected_output;
} tests[] = {
{ "\x1d", "?" },
{ "\t\r\n ", "\t\r\n " },
{ "\"&'/<=>", "&quot;&amp;&apos;/&lt;=&gt;" },
{ "~\x7f\xc2\x80", "~\x7f\xc2\x80" },
{ "~\x7f\xc2\x80", "~\x7f\xc2\x80" },
{ "\x80\xbf", "??" }, /* start with intermediate bytes 10xxxxxx */
{ "\xc0\xa0\xc0\xbc\xc0\xbe", "???" }, /* non-short form */
{ "\xc2\xa2", "\xc2\xa2" }, /* cent */
{ "\xe2\x82\xac", "\xe2\x82\xac" }, /* euro */
{ "\xf0\xa4\xad\xa2", "\xf0\xa4\xad\xa2" }, /* 4 bytes char */
{ "\xed\x9f\xbf", "\xed\x9f\xbf" }, /* 0xd7ff */
{ "\xed\xa0\x80", "?" }, /* 0xd800 */
{ "\xed\xbf\xbf", "?" }, /* 0xdfff */
{ "\xee\x80\x80", "\xee\x80\x80" }, /* 0xe000 */
{ "\xef\xbf\xbd", "\xef\xbf\xbd" }, /* 0xfffd */
{ "\xef\xbf\xbe", "?" }, /* 0xfffe */
{ "\xf0\x90\x80\x80", "\xf0\x90\x80\x80" }, /* 0x10000 */
{ "\xf4\x8f\xbf\xbf", "\xf4\x8f\xbf\xbf" }, /* 0x10ffff */
{ "\xf4\x90\x80\x80", "?" }, /* 0x110000 */
{ "\xf8\xfc\xff", "???" }, /* invalid UTF-8 bytes 11111xxx */
};
int i;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
test_input(tests[i].input, tests[i].expected_output);
return failures < 255 ? failures : 255;
}
MODULE = main
SOURCES = $(wildcard *.c)
OBJS = $(SOURCES:%.c=%.o)
CFLAGS = -Wall -I. -ggdb -O0
all: $(MODULE)
$(MODULE): $(OBJS)
clean:
rm -f $(OBJS) $(MODULE)
.PHONY:
all clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment