/* 附录 C 宿主后端实文件测试:同步阶段中断、重开恢复、介质损坏与快照隔离。 * 只操作传入目录下本测试创建的子目录,按已知文件名清理,不递归删除。 * 本测试模拟操作系统可观察的同步边界,不代表硬件突然失电验收。 */ #include "iec104_example_user_log.h" #include #include #include #include static ExampleUserLog log_store, second; static unsigned interrupt_after; static const uint64_t start = UINT64_C(1794412800000); static uint8_t before[4096], after[4096]; /** * 功能:在指定真实同步完成点注入中断,用于核对用户日志的持久恢复边界。 * 输入参数:无。 * 输出参数:无。 * 返回值:true 允许继续当前持久操作;false 表示命中本轮指定中断点。 * 说明:生产构建没有此符号;在指定的真实 Flush 完成点拒绝返回成功。 */ bool Example_User_Log_Test_Continue(void) { if (interrupt_after == 0U) { return true; } return --interrupt_after != 0U; } /** * 功能:用系统唯一名字隔离用例。 * 输入参数:parent——测试父目录(只读);directory——测试目录路径。 * 输出参数:directory——生成的测试目录或文件路径。 * 返回值:无。 * 说明:调用方提供 MAX_PATH 缓冲,父目录必须由测试入口限定。 */ static void Create_Directory(const char *parent, char *directory) { assert(GetTempFileNameA(parent, "ult", 0U, directory)); assert(DeleteFileA(directory)); assert(CreateDirectoryA(directory, NULL)); } /** * 功能:只拼接固定存储名,不接受协议侧虚拟路径。 * 输入参数:directory——测试目录路径(只读);path——测试路径。 * 输出参数:path——生成的测试目录或文件路径。 * 返回值:无。 * 说明:超长路径立即使测试失败。 */ static void Store_Path(const char *directory, char *path) { int length = snprintf(path, MAX_PATH, "%s/ulog.store", directory); assert(length > 0 && length < MAX_PATH); } /** * 功能:删除当前用例拥有的已知持久文件和空测试目录。 * 输入参数:directory——测试目录路径(只读)。 * 输出参数:无。 * 返回值:无。 * 说明:若有遗漏快照句柄或未知文件,删除目录失败会暴露资源未收尾。 */ static void Remove_Directory(const char *directory) { char path[MAX_PATH]; Store_Path(directory, path); assert(DeleteFileA(path)); assert(RemoveDirectoryA(directory)); } /** * 功能:正式后端须先关闭并释放独占锁,才允许用例直接操作介质字节。 * 输入参数:directory——测试目录路径(只读)。 * 输出参数:无。 * 返回值:成功打开的独占文件句柄,由调用方负责关闭;打开失败由断言终止用例。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static HANDLE Open_Raw(const char *directory) { char path[MAX_PATH]; HANDLE file; Store_Path(directory, path); file = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0U, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); assert(file != INVALID_HANDLE_VALUE); return file; } /** * 功能:故障注入使用已知绝对偏移,与正式后端使用同一套 Windows 文件定位语义。 * 输入参数:file——已打开的测试文件句柄;offset——文件、记录或缓冲区偏移。 * 输出参数:无。 * 返回值:无。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static void Seek_Raw(HANDLE file, uint32_t offset) { LARGE_INTEGER position; position.QuadPart = offset; assert(SetFilePointerEx(file, position, NULL, FILE_BEGIN)); } /** * 功能:使用实际字节翻转触发 CRC 失败,避免只用返回值模拟掩盖读取路径。 * 输入参数:file——已打开的测试文件句柄;offset——文件、记录或缓冲区偏移。 * 输出参数:无。 * 返回值:无。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static void Corrupt_Byte(HANDLE file, uint32_t offset) { uint8_t byte; DWORD done; Seek_Raw(file, offset); assert(ReadFile(file, &byte, 1U, &done, NULL) && done == 1U); byte ^= 0x55U; Seek_Raw(file, offset); assert(WriteFile(file, &byte, 1U, &done, NULL) && done == 1U); assert(FlushFileBuffers(file)); } /** * 功能:修改指定 bank 的代次并重算头 CRC,构造两份均通过完整性校验的同代次记录。 * 输入参数:file——已打开的测试存储;offset——bank 头的文件偏移;generation——待写入的代次。 * 输出参数:无。 * 返回值:无。 * 说明:保留载荷及其 CRC,只写回完整头并同步,用于单独检验代次冲突规则。 */ static void Set_Bank_Generation(HANDLE file, uint32_t offset, uint64_t generation) { uint8_t header[32]; uint32_t crc = UINT32_MAX; DWORD done; unsigned i, bit; Seek_Raw(file, offset); assert(ReadFile(file, header, sizeof(header), &done, NULL) && done == sizeof(header)); for (i = 0U; i < 8U; ++i) { header[8U + i] = (uint8_t)(generation >> (8U * i)); } for (i = 0U; i < 20U; ++i) { crc ^= header[i]; for (bit = 0U; bit < 8U; ++bit) { crc = (crc >> 1) ^ ((crc & 1U) != 0U ? UINT32_C(0xedb88320) : 0U); } } crc = ~crc; for (i = 0U; i < 4U; ++i) { header[20U + i] = (uint8_t)(crc >> (8U * i)); } Seek_Raw(file, offset); assert(WriteFile(file, header, sizeof(header), &done, NULL) && done == sizeof(header)); assert(FlushFileBuffers(file)); } /** * 功能:流式计算全文件摘要,确认失败打开没有同长度重写或静默格式化。 * 输入参数:file——已打开的测试文件句柄;size——数据长度或文件大小。 * 输出参数:size——本次数据、文件或 ASDU 的实际长度。 * 返回值:独立计算得到的 32 位校验摘要。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static uint32_t File_Digest(HANDLE file, uint32_t *size) { uint8_t bytes[256]; uint32_t hash = 2166136261UL, total = 0U; DWORD done; Seek_Raw(file, 0U); do { DWORD i; assert(ReadFile(file, bytes, sizeof(bytes), &done, NULL)); for (i = 0U; i < done; ++i) { hash = (hash ^ bytes[i]) * 16777619UL; } total += done; } while (done != 0U); *size = total; return hash; } /** * 功能:基线通过公开核心 API 写入,不能直接构造 bank 绕过提交同步与循环序号。 * 输入参数:time——测试时间值;text——待匹配或写入的文本(只读)。 * 输出参数:无。 * 返回值:无。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static void Append(uint64_t time, const char *text) { assert(IEC60870_UserLog_Append(&log_store.core, 2U, time, 1U, text) == IEC60870_USER_LOG_OK); } /** * 功能:基线只做一次完整环写入,各故障用例复制到独立目录,仍通过真实文件恢复。 * 输入参数:parent——测试父目录(只读);directory——测试目录路径。 * 输出参数:directory——生成的测试目录或文件路径。 * 返回值:无。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static void Create_Seed(const char *parent, char *directory) { unsigned i; Create_Directory(parent, directory); assert(Example_User_Log_Open(&log_store, directory, "SEED")); for (i = 0U; i < IEC60870_USER_LOG_CAPACITY; ++i) { Append(start + i, "baseline"); } assert(IEC60870_UserLog_Count(&log_store.core) == IEC60870_USER_LOG_CAPACITY); Example_User_Log_Close(&log_store); } /** * 功能:复制已关闭的完整基线。 * 输入参数:parent——测试父目录(只读);seed——种子文件路径(只读);directory——测试目录路径。 * 输出参数:directory——生成的测试目录或文件路径。 * 返回值:无。 * 说明:每个阶段有独立副本,上一用例的损坏不会污染下一用例。 */ static void Copy_Seed(const char *parent, const char *seed, char *directory) { char source[MAX_PATH], destination[MAX_PATH]; Create_Directory(parent, directory); Store_Path(seed, source); Store_Path(directory, destination); assert(CopyFileA(source, destination, TRUE)); } /** * 功能:通过核心读取最旧/最新序号,核对满环保持 1024 条及覆盖后的逻辑顺序。 * 输入参数:first——首项地址或保留范围起点;last——末段标志或范围终点。 * 输出参数:无。 * 返回值:无。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static void Assert_Range(uint64_t first, uint64_t last) { IEC60870_UserLogRecord_t record; assert(IEC60870_UserLog_Count(&log_store.core) == IEC60870_USER_LOG_CAPACITY); assert(IEC60870_UserLog_Get(&log_store.core, 0U, &record) == IEC60870_USER_LOG_OK); assert(record.sequence == first); assert(IEC60870_UserLog_Get(&log_store.core, IEC60870_USER_LOG_CAPACITY - 1U, &record) == IEC60870_USER_LOG_OK); assert(record.sequence == last); } /** * 功能:验证槽 0 损坏或冲突时后端拒绝读写,核心查询闭锁且不改变输出或介质。 * 输入参数:candidate——原本有效的槽载荷;length——载荷长度;oldest_index——槽 0 对应的逻辑查询下标。 * 输出参数:无。 * 返回值:无。 * 说明:直接调用 commit 以确保拒绝来自后端校验;随后核对核心故障状态和追加阻断。 */ static void Assert_Slot_Zero_Rejected(const uint8_t *candidate, size_t length, size_t oldest_index) { HANDLE file = (HANDLE)log_store.storage; uint8_t bytes[IEC60870_USER_LOG_WIRE_MAX], expected[IEC60870_USER_LOG_WIRE_MAX]; IEC60870_UserLogRecord_t record, saved_record; uint32_t size_before, size_after, hash_before, hash_after; uint64_t sequence = log_store.core.sequence; size_t count = log_store.core.count, unchanged = 123U; hash_before = File_Digest(file, &size_before); memset(bytes, 0xa5, sizeof(bytes)); memcpy(expected, bytes, sizeof(expected)); assert(log_store.core.store.read(log_store.core.store.context, 0U, bytes, sizeof(bytes), &unchanged) == IEC60870_USER_LOG_ERROR); assert(unchanged == 123U && memcmp(bytes, expected, sizeof(bytes)) == 0); assert(!log_store.core.store.commit(log_store.core.store.context, 0U, candidate, length)); memset(&record, 0xa5, sizeof(record)); memcpy(&saved_record, &record, sizeof(record)); assert(IEC60870_UserLog_Get(&log_store.core, oldest_index, &record) == IEC60870_USER_LOG_ERROR); assert(log_store.core.faulted && memcmp(&record, &saved_record, sizeof(record)) == 0); assert(IEC60870_UserLog_Append(&log_store.core, 2U, start + 1025U, 1U, "must not write") == IEC60870_USER_LOG_ERROR); assert(log_store.core.sequence == sequence && log_store.core.count == count); hash_after = File_Digest(file, &size_after); assert(hash_before == hash_after && size_before == size_after); } /** * 功能:验证以下场景与约束:第 1/2 阶段失败保留旧环,第 3 阶段失败可能已经可靠提交。 * 输入参数:parent——测试父目录(只读);seed——种子文件路径(只读)。 * 输出参数:无。 * 返回值:无。 * 说明:禁止在故障实例继续写。 * 注入点位于 Flush 返回之后,重开验证实际磁盘版本;没有切断电源,也未模拟 * 同步内部的扇区撕裂,因此本用例不能替代目标介质的突然失电验收。 */ static void Test_Interruptions(const char *parent, const char *seed) { unsigned stage; for (stage = 1U; stage <= 3U; ++stage) { char directory[MAX_PATH]; Copy_Seed(parent, seed, directory); assert(Example_User_Log_Open(&log_store, directory, "INTERRUPT")); assert(!Example_User_Log_Open(&second, directory, "LOCKED")); interrupt_after = stage; assert(IEC60870_UserLog_Append(&log_store.core, 2U, start + 1024U, 0U, "newest") == IEC60870_USER_LOG_ERROR); assert(log_store.core.faulted); assert(IEC60870_UserLog_Append(&log_store.core, 2U, start + 1025U, 1U, "must not write") == IEC60870_USER_LOG_ERROR); Example_User_Log_Close(&log_store); interrupt_after = 0U; assert(Example_User_Log_Open(&log_store, directory, "INTERRUPT")); Assert_Range(stage == 3U ? 2U : 1U, stage == 3U ? 1025U : 1024U); Example_User_Log_Close(&log_store); Remove_Directory(directory); } /* 首次写入没有旧副本,前两阶段留下的候选必须恢复为空库。 */ for (stage = 1U; stage <= 2U; ++stage) { char directory[MAX_PATH]; Create_Directory(parent, directory); assert(Example_User_Log_Open(&log_store, directory, "FIRST")); interrupt_after = stage; assert(IEC60870_UserLog_Append(&log_store.core, 1U, start, 1U, "startup") == IEC60870_USER_LOG_ERROR); Example_User_Log_Close(&log_store); interrupt_after = 0U; assert(Example_User_Log_Open(&log_store, directory, "FIRST")); assert(IEC60870_UserLog_Count(&log_store.core) == 0U); Example_User_Log_Close(&log_store); Remove_Directory(directory); } } /** * 功能:检查空库、目录旧版本、独立下载、任意偏移、大小写以及拒绝重开活对象。 * 输入参数:parent——测试父目录(只读)。 * 输出参数:无。 * 返回值:无。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static void Test_Snapshots(const char *parent) { char directory[MAX_PATH]; uint32_t size, unchanged; ExampleUserLogEntry entry, second_entry; IEC60870_UserLogRecord_t record; Create_Directory(parent, directory); assert(!Example_User_Log_Open(&log_store, directory, "")); assert(!Example_User_Log_Open(&log_store, directory, "1234567890123456789012345")); assert(!Example_User_Log_Open(&log_store, directory, "bad\nidentifier")); assert(Example_User_Log_Open(&log_store, directory, "A&B\"unit")); assert(!Example_User_Log_Open(&log_store, directory, "SECOND")); assert(!Example_User_Log_Open(&second, directory, "LOCKED")); assert(Example_User_Log_Directory_Count(&log_store) == 0U); assert(!Example_User_Log_Directory_Open(&log_store, NULL, 0U)); assert(!Example_User_Log_Directory_Open(&log_store, (const uint8_t *)"HISTORY/ulog", 12U)); assert(Example_User_Log_Read_Open(&log_store, (const uint8_t *)IEC60870_USER_LOG_FILE_NAME, strlen(IEC60870_USER_LOG_FILE_NAME), &size)); assert(size < sizeof(before)); assert(Example_User_Log_Read(&log_store, 0U, before, size)); before[size] = 0U; assert(strstr((const char *)before, "num=\"0\"") != NULL); assert(strstr((const char *)before, "A&B"unit") != NULL); assert(Example_User_Log_Read(&log_store, size, NULL, 0U)); assert(!Example_User_Log_Read(&log_store, size + 1U, NULL, 0U)); assert(!Example_User_Log_Read(&log_store, size, after, 1U)); Example_User_Log_Read_Close(&log_store); assert(Example_User_Log_Directory_Open(&log_store, (const uint8_t *)"HISTORY/ULOG", 12U)); assert(Example_User_Log_Directory_Get(&log_store, 0U, &entry)); assert(entry.time.time.year == 2000U && entry.time.time.month == 1U && entry.time.time.day == 1U); assert(Example_User_Log_Directory_Count(&log_store) == 1U); assert(!Example_User_Log_Directory_Get(&log_store, 1U, &second_entry)); Example_User_Log_Directory_Close(&log_store); Append(start, "first"); assert(Example_User_Log_Directory_Open(&log_store, (const uint8_t *)"HISTORY/ULOG", 12U)); assert(!Example_User_Log_Directory_Open(&log_store, (const uint8_t *)"HISTORY/ULOG", 12U)); assert(Example_User_Log_Directory_Get(&log_store, 0U, &entry)); assert(strcmp(entry.name, IEC60870_USER_LOG_FILE_NAME) == 0); Example_User_Log_Directory_Close(&log_store); Append(start - 1000U, "second after clock reversal"); assert(Example_User_Log_Read_Open(&log_store, (const uint8_t *)entry.name, strlen(entry.name), &size)); assert(size == entry.size && size < sizeof(before)); assert(Example_User_Log_Read(&log_store, 0U, before, size)); before[size] = 0U; assert(strstr((const char *)before, "num=\"1\"") != NULL); assert(strstr((const char *)before, "first") != NULL); unchanged = 123U; assert(!Example_User_Log_Read_Open(&log_store, (const uint8_t *)entry.name, strlen(entry.name), &unchanged)); assert(unchanged == 123U); assert(Example_User_Log_Directory_Open(&log_store, (const uint8_t *)"HISTORY/ULOG", 12U)); assert(Example_User_Log_Directory_Get(&log_store, 0U, &second_entry)); assert(second_entry.time.time.second == 59U); Example_User_Log_Directory_Close(&log_store); assert(Example_User_Log_Read(&log_store, 0U, after, size)); assert(memcmp(before, after, size) == 0); memset(after, 0, sizeof(after)); assert(Example_User_Log_Read(&log_store, 7U, after, size - 7U)); assert(memcmp(before + 7U, after, size - 7U) == 0); Example_User_Log_Read_Close(&log_store); assert(!Example_User_Log_Read_Open(&log_store, (const uint8_t *)"HISTORY/ULOG/ULOG.xml", 21U, &unchanged)); assert(!Example_User_Log_Read_Open(&log_store, (const uint8_t *)"../ulog.store", 13U, &unchanged)); assert(unchanged == 123U); assert(Example_User_Log_Read_Open(&log_store, (const uint8_t *)entry.name, strlen(entry.name), &size)); assert(size == second_entry.size && size < sizeof(after)); assert(Example_User_Log_Read(&log_store, 0U, after, size)); after[size] = 0U; assert(strstr((const char *)after, "num=\"2\"") != NULL); Example_User_Log_Read_Close(&log_store); /* 模拟临时缓存短读:失败不得改变 size,也不得把部分副本发布给协议。 */ Seek_Raw((HANDLE)log_store.directory_file, 1U); assert(SetEndOfFile((HANDLE)log_store.directory_file)); assert(!Example_User_Log_Read_Open(&log_store, (const uint8_t *)entry.name, strlen(entry.name), &unchanged)); assert(unchanged == 123U && log_store.read_file == NULL); assert(Example_User_Log_Directory_Open(&log_store, (const uint8_t *)"HISTORY/ULOG", 12U)); Example_User_Log_Close(&log_store); assert(!log_store.core.initialized && !log_store.opened); Example_User_Log_Close(&log_store); assert(Example_User_Log_Open(&log_store, directory, "RESTART")); assert(IEC60870_UserLog_Count(&log_store.core) == 2U); assert(IEC60870_UserLog_Get(&log_store.core, 1U, &record) == IEC60870_USER_LOG_OK); assert(record.utc_ms == start - 1000U); assert(Example_User_Log_Read_Open(&log_store, (const uint8_t *)entry.name, strlen(entry.name), &size)); assert(size < sizeof(after)); assert(Example_User_Log_Read(&log_store, 0U, after, size)); after[size] = 0U; assert(strstr((const char *)after, "num=\"2\"") != NULL); Example_User_Log_Close(&log_store); Remove_Directory(directory); } /** * 功能:验证以下场景与约束:本节注入的零长、短载荷、头/载荷 CRC 错及超界尾部必须拒绝且不改现有字节。 * 输入参数:parent——测试父目录(只读);seed——种子文件路径(只读)。 * 输出参数:无。 * 返回值:无。 * 说明:不宣称覆盖任意介质破坏:无外部账本时,完全移除后仍合法的历史前缀不可辨别。 * 双 bank 用例分别破坏旧版和新版的头、载荷或提交标记,均须拒绝读写和重开。 */ static void Test_Corruption(const char *parent, const char *seed) { unsigned scenario; for (scenario = 0U; scenario < 5U; ++scenario) { char directory[MAX_PATH]; HANDLE file; LARGE_INTEGER old_size; uint32_t size_before, size_after, hash_before, hash_after; Copy_Seed(parent, seed, directory); file = Open_Raw(directory); assert(GetFileSizeEx(file, &old_size)); if (scenario <= 1U) { Seek_Raw(file, scenario == 0U ? 0U : (uint32_t)old_size.QuadPart - 1U); assert(SetEndOfFile(file) && FlushFileBuffers(file)); } else if (scenario == 2U || scenario == 3U) { Corrupt_Byte(file, scenario == 2U ? 16U : 64U); } else { Seek_Raw(file, 32U + IEC60870_USER_LOG_CAPACITY * 2U * (32U + IEC60870_USER_LOG_WIRE_MAX) + 1U); assert(SetEndOfFile(file) && FlushFileBuffers(file)); } hash_before = File_Digest(file, &size_before); CloseHandle(file); assert(!Example_User_Log_Open(&log_store, directory, "CORRUPT")); Example_User_Log_Close(&log_store); file = Open_Raw(directory); hash_after = File_Digest(file, &size_after); assert(hash_before == hash_after && size_before == size_after); CloseHandle(file); Remove_Directory(directory); } for (scenario = 0U; scenario < 6U; ++scenario) { char directory[MAX_PATH]; HANDLE file; uint8_t candidate[IEC60870_USER_LOG_WIRE_MAX]; size_t length; uint32_t size_before, size_after, hash_before, hash_after; uint32_t target = 32U + (scenario / 3U) * (32U + IEC60870_USER_LOG_WIRE_MAX) + (scenario % 3U == 0U ? 20U : (scenario % 3U == 1U ? 32U : 24U)); Copy_Seed(parent, seed, directory); assert(Example_User_Log_Open(&log_store, directory, "BANKS")); Append(start + 1024U, "overwritten oldest"); assert(log_store.core.store.read(log_store.core.store.context, 0U, candidate, sizeof(candidate), &length) == IEC60870_USER_LOG_OK); file = (HANDLE)log_store.storage; Corrupt_Byte(file, target); hash_before = File_Digest(file, &size_before); Assert_Slot_Zero_Rejected(candidate, length, IEC60870_USER_LOG_CAPACITY - 1U); Example_User_Log_Close(&log_store); assert(!Example_User_Log_Open(&log_store, directory, "BANKS")); file = Open_Raw(directory); hash_after = File_Digest(file, &size_after); assert(hash_before == hash_after && size_before == size_after); CloseHandle(file); Remove_Directory(directory); } } /** * 功能:验证同代次同内容仍可读写,同代次载荷内容或长度冲突则拒绝读写和恢复。 * 输入参数:parent——测试父目录;seed——已写满且关闭的基线目录。 * 输出参数:无。 * 返回值:无。 * 说明:通过真实提交建立有效 bank,再仅调整代次及头 CRC,避免把冲突误测成 CRC 损坏。 */ static void Test_Equal_Generations(const char *parent, const char *seed) { unsigned scenario; for (scenario = 0U; scenario < 3U; ++scenario) { char directory[MAX_PATH]; HANDLE file; uint8_t candidate[IEC60870_USER_LOG_WIRE_MAX], actual[IEC60870_USER_LOG_WIRE_MAX]; size_t length, actual_length; uint32_t size_before, size_after, hash_before, hash_after; Copy_Seed(parent, seed, directory); assert(Example_User_Log_Open(&log_store, directory, "EQUAL_GENERATION")); assert(log_store.core.store.read(log_store.core.store.context, 0U, candidate, sizeof(candidate), &length) == IEC60870_USER_LOG_OK); if (scenario == 0U) { assert(log_store.core.store.commit(log_store.core.store.context, 0U, candidate, length)); } else { Append(start + 1024U, scenario == 1U ? "baseline" : "different length"); } file = (HANDLE)log_store.storage; Set_Bank_Generation(file, 32U + 32U + IEC60870_USER_LOG_WIRE_MAX, 1U); if (scenario == 0U) { Example_User_Log_Close(&log_store); assert(Example_User_Log_Open(&log_store, directory, "EQUAL_GENERATION")); Assert_Range(1U, 1024U); assert(log_store.core.store.read(log_store.core.store.context, 0U, actual, sizeof(actual), &actual_length) == IEC60870_USER_LOG_OK); assert(actual_length == length && memcmp(actual, candidate, length) == 0); assert(log_store.core.store.commit(log_store.core.store.context, 0U, candidate, length)); Example_User_Log_Close(&log_store); assert(Example_User_Log_Open(&log_store, directory, "EQUAL_GENERATION")); Assert_Range(1U, 1024U); Example_User_Log_Close(&log_store); } else { hash_before = File_Digest(file, &size_before); Assert_Slot_Zero_Rejected(candidate, length, IEC60870_USER_LOG_CAPACITY - 1U); Example_User_Log_Close(&log_store); assert(!Example_User_Log_Open(&log_store, directory, "EQUAL_GENERATION")); file = Open_Raw(directory); hash_after = File_Digest(file, &size_after); assert(hash_before == hash_after && size_before == size_after); CloseHandle(file); } Remove_Directory(directory); } } /** * 功能:验证以下场景与约束:同目录既有 history.store 的锁与内容不属于此后端,ULOG 不能读取或改写它。 * 输入参数:parent——测试父目录(只读)。 * 输出参数:无。 * 返回值:无。 * 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。 */ static void Test_Separate_Store(const char *parent) { char directory[MAX_PATH], path[MAX_PATH], missing[MAX_PATH]; HANDLE other; DWORD done; char bytes[8] = {0}; int length; Create_Directory(parent, directory); length = snprintf(path, sizeof(path), "%s/history.store", directory); assert(length > 0 && (size_t)length < sizeof(path)); other = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0U, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); assert(other != INVALID_HANDLE_VALUE); assert(WriteFile(other, "sentinel", 8U, &done, NULL) && done == 8U); assert(Example_User_Log_Open(&log_store, directory, "SEPARATE")); Append(start, "independent"); Example_User_Log_Close(&log_store); Seek_Raw(other, 0U); assert(ReadFile(other, bytes, sizeof(bytes), &done, NULL) && done == sizeof(bytes)); assert(memcmp(bytes, "sentinel", sizeof(bytes)) == 0); CloseHandle(other); assert(DeleteFileA(path)); length = snprintf(missing, sizeof(missing), "%s/missing", directory); assert(length > 0 && (size_t)length < sizeof(missing)); assert(!Example_User_Log_Open(&log_store, missing, "MISSING")); assert(!Example_User_Log_Open(&log_store, "", "EMPTY_PATH")); Remove_Directory(directory); } /** * 功能:只接受一个由构建脚本提供的隔离父目录。 * 输入参数:argc——命令行参数数量;argv——命令行参数数组。 * 输出参数:无。 * 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。 * 说明:所有成功用例清除自己的已知文件。 */ int main(int argc, char **argv) { char seed[MAX_PATH]; assert(argc == 2); Create_Seed(argv[1], seed); Test_Interruptions(argv[1], seed); Test_Snapshots(argv[1]); Test_Corruption(argv[1], seed); Test_Equal_Generations(argv[1], seed); Test_Separate_Store(argv[1]); Remove_Directory(seed); puts("UserLog host: persistence, interrupted commits, corruption, locks and snapshots passed"); return 0; }