Files
STM32F4-Base/IEC60870-Slave/test/iec104_example_history_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

537 lines
26 KiB
C

/* 真实宿主文件验收:在可靠同步边界中断提交,重开后检查恢复和不可变下载。
* 测试只操作传入目录下自己创建的独立子目录,不将文件故障注入当作物理掉电实测。 */
#include "iec104_example_history.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
static ExampleHistory history, second;
static unsigned interrupt_after;
static const uint32_t soe[] = {1001U}, co[] = {6001U}, analog[] = {2001U};
static const IEC60870_HistoryConfig_t config = {soe, 1U, co, 1U, analog, 1U, 1000U};
static const uint64_t start = 1794412800000ULL;
/**
* 功能:在指定真实同步完成点注入中断,模拟持久操作完成后仍报告失败。
* 输入参数:无。
* 输出参数:无。
* 返回值:true 允许继续当前持久操作;false 表示命中本轮指定中断点。
* 说明:仅测试构建链接此钩子;计数归零后拒绝继续,使已完成的文件同步成为实际恢复边界。
*/
bool Example_History_Test_Continue(void)
{
if (interrupt_after == 0U) { return true; }
return --interrupt_after != 0U;
}
/**
* 功能:以系统唯一名称建立隔离用例目录,后续清理只针对本函数创建的资源。
* 输入参数:parent——测试父目录(只读);path——测试路径。
* 输出参数:path——生成的测试目录或文件路径。
* 返回值:无。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
static void Create_Test_Directory(const char *parent, char *path)
{
assert(GetTempFileNameA(parent, "iht", 0U, path));
assert(DeleteFileA(path));
assert(CreateDirectoryA(path, NULL));
}
/**
* 功能:删除当前用例拥有的存储文件与空测试目录,并检查资源已释放。
* 输入参数:path——测试路径(只读)。
* 输出参数:无。
* 返回值:无。
* 说明:不递归清空目录;若存在未释放的快照或额外文件,删除失败会直接暴露资源泄漏。
*/
static void Remove_Test_Directory(const char *path)
{
char file[MAX_PATH];
int n = snprintf(file, sizeof(file), "%s/history.store", path);
assert(n > 0 && (size_t)n < sizeof(file));
assert(DeleteFileA(file));
assert(RemoveDirectoryA(path));
}
/**
* 功能:验证以下场景与约束:依次在清旧标志、写新载荷、提交新标志三个同步点之后中断。
* 输入参数:parent——测试父目录(只读)。
* 输出参数:无。
* 返回值:无。
* 说明:前两点恢复旧版本,第三点虽返回失败却可能已完成,因此重开必须选新版本。
*/
static void Test_Interruptions(const char *parent)
{
unsigned stage;
for (stage = 1U; stage <= 3U; ++stage) {
char directory[MAX_PATH];
IEC60870_HistoryEvent_t event;
unsigned i;
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "HISTORY_TEST"));
assert(!Example_History_Open(&second, directory, &config, "LOCK_TEST"));
for (i = 0U; i < 30U; ++i) {
assert(IEC60870_History_Append_Control(&history.core, 6001U, IEC60870_HISTORY_SELECT,
1U, start + i, true) == IEC60870_HISTORY_OK);
}
interrupt_after = stage;
assert(IEC60870_History_Append_Control(&history.core, 6001U, IEC60870_HISTORY_OPER,
2U, start + 30U, true) == IEC60870_HISTORY_ERROR);
assert(history.core.faulted);
Example_History_Close(&history);
interrupt_after = 0U;
assert(Example_History_Open(&history, directory, &config, "HISTORY_TEST"));
assert(IEC60870_History_Event_Count(&history.core, IEC60870_HISTORY_CO) == 30U);
assert(IEC60870_History_Get_Event(&history.core, IEC60870_HISTORY_CO, 29U, &event) == IEC60870_HISTORY_OK);
assert(event.sequence == (stage == 3U ? 31U : 30U));
assert(IEC60870_History_Get_Event(&history.core, IEC60870_HISTORY_CO, 0U, &event) == IEC60870_HISTORY_OK);
assert(event.sequence == (stage == 3U ? 2U : 1U));
Example_History_Close(&history);
Remove_Test_Directory(directory);
}
/* The very first interrupted append has no previous copy; it stays empty. */
{
char directory[MAX_PATH];
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "HISTORY_TEST"));
interrupt_after = 1U;
assert(IEC60870_History_Append_Soe(&history.core, 1001U, 1U, 0U, start, true) == IEC60870_HISTORY_ERROR);
Example_History_Close(&history);
assert(Example_History_Open(&history, directory, &config, "HISTORY_TEST"));
assert(IEC60870_History_Event_Count(&history.core, IEC60870_HISTORY_SOE) == 0U);
Example_History_Close(&history);
Remove_Test_Directory(directory);
}
}
/**
* 功能:验证以下场景与约束:目录生成后追加事件、读文件期间再次召目录,原读会话都必须保持字节和大小稳定。
* 输入参数:parent——测试父目录(只读)。
* 输出参数:无。
* 返回值:无。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
static void Test_Snapshots(const char *parent)
{
char directory[MAX_PATH];
uint8_t before[2048], after[2048];
uint32_t size;
ExampleHistoryEntry entry;
float value = 12.5f;
uint8_t quality = 0U;
uint32_t day = IEC60870_History_Local_Day(start);
uint64_t midnight = (uint64_t)day * 86400000ULL - 28800000ULL;
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "A&B\"test"));
assert(!Example_History_Open(&history, directory, &config, "DOUBLE_OPEN"));
assert(IEC60870_History_Append_Soe(&history.core, 1001U, 1U, 0U, start, true) == IEC60870_HISTORY_OK);
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/SOE", 11U));
assert(Example_History_Directory_Count(&history) == 1U);
assert(Example_History_Directory_Get(&history, 0U, &entry));
assert(strcmp(entry.name, "HISTORY/SOE/soe.xml") == 0);
Example_History_Directory_Close(&history);
assert(IEC60870_History_Append_Soe(&history.core, 1001U, 0U, 0U, start + 1U, true) == IEC60870_HISTORY_OK);
assert(Example_History_Read_Open(&history, (const uint8_t *)entry.name, strlen(entry.name), &size));
assert(size == entry.size && size < sizeof(before));
assert(Example_History_Read(&history, 0U, before, size)); before[size] = 0U;
assert(strstr((const char *)before, "num=\"1\"") != NULL);
assert(strstr((const char *)before, "A&amp;B&quot;test") != NULL);
assert(!Example_History_Read(&history, size + 1U, before, 0U));
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/SOE", 11U));
Example_History_Directory_Close(&history);
assert(Example_History_Read(&history, 0U, after, size));
assert(memcmp(before, after, size) == 0);
Example_History_Read_Close(&history);
assert(!Example_History_Read_Open(&history, (const uint8_t *)"../history.store", 16U, &size));
assert(!Example_History_Read_Open(&history, (const uint8_t *)"HISTORY/SOE/SOE.xml", 18U, &size));
/* Incomplete days are retained, but only completed days are advertised. */
assert(IEC60870_History_Observe(&history.core, midnight + 900000U, &value, &quality, 1U) == IEC60870_HISTORY_OK);
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/FIXPT", 13U));
assert(Example_History_Directory_Count(&history) == 0U);
Example_History_Directory_Close(&history);
assert(IEC60870_History_Observe(&history.core, midnight + 86400000ULL, &value, &quality, 1U) == IEC60870_HISTORY_OK);
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/FIXPT", 13U));
assert(Example_History_Directory_Count(&history) == 1U);
assert(Example_History_Directory_Get(&history, 0U, &entry));
assert(entry.time.time.hour == 0U && entry.time.time.minute == 0U);
Example_History_Directory_Close(&history);
assert(Example_History_Read_Open(&history, (const uint8_t *)entry.name, strlen(entry.name), &size));
assert(size < sizeof(before));
assert(Example_History_Read(&history, 0U, before, size)); before[size] = 0U;
assert(strstr((const char *)before, "sectNum=\"2\"") != NULL);
assert(strstr((const char *)before, "_240000") != NULL);
Example_History_Read_Close(&history);
Example_History_Close(&history);
assert(!history.core.initialized);
assert(Example_History_Open(&history, directory, &config, "HISTORY_TEST"));
assert(IEC60870_History_Event_Count(&history.core, IEC60870_HISTORY_SOE) == 2U);
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/EXV", 11U));
assert(Example_History_Directory_Count(&history) == 1U);
Example_History_Close(&history);
Remove_Test_Directory(directory);
}
/**
* 功能:独占打开测试存储文件,供后续按固定偏移注入介质损坏。
* 输入参数:directory——测试目录路径(只读)。
* 输出参数:无。
* 返回值:成功打开的独占文件句柄,由调用方负责关闭;打开失败由断言终止用例。
* 说明:必须先关闭正式实例释放独占锁,再从外部直接修改测试文件以模拟介质损坏。
*/
static HANDLE Open_Raw_Store(const char *directory)
{
char file[MAX_PATH];
HANDLE handle;
int n = snprintf(file, sizeof(file), "%s/history.store", directory);
assert(n > 0 && (size_t)n < sizeof(file));
handle = CreateFileA(file, GENERIC_READ | GENERIC_WRITE, 0U, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
assert(handle != INVALID_HANDLE_VALUE);
return handle;
}
/**
* 功能:确认指定字节实际落盘改变。
* 输入参数:file——已打开的测试文件句柄;offset——文件、记录或缓冲区偏移。
* 输出参数:无。
* 返回值:无。
* 说明:通过测试句柄或实例持有的句柄注入,下一次读取、提交或打开必须检查该破坏。
*/
static void Corrupt_Byte(HANDLE file, uint32_t offset)
{
LARGE_INTEGER at;
DWORD done;
uint8_t byte;
at.QuadPart = offset;
assert(SetFilePointerEx(file, at, NULL, FILE_BEGIN));
assert(ReadFile(file, &byte, 1U, &done, NULL) && done == 1U);
byte ^= 0x55U;
assert(SetFilePointerEx(file, at, NULL, FILE_BEGIN));
assert(WriteFile(file, &byte, 1U, &done, NULL) && done == 1U);
assert(FlushFileBuffers(file));
}
/**
* 功能:计算整个存储文件的摘要并记录长度,检查拒绝操作是否保持介质不变。
* 输入参数:file——已打开的测试存储句柄。
* 输出参数:size——文件实际字节数。
* 返回值:文件全部字节的FNV-1a摘要。
* 说明:仅移动同步文件指针,不写入文件;读错或短读由断言报告。
*/
static uint32_t File_Digest(HANDLE file, uint32_t *size)
{
uint8_t bytes[512];
LARGE_INTEGER at, length;
uint32_t hash = UINT32_C(2166136261), remaining;
DWORD done, wanted, i;
assert(GetFileSizeEx(file, &length) && length.QuadPart >= 0 && length.QuadPart <= UINT32_MAX);
*size = remaining = (uint32_t)length.QuadPart;
at.QuadPart = 0;
assert(SetFilePointerEx(file, at, NULL, FILE_BEGIN));
while (remaining != 0U) {
wanted = remaining < sizeof(bytes) ? remaining : (DWORD)sizeof(bytes);
assert(ReadFile(file, bytes, wanted, &done, NULL) && done == wanted);
for (i = 0U; i < done; ++i) { hash = (hash ^ bytes[i]) * UINT32_C(16777619); }
remaining -= done;
}
return hash;
}
/**
* 功能:验证损坏或冲突槽的读写均失败且不改变输出和介质。
* 输入参数:kind——记录类别;index——逻辑槽号;wire——损坏前的可靠载荷;size——载荷长度。
* 输出参数:无。
* 返回值:无。
* 说明:直接调用真实后端回调,避免核心先闭锁后跳过提交而漏测写入路径。
*/
static void Assert_Store_Rejected(IEC60870_HistoryKind_t kind, uint32_t index,
const uint8_t *wire, size_t size)
{
uint8_t output[IEC60870_HISTORY_WIRE_MAX], expected[IEC60870_HISTORY_WIRE_MAX];
uint32_t size_before, size_after, hash_before;
size_t output_size = 12345U;
memset(output, 0xa5, sizeof(output)); memcpy(expected, output, sizeof(output));
hash_before = File_Digest((HANDLE)history.storage, &size_before);
assert(history.core.store.read(history.core.store.context, kind, index,
output, sizeof(output), &output_size) == IEC60870_HISTORY_ERROR);
assert(output_size == 12345U && memcmp(output, expected, sizeof(output)) == 0);
assert(!history.core.store.commit(history.core.store.context, kind, index, wire, size));
assert(File_Digest((HANDLE)history.storage, &size_after) == hash_before && size_after == size_before);
}
/**
* 功能:验证重开拒绝损坏档案,且不重新格式化或改写原文件。
* 输入参数:directory——当前已关闭的测试目录。
* 输出参数:无。
* 返回值:无。
* 说明:拒绝后仍能独占打开原文件,兼验失败路径释放句柄。
*/
static void Assert_Open_Rejected(const char *directory)
{
HANDLE file = Open_Raw_Store(directory);
uint32_t size_before, size_after, hash_before = File_Digest(file, &size_before);
CloseHandle(file);
assert(!Example_History_Open(&history, directory, &config, "REJECT_TEST"));
assert(!history.opened && !history.core.initialized);
file = Open_Raw_Store(directory);
assert(File_Digest(file, &size_after) == hash_before && size_before == size_after);
CloseHandle(file);
}
/**
* 功能:覆盖零长度、短载荷、文件头及载荷CRC损坏。
* 输入参数:parent——测试父目录(只读)。
* 输出参数:无。
* 返回值:无。
* 说明:不可恢复时拒绝打开且不自动重建。
*/
static void Test_Physical_Corruption(const char *parent)
{
unsigned case_index;
/* These are external disk mutations, not a mock read returning an error. */
for (case_index = 0U; case_index < 4U; ++case_index) {
char directory[MAX_PATH];
HANDLE file;
LARGE_INTEGER at, size_before, size_after;
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "CORRUPTION_TEST"));
assert(IEC60870_History_Append_Soe(&history.core, 1001U, 1U, 0U, start, true) == IEC60870_HISTORY_OK);
Example_History_Close(&history);
file = Open_Raw_Store(directory);
assert(GetFileSizeEx(file, &size_before));
if (case_index <= 1U) {
/* A total truncation must not be mistaken for a new empty store;
* a one-byte-short committed payload must not become EMPTY either. */
at.QuadPart = case_index == 0U ? 0 : size_before.QuadPart - 1;
assert(SetFilePointerEx(file, at, NULL, FILE_BEGIN));
assert(SetEndOfFile(file) && FlushFileBuffers(file));
} else {
Corrupt_Byte(file, case_index == 2U ? 0U : 64U);
}
assert(GetFileSizeEx(file, &size_before));
CloseHandle(file);
assert(!Example_History_Open(&history, directory, &config, "CORRUPTION_TEST"));
file = Open_Raw_Store(directory);
assert(GetFileSizeEx(file, &size_after) && size_after.QuadPart == size_before.QuadPart);
CloseHandle(file);
Example_History_Close(&history);
Remove_Test_Directory(directory);
}
for (case_index = 0U; case_index < 8U; ++case_index) {
char directory[MAX_PATH];
unsigned i;
IEC60870_HistoryEvent_t event, expected;
uint8_t wire[48];
size_t size;
static const uint32_t damage[] = {4U, 20U, 24U, 32U};
const uint32_t co_offset = 32U + 1024U * 2U * 80U;
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "BANK_TEST"));
for (i = 0U; i < 31U; ++i) {
assert(IEC60870_History_Append_Control(&history.core, 6001U,
IEC60870_HISTORY_OPER, 1U, start + i, true) == IEC60870_HISTORY_OK);
}
assert(IEC60870_History_Get_Event(&history.core, IEC60870_HISTORY_CO, 29U, &event) == IEC60870_HISTORY_OK);
assert(event.sequence == 31U);
assert(history.core.store.read(history.core.store.context, IEC60870_HISTORY_CO, 0U,
wire, sizeof(wire), &size) == IEC60870_HISTORY_OK);
/* 两个bank分别保存1和31;新副本坏也不能重建为连续的1..30。 */
Corrupt_Byte((HANDLE)history.storage, co_offset + (case_index / 4U) * 80U + damage[case_index % 4U]);
Assert_Store_Rejected(IEC60870_HISTORY_CO, 0U, wire, size);
memset(&event, 0xa5, sizeof(event)); memcpy(&expected, &event, sizeof(event));
assert(IEC60870_History_Get_Event(&history.core, IEC60870_HISTORY_CO, 29U, &event) == IEC60870_HISTORY_ERROR);
assert(history.core.faulted && memcmp(&event, &expected, sizeof(event)) == 0);
Example_History_Close(&history);
Assert_Open_Rejected(directory);
Remove_Test_Directory(directory);
}
}
/**
* 功能:验证同日极值已提交新版损坏后,在线读取和重开均不会退回旧极值。
* 输入参数:parent——测试父目录。
* 输出参数:无。
* 返回值:无。
* 说明:依次持久保存最大值10和20,再分别损坏旧、新bank,兼验提交拒绝与输出不变。
*/
static void Test_Extrema_Corruption(const char *parent)
{
unsigned bank;
const uint32_t day = IEC60870_History_Local_Day(start);
const uint32_t base = 32U + (1024U + 30U) * 160U + 32U * 96U * 464U;
for (bank = 0U; bank < 2U; ++bank) {
char directory[MAX_PATH];
float value = 10.0f;
uint8_t quality = 0U, wire[IEC60870_HISTORY_WIRE_MAX];
size_t size;
IEC60870_HistoryExtrema_t result, expected;
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "EXTREMA_TEST"));
assert(IEC60870_History_Observe(&history.core, start + 12345U, &value, &quality, 1U) == IEC60870_HISTORY_OK);
value = 20.0f;
assert(IEC60870_History_Observe(&history.core, start + 12346U, &value, &quality, 1U) == IEC60870_HISTORY_OK);
assert(IEC60870_History_Read_Extrema(&history.core, day, &result) == IEC60870_HISTORY_OK);
assert(result.points[0].max == 20.0f);
assert(history.core.store.read(history.core.store.context, IEC60870_HISTORY_EXV, day % 32U,
wire, sizeof(wire), &size) == IEC60870_HISTORY_OK);
Corrupt_Byte((HANDLE)history.storage, base + ((day % 32U) * 2U + bank) * 872U + 32U);
Assert_Store_Rejected(IEC60870_HISTORY_EXV, day % 32U, wire, size);
memset(&result, 0xa5, sizeof(result)); memcpy(&expected, &result, sizeof(result));
assert(IEC60870_History_Read_Extrema(&history.core, day, &result) == IEC60870_HISTORY_ERROR);
assert(history.core.faulted && memcmp(&result, &expected, sizeof(result)) == 0);
Example_History_Close(&history);
Assert_Open_Rejected(directory);
Remove_Test_Directory(directory);
}
}
/**
* 功能:为构造校验正确的冲突副本计算IEEE CRC32。
* 输入参数:bytes——待校验的字节区;size——字节数。
* 输出参数:无。
* 返回值:CRC32值。
* 说明:测试独立编码存储封装,不调用生产CRC辅助函数。
*/
static uint32_t Test_Crc(const uint8_t *bytes, size_t size)
{
uint32_t crc = UINT32_MAX;
size_t i;
unsigned bit;
for (i = 0U; i < size; ++i) {
crc ^= bytes[i];
for (bit = 0U; bit < 8U; ++bit) { crc = (crc >> 1U) ^ ((crc & 1U) != 0U ? UINT32_C(0xedb88320) : 0U); }
}
return ~crc;
}
/**
* 功能:按小端格式设置测试封装的长度或CRC字段。
* 输入参数:value——待写入的32位值。
* 输出参数:bytes——四字节目标区。
* 返回值:无。
* 说明:仅用于故障夹具构造,不修改生产编码逻辑。
*/
static void Test_Put32(uint8_t *bytes, uint32_t value)
{
unsigned i;
for (i = 0U; i < 4U; ++i) { bytes[i] = (uint8_t)(value >> (8U * i)); }
}
/**
* 功能:验证同代次相同内容可用,而不同内容或长度必须拒绝读写。
* 输入参数:parent——测试父目录。
* 输出参数:无。
* 返回值:无。
* 说明:两个bank的外层CRC均正确,使断言专门覆盖代次歧义而非普通CRC错误。
*/
static void Test_Equal_Generations(const char *parent)
{
unsigned scenario;
const uint32_t offset = 32U + 1024U * 2U * 80U;
for (scenario = 0U; scenario < 3U; ++scenario) {
char directory[MAX_PATH];
uint8_t bank[80], wire[48], output[48];
size_t size, output_size, payload_size = sizeof(wire);
LARGE_INTEGER at;
DWORD done;
HANDLE file;
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "GENERATION_TEST"));
assert(IEC60870_History_Append_Control(&history.core, 6001U,
IEC60870_HISTORY_OPER, 1U, start, true) == IEC60870_HISTORY_OK);
assert(history.core.store.read(history.core.store.context, IEC60870_HISTORY_CO, 0U,
wire, sizeof(wire), &size) == IEC60870_HISTORY_OK);
file = (HANDLE)history.storage; at.QuadPart = offset;
assert(SetFilePointerEx(file, at, NULL, FILE_BEGIN));
assert(ReadFile(file, bank, sizeof(bank), &done, NULL) && done == sizeof(bank));
if (scenario == 1U) {
bank[72] = 2U;
Test_Put32(bank + 76U, Test_Crc(bank + 32U, 44U));
} else if (scenario == 2U) { --payload_size; }
Test_Put32(bank + 4U, (uint32_t)payload_size);
Test_Put32(bank + 16U, Test_Crc(bank + 32U, payload_size));
Test_Put32(bank + 20U, Test_Crc(bank, 20U));
at.QuadPart = offset + 80U;
assert(SetFilePointerEx(file, at, NULL, FILE_BEGIN));
assert(WriteFile(file, bank, sizeof(bank), &done, NULL) && done == sizeof(bank));
assert(FlushFileBuffers(file));
if (scenario == 0U) {
assert(history.core.store.read(history.core.store.context, IEC60870_HISTORY_CO, 0U,
output, sizeof(output), &output_size) == IEC60870_HISTORY_OK);
assert(output_size == size && memcmp(output, wire, size) == 0);
assert(history.core.store.commit(history.core.store.context, IEC60870_HISTORY_CO, 0U, wire, size));
} else { Assert_Store_Rejected(IEC60870_HISTORY_CO, 0U, wire, size); }
Example_History_Close(&history);
if (scenario == 0U) {
assert(Example_History_Open(&history, directory, &config, "GENERATION_TEST"));
Example_History_Close(&history);
} else { Assert_Open_Rejected(directory); }
Remove_Test_Directory(directory);
}
}
/**
* 功能:验证以下场景与约束:写入34个自然日越过保存环,检查对外目录恰含最新30个完成日且重开后边界不变。
* 输入参数:parent——测试父目录(只读)。
* 输出参数:无。
* 返回值:无。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
static void Test_Thirty_Days(const char *parent)
{
char directory[MAX_PATH];
uint32_t first_day = IEC60870_History_Local_Day(start);
uint32_t i;
float value = 1.0f;
uint8_t quality = 0U;
ExampleHistoryEntry entry;
IEC60870_WallTime_t expected;
Create_Test_Directory(parent, directory);
assert(Example_History_Open(&history, directory, &config, "RETENTION_TEST"));
for (i = 0U; i < 34U; ++i) {
uint64_t sample = (uint64_t)(first_day + i) * 86400000ULL - 28800000ULL + 900000ULL;
value = (float)i;
assert(IEC60870_History_Observe(&history.core, sample, &value, &quality, 1U) == IEC60870_HISTORY_OK);
}
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/FIXPT", 13U));
assert(Example_History_Directory_Count(&history) == 30U);
assert(Example_History_Directory_Get(&history, 0U, &entry));
assert(IEC60870_Time_Seconds_To_Local((first_day + 3U) * 86400U - 28800U, 0U, &expected));
assert(entry.time.time.year == expected.year && entry.time.time.month == expected.month && entry.time.time.day == expected.day);
assert(Example_History_Directory_Get(&history, 29U, &entry));
assert(IEC60870_Time_Seconds_To_Local((first_day + 32U) * 86400U - 28800U, 0U, &expected));
assert(entry.time.time.year == expected.year && entry.time.time.month == expected.month && entry.time.time.day == expected.day);
Example_History_Directory_Close(&history);
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/EXV", 11U));
assert(Example_History_Directory_Count(&history) == 30U);
Example_History_Close(&history);
/* Reuse arguments stored inside the closed object without alias damage. */
assert(Example_History_Open(&history, history.parent, &history.core.config, history.device_name));
assert(Example_History_Directory_Open(&history, (const uint8_t *)"HISTORY/FIXPT", 13U));
assert(Example_History_Directory_Count(&history) == 30U);
Example_History_Close(&history);
Remove_Test_Directory(directory);
}
/**
* 功能:使用指定测试目录验证历史适配器的中断恢复、读取快照、实体损坏及30天保留范围。
* 输入参数:argc——命令行参数数量;argv——命令行参数数组。
* 输出参数:无。
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
int main(int argc, char **argv)
{
assert(argc == 2);
Test_Interruptions(argv[1]);
Test_Snapshots(argv[1]);
Test_Physical_Corruption(argv[1]);
Test_Extrema_Corruption(argv[1]);
Test_Equal_Generations(argv[1]);
Test_Thirty_Days(argv[1]);
puts("History host adapter: interruptions, corruption, 30-day retention and snapshots passed");
return 0;
}