ovni/test/unit/stream.c

81 lines
1.4 KiB
C
Raw Normal View History

2023-02-27 13:40:20 +01:00
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
2023-01-20 15:47:41 +01:00
#define _GNU_SOURCE
#include "emu/emu.h"
#include "common.h"
#include "unittest.h"
2023-01-20 15:47:41 +01:00
#include <stdio.h>
#include <unistd.h>
static void
test_ok(char *fname)
{
FILE *f = fopen(fname, "w");
if (f == NULL)
die("fopen failed:");
2023-01-20 15:47:41 +01:00
/* 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:");
2023-01-20 15:47:41 +01:00
fclose(f);
struct stream stream;
2023-01-20 15:47:41 +01:00
const char *relpath = &fname[5];
OK(stream_load(&stream, "/tmp", relpath));
2023-01-20 17:56:36 +01:00
if (stream.active)
die("stream is active");
err("OK");
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:");
2023-01-20 15:47:41 +01:00
/* 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:");
2023-01-20 15:47:41 +01:00
fclose(f);
struct stream stream;
2023-01-20 15:47:41 +01:00
const char *relpath = &fname[5];
ERR(stream_load(&stream, "/tmp", relpath));
err("OK");
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:");
2023-01-20 15:47:41 +01:00
test_ok(fname);
test_bad(fname);
close(fd);
return 0;
2023-01-20 15:47:41 +01:00
}