Files
STM32F4-Base/IEC60870-Slave/test/iec104_response_transport_test.c
T
STF bdcc8b6500
CI / L0 静态门禁 (pre-commit) (push) Failing after 7m46s
CI / L0.5 主机侧单测 (MSVC) (push) Successful in 21s
添加遥控、参数装载、文件传输等业务
2026-11-16 22:19:17 -08:00

859 lines
38 KiB
C

/* Response transport regression over the controlled Net backend.
* The shared fixture supplies TcpServer.c and Net; do not link either twice. */
/*
* 文件内导航(搜索方括号标记;Suite:Transport):
* 复用公共夹具的fake Net、sut、收发和断言;本文件的response_*只服务应答传输专项。
* [COMMON/BASE] 公共基础:依赖、编译选项与断言入口
* [COMMON/FIXTURE] 本专项共用辅助:载荷、双队列提交、交接凭证与输出核对
* [RESPONSE/FIFO] 提交用例:普通/Tracked入口、FIFO及失败输出
* [RESPONSE/FAIRNESS] 调度用例:持续双队列公平、共享配额与点值顺序边界
* [RESPONSE/BACKPRESSURE] 背压用例:丢弃、发送窗口、到期协议处理与ACK
* [RESPONSE/LIFECYCLE] 会话用例:EOF、STOP、已交接凭证及Close后重建
* [ENTRY/MAIN] 测试入口:通用应答传输用例
* 公共辅助、业务专属辅助和用例按分区阅读;RUN/CASE标记用于细分执行组。
*/
/* ============================================================================
* [COMMON/BASE] 公共基础:依赖、编译选项与断言入口
* ============================================================================ */
#include "iec104_test_config.h"
#include "iec104_tcp_fixture.h"
/* ============================================================================
* [COMMON/FIXTURE] 本专项共用辅助:载荷、双队列提交、交接凭证与输出核对
* ============================================================================ */
/**
* 功能:tag写在IOA低字节,用独立地址标识发送顺序。
* 输入参数:bytes——报文字节缓冲区;tag——区分帧顺序的测试标签。
* 输出参数:bytes——写入或读出的数据字节。
* 返回值:无。
* 说明:它不是品质位或应答票据。
*/
static void response_payload(uint8_t *bytes, uint8_t tag)
{
static const uint8_t prototype[] = {1, 1, 3, 0, 1, 0, 1, 0, 0, 1};
memcpy(bytes, prototype, sizeof(prototype));
bytes[6] = tag;
}
/**
* 功能:通过公开事件入口加入NORMAL,tag只用于核对线上先后。
* 输入参数:tag——区分帧顺序的测试标签。
* 输出参数:无。
* 返回值:无。
* 说明:不在这里推进Poll。
*/
static void response_normal(uint8_t tag)
{
uint8_t bytes[10];
response_payload(bytes, tag);
CHECK(IEC104_TcpServer_Enqueue_Event(sut, bytes, sizeof(bytes)) == IEC104_TCP_SUBMIT_OK);
}
/**
* 功能:Retain an earlier HIGH response to exercise the shared FIFO.
* 输入参数:tag——区分帧顺序的测试标签。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void response_high(uint8_t tag)
{
uint8_t bytes[10];
response_payload(bytes, tag);
CHECK(IEC104_SendManager_Enqueue(&sut->connection.send_manager, sut->generation,
IEC104_ASDU_PRIORITY_HIGH, bytes, sizeof(bytes)) == IEC104_SEND_MANAGER_OK);
}
/**
* 功能:提交带交接凭证的应答后立即改写调用方缓冲,验证发送层已复制原文。
* 输入参数:tag——区分帧顺序的测试标签。
* 输出参数:无。
* 返回值:本次响应票据;0 表示直接交接,非零表示进入可跟踪的 HIGH 队列。
* 说明:ticket=0表示本次直接交接,非零表示可查询的HIGH排队凭证;两者都不代表主站ACK。
*/
static uint64_t response_response(uint8_t tag)
{
uint8_t bytes[10];
uint64_t ticket = UINT64_MAX;
response_payload(bytes, tag);
CHECK(IEC104_TcpServer_Send_Response_Tracked(sut, sut->generation,
bytes, sizeof(bytes), &ticket) == IEC104_TCP_SUBMIT_OK);
CHECK(ticket != UINT64_MAX);
memset(bytes, 0xCC, sizeof(bytes)); /* Accepted data must no longer borrow the caller. */
return ticket;
}
/**
* 功能:只给一次I帧写出预算,逐帧观察公平调度。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:避免一次Poll排出多帧掩盖中间票据状态。
*/
static void response_one_frame(void)
{
unsigned budget = 1U;
CHECK(Flush_Output(sut, &budget, NULL));
CHECK(budget == 0U);
}
/**
* 功能:遍历真实网络输出,过滤控制帧后核对业务帧顺序和原文。
* 输入参数:tags——发送标签数组(只读);count——对象、条目或站点数量。
* 输出参数:无。
* 返回值:无。
* 说明:输出混有START/STOP/S确认;按APCI长度跨帧,只对I帧的tag逐项比较,不能按固定偏移跳帧。
*/
static void response_check_output(const uint8_t *tags, size_t count)
{
size_t offset = 0U, found = 0U;
while (offset < fake.sent_bytes) {
size_t length = (size_t)fake.output[offset + 1U] + 2U;
CHECK(length >= IEC104_APCI_LEN && offset + length <= fake.sent_bytes);
if ((fake.output[offset + 2U] & 1U) == 0U) {
CHECK(length == 16U && found < count);
CHECK(fake.output[offset + IEC104_APCI_LEN + 6U] == tags[found]);
++found;
}
offset += length;
}
CHECK(found == count);
}
/**
* 功能:同时设置连接配置及Manager当前配额,构造可重复的调度边界。
* 输入参数:limit——容量或处理数量限制;used——已用容量。
* 输出参数:无。
* 返回值:无。
* 说明:只用于白盒测试。
*/
static void response_set_quota(uint16_t limit, uint16_t used)
{
sut->connection.config.high_priority_burst_limit = limit;
sut->connection.send_manager.high_priority_burst_limit = limit;
sut->connection.send_manager.high_priority_burst_count = used;
}
/* ============================================================================
* [RESPONSE/FIFO] 提交用例:普通/Tracked入口、FIFO及失败输出
* ============================================================================ */
/**
* 功能:验证以下场景与约束:Net不可写时,直接构帧仍可返回ticket=0/HANDED。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:查询状态必须只读,不推进发送或消费通知。
*/
static void test_response_direct_and_readonly(void)
{
IEC104_TcpServer_t snapshot;
const uint8_t expected[] = {20};
uint64_t ticket;
begin();
fake.send_blocked = 1U;
ticket = response_response(20);
CHECK(ticket == 0U && IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
snapshot = *sut;
CHECK(IEC104_TcpServer_Get_Response_State(sut, 0U) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, sut->queue.high_next_sequence) == IEC104_RESPONSE_ERROR);
CHECK(IEC104_TcpServer_Get_Response_State(NULL, 0U) == IEC104_RESPONSE_ERROR);
CHECK(memcmp(&snapshot, sut, sizeof(snapshot)) == 0);
fake.send_blocked = 0U;
poll_once();
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:交错插入普通HIGH、带凭证HIGH及NORMAL,以配额1逐帧放行。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:既核对HIGH内部FIFO,也核对NORMAL轮换;最后再确认两个凭证均已交接。
*/
static void test_response_interleaved_fifo(void)
{
const uint8_t expected[] = {10, 1, 20, 2, 21, 3};
uint64_t first, second;
unsigned i;
begin();
response_set_quota(1U, 0U);
response_normal(1);
response_high(10);
first = response_response(20);
response_normal(2);
second = response_response(21);
response_normal(3);
CHECK(first != 0U && second > first);
CHECK(IEC104_TcpServer_Get_Response_State(sut, first) == IEC104_RESPONSE_PENDING);
for (i = 0U; i < sizeof(expected); ++i) { response_one_frame(); }
response_check_output(expected, sizeof(expected));
CHECK(IEC104_TcpServer_Get_Response_State(sut, first) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, second) == IEC104_RESPONSE_HANDED);
end();
}
/**
* 功能:验证以下场景与约束:注入本地发送错误,验证FATAL关闭连接且保持调用方票据输出不变,避免产生可误用的凭证。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_failed_send_preserves_ticket_output(void)
{
uint8_t bytes[10];
uint64_t ticket = UINT64_MAX;
begin();
response_payload(bytes, 20);
fake.send_error = 1U;
CHECK(IEC104_TcpServer_Send_Response_Tracked(sut, sut->generation,
bytes, sizeof(bytes), &ticket) == IEC104_TCP_SUBMIT_FATAL);
CHECK(ticket == UINT64_MAX);
CHECK(sut->client_socket == IEC104_NET_INVALID_SOCKET);
end();
}
/* ============================================================================
* [RESPONSE/FAIRNESS] 调度用例:持续双队列公平、共享配额与点值顺序边界
* ============================================================================ */
/**
* 功能:验证以下场景与约束:每次放行一帧前继续产生NORMAL事件。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:在固定HIGH配额3下核对两条应答仍能交接。
* 配额用尽后必须轮到NORMAL;此用例证明指定配额下的顺序,不给出网络时延保证。
*/
static void test_response_continuous_events_do_not_block(void)
{
const uint8_t expected[] = {10, 20, 21, 1};
uint64_t first, second;
begin();
response_set_quota(3U, 0U);
response_high(10);
first = response_response(20);
second = response_response(21);
response_normal(1);
response_one_frame();
response_normal(2);
response_one_frame();
CHECK(IEC104_TcpServer_Get_Response_State(sut, first) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, second) == IEC104_RESPONSE_PENDING);
response_normal(3);
response_one_frame();
CHECK(IEC104_TcpServer_Get_Response_State(sut, second) == IEC104_RESPONSE_HANDED);
CHECK(sut->connection.send_manager.high_priority_burst_count == 3U);
response_high(22);
response_normal(4);
response_one_frame();
CHECK(sut->connection.send_manager.high_priority_burst_count == 0U);
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:从HIGH配额已耗尽开始,既有HIGH和新应答共同排队。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:验证先让出NORMAL,再按FIFO交接。
*/
static void test_response_fairness_with_queued_high(void)
{
const uint8_t expected[] = {1, 10, 2, 20};
unsigned i;
begin();
response_set_quota(1U, 1U);
response_normal(1);
response_high(10);
CHECK(response_response(20) != 0U);
response_normal(2);
for (i = 0U; i < sizeof(expected); ++i) { response_one_frame(); }
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:Neither public response API may bypass NORMAL when the shared quota is spent.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_public_paths_share_fifo_and_quota(void)
{
const uint8_t expected[] = {10, 11, 12, 13, 1, 14, 15, 2};
uint8_t bytes[10];
uint64_t ticket;
unsigned i;
begin();
CHECK(sut->connection.config.high_priority_burst_limit == 4U);
response_normal(1);
for (i = 0U; i < 4U; ++i) {
response_payload(bytes, (uint8_t)(10U + i));
if ((i & 1U) == 0U) {
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation,
bytes, sizeof(bytes)) == IEC104_TCP_SUBMIT_OK);
} else {
CHECK(response_response((uint8_t)(10U + i)) == 0U);
}
}
CHECK(sut->connection.send_manager.high_priority_burst_count == 4U);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_HIGH].unsubmitted_count == 0U);
ticket = response_response(14);
response_payload(bytes, 15);
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation,
bytes, sizeof(bytes)) == IEC104_TCP_SUBMIT_OK);
CHECK(ticket != 0U && IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_PENDING);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_HIGH].unsubmitted_count == 2U);
CHECK(sut->connection.send_manager.high_priority_burst_count == 4U);
response_normal(2);
response_one_frame();
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_PENDING);
CHECK(sut->connection.send_manager.high_priority_burst_count == 0U);
response_one_frame(); response_one_frame(); response_one_frame();
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:Both producers remain active for multiple k windows and record-ring turnovers.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_sustained_bidirectional_fairness(void)
{
uint8_t expected[60];
unsigned step, high_sent = 0U, normal_sent = 0U;
begin();
CHECK(sut->connection.config.high_priority_burst_limit == 4U);
sut->connection.config.link.k = 5U;
sut->connection.send_manager.send_window_k = 5U;
for (step = 0U; step < sizeof(expected); ++step) {
response_high((uint8_t)(100U + step));
response_normal((uint8_t)(1U + step));
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_HIGH].unsubmitted_count != 0U);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count != 0U);
/* 两队列始终非空,默认配额4给出4个HIGH加1个NORMAL;每5帧ACK后继续验证下一窗口。 */
expected[step] = (step % 5U == 4U) ? (uint8_t)(++normal_sent) : (uint8_t)(100U + high_sent++);
response_one_frame();
if (step % 5U == 4U) {
IEC104_ApciInfo_t ack;
uint8_t frame[] = {0x68, 4, 1, 0, 0, 0};
unsigned nr = step + 1U;
frame[4] = (uint8_t)(nr << 1); frame[5] = (uint8_t)(nr >> 7);
CHECK(IEC104_Parse_APCI(frame, sizeof(frame), &ack) == 1);
CHECK(IEC104_SendManager_Handle_I_S_Frame(&sut->connection.send_manager,
sut->generation, &ack, fake.now) == IEC104_SEND_MANAGER_OK);
/* Model a fresh receive/record batch without flushing extra I frames. */
sut->event_budget = TCP_EVENT_BUDGET;
sut->event_yield = 0U;
CHECK(Consume_Records(sut, NULL));
CHECK(drain() == 5U);
}
}
response_check_output(expected, sizeof(expected));
CHECK(high_sent == 48U && normal_sent == 12U);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_HIGH].unsubmitted_count == 12U);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count == 48U);
CHECK(sut->connection.send_manager.record_count == 0U);
end();
}
/**
* 功能:验证以下场景与约束:HIGH is not a point-version fence: a current GI value may precede an older event.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_current_value_can_precede_old_event(void)
{
const uint8_t older[] = {11, 1, 3, 0, 1, 0, 1, 0, 0, 20, 0, 0};
const uint8_t current[] = {11, 1, 20, 0, 1, 0, 1, 0, 0, 30, 0, 0};
size_t offset;
unsigned seen = 0U;
begin();
CHECK(IEC104_TcpServer_Enqueue_Event(sut, older, sizeof(older)) == IEC104_TCP_SUBMIT_OK);
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation,
current, sizeof(current)) == IEC104_TCP_SUBMIT_OK);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count == 1U);
response_one_frame();
for (offset = 0U; offset < fake.sent_bytes; offset += (size_t)fake.output[offset + 1U] + 2U) {
if ((fake.output[offset + 2U] & 1U) == 0U) {
const uint8_t *payload = fake.output + offset + IEC104_APCI_LEN;
CHECK(seen < 2U && fake.output[offset + 1U] + 2U == 18U);
CHECK(memcmp(payload, seen == 0U ? current : older, sizeof(current)) == 0);
++seen;
}
}
CHECK(seen == 2U);
end();
}
/* ============================================================================
* [RESPONSE/BACKPRESSURE] 背压用例:丢弃、发送窗口、到期协议处理与ACK
* ============================================================================ */
/**
* 功能:验证以下场景与约束:显式丢弃第一条待发HIGH,只将其凭证标为CANCELLED。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:后继凭证仍PENDING并可正常交接。
*/
static void test_response_discard_keeps_next_pending(void)
{
const uint8_t expected[] = {21, 1, 2};
IEC104_AsduHeadInfo_t head;
uint64_t first, second;
begin();
response_set_quota(0U, 0U);
response_normal(1);
CHECK(IEC104_AsduQueue_Enqueue_Response(&sut->queue, sut->generation,
(const uint8_t[]){1,1,3,0,1,0,20,0,0,1}, 10U, &first) == IEC104_ASDU_QUEUE_OK);
response_normal(2);
second = response_response(21);
CHECK(IEC104_AsduQueue_Peek_Unsubmitted(&sut->queue, IEC104_ASDU_PRIORITY_HIGH, &head) == IEC104_ASDU_QUEUE_OK);
CHECK(IEC104_AsduQueue_Discard_Unsubmitted_Head(&sut->queue, IEC104_ASDU_PRIORITY_HIGH,
head.entry_id) == IEC104_ASDU_QUEUE_OK);
CHECK(IEC104_TcpServer_Get_Response_State(sut, first) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, second) == IEC104_RESPONSE_PENDING);
response_one_frame();
response_one_frame();
response_one_frame();
CHECK(IEC104_TcpServer_Get_Response_State(sut, second) == IEC104_RESPONSE_HANDED);
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:k=1制造真实窗口背压:第二条应答已被HIGH接手但未HANDED,收到首帧ACK后才交接。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_window_backpressure(void)
{
const uint8_t expected[] = {20, 21, 1, 2};
uint64_t ticket;
begin();
sut->connection.config.link.k = 1U;
sut->connection.send_manager.send_window_k = 1U;
CHECK(response_response(20) == 0U);
response_normal(1);
ticket = response_response(21);
response_normal(2);
poll_once();
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_PENDING);
push_ack(1U); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
push_ack(2U); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
push_ack(3U); poll_once(); drain();
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:t3已到期时业务提交只接手入队。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:发送与TESTFR等到下一轮网络推进协调,提交本身不抢跑。
*/
static void test_response_due_protocol_only_enqueues(void)
{
IEC104_SendBufferHeadInfo_t head;
uint64_t ticket;
size_t sent;
begin();
fake.now += sut->connection.config.link.t3_ms;
sent = fake.sent_bytes;
ticket = response_response(20);
CHECK(ticket != 0U && fake.sent_bytes == sent);
CHECK(IEC104_SendBuffer_Peek_Send_Head(sut->connection.send_manager.send_buffer, &head) == IEC104_SEND_BUFFER_EMPTY);
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_PENDING);
end();
}
/**
* 功能:验证以下场景与约束:A full k window queues the response without waiting for NORMAL to drain.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_empty_queue_window_fallback(void)
{
const uint8_t expected[] = {20, 21, 1};
uint64_t ticket;
begin();
sut->connection.config.link.k = 1U;
sut->connection.send_manager.send_window_k = 1U;
CHECK(response_response(20) == 0U);
ticket = response_response(21);
CHECK(ticket != 0U && IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_PENDING);
response_normal(1);
push_ack(1U); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
push_ack(2U); poll_once(); drain();
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:t1边界已有ACK等待读取:提交应答不能提前判链路超时,Poll先处理可读输入再推进发送。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_due_ack_is_received_before_timeout(void)
{
const uint8_t expected[] = {20, 21};
uint64_t ticket;
size_t sent;
begin();
CHECK(response_response(20) == 0U);
fake.now += sut->connection.config.link.t1_ms;
push_ack(1U);
sent = fake.sent_bytes;
ticket = response_response(21);
CHECK(ticket != 0U && fake.sent_bytes == sent);
CHECK(sut->client_socket != IEC104_NET_INVALID_SOCKET);
poll_once(); drain();
CHECK(sut->client_socket != IEC104_NET_INVALID_SOCKET);
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
response_check_output(expected, sizeof(expected));
end();
}
/* ============================================================================
* [RESPONSE/LIFECYCLE] 会话用例:EOF、STOP、已交接凭证及Close后重建
* ============================================================================ */
/**
* 功能:验证以下场景与约束:对端EOF时排空已受理HIGH,应答凭证变HANDED。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:未交接NORMAL仍保留供重连处理。
* 只检查本地交接事实,EOF之后不假定对端还会确认。
*/
static void test_response_eof_drains_high_before_retaining_events(void)
{
const uint8_t expected[] = {10, 20};
uint64_t ticket;
begin();
response_normal(1);
response_high(10);
ticket = response_response(20);
response_normal(2);
fake.recv_eof = 1U;
poll_once();
CHECK(sut->client_socket == IEC104_NET_INVALID_SOCKET);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count == 2U);
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:HIGH先于全部NORMAL入队的EOF变体:应答排空和事件保留规则不能依赖是否已有旧事件。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_response_eof_high_without_old_events(void)
{
const uint8_t expected[] = {10, 20};
uint64_t ticket;
begin();
response_high(10);
ticket = response_response(20);
response_normal(1);
fake.recv_eof = 1U;
poll_once();
CHECK(sut->client_socket == IEC104_NET_INVALID_SOCKET);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].unsubmitted_count == 1U);
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
response_check_output(expected, sizeof(expected));
end();
}
/**
* 功能:验证以下场景与约束:STOP受理时尚未交接的HIGH凭证必须取消。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:随后START时旧凭证仍须保持CANCELLED。
*/
static void test_response_stop_cancels_pending_ticket(void)
{
uint64_t ticket;
begin();
response_normal(1);
response_high(10);
ticket = response_response(20);
push(stop_act, sizeof(stop_act)); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_CANCELLED);
push(start_act, sizeof(start_act)); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, 0U) == IEC104_RESPONSE_HANDED);
end();
}
/**
* 功能:验证以下场景与约束:在同一队列中保留一个已交接和一个待交接凭证。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:STOP/断开后分别保持HANDED和CANCELLED。
* 历史交接结果与尚未执行的排队请求必须分开处理。
*/
static void test_response_handed_ticket_survives_stop_and_disconnect(void)
{
uint64_t handed, pending;
begin();
response_high(10);
handed = response_response(20);
pending = response_response(21);
CHECK(handed != 0U && pending > handed);
response_one_frame(); response_one_frame();
CHECK(IEC104_TcpServer_Get_Response_State(sut, handed) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, pending) == IEC104_RESPONSE_PENDING);
push_ack(2U); push(stop_act, sizeof(stop_act)); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, handed) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, pending) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Close_Connection(sut, sut->generation, NULL) == IEC104_TCP_OK);
drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, handed) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, pending) == IEC104_RESPONSE_CANCELLED);
end();
}
/**
* 功能:覆盖Close、初始化失败重试、Destroy/Create槽复用,核对应答序号持续递增。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:旧凭证不能因新实例占用同一地址而匹配到新的应答。
*/
static void test_response_close_reinit_and_failed_retries(void)
{
IEC104_TcpServerConfig_t config;
IEC104_TcpServer_t *original;
uint64_t old_ticket, next, new_ticket;
begin();
response_normal(1);
response_high(10);
old_ticket = response_response(20);
next = sut->queue.high_next_sequence;
CHECK(IEC104_TcpServer_Close(sut, NULL) == IEC104_TCP_OK);
drain();
CHECK(sut->saved_high_next_sequence == next);
CHECK(IEC104_TcpServer_Get_Response_State(sut, old_ticket) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, next) == IEC104_RESPONSE_ERROR);
test_transport_config(&config);
fake.init_error = 1U;
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_NETWORK_ERROR);
CHECK(sut->saved_high_next_sequence == next);
CHECK(IEC104_TcpServer_Get_Response_State(sut, old_ticket) == IEC104_RESPONSE_CANCELLED);
fake.init_error = 0U;
fake.listen_error = 1U;
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_NETWORK_ERROR);
CHECK(sut->saved_high_next_sequence == next);
fake.listen_error = 0U;
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
CHECK(sut->queue.high_next_sequence == next);
fake.accept_pending = 1U; poll_once(); drain();
push(start_act, sizeof(start_act)); poll_once(); drain();
response_normal(2);
response_high(10);
new_ticket = response_response(21);
CHECK(new_ticket >= next && new_ticket > old_ticket);
CHECK(IEC104_TcpServer_Get_Response_State(sut, old_ticket) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Close(sut, NULL) == IEC104_TCP_OK); drain();
next = sut->saved_high_next_sequence;
original = sut;
CHECK(IEC104_TcpServer_Destroy(sut, NULL) == IEC104_TCP_OK);
CHECK(IEC104_TcpServer_Get_Response_State(original, old_ticket) == IEC104_RESPONSE_ERROR);
sut = IEC104_TcpServer_Create();
CHECK(sut == original && sut->saved_high_next_sequence == next);
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
CHECK(sut->queue.high_next_sequence == next);
CHECK(IEC104_TcpServer_Get_Response_State(sut, new_ticket) == IEC104_RESPONSE_CANCELLED);
end();
}
/**
* 功能:验证以下场景与约束:启动门禁在应用读取ACTIVATED之前已经生效。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:超过HIGH公平配额也不能插入NORMAL。
* NORMAL仍缓存,暂停不会反复唤醒发送;开放后按原FIFO发送,ACK清理规则不变。
*/
static void test_response_startup_gate(void)
{
IEC104_TcpServerConfig_t config;
IEC104_TcpServerStatus_t status;
IEC104_TcpWaitInfo_t wait;
IEC104_TcpEvent_t event;
uint64_t epoch;
uint8_t bytes[10], expected[8];
unsigned index;
memset(&fake, 0, sizeof(fake)); fake.now = 1000U;
sut = IEC104_TcpServer_Create(); CHECK(sut != NULL);
test_transport_config(&config);
CHECK(config.startup_event_gate == 0U);
config.startup_event_gate = 1U; config.protocol.link.k = 16U;
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
response_normal(10U);
fake.accept_pending = 1U; poll_once();
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY &&
event.type == IEC104_TCP_EVENT_CONNECTED && event.activation_id == 0U);
push(start_act, sizeof(start_act)); poll_once();
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
epoch = status.activation_id;
CHECK(epoch != 0U && status.startup_pending && status.startup_event_gate && fake.sent_bytes == 6U);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, epoch) == IEC104_TCP_SUBMIT_WAIT);
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY &&
event.type == IEC104_TCP_EVENT_ACTIVATED && event.activation_id == epoch);
CHECK(drain() == 0U);
response_normal(11U); poll_once();
CHECK(IEC104_TcpServer_Prepare(sut, &wait, NULL) == IEC104_TCP_OK && !wait.need_process);
for (index = 0U; index < wait.socket_count; ++index) { CHECK((wait.sockets[index].events & IEC104_TCP_IO_WRITE) == 0U); }
response_payload(bytes, 50U);
CHECK(IEC104_TcpServer_Query_Idle_Send(sut, sut->generation, 1U) == IEC104_TCP_SUBMIT_WAIT);
CHECK(IEC104_TcpServer_Send_Idle(sut, sut->generation, bytes, sizeof(bytes), 1U) == IEC104_TCP_SUBMIT_WAIT);
for (index = 0U; index < 6U; ++index) {
expected[index] = (uint8_t)(20U + index);
(void)response_response(expected[index]);
poll_once(); drain();
}
response_check_output(expected, 6U);
CHECK(sut->connection.send_manager.high_priority_burst_count == 0U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK &&
status.normal_queue.pending_asdu_count == 2U);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, epoch + 1U) == IEC104_TCP_SUBMIT_ERROR);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, epoch) == IEC104_TCP_SUBMIT_OK);
CHECK(IEC104_TcpServer_Prepare(sut, &wait, NULL) == IEC104_TCP_OK && wait.need_process);
poll_once(); drain();
expected[6] = 10U; expected[7] = 11U; response_check_output(expected, sizeof(expected));
push_ack(8U); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK &&
!status.startup_pending && status.normal_queue.stored_asdu_count == 0U);
poll_once(); /* 归还RECORD通知空间会唤醒一次原有调度,先完成该次推进再检查幂等释放。 */
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, epoch) == IEC104_TCP_SUBMIT_OK);
CHECK(IEC104_TcpServer_Prepare(sut, &wait, NULL) == IEC104_TCP_OK && !wait.need_process);
/* 同TCP重复START不重新关门、不分配新轮次;真正STOP/START即使没有NORMAL也必须拦住IDLE。 */
push(start_act, sizeof(start_act)); poll_once(); CHECK(drain() == 0U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK &&
status.activation_id == epoch && !status.startup_pending);
push(stop_act, sizeof(stop_act)); push(start_act, sizeof(start_act)); poll_once();
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, epoch) != IEC104_TCP_SUBMIT_OK);
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY &&
event.type == IEC104_TCP_EVENT_DEACTIVATED && event.activation_id == epoch);
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY &&
event.type == IEC104_TCP_EVENT_ACTIVATED && event.activation_id > epoch);
CHECK(IEC104_TcpServer_Query_Idle_Send(sut, sut->generation, 1U) == IEC104_TCP_SUBMIT_WAIT);
CHECK(IEC104_TcpServer_Send_Idle(sut, sut->generation, bytes, sizeof(bytes), 1U) == IEC104_TCP_SUBMIT_WAIT);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, epoch) == IEC104_TCP_SUBMIT_ERROR);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, event.activation_id) == IEC104_TCP_SUBMIT_OK);
end();
}
/**
* 功能:验证以下场景与约束:未ACK事件重连恢复待发,但新TCP及新START必须重新关门。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:旧epoch/代次不能解锁。
* 启动编号耗尽时关闭当前会话而不回绕,旧token不会重新生效。
*/
static void test_response_startup_reconnect(void)
{
IEC104_TcpServerConfig_t config;
IEC104_TcpServerStatus_t status;
IEC104_TcpEvent_t event;
uint64_t generation, epoch;
const uint8_t tags[] = {10U, 10U};
memset(&fake, 0, sizeof(fake)); fake.now = 1000U;
sut = IEC104_TcpServer_Create(); CHECK(sut != NULL);
test_transport_config(&config); config.startup_event_gate = 1U;
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
fake.accept_pending = 1U; poll_once(); drain();
push(start_act, sizeof(start_act)); poll_once();
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY);
generation = event.generation; epoch = event.activation_id;
CHECK(IEC104_TcpServer_Release_Startup(sut, generation, epoch) == IEC104_TCP_SUBMIT_OK);
response_normal(10U); poll_once(); drain();
fake.recv_eof = 1U; poll_once(); drain(); fake.recv_eof = 0U;
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK &&
!status.connected && status.startup_pending && status.normal_queue.pending_asdu_count == 1U);
fake.accept_pending = 1U; poll_once();
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY &&
event.type == IEC104_TCP_EVENT_CONNECTED && event.activation_id == 0U && event.generation > generation);
push(start_act, sizeof(start_act)); poll_once();
CHECK(IEC104_TcpServer_NextEvent(sut, &event) == IEC104_TCP_EVENT_READY && event.activation_id > epoch);
response_check_output(tags, 1U);
CHECK(IEC104_TcpServer_Release_Startup(sut, generation, epoch) == IEC104_TCP_SUBMIT_ERROR);
CHECK(IEC104_TcpServer_Release_Startup(sut, event.generation, epoch) == IEC104_TCP_SUBMIT_ERROR);
CHECK(IEC104_TcpServer_Release_Startup(sut, event.generation, event.activation_id) == IEC104_TCP_SUBMIT_OK);
poll_once(); drain(); response_check_output(tags, 2U);
push_ack(1U); poll_once(); drain();
push(stop_act, sizeof(stop_act)); poll_once(); drain();
sut->activation_serial = UINT32_MAX;
push(start_act, sizeof(start_act)); poll_once(); drain();
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK && !status.connected);
CHECK(sut->activation_serial == UINT32_MAX);
sut->activation_serial = (uint32_t)epoch + 2U; /* 仅恢复耗尽注入,供后续独立用例复用静态槽。 */
end();
}
/* ============================================================================
* [ENTRY/MAIN] 测试入口:通用应答传输用例
* ============================================================================ */
/**
* 功能:测试入口:通用应答传输用例
* 输入参数:无。
* 输出参数:无。
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
int main(void)
{
/* [RUN/FIFO] 提交、FIFO和失败输出 */
test_response_direct_and_readonly();
test_response_failed_send_preserves_ticket_output();
test_response_interleaved_fifo();
/* [RUN/FAIRNESS] 双队列公平与点值顺序 */
test_response_continuous_events_do_not_block();
test_response_fairness_with_queued_high();
test_response_public_paths_share_fifo_and_quota();
test_response_sustained_bidirectional_fairness();
test_response_current_value_can_precede_old_event();
/* [RUN/BACKPRESSURE] 容量、窗口与协议期限 */
test_response_discard_keeps_next_pending();
test_response_window_backpressure();
test_response_empty_queue_window_fallback();
test_response_due_protocol_only_enqueues();
test_response_due_ack_is_received_before_timeout();
/* [RUN/LIFECYCLE] EOF、STOP及重建 */
test_response_eof_drains_high_before_retaining_events();
test_response_eof_high_without_old_events();
test_response_stop_cancels_pending_ticket();
test_response_handed_ticket_survives_stop_and_disconnect();
test_response_close_reinit_and_failed_retries();
test_response_startup_gate();
test_response_startup_reconnect();
printf("Response transport: 20 groups passed (FIFO, fairness, tickets, startup gate, EOF, STOP, deadlines, reinit)\n");
return 0;
}