summaryrefslogtreecommitdiff
path: root/test/regression/stream-nomem.c
blob: 2f7707ec19816f129f928a961c03fe67a0387e47 (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
#include <stdio.h>
#include <string.h>

#include <parserutils/parserutils.h>
#include <parserutils/input/inputstream.h>

#include "utils/utils.h"

#include "testutils.h"

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

	return realloc(ptr, len);
}

int main(int argc, char **argv)
{
	parserutils_inputstream *stream;

	/* This is specially calculated so that the inputstream is forced to 
	 * reallocate (it assumes that the inputstream's buffer chunk size 
	 * is 4k) */
#define BUFFER_SIZE (4096 + 4)
	uint8_t input_buffer[BUFFER_SIZE];
//	uint8_t *buffer;
//	size_t buflen;
	const uint8_t *c;
	size_t clen;

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

	/* Populate the buffer with something sane */
	memset(input_buffer, 'a', BUFFER_SIZE);
	/* Now, set up our test data */
	input_buffer[BUFFER_SIZE - 1] = '5';
	input_buffer[BUFFER_SIZE - 2] = '4';
	input_buffer[BUFFER_SIZE - 3] = '\xbd';
	input_buffer[BUFFER_SIZE - 4] = '\xbf';
	/* This byte will occupy the 4095th byte in the buffer and
	 * thus cause the entirety of U+FFFD to be buffered until after
	 * the buffer has been enlarged */
	input_buffer[BUFFER_SIZE - 5] = '\xef';
	input_buffer[BUFFER_SIZE - 6] = '3';
	input_buffer[BUFFER_SIZE - 7] = '2';
	input_buffer[BUFFER_SIZE - 8] = '1';

	assert(parserutils_inputstream_create("UTF-8", 0, 
			NULL, myrealloc, NULL, &stream) == PARSERUTILS_OK);

	assert(parserutils_inputstream_append(stream, 
			input_buffer, BUFFER_SIZE) == PARSERUTILS_OK);

	assert(parserutils_inputstream_append(stream, NULL, 0) == 
			PARSERUTILS_OK);

	while (parserutils_inputstream_peek(stream, 0, &c, &clen) != 
			PARSERUTILS_EOF)
		parserutils_inputstream_advance(stream, clen);

/*
	assert(css_inputstream_claim_buffer(stream, &buffer, &buflen) == 
			CSS_OK);

	assert(buflen == BUFFER_SIZE);

	printf("Buffer: '%.*s'\n", 8, buffer + (BUFFER_SIZE - 8));

	assert( buffer[BUFFER_SIZE - 6] == '3' && 
		buffer[BUFFER_SIZE - 5] == (uint8_t) '\xef' && 
		buffer[BUFFER_SIZE - 4] == (uint8_t) '\xbf' && 
		buffer[BUFFER_SIZE - 3] == (uint8_t) '\xbd' && 
		buffer[BUFFER_SIZE - 2] == '4');

	free(buffer);
*/

	parserutils_inputstream_destroy(stream);

	printf("PASS\n");

	return 0;
}