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