From fb457e35ec4067532cf0d96c2548e7d6167bb2a2 Mon Sep 17 00:00:00 2001 From: xfy911 Date: Thu, 4 Jun 2026 00:26:29 +0800 Subject: [PATCH] =?UTF-8?q?test(server):=20=E6=96=B0=E5=A2=9E=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E6=A0=B8=E5=BF=83=20null=20=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E4=B8=8E=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- Makefile | 3 ++ tests/unit/test_server.c | 65 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/unit/test_server.c diff --git a/Makefile b/Makefile index a86719a..0b7ba59 100644 --- a/Makefile +++ b/Makefile @@ -78,6 +78,9 @@ unit-test: $(UNIT_TEST_BINS) 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) $(CC) $(CFLAGS) -I. -I$(UNIT_TEST_DIR)/../unity -o $@ $(UNIT_TEST_DIR)/test_multipart.c multipart.c $(UNITY_SRC) -lm diff --git a/tests/unit/test_server.c b/tests/unit/test_server.c new file mode 100644 index 0000000..ef20161 --- /dev/null +++ b/tests/unit/test_server.c @@ -0,0 +1,65 @@ +#include "unity.h" +#include "server.h" +#include "config.h" +#include + +/* ===== 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(); +}