245 lines
11 KiB
C
245 lines
11 KiB
C
/* 附录 C XML 单测:内容、转义、完整范围、循环顺序与所有 sink 失败边界。 */
|
|
#include "IEC60870_UserLogFile.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define CHECK(expression) do { if (!(expression)) { \
|
|
fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #expression); exit(1); \
|
|
} } while (0)
|
|
|
|
/* 与核心单测相同的内存介质模型;导出仍通过正式Get读取并校验真实wire。 */
|
|
typedef struct {
|
|
size_t size;
|
|
uint8_t bytes[IEC60870_USER_LOG_WIRE_MAX];
|
|
} Cell;
|
|
|
|
/* 只有测试收集完整XML便于断言;生产编码器不要求这块大缓冲。
|
|
* calls计入失败调用,fail_call为0表示关闭注入,否则拒绝恰好指定的片段。 */
|
|
typedef struct {
|
|
char bytes[300000];
|
|
size_t length;
|
|
unsigned calls;
|
|
unsigned fail_call;
|
|
} Output;
|
|
|
|
static Cell cells[IEC60870_USER_LOG_CAPACITY];
|
|
static IEC60870_UserLog_t log_core;
|
|
static Output output;
|
|
static int fail_read;
|
|
|
|
/**
|
|
* 功能:从内存槽复制真实记录。
|
|
* 输入参数:context——业务回调的测试上下文;index——待访问或核对的条目下标;bytes——报文字节缓冲区;capacity——调用方提供的缓冲容量;size——数据长度或文件大小。
|
|
* 输出参数:bytes——写入或读出的数据字节;size——本次数据、文件或 ASDU 的实际长度。
|
|
* 返回值:本次核心或后端操作的状态枚举,保留成功、空记录、拒绝及错误等区别。
|
|
* 说明:指定槽可注入读取错误,空槽返回EMPTY且不写输出。
|
|
*/
|
|
static IEC60870_UserLogResult_t Read(void *context, uint32_t index,
|
|
uint8_t *bytes, size_t capacity, size_t *size)
|
|
{
|
|
Cell *cell = &cells[index];
|
|
(void)context;
|
|
if ((int)index == fail_read) { return IEC60870_USER_LOG_ERROR; }
|
|
if (cell->size == 0U) { return IEC60870_USER_LOG_EMPTY; }
|
|
CHECK(cell->size <= capacity);
|
|
memcpy(bytes, cell->bytes, cell->size);
|
|
*size = cell->size;
|
|
return IEC60870_USER_LOG_OK;
|
|
}
|
|
|
|
/**
|
|
* 功能:检查槽号和记录容量后完整保存字节及长度,为XML测试提供总是成功的提交后端。
|
|
* 输入参数:context——业务回调的测试上下文;index——待访问或核对的条目下标;bytes——报文字节缓冲区(只读);size——数据长度或文件大小。
|
|
* 输出参数:无。
|
|
* 返回值:true 表示本次操作或核对成功;false 表示拒绝、失败或命中注入故障。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static bool Commit(void *context, uint32_t index, const uint8_t *bytes, size_t size)
|
|
{
|
|
(void)context;
|
|
CHECK(index < IEC60870_USER_LOG_CAPACITY && size <= IEC60870_USER_LOG_WIRE_MAX);
|
|
memcpy(cells[index].bytes, bytes, size); cells[index].size = size;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 功能:同步接收片段并为测试查询额外加NUL。
|
|
* 输入参数:context——业务回调的测试上下文;bytes——报文字节缓冲区(只读);length——字节长度。
|
|
* 输出参数:无。
|
|
* 返回值:true 表示本次操作或核对成功;false 表示拒绝、失败或命中注入故障。
|
|
* 说明:这个NUL不计入编码器返回的文件长度。
|
|
*/
|
|
static bool Sink(void *context, const uint8_t *bytes, size_t length)
|
|
{
|
|
Output *sink = (Output *)context;
|
|
++sink->calls;
|
|
if (sink->calls == sink->fail_call) { return false; }
|
|
CHECK(sink->length + length < sizeof(sink->bytes));
|
|
memcpy(sink->bytes + sink->length, bytes, length); sink->length += length;
|
|
sink->bytes[sink->length] = '\0';
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 功能:清空日志介质和XML收集状态,关闭读取故障后从空库初始化日志核心。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Fresh(void)
|
|
{
|
|
IEC60870_UserLogStore_t store = {NULL, Read, Commit};
|
|
memset(cells, 0, sizeof(cells));
|
|
memset(&output, 0, sizeof(output));
|
|
fail_read = -1;
|
|
CHECK(IEC60870_UserLog_Init(&log_core, &store) == IEC60870_USER_LOG_OK);
|
|
}
|
|
|
|
/**
|
|
* 功能:空库也有完整XML骨架。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:参数非法必须在首次sink调用前拒绝,并保留size。
|
|
*/
|
|
static void Empty_And_Validation(void)
|
|
{
|
|
uint32_t size = 123U;
|
|
char device[26];
|
|
unsigned calls;
|
|
Fresh();
|
|
CHECK(strcmp(IEC60870_USER_LOG_FILE_NAME, "HISTORY/ULOG/ulog.xml") == 0);
|
|
CHECK(IEC60870_UserLogFile_Write(&log_core, "device", Sink, &output, &size));
|
|
CHECK(size == output.length && strlen(output.bytes) == size);
|
|
CHECK(strcmp(output.bytes, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DataFile>\r\n"
|
|
"<Header fileType=\"Ulog\" fileVer=\"1.00\" devID=\"device\"/>\r\n"
|
|
"<DataRec num=\"0\">\r\n</DataRec>\r\n</DataFile>\r\n") == 0);
|
|
calls = output.calls; size = 123U;
|
|
CHECK(!IEC60870_UserLogFile_Write(NULL, "device", Sink, &output, &size));
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, "device", NULL, &output, &size));
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, NULL, Sink, &output, &size));
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, "", Sink, &output, &size));
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, "a\nb", Sink, &output, &size));
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, "\x80", Sink, &output, &size));
|
|
memset(device, 'D', 25U); device[25] = '\0';
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, device, Sink, &output, &size));
|
|
CHECK(calls == output.calls && size == 123U);
|
|
device[24] = '\0';
|
|
CHECK(IEC60870_UserLogFile_Write(&log_core, device, Sink, &output, NULL));
|
|
}
|
|
|
|
/**
|
|
* 功能:验证附录C字段大小写、两位类别、五种属性转义及UTC+8日历首尾时刻。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:按精确属性文本断言,避免“只生成了可见文字”被误判为正确日志XML。
|
|
*/
|
|
static void Content_And_Time_Boundaries(void)
|
|
{
|
|
uint32_t size;
|
|
char needle[32];
|
|
unsigned type;
|
|
Fresh();
|
|
for (type = 1U; type <= 11U; ++type) {
|
|
CHECK(IEC60870_UserLog_Append(&log_core, (uint8_t)type, IEC60870_USER_LOG_UTC_MIN,
|
|
1U, "state & < > \" '") == IEC60870_USER_LOG_OK);
|
|
}
|
|
CHECK(IEC60870_UserLog_Append(&log_core, 2U, IEC60870_USER_LOG_UTC_MAX, 0U, "last") == IEC60870_USER_LOG_OK);
|
|
CHECK(IEC60870_UserLogFile_Write(&log_core, "D&<>\"'", Sink, &output, &size));
|
|
CHECK(size == output.length && strstr(output.bytes, "devID=\"D&<>"'\"") != NULL);
|
|
CHECK(strstr(output.bytes, "<DataRec num=\"12\">") != NULL);
|
|
CHECK(strstr(output.bytes, "tm=\"000101_000000_000\"") != NULL);
|
|
CHECK(strstr(output.bytes, "tm=\"991231_235959_999\" txt=\"last\" val=\"0\"") != NULL);
|
|
CHECK(strstr(output.bytes, "txt=\"state & < > " '\" val=\"1\"") != NULL);
|
|
for (type = 1U; type <= 11U; ++type) {
|
|
(void)snprintf(needle, sizeof(needle), "logType=\"%02u\"", type);
|
|
CHECK(strstr(output.bytes, needle) != NULL);
|
|
}
|
|
CHECK(strstr(output.bytes, "logType=\"1\"") == NULL);
|
|
}
|
|
|
|
/**
|
|
* 功能:文件必须包含最新1024条且保持提交顺序。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:128个特殊字符转义后不得截断,
|
|
* size须计算实际扩展后的XML字节,不可误用原文字符数。
|
|
*/
|
|
static void Rotation_And_Backward_Time(void)
|
|
{
|
|
char text[129];
|
|
uint32_t size;
|
|
unsigned i, count = 0U;
|
|
const char *position, *first, *second;
|
|
Fresh();
|
|
for (i = 1U; i <= 1025U; ++i) {
|
|
(void)snprintf(text, sizeof(text), "record-%04u", i);
|
|
CHECK(IEC60870_UserLog_Append(&log_core, 2U, IEC60870_USER_LOG_UTC_MIN + 2000U - i,
|
|
(uint8_t)(i % 2U), text) == IEC60870_USER_LOG_OK);
|
|
}
|
|
CHECK(IEC60870_UserLogFile_Write(&log_core, "device", Sink, &output, &size));
|
|
CHECK(strstr(output.bytes, "num=\"1024\"") != NULL && strstr(output.bytes, "record-0001") == NULL);
|
|
position = strstr(output.bytes, "<DI ");
|
|
CHECK(position != NULL);
|
|
first = strstr(position, "record-0002"); second = strstr(position, "record-0003");
|
|
CHECK(first != NULL && second != NULL && first < second);
|
|
CHECK(strstr(output.bytes, "record-1025") != NULL);
|
|
while (position != NULL) { ++count; position = strstr(position + 1, "<DI "); }
|
|
CHECK(count == 1024U);
|
|
Fresh();
|
|
memset(text, '&', 128U); text[128] = '\0';
|
|
CHECK(IEC60870_UserLog_Append(&log_core, 1U, IEC60870_USER_LOG_UTC_MIN, 1U, text) == IEC60870_USER_LOG_OK);
|
|
CHECK(IEC60870_UserLogFile_Write(&log_core, "device", Sink, &output, &size));
|
|
count = 0U; position = output.bytes;
|
|
while ((position = strstr(position, "&")) != NULL) { ++count; position += 5; }
|
|
CHECK(count == 128U && size == output.length);
|
|
}
|
|
|
|
/**
|
|
* 功能:先数出成功导出的全部片段,再逐个片段注入失败,确认立即停止且不更新size。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:sink失败不故障闭锁核心;介质读取失败则闭锁,下一次导出必须在输出前拒绝。
|
|
*/
|
|
static void Every_Sink_Failure_And_Read_Error(void)
|
|
{
|
|
unsigned calls, failure;
|
|
uint32_t size;
|
|
Fresh();
|
|
CHECK(IEC60870_UserLog_Append(&log_core, 1U, IEC60870_USER_LOG_UTC_MIN, 1U, "boot & ready") == IEC60870_USER_LOG_OK);
|
|
CHECK(IEC60870_UserLogFile_Write(&log_core, "device", Sink, &output, NULL));
|
|
calls = output.calls;
|
|
for (failure = 1U; failure <= calls; ++failure) {
|
|
memset(&output, 0, sizeof(output)); output.fail_call = failure; size = 987U;
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, "device", Sink, &output, &size));
|
|
CHECK(output.calls == failure && size == 987U && !log_core.faulted);
|
|
}
|
|
memset(&output, 0, sizeof(output)); fail_read = 0; size = 987U;
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, "device", Sink, &output, &size));
|
|
CHECK(log_core.faulted && size == 987U && strstr(output.bytes, "</DataFile>") == NULL);
|
|
calls = output.calls;
|
|
CHECK(!IEC60870_UserLogFile_Write(&log_core, "device", Sink, &output, &size));
|
|
CHECK(output.calls == calls);
|
|
}
|
|
|
|
/**
|
|
* 功能:验证XML骨架、转义与时间边界、循环记录顺序及每个输出片段的失败传播。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
int main(void)
|
|
{
|
|
Empty_And_Validation(); Content_And_Time_Boundaries();
|
|
Rotation_And_Backward_Time(); Every_Sink_Failure_And_Read_Error();
|
|
puts("UserLog XML: 4 groups passed");
|
|
return 0;
|
|
}
|