Mongoose函数测试
来源:互联网 发布:ppt数据编辑不了 编辑:程序博客网 时间:2024/06/07 09:21
remove_double_dots_and_double_slashes函数测试:
static void test_remove_double_dots() { struct { char before[20], after[20]; } data[] = { {"////a", "/a"}, {"/.....", "/."}, {"/......", "/"}, {"...", "..."}, {"/...///", "/./"}, {"/a...///", "/a.../"}, {"/.x", "/.x"}, {"/\\", "/"}, {"/a\\", "/a\\"}, {"/a\\\\...", "/a\\."}, }; size_t i; for (i = 0; i < ARRAY_SIZE(data); i++) { printf("[%s] -> [%s]\n", data[i].before, data[i].after); remove_double_dots_and_double_slashes(data[i].before); ASSERT(strcmp(data[i].before, data[i].after) == 0); }}
测试结果:
match_prefix函数测试:类似于正则表达式
static void test_match_prefix(void) { ASSERT(match_prefix("/api", 4, "/api") == 4); ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3); ASSERT(match_prefix("/a/", 3, "/ab/c") == -1); ASSERT(match_prefix("/*/", 3, "/ab/c") == 4); ASSERT(match_prefix("**", 2, "/a/b/c") == 6); ASSERT(match_prefix("/*", 2, "/a/b/c") == 2); ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2); ASSERT(match_prefix("**/", 3, "/a/b/c") == 5); ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5); ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2); ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2); ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1); ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1); ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12); ASSERT(match_prefix("**/", 3, "/a/b/c") == 5); ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1); ASSERT(match_prefix("**/$", 4, "/a/b/") == 5); ASSERT(match_prefix("$", 1, "") == 0); ASSERT(match_prefix("$", 1, "x") == -1); ASSERT(match_prefix("*$", 2, "x") == 1); ASSERT(match_prefix("/$", 2, "/") == 1); ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1); ASSERT(match_prefix("**/$", 4, "/a/b/") == 5); ASSERT(match_prefix("*", 1, "/hello/") == 0); ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1); ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6); ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);}
测试结果:以上都正确
skip_quoted函数测试
static void test_skip_quoted(void) { char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p; p = skip_quoted(&s, ", ", ", ", 0); ASSERT(p != NULL && !strcmp(p, "a=1")); p = skip_quoted(&s, ", ", ", ", 0); ASSERT(p != NULL && !strcmp(p, "b=2")); // TODO(lsm): fix this#if 0 p = skip_quoted(&s, "'", ", ", '\\'); p = skip_quoted(&s, "'", ", ", '\\'); printf("[%s]\n", p); ASSERT(p != NULL && !strcmp(p, "hi ' there"));#endif}
parse_http_request()函数测试
static void test_parse_http_request() { struct mg_request_info ri; char req1[] = "GET / HTTP/1.1\r\n\r\n"; char req2[] = "BLAH / HTTP/1.1\r\n\r\n"; char req3[] = "GET / HTTP/1.1\r\nBah\r\n"; char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n"; ASSERT(parse_http_request(req1, sizeof(req1), &ri) == sizeof(req1) - 1); ASSERT(strcmp(ri.http_version, "1.1") == 0); ASSERT(ri.num_headers == 0); ASSERT(parse_http_request(req2, sizeof(req2), &ri) == -1); ASSERT(parse_http_request(req3, sizeof(req3), &ri) == -1); // TODO(lsm): Fix this. Header value may span multiple lines. ASSERT(parse_http_request(req4, sizeof(req4), &ri) == sizeof(req4) - 1); ASSERT(ri.num_headers == 3); ASSERT(strcmp(ri.http_headers[0].name, "A") == 0); ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0); ASSERT(strcmp(ri.http_headers[1].name, "B") == 0); ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0); ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0); ASSERT(strcmp(ri.http_headers[2].value, "") == 0); // TODO(lsm): add more tests.}
should_keep_alive函数测试 :
返回0的条件是其中一个成立 ,否则返回1
conn->must_close =1
conn->status_code == 401
config[ENABLE_KEEP_ALIVE], "yes") != 0 ||
header != NULL && mg_strcasecmp(header, "keep-alive") != 0
header == NULL && http_version && strcmp(http_version, "1.1")
static void test_should_keep_alive(void) { struct mg_connection conn; struct mg_context ctx; char req1[] = "GET / HTTP/1.1\r\n\r\n"; char req2[] = "GET / HTTP/1.0\r\n\r\n"; char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"; char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"; memset(&conn, 0, sizeof(conn)); conn.ctx = &ctx; parse_http_request(req1, sizeof(req1), &conn.request_info); ctx.config[ENABLE_KEEP_ALIVE] = "no"; ASSERT(should_keep_alive(&conn) == 0); ctx.config[ENABLE_KEEP_ALIVE] = "yes"; ASSERT(should_keep_alive(&conn) == 1); conn.must_close = 1; ASSERT(should_keep_alive(&conn) == 0); conn.must_close = 0; parse_http_request(req2, sizeof(req2), &conn.request_info); ASSERT(should_keep_alive(&conn) == 0); parse_http_request(req3, sizeof(req3), &conn.request_info); ASSERT(should_keep_alive(&conn) == 0); parse_http_request(req4, sizeof(req4), &conn.request_info); ASSERT(should_keep_alive(&conn) == 1); conn.status_code = 401; ASSERT(should_keep_alive(&conn) == 0); conn.status_code = 200; conn.must_close = 1; ASSERT(should_keep_alive(&conn) == 0);}
mg_stat函数测试:
static void test_mg_stat(void) { static struct mg_context ctx; struct file file; memset(&file, 'A', sizeof(file)); ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));}
fc函数 用来返回假的连接上下文,如果连接不适合现在登录,则用于登录
// Return fake connection structure. Used for logging, if connection// is not applicable at the moment of logging.static struct mg_connection *fc(struct mg_context *ctx) { static struct mg_connection fake_connection; fake_connection.ctx = ctx; return &fake_connection;}
mg_fetch函数测试:
static void test_mg_fetch(void) { static const char *options[] = { "document_root", ".", "listening_ports", UNUSED_PORT, NULL, }; char buf[2000], buf2[2000]; int n, length; struct mg_context *ctx; struct mg_request_info ri; const char *tmp_file = "temporary_file_name_for_unit_test.txt"; struct file file; FILE *fp; ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL); // Failed fetch, pass invalid URL ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL); ASSERT(mg_fetch(ctx, "localhost:" UNUSED_PORT, tmp_file, buf, sizeof(buf), &ri) == NULL); ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file, buf, sizeof(buf), &ri) == NULL); // Failed fetch, pass invalid file name ASSERT(mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/data", "/this/file/must/not/exist/ever", buf, sizeof(buf), &ri) == NULL); // Successful fetch ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/data", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(ri.num_headers == 2); ASSERT(!strcmp(ri.request_method, "HTTP/1.1")); ASSERT(!strcmp(ri.uri, "200")); ASSERT(!strcmp(ri.http_version, "OK")); ASSERT((length = ftell(fp)) == (int) strlen(fetch_data)); fseek(fp, 0, SEEK_SET); ASSERT(fread(buf2, 1, length, fp) == (size_t) length); ASSERT(memcmp(buf2, fetch_data, length) == 0); fclose(fp); // Fetch big file, mongoose.c ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/mongoose.c", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(mg_stat(fc(ctx), "mongoose.c", &file)); ASSERT(file.size == ftell(fp)); ASSERT(!strcmp(ri.request_method, "HTTP/1.1")); // Fetch nonexistent file, /blah ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/boo", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(!mg_strcasecmp(ri.uri, "404")); fclose(fp); // Fetch existing inmemory file ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/blah", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(!mg_strcasecmp(ri.uri, "200")); n = ftell(fp); fseek(fp, 0, SEEK_SET); printf("%s %d %d [%.*s]\n", __func__, n, (int) feof(fp), n, buf2); n = fread(buf2, 1, n, fp); printf("%s %d %d [%.*s]\n", __func__, n, (int) feof(fp), n, buf2); ASSERT((size_t) ftell(fp) == (size_t) strlen(inmemory_file_data)); ASSERT(!memcmp(inmemory_file_data, buf2, ftell(fp))); fclose(fp); remove(tmp_file); mg_stop(ctx);}
mg_get_var函数测试:
static void test_mg_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 }; char buf[20]; ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1); ASSERT(buf[0] == '1' && buf[1] == '\0'); ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1); ASSERT(buf[0] == '2' && buf[1] == '\0'); ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2); ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0'); ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0); ASSERT(buf[0] == '\0'); ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1); ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2); ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2); ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2); ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2); ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);}
- Mongoose函数测试
- mongoose之main函数
- mongoose之master_thread函数
- mongoose之worker_thread()函数
- Mongoose----handle_request函数解析
- mongoose之get_option_index函数解析
- mongoose
- mongoose
- Mongoose
- Mongoose
- mongoose
- mongoose
- mongoose
- Mongoose
- mongoose
- Mongoose
- mongoose
- mongoose
- 10 个 Visual Studio 原生开发的调试技巧
- JS报错:Permission denied to access property 'document'---document.domain - JavaScript的同源策略问题
- Linux下设备热插拔事件程序
- 30个设计精美的Email Templates[邮件模版] (建议收藏)
- 帝国cms数据库表结构
- Mongoose函数测试
- Highcharts详细翻译系列一:chart 图表选项篇
- 详解Linux中断处理中的hardirq与softirq机制
- #gcc4.7.2编译以支持c++11标准,解决not a member问题
- 未进行,标题占位(设计模式,抽象类工厂模式二)
- Eclipse 各种小图标的含义
- 为你推荐Wordpress & Buddy Press 的18个主题应用
- 1003 Exchange for Cola
- DB2、ORACLE SQL写法的主要区别