ovni/test/unit/stream.c

74 lines
1.3 KiB
C
Raw Normal View History

2023-01-20 15:47:41 +01:00
#define _GNU_SOURCE
#include "emu/emu.h"
#include "common.h"
#include <stdio.h>
#include <unistd.h>
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 stream stream;
2023-01-20 15:47:41 +01:00
const char *relpath = &fname[5];
if (stream_load(&stream, "/tmp", relpath) != 0)
die("stream_load failed");
2023-01-20 17:56:36 +01:00
if (stream.active)
die("stream is active\n");
2023-01-20 15:47:41 +01:00
}
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 stream stream;
2023-01-20 15:47:41 +01:00
const char *relpath = &fname[5];
if (stream_load(&stream, "/tmp", relpath) == 0)
die("stream_load didn't fail");
2023-01-20 15:47:41 +01:00
}
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);
}