From 5bd04d8435ea01d103a8ba1a1ff104c27f8efc75 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Fri, 20 Jan 2023 15:47:41 +0100 Subject: [PATCH] Add emu_stream unit test --- test/unit/CMakeLists.txt | 1 + test/unit/emu_stream.c | 70 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 test/unit/emu_stream.c diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 101d2f1..97458a7 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -18,3 +18,4 @@ unit_test(prv.c) unit_test(emu_trace.c) unit_test(emu.c) unit_test(clkoff.c) +unit_test(emu_stream.c) diff --git a/test/unit/emu_stream.c b/test/unit/emu_stream.c new file mode 100644 index 0000000..a1694ed --- /dev/null +++ b/test/unit/emu_stream.c @@ -0,0 +1,70 @@ +#define _GNU_SOURCE + +#include "emu/emu.h" +#include "common.h" + +#include +#include + +static void +test_ok(char *fname) +{ + FILE *f = fopen(fname, "w"); + + if (f == NULL) + die("fopen failed\n"); + + /* Write bogus header */ + struct ovni_stream_header header; + memcpy(&header.magic, OVNI_STREAM_MAGIC, 4); + header.version = OVNI_STREAM_VERSION; + + if (fwrite(&header, sizeof(header), 1, f) != 1) + die("fwrite failed\n"); + + fclose(f); + + struct emu_stream stream; + const char *relpath = &fname[5]; + if (emu_stream_load(&stream, "/tmp", relpath) != 0) + die("emu_stream_load failed"); +} + +static void +test_bad(char *fname) +{ + FILE *f = fopen(fname, "w"); + + if (f == NULL) + die("fopen failed\n"); + + /* Write bogus header */ + struct ovni_stream_header header; + memcpy(&header.magic, OVNI_STREAM_MAGIC, 4); + header.version = 1234; /* Wrong version */ + + if (fwrite(&header, sizeof(header), 1, f) != 1) + die("fwrite failed\n"); + + fclose(f); + + struct emu_stream stream; + const char *relpath = &fname[5]; + if (emu_stream_load(&stream, "/tmp", relpath) == 0) + die("emu_stream_load didn't fail"); +} + +int main(void) +{ + /* Create temporary trace file */ + char fname[] = "/tmp/ovni.stream.XXXXXX"; + int fd = mkstemp(fname); + if (fd < 0) + die("mkstemp failed\n"); + + test_ok(fname); + test_bad(fname); + + close(fd); + +}