test(server): 新增服务器核心 null 安全与生命周期测试
- test_create_null_config: NULL config 返回 NULL - test_create_null_root_dir: 无 root_dir 提前返回 NULL - test_stop_null / test_destroy_null: NULL 指针不 crash - test_create_and_destroy: 正常创建+停止+销毁生命周期(高位端口避免冲突) - Makefile: 添加 test_server 编译规则,链接全部依赖 + libcoco
This commit is contained in:
parent
19c97ae6a9
commit
fb457e35ec
3
Makefile
3
Makefile
@ -78,6 +78,9 @@ unit-test: $(UNIT_TEST_BINS)
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# 单元测试编译规则
|
# 单元测试编译规则
|
||||||
|
$(UNIT_TEST_DIR)/test_server: $(UNIT_TEST_DIR)/test_server.c server.c http.c static.c log.c config.c multipart.c $(UNITY_SRC)
|
||||||
|
$(CC) $(CFLAGS) -I. -I$(UNIT_TEST_DIR)/../unity -o $@ $(UNIT_TEST_DIR)/test_server.c server.c http.c static.c log.c config.c multipart.c $(UNITY_SRC) $(LDFLAGS)
|
||||||
|
|
||||||
$(UNIT_TEST_DIR)/test_multipart: $(UNIT_TEST_DIR)/test_multipart.c multipart.c $(UNITY_SRC)
|
$(UNIT_TEST_DIR)/test_multipart: $(UNIT_TEST_DIR)/test_multipart.c multipart.c $(UNITY_SRC)
|
||||||
$(CC) $(CFLAGS) -I. -I$(UNIT_TEST_DIR)/../unity -o $@ $(UNIT_TEST_DIR)/test_multipart.c multipart.c $(UNITY_SRC) -lm
|
$(CC) $(CFLAGS) -I. -I$(UNIT_TEST_DIR)/../unity -o $@ $(UNIT_TEST_DIR)/test_multipart.c multipart.c $(UNITY_SRC) -lm
|
||||||
|
|
||||||
|
|||||||
65
tests/unit/test_server.c
Normal file
65
tests/unit/test_server.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include "unity.h"
|
||||||
|
#include "server.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* ===== server_create 参数校验 ===== */
|
||||||
|
|
||||||
|
void test_create_null_config(void) {
|
||||||
|
TEST_ASSERT_NULL(server_create(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_create_null_root_dir(void) {
|
||||||
|
cocoon_config_t cfg = {.port = 8080};
|
||||||
|
/* root_dir 为 NULL,应提前返回 NULL */
|
||||||
|
TEST_ASSERT_NULL(server_create(&cfg));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ===== server_stop / server_destroy 安全路径 ===== */
|
||||||
|
|
||||||
|
void test_stop_null(void) {
|
||||||
|
/* 不应 crash */
|
||||||
|
server_stop(NULL);
|
||||||
|
TEST_ASSERT_TRUE(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_destroy_null(void) {
|
||||||
|
/* 不应 crash */
|
||||||
|
server_destroy(NULL);
|
||||||
|
TEST_ASSERT_TRUE(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ===== server_create 成功后销毁 ===== */
|
||||||
|
|
||||||
|
void test_create_and_destroy(void) {
|
||||||
|
cocoon_config_t cfg = {
|
||||||
|
.root_dir = "/tmp",
|
||||||
|
.port = 29999, /* 使用高位端口减少冲突 */
|
||||||
|
.threaded = false
|
||||||
|
};
|
||||||
|
server_context_t *ctx = server_create(&cfg);
|
||||||
|
/* 高位端口通常可用,但测试环境可能被占用,保守处理 */
|
||||||
|
if (ctx) {
|
||||||
|
server_stop(ctx);
|
||||||
|
server_destroy(ctx);
|
||||||
|
TEST_ASSERT_TRUE(1);
|
||||||
|
} else {
|
||||||
|
/* 端口被占用等外部原因导致失败,非逻辑错误,标记忽略 */
|
||||||
|
TEST_IGNORE_MESSAGE("server_create failed, possibly port in use");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
RUN_TEST(test_create_null_config);
|
||||||
|
RUN_TEST(test_create_null_root_dir);
|
||||||
|
RUN_TEST(test_stop_null);
|
||||||
|
RUN_TEST(test_destroy_null);
|
||||||
|
RUN_TEST(test_create_and_destroy);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user