cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 23ba5868e6439d93be220ecdfb567c3e9cd3be74 5424 bytes (raw)
$ git show HEAD:test/chunk-parser-1.c	# shows this blob on the CLI

  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
 
/*
 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 */
#include "check.h"

static struct mog_http xhttp;
static struct mog_http *http = &xhttp;
static char *buf;
static size_t len;
static enum mog_parser_state state;
static FILE *tmpfp;
static int tmpfd;

static void reset(void)
{
	free(buf);
	assert(0 == fclose(tmpfp));
	mog_chunk_init(http);
}

/*
 * we do this because we used tmpfile to generate a temporary file
 * and did not use any existing API to open the file.
 */
static void test_mog_file_init(struct mog_file *file)
{
	struct mog_svc *svc = file->svc;
	struct mog_ioq *ioq = file->ioq;

	memset(file, 0, sizeof(struct mog_file));
	file->svc = svc;
	file->ioq = ioq;
}

static void buf_set(const char *s)
{
	struct mog_file *file;

	http->_p.chunked = 1;
	reset();
	tmpfp = tmpfile();
	assert(tmpfp != NULL && "tmpfile(3) failed");
	tmpfd = fileno(tmpfp);
	assert(tmpfd >= 0 && "invalid fd");
	http->forward = mog_fd_init(tmpfd, MOG_FD_TYPE_FILE);
	file = &http->forward->as.file;
	test_mog_file_init(file);
	buf = xstrdup(s);
	len = strlen(s);
}

int main(void)
{
	tmpfp = tmpfile();
	assert(tmpfp != NULL && "tmpfile(3) failed");

	if ("normal chunk") {
		buf_set("666\r\n");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_DONE);
		assert(http->_p.content_len == 0x666);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_DATA);
	}

	if ("incomplete chunk") {
		buf_set("666\r");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_CONTINUE);
		assert(http->_p.content_len == 0x666);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_SIZE);
	}

	if ("bad chunk") {
		buf_set("zzz\r\n");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_ERROR);
	}

	if ("normal chunk with extension") {
		buf_set("abcde; foo=bar\r\n");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_DONE);
		assert(http->_p.content_len == 0xabcde);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_DATA);
	}

	if ("chunk with complete header and data") {
		char tmp[5];
		buf_set("5\r\nabcde");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_CONTINUE);
		assert(http->_p.content_len == 0);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_SIZE);
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abcde", sizeof(tmp)));
	}

	if ("chunk with complete header and data and incomplete chunk") {
		char tmp[5];
		buf_set("5\r\nabcde\r\n3");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_CONTINUE);
		assert(http->_p.content_len == 3);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_SIZE);
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abcde", sizeof(tmp)));
		assert(http->_p.buf_off == len);
	}
	if ("multiple chunks with end") {
		char tmp[8];
		buf_set("5\r\nabcde\r\n3\r\n123\r\n0\r\n\r\n");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_DONE);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_DONE);
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abcde123", sizeof(tmp)));
		assert(http->_p.buf_off == len);
	}

	if ("multiple chunks with trailer") {
		char tmp[8];
		buf_set("5\r\nabcde\r\n3\r\n123\r\n0\r\nFoo: bar\r\n\r\n");
		state = mog_chunk_parse(http, buf, len);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_DONE);
		assert(state == MOG_PARSER_DONE);
		assert(http->_p.content_len == 0);
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abcde123", sizeof(tmp)));
		assert(http->_p.buf_off == len);
	}

	if ("multiple chunks with almost end") {
		char tmp[8];
		buf_set("5\r\nabcde\r\n3\r\n123\r\n0\r\n");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_DONE);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_DATA);
		assert(http->_p.content_len == 0);
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abcde123", sizeof(tmp)));
		assert(http->_p.buf_off == len);
	}

	if ("multiple chunks with almost end (more)") {
		char tmp[8];
		buf_set("5\r\nabcde\r\n3\r\n123\r\n0\r\n\r");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_CONTINUE);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_TRAILER);
		assert(http->_p.content_len == 0);
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abcde123", sizeof(tmp)));
		assert(http->_p.buf_off == len);
	}

	if ("multiple chunks with incomplete") {
		char tmp[7];
		buf_set("5\r\nabcde\r\n3\r\n12");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_DONE);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_DATA);
		assert(http->_p.content_len == 1);
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abcde12", sizeof(tmp)));
		assert(http->_p.buf_off == len);
	}

	if ("incomplete data") {
		char tmp[3];
		buf_set("666\r\nabc");
		state = mog_chunk_parse(http, buf, len);
		assert(state == MOG_PARSER_DONE);
		assert(http->_p.chunk_state == MOG_CHUNK_STATE_DATA);
		assert(http->_p.content_len == (0x666 - sizeof(tmp)));
		assert(sizeof(tmp) == pread(tmpfd, tmp, sizeof(tmp), 0));
		assert(0 == memcmp(tmp, "abc", sizeof(tmp)));
		assert(http->_p.buf_off == len);
	}

	reset();
	return 0;
}

git clone https://yhbt.net/cmogstored.git