Fix unit test

This commit is contained in:
Sergey Lyubka 2015-05-20 10:50:32 +01:00
parent 29454e2296
commit 14d6f71f82
3 changed files with 27 additions and 33 deletions

View File

@ -5016,20 +5016,16 @@ struct mg_connection *mg_next(struct mg_server *s, struct mg_connection *c) {
static int get_var(const char *data, size_t data_len, const char *name,
char *dst, size_t dst_len, int n) {
const char *p, *e, *s;
const char *p, *e = data + data_len, *s;
size_t name_len;
int i, len;
int i = 0, len = -1;
if (dst == NULL || dst_len == 0) {
len = -2;
} else if (data == NULL || name == NULL || data_len == 0) {
len = -1;
dst[0] = '\0';
} else {
i = 0;
name_len = strlen(name);
e = data + data_len;
len = -1;
dst[0] = '\0';
// data is "var1=val1&var2=val2...". Find variable first
@ -5037,8 +5033,7 @@ static int get_var(const char *data, size_t data_len, const char *name,
if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
!mg_strncasecmp(name, p, name_len)) {
if(n != i++)
continue;
if (n != i++) continue;
// Point p to variable value
p += name_len + 1;
@ -5065,21 +5060,21 @@ static int get_var(const char *data, size_t data_len, const char *name,
return len;
}
int mg_get_var(const struct mg_connection *conn, const char *name,
char *dst, size_t dst_len) {
return mg_get_n_var(conn, name, dst, dst_len, 0);
}
int mg_get_n_var(const struct mg_connection *conn, const char *name,
int mg_get_var_n(const struct mg_connection *conn, const char *name,
char *dst, size_t dst_len, int n) {
int len = get_var(conn->query_string, conn->query_string == NULL ? 0 :
strlen(conn->query_string), name, dst, dst_len, n);
if (len == -1) {
len = get_var(conn->content, conn->content_len, name, dst, dst_len, n);;
len = get_var(conn->content, conn->content_len, name, dst, dst_len, n);
}
return len;
}
int mg_get_var(const struct mg_connection *conn, const char *name,
char *dst, size_t dst_len) {
return mg_get_var_n(conn, name, dst, dst_len, 0);
}
static int get_line_len(const char *buf, int buf_len) {
int len = 0;
while (len < buf_len && buf[len] != '\n') len++;
@ -5308,7 +5303,7 @@ static void send_ns_event(struct ns_connection *nc, int ev, void *p) {
}
#else
static void send_ns_event(struct ns_connection *nc, int ev, void *p) {
(void) nc; (void) p;
(void) nc; (void) p; (void) ev;
}
#endif

View File

@ -121,8 +121,8 @@ const char *mg_get_header(const struct mg_connection *, const char *name);
const char *mg_get_mime_type(const char *name, const char *default_mime_type);
int mg_get_var(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len);
int mg_get_n_var(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len, int n);
int mg_get_var_n(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len, int n);
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
int mg_parse_multipart(const char *buf, int buf_len,
char *var_name, int var_name_len,

View File

@ -224,29 +224,28 @@ static const char *test_remove_double_dots() {
}
static const char *test_get_var(void) {
static const char *post[] = {
"a=1&&b=2&d&=&c=3%20&e=",
"q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
NULL
};
static const char *data = "a=1&&b=2&d&=&c=3%20&e=&k=aa&a=23";
static const char *data2 = "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=";
char buf[20];
ASSERT(get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
ASSERT(get_var(data, strlen(data), "a", buf, sizeof(buf), 0) == 1);
ASSERT(buf[0] == '1' && buf[1] == '\0');
ASSERT(get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
ASSERT(get_var(data, strlen(data), "a", buf, sizeof(buf), 1) == 2);
ASSERT(strcmp(buf, "23") == 0);
ASSERT(get_var(data, strlen(data), "b", buf, sizeof(buf), 0) == 1);
ASSERT(buf[0] == '2' && buf[1] == '\0');
ASSERT(get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
ASSERT(get_var(data, strlen(data), "c", buf, sizeof(buf), 0) == 2);
ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
ASSERT(get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
ASSERT(get_var(data, strlen(data), "e", buf, sizeof(buf), 0) == 0);
ASSERT(buf[0] == '\0');
ASSERT(get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
ASSERT(get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
ASSERT(get_var(data, strlen(data), "d", buf, sizeof(buf), 0) == -1);
ASSERT(get_var(data, strlen(data), "c", buf, 2, 0) == -2);
ASSERT(get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
ASSERT(get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
ASSERT(get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
ASSERT(get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
ASSERT(get_var(data, strlen(data), "x", NULL, 10, 0) == -2);
ASSERT(get_var(data, strlen(data), "x", buf, 0, 0) == -2);
ASSERT(get_var(data2, strlen(data2), "st", buf, 16, 0) == -2);
ASSERT(get_var(data2, strlen(data2), "st", buf, 17, 0) == 16);
return NULL;
}