cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob d9d2b5d96e0a37022344fa87908c8eca4d54d58d 5050 bytes (raw)
$ git show systemtap: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
 
/*
 * Copyright (C) 2012-2013, Eric Wong <normalperson@yhbt.net>
 * License: GPLv3 or later (see COPYING for details)
 */
#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);
}

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;
	file->foff = 0;
	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.offset == 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.offset == 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.offset == 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.offset == 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.offset == 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.offset == 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.offset == len);
	}

	reset();
	return 0;
}

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