test/harness/testcaseio.c
author ali <ali@juiblex.co.uk>
Mon Jan 30 00:36:31 2012 +0000 (2012-01-30)
changeset 9 6a13fe0fc19e
parent 8 cf332d440466
child 11 4a80c6053a66
permissions -rw-r--r--
Add a testcase for user-defined typos
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <glib.h>
     5 #include <bl/bl.h>
     6 #include "testcaseparser.h"
     7 #include "testcaseinput.h"
     8 #include "testcaseio.h"
     9 
    10 /*
    11  * Read a testcase in from a file.
    12  * On error, print a suitable message on stderr and return NULL.
    13  * The returned testcase should be freed with testcase_free().
    14  */
    15 Testcase *testcase_parse_file(const char *filename)
    16 {
    17     Testcase *testcase;
    18     TestcaseParser *parser;
    19     TestcaseInput *input=NULL;
    20     char *s,*arg;
    21     const char *tag,*text;
    22     gboolean found_tag=FALSE;
    23     parser=testcase_parser_new_from_file(filename);
    24     if (!parser)
    25 	return NULL;
    26     if (!*testcase_parser_get_flag(parser))
    27     {
    28 	fprintf(stderr,"%s: Not a valid testcase (flag)\n",filename);
    29 	testcase_parser_free(parser);
    30 	return NULL;
    31     }
    32     testcase=g_new0(Testcase,1);
    33     testcase->basename=g_path_get_basename(filename);
    34     s=strrchr(testcase->basename,'.');
    35     if (s)
    36 	*s='\0';
    37     while(testcase_parser_get_next_tag(parser,&tag,&text))
    38     {
    39 	if (!input && !strcmp(tag,"INPUT"))
    40 	    input=testcase_input_new("TEST-XXXXXX",text);
    41 	else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')')
    42 	{
    43 	    arg=g_strndup(tag+6,strlen(tag)-7);
    44 	    s=g_path_get_dirname(arg);
    45 	    if (strcmp(s,"."))
    46 	    {
    47 		/*
    48 		 * Otherwise it would be possible to overwrite an arbitary
    49 		 * file on somebody's computer by getting them to run a
    50 		 * testcase!
    51 		 */
    52 		fprintf(stderr,
    53 		  "%s: Input files may not have a directory component\n",arg);
    54 		g_free(s);
    55 		g_free(arg);
    56 		testcase_free(testcase);
    57 		testcase_parser_free(parser);
    58 		return NULL;
    59 	    }
    60 	    g_free(s);
    61 	    testcase->inputs=g_slist_prepend(testcase->inputs,
    62 	      testcase_input_new(arg,text));
    63 	    if (!strstr(arg,"XXXXXX"))
    64 		testcase->flags|=TESTCASE_TMP_DIR;
    65 	    g_free(arg);
    66 	}
    67 	else if (!testcase->expected && !strcmp(tag,"EXPECTED"))
    68 	    testcase->expected=g_strdup(text);
    69 	else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
    70 	    testcase->encoding=g_strchomp(g_strdup(text));
    71 	else if (!testcase->encoding && !strcmp(tag,"OPTIONS"))
    72 	{
    73 	    testcase->options=g_strsplit(text,"\n",0);
    74 	    g_free(testcase->options[g_strv_length(testcase->options)-1]);
    75 	    testcase->options[g_strv_length(testcase->options)-1]=NULL;
    76 	}
    77 	else
    78 	{
    79 	    fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag);
    80 	    testcase_free(testcase);
    81 	    testcase_parser_free(parser);
    82 	    return NULL;
    83 	}
    84 	found_tag=TRUE;
    85     }
    86     if (!testcase_parser_at_eof(parser))
    87     {
    88 	if (found_tag)
    89 	    fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n",
    90 	      filename);
    91 	else
    92 	    fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n",
    93 	      filename);
    94 	testcase_free(testcase);
    95 	testcase_parser_free(parser);
    96 	return NULL;
    97     }
    98     if (!input)
    99 	input=testcase_input_new("TEST-XXXXXX",NULL);
   100     testcase->inputs=g_slist_prepend(testcase->inputs,input);
   101     testcase_parser_free(parser);
   102     return testcase;
   103 }