summaryrefslogtreecommitdiff
path: root/test/testutils.h
blob: 6e07660a19e50a9df8dd23d70d9485c4a95d798f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef dom_test_testutils_h_
#define dom_test_testutils_h_

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <setjmp.h>

#include "exceptions.h"
#include "utils.h"
#include "xmlbinding.h"
#include "xmlparser.h"

#ifndef UNUSED
#define UNUSED(x) ((x) = (x))
#endif

/* Usage:
 	TRY
 		THROW(DOM_NOT_FOUND_ERR);
		THROW_IF_ERR(dom_document_get_doctype(...));
	CATCH(ex)
		printf("exception: %d\n", ex);
	ENDTRY
*/
#define TRY              __exvalue=setjmp(__exbuf); \
                         if (__exvalue==0) {
#define CATCH(x)         } else {                   \
                         int x = __exvalue;
#define ENDTRY           }
#define THROW(x)         longjmp(__exbuf, x)

#define THROW_IF_ERR(x)    \
  do {                     \
    int err = x;           \
    if (err != DOM_NO_ERR) \
      THROW(err);          \
    } while (0)

jmp_buf __exbuf;
int __exvalue;

/* Redefine assert, so we can simply use the standard assert mechanism
 * within testcases and exit with the right output for the testrunner
 * to do the right thing. */
void __assert2(const char *expr, const char *function,
		const char *file, int line);

void __assert2(const char *expr, const char *function,
		const char *file, int line)
{
	UNUSED(function);
	UNUSED(file);

	printf("FAIL - %s at line %d\n", expr, line);

	exit(EXIT_FAILURE);
}

#define assert(expr) \
  ((void) ((expr) || (__assert2 (#expr, __func__, __FILE__, __LINE__), 0)))

static void *myrealloc(void *ptr, size_t len, void *pw)
{
	UNUSED(pw);

	return realloc(ptr, len);
}

static void mymsg(uint32_t severity, void *ctx, const char *msg, ...)
{
	va_list l;

	UNUSED(ctx);

	va_start(l, msg);

	fprintf(stderr, "%d: ", severity);
	vfprintf(stderr, msg, l);
	fprintf(stderr, "\n");
}

typedef struct TestObject {
	xml_parser *parser;
	struct dom_document *doc;
} TestObject;

TestObject *test_object_create(int argc, char **argv,
		const char *uri, bool will_be_modified);
struct dom_document *test_object_get_doc(TestObject *obj);
const char *test_object_get_mimetype(TestObject *obj);

TestObject *test_object_create(int argc, char **argv,
		const char *uri, bool will_be_modified)
{
	static bool xml_parser_initialised;

	char fnbuf[1024];
#define CHUNK_SIZE 4096
	uint8_t buf[CHUNK_SIZE];
	FILE *fp;
	size_t len;
	TestObject *ret;

	UNUSED(will_be_modified);

	if (argc != 2) {
		printf("Usage: %s <datapath>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	if (xml_parser_initialised == false) {
		assert(xml_dom_binding_initialise(myrealloc, NULL) == XML_OK);

		xml_parser_initialised = true;
	}

	snprintf(fnbuf, sizeof fnbuf, "%s/%s", argv[1], uri);

	ret = malloc(sizeof(TestObject));
	if (ret == NULL)
		return NULL;

	ret->parser = xml_parser_create(NULL, "UTF-8", myrealloc, NULL,
			mymsg, NULL);
	if (ret->parser == NULL) {
		free(ret);
		return NULL;
	}

	fp = fopen(fnbuf, "r");
	if (fp == NULL) {
		xml_parser_destroy(ret->parser);
		free(ret);
		return NULL;
	}

	fseek(fp, 0, SEEK_END);
	len = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	while (len > CHUNK_SIZE) {
		fread(buf, 1, CHUNK_SIZE, fp);

		assert(xml_parser_parse_chunk(ret->parser, buf,
				CHUNK_SIZE) == XML_OK);

		len -= CHUNK_SIZE;
	}

	if (len > 0) {
		fread(buf, 1, len, fp);

		assert(xml_parser_parse_chunk(ret->parser, buf,
				len) == XML_OK);

		len = 0;
	}

	assert(xml_parser_completed(ret->parser) == XML_OK);

	fclose(fp);

	ret->doc = xml_parser_get_document(ret->parser);

	xml_parser_destroy(ret->parser);
	ret->parser = NULL;

	return ret;

#undef CHUNK_SIZE
}

struct dom_document *test_object_get_doc(TestObject *obj)
{
	return obj->doc;
}

const char *test_object_get_mimetype(TestObject *obj)
{
	UNUSED(obj);

	return "text/xml";
}

#endif