364 lines
16 KiB
C
364 lines
16 KiB
C
/* Host recording adapter checks. argv[1] is an existing, repository-local output
|
|
* parent supplied by the test runner. Existing files are never overwritten or
|
|
* removed; cleanup is limited to exact paths this process successfully created. */
|
|
#include "iec104_example_recording.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
typedef struct { char path[1024]; bool owned; } OwnedFile;
|
|
static OwnedFile owned_files[6];
|
|
static ExampleRecording recording, second;
|
|
static unsigned groups;
|
|
|
|
/**
|
|
* 功能:只删除本进程已登记拥有的精确文件路径,删除成功才清除归属标志,不扫描或递归清理目录。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static void Cleanup(void)
|
|
{
|
|
size_t index;
|
|
for (index = 0U; index < sizeof(owned_files) / sizeof(owned_files[0]); ++index) {
|
|
if (owned_files[index].owned && remove(owned_files[index].path) == 0) { owned_files[index].owned = false; }
|
|
}
|
|
}
|
|
|
|
#define CHECK(condition) do { if (!(condition)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #condition); Cleanup(); exit(1); \
|
|
} } while (0)
|
|
|
|
/**
|
|
* 功能:拼接测试父目录和已知后缀,断言路径未截断,供保存及清理使用同一精确路径。
|
|
* 输入参数:path——测试路径;capacity——调用方提供的缓冲容量;parent——测试父目录(只读);suffix——待拼接的固定路径后缀(只读)。
|
|
* 输出参数:path——生成的测试目录或文件路径。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Path(char *path, size_t capacity, const char *parent, const char *suffix)
|
|
{
|
|
int length = snprintf(path, capacity, "%s/%s", parent, suffix);
|
|
CHECK(length > 0 && (size_t)length < capacity);
|
|
}
|
|
|
|
/**
|
|
* 功能:要求目标确实不存在,权限错误等其他stat失败不能当作可安全创建的空路径。
|
|
* 输入参数:path——测试路径(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Absent(const char *path)
|
|
{
|
|
struct stat entry;
|
|
CHECK(stat(path, &entry) != 0 && errno == ENOENT);
|
|
}
|
|
|
|
/**
|
|
* 功能:将本进程成功创建的文件登记到有界清理表,避免失败清理时删除运行前已有内容。
|
|
* 输入参数:path——测试路径(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Own(const char *path)
|
|
{
|
|
size_t index;
|
|
for (index = 0U; index < sizeof(owned_files) / sizeof(owned_files[0]); ++index) {
|
|
if (!owned_files[index].owned) {
|
|
CHECK(strlen(path) < sizeof(owned_files[index].path));
|
|
strcpy(owned_files[index].path, path); owned_files[index].owned = true;
|
|
return;
|
|
}
|
|
}
|
|
CHECK(false);
|
|
}
|
|
|
|
/**
|
|
* 功能:在已确认不存在的路径写入含NUL和高位字节的哨兵文件,并登记归属,供验证失败路径不覆盖已有文件。
|
|
* 输入参数:path——测试路径(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Create_Sentinel(const char *path)
|
|
{
|
|
static const uint8_t bytes[] = {0x51U, 0x00U, 0xFFU, 0x82U};
|
|
FILE *file;
|
|
Absent(path);
|
|
file = fopen(path, "wb"); CHECK(file != NULL); Own(path);
|
|
CHECK(fwrite(bytes, 1U, sizeof(bytes), file) == sizeof(bytes));
|
|
CHECK(fclose(file) == 0);
|
|
}
|
|
|
|
/**
|
|
* 功能:核对哨兵仍恰为原始4字节,既检查内容也检查长度,发现截断、追加或覆盖。
|
|
* 输入参数:path——测试路径(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Check_Sentinel(const char *path)
|
|
{
|
|
uint8_t bytes[5];
|
|
FILE *file = fopen(path, "rb");
|
|
CHECK(file != NULL);
|
|
CHECK(fread(bytes, 1U, sizeof(bytes), file) == 4U && !ferror(file));
|
|
CHECK(bytes[0] == 0x51U && bytes[1] == 0U && bytes[2] == 0xFFU && bytes[3] == 0x82U);
|
|
CHECK(fclose(file) == 0);
|
|
}
|
|
|
|
/**
|
|
* 功能:生成固定有效CP56触发时间,用于复现CFG内容、文件名及内部时间别名输入。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:按测试样本构造的 CP56Time2a 时间值。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static IEC60870_CP56Time2a_t Trigger_Time(void)
|
|
{
|
|
IEC60870_CP56Time2a_t time;
|
|
memset(&time, 0, sizeof(time));
|
|
time.time.year = 2014U; time.time.month = 11U; time.time.day = 14U;
|
|
time.time.hour = 19U; time.time.minute = 21U; time.time.second = 35U; time.time.ms = 830U;
|
|
return time;
|
|
}
|
|
|
|
/**
|
|
* 功能:独立解码DAT小端32位序号与时标,直接检查文件字节,不调用生产解码逻辑。
|
|
* 输入参数:bytes——报文字节缓冲区(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:四个小端字节还原的无符号 32 位值。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static uint32_t Read32(const uint8_t *bytes)
|
|
{
|
|
return (uint32_t)bytes[0] | ((uint32_t)bytes[1] << 8U) |
|
|
((uint32_t)bytes[2] << 16U) | ((uint32_t)bytes[3] << 24U);
|
|
}
|
|
|
|
/**
|
|
* 功能:将DAT小端16位补码显式扩展为int32,避免测试依赖宿主有符号窄化转换行为。
|
|
* 输入参数:bytes——报文字节缓冲区(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:小端 16 位补码显式扩展后的有符号 32 位值。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static int32_t Read16(const uint8_t *bytes)
|
|
{
|
|
uint32_t raw = bytes[0] | ((uint32_t)bytes[1] << 8U);
|
|
return raw <= 32767U ? (int32_t)raw : (int32_t)raw - 65536;
|
|
}
|
|
|
|
/**
|
|
* 功能:以137字节非记录对齐分片比较磁盘CFG/DAT和适配层读取结果,并要求文件末尾无多余字节。
|
|
* 输入参数:value——测试对象值或原始位模式(只读);index——待访问或核对的条目下标;path——测试路径(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Check_Disk(const ExampleRecording *value, unsigned index, const char *path)
|
|
{
|
|
uint8_t actual[137], expected[137];
|
|
uint32_t offset = 0U;
|
|
uint32_t size = index == 0U ? (uint32_t)value->cfg_size : value->dat_size;
|
|
FILE *file = fopen(path, "rb");
|
|
CHECK(file != NULL);
|
|
while (offset < size) {
|
|
size_t length = size - offset < sizeof(actual) ? size - offset : sizeof(actual);
|
|
CHECK(fread(actual, 1U, length, file) == length);
|
|
CHECK(Example_Recording_Read(value, index, offset, expected, length));
|
|
CHECK(memcmp(actual, expected, length) == 0);
|
|
offset += (uint32_t)length;
|
|
}
|
|
CHECK(fgetc(file) == EOF && !ferror(file));
|
|
CHECK(fclose(file) == 0);
|
|
}
|
|
|
|
/**
|
|
* 功能:验证模拟录波完成状态、961帧窗口、规范路径及CFG标度/时标,
|
|
* 直接核对触发前、触发点、断路器变位和末帧的模拟量、数字量及任意尾片段。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Test_Demo(void)
|
|
{
|
|
IEC60870_CP56Time2a_t time = Trigger_Time();
|
|
IEC60870_FaultRecorderInfo_t info;
|
|
uint8_t before[26], trigger[26], breaker[26], last[26], tail[7];
|
|
size_t index;
|
|
CHECK(Example_Recording_Demo(&recording, &time, 8U, 10U));
|
|
CHECK(recording.complete && !recording.saved && !recording.published);
|
|
CHECK(IEC60870_FaultRecorder_Get_Info(&recording.recorder, &info));
|
|
CHECK(info.state == IEC60870_FAULT_RECORDER_READY && info.frame_count == 961U && info.trigger_index == 320U);
|
|
CHECK(recording.dat_size == 24986U);
|
|
CHECK(strcmp(recording.names[0], "BAY08_0010_20141114_192135_830.cfg") == 0);
|
|
CHECK(strcmp(recording.names[1], "BAY08_0010_20141114_192135_830.dat") == 0);
|
|
CHECK(strcmp(recording.paths[0], "COMTRADE/BAY08_0010_20141114_192135_830.cfg") == 0);
|
|
CHECK(strcmp(recording.paths[1], "COMTRADE/BAY08_0010_20141114_192135_830.dat") == 0);
|
|
CHECK(strlen(recording.cfg) == recording.cfg_size);
|
|
CHECK(strncmp(recording.cfg, "SIMULATION,IEC104_DEMO,1999\r\n24,8A,16D\r\n",
|
|
strlen("SIMULATION,IEC104_DEMO,1999\r\n24,8A,16D\r\n")) == 0);
|
|
CHECK(strstr(recording.cfg, "\r\n1,Ua,A,,V,0.01,0,") != NULL);
|
|
CHECK(strstr(recording.cfg, "\r\n5,Ia,A,,A,0.001,0,") != NULL);
|
|
CHECK(strstr(recording.cfg, "\r\n50\r\n1\r\n4000,961\r\n14/11/2014,19:21:35.750000\r\n14/11/2014,19:21:35.830000\r\nBINARY\r\n1\r\n") != NULL);
|
|
for (index = 0U; index < recording.cfg_size; ++index) {
|
|
CHECK((unsigned char)recording.cfg[index] < 128U && recording.cfg[index] != '\0');
|
|
if (recording.cfg[index] == '\n') { CHECK(index > 0U && recording.cfg[index - 1U] == '\r'); }
|
|
if (recording.cfg[index] == '\r') { CHECK(index + 1U < recording.cfg_size && recording.cfg[index + 1U] == '\n'); }
|
|
}
|
|
CHECK(Example_Recording_Read(&recording, 1U, 0U, before, sizeof(before)));
|
|
CHECK(Example_Recording_Read(&recording, 1U, 320U * 26U, trigger, sizeof(trigger)));
|
|
CHECK(Example_Recording_Read(&recording, 1U, 360U * 26U, breaker, sizeof(breaker)));
|
|
CHECK(Example_Recording_Read(&recording, 1U, 960U * 26U, last, sizeof(last)));
|
|
CHECK(Read32(before) == 1U && Read32(before + 4U) == 0U);
|
|
CHECK(Read32(trigger) == 321U && Read32(trigger + 4U) == 80000U);
|
|
CHECK(Read32(last) == 961U && Read32(last + 4U) == 240000U);
|
|
CHECK(Read16(before + 8U) == 5225 && Read16(trigger + 8U) == 2612);
|
|
CHECK(Read16(before + 16U) == 522 && Read16(trigger + 16U) == 2612);
|
|
CHECK(Read16(before + 14U) == 0 && Read16(trigger + 14U) == 261);
|
|
CHECK(Read16(before + 22U) == 0 && Read16(trigger + 22U) == 522);
|
|
CHECK(before[24] == 0U && trigger[24] == 2U && breaker[24] == 3U && last[24] == 3U);
|
|
CHECK(before[25] == 0U && trigger[25] == 0U && breaker[25] == 0U && last[25] == 0U);
|
|
CHECK(Example_Recording_Read(&recording, 1U, recording.dat_size - sizeof(tail), tail, sizeof(tail)));
|
|
CHECK(memcmp(tail, last + sizeof(last) - sizeof(tail), sizeof(tail)) == 0);
|
|
++groups;
|
|
}
|
|
|
|
/**
|
|
* 功能:验证无效对象、文件索引、范围及NULL缓冲读取失败且输出不变。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:文件末尾零长度读取允许成功。
|
|
*/
|
|
static void Test_Read_Errors(void)
|
|
{
|
|
uint8_t bytes[8], prior[8];
|
|
memset(bytes, 0xA5, sizeof(bytes)); memcpy(prior, bytes, sizeof(bytes));
|
|
CHECK(!Example_Recording_Read(NULL, 0U, 0U, bytes, 1U));
|
|
CHECK(!Example_Recording_Read(&second, 0U, 0U, bytes, 1U));
|
|
CHECK(!Example_Recording_Read(&recording, 2U, 0U, bytes, 1U));
|
|
CHECK(!Example_Recording_Read(&recording, 0U, (uint32_t)recording.cfg_size, bytes, 1U));
|
|
CHECK(!Example_Recording_Read(&recording, 1U, recording.dat_size, bytes, 1U));
|
|
CHECK(!Example_Recording_Read(&recording, 1U, UINT32_MAX, bytes, 1U));
|
|
CHECK(!Example_Recording_Read(&recording, 1U, 0U, bytes, SIZE_MAX));
|
|
CHECK(!Example_Recording_Read(&recording, 0U, 0U, NULL, 1U));
|
|
CHECK(Example_Recording_Read(&recording, 0U, (uint32_t)recording.cfg_size, NULL, 0U));
|
|
CHECK(Example_Recording_Read(&recording, 1U, recording.dat_size, NULL, 0U));
|
|
CHECK(memcmp(bytes, prior, sizeof(bytes)) == 0);
|
|
++groups;
|
|
}
|
|
|
|
/**
|
|
* 功能:验证以下场景与约束:将实例内部trigger_time直接作为Demo输入,验证初始化清理不会先覆盖来源时间,文件名仍正确。
|
|
* 输入参数:无。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Test_Aliased_Time(void)
|
|
{
|
|
IEC60870_CP56Time2a_t time = Trigger_Time();
|
|
second.trigger_time = time;
|
|
CHECK(Example_Recording_Demo(&second, &second.trigger_time, 9U, 11U));
|
|
CHECK(memcmp(&time, &second.trigger_time, sizeof(time)) == 0);
|
|
CHECK(strcmp(second.names[0], "BAY09_0011_20141114_192135_830.cfg") == 0);
|
|
CHECK(second.complete && !second.saved);
|
|
++groups;
|
|
}
|
|
|
|
/**
|
|
* 功能:验证CFG/DAT成功成对落盘、临时文件消失且内容一致。
|
|
* 输入参数:parent——测试父目录(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:重复保存或同名新录波均不得覆盖已保存文件。
|
|
*/
|
|
static void Test_Save(const char *parent)
|
|
{
|
|
char paths[2][1024], temporary[2][1024];
|
|
unsigned index;
|
|
for (index = 0U; index < 2U; ++index) {
|
|
int length;
|
|
Path(paths[index], sizeof(paths[index]), parent, recording.paths[index]);
|
|
length = snprintf(temporary[index], sizeof(temporary[index]), "%s.tmp", paths[index]);
|
|
CHECK(length > 0 && (size_t)length < sizeof(temporary[index]));
|
|
Absent(paths[index]); Absent(temporary[index]);
|
|
}
|
|
CHECK(!Example_Recording_Save(&recording, NULL));
|
|
CHECK(!Example_Recording_Save(&recording, ""));
|
|
CHECK(!recording.saved);
|
|
CHECK(Example_Recording_Save(&recording, parent));
|
|
Own(paths[0]); Own(paths[1]);
|
|
CHECK(recording.saved && !recording.published);
|
|
for (index = 0U; index < 2U; ++index) { Check_Disk(&recording, index, paths[index]); Absent(temporary[index]); }
|
|
CHECK(!Example_Recording_Save(&recording, parent) && recording.saved);
|
|
{
|
|
IEC60870_CP56Time2a_t time = Trigger_Time();
|
|
CHECK(Example_Recording_Demo(&second, &time, 8U, 10U));
|
|
second.cfg[0] = 'X'; /* Would visibly damage CFG if the duplicate were allowed. */
|
|
CHECK(!Example_Recording_Save(&second, parent) && !second.saved);
|
|
}
|
|
for (index = 0U; index < 2U; ++index) { Check_Disk(&recording, index, paths[index]); Absent(temporary[index]); }
|
|
++groups;
|
|
}
|
|
|
|
/**
|
|
* 功能:验证以下场景与约束:用已存在的DAT临时文件和普通文件充当父目录阻断保存,验证哨兵保留、saved为false且没有半份最终文件。
|
|
* 输入参数:parent——测试父目录(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:无。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
static void Test_Save_Failures(const char *parent)
|
|
{
|
|
char paths[2][1024], temporary[1024], blocked_parent[1024];
|
|
IEC60870_CP56Time2a_t time = Trigger_Time();
|
|
int length;
|
|
unsigned index;
|
|
CHECK(Example_Recording_Demo(&second, &time, 9U, 11U));
|
|
for (index = 0U; index < 2U; ++index) {
|
|
Path(paths[index], sizeof(paths[index]), parent, second.paths[index]); Absent(paths[index]);
|
|
}
|
|
length = snprintf(temporary, sizeof(temporary), "%s.tmp", paths[1]);
|
|
CHECK(length > 0 && (size_t)length < sizeof(temporary));
|
|
Create_Sentinel(temporary);
|
|
CHECK(!Example_Recording_Save(&second, parent) && !second.saved);
|
|
Check_Sentinel(temporary);
|
|
Absent(paths[0]); Absent(paths[1]);
|
|
Path(blocked_parent, sizeof(blocked_parent), parent, "recording-adapter-parent-file");
|
|
Create_Sentinel(blocked_parent);
|
|
CHECK(!Example_Recording_Save(&second, blocked_parent) && !second.saved);
|
|
Check_Sentinel(blocked_parent);
|
|
Absent(paths[0]); Absent(paths[1]);
|
|
++groups;
|
|
}
|
|
|
|
/**
|
|
* 功能:在调用方提供的已有本地目录运行五组录波适配用例,最后仅清理本进程创建的文件并核对归属表清空。
|
|
* 输入参数:argc——命令行参数数量;argv——命令行参数数组。
|
|
* 输出参数:无。
|
|
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
|
|
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
|
*/
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct stat parent;
|
|
size_t index;
|
|
if (argc != 2) { fputs("Usage: recording-adapter-test <existing-local-output-directory>\n", stderr); return 2; }
|
|
CHECK(stat(argv[1], &parent) == 0 && (parent.st_mode & S_IFMT) == S_IFDIR);
|
|
Test_Demo(); Test_Read_Errors(); Test_Aliased_Time(); Test_Save(argv[1]); Test_Save_Failures(argv[1]);
|
|
Cleanup();
|
|
for (index = 0U; index < sizeof(owned_files) / sizeof(owned_files[0]); ++index) { CHECK(!owned_files[index].owned); }
|
|
printf("Example recording: %u groups passed\n", groups);
|
|
return 0;
|
|
}
|