ovni/test/unit/stream.c

74 lines
1.3 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2023-01-20 15:47:41 +01:00
#include <unistd.h>
#include <sys/stat.h>
#include "common.h"
#include "emu/stream.h"
#include "ovni.h"
#include "unittest.h"
2023-01-20 15:47:41 +01:00
static void
test_ok(void)
2023-01-20 15:47:41 +01:00
{
const char *fname = "stream-ok.obs";
2023-01-20 15:47:41 +01:00
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;
OK(stream_load(&stream, ".", fname));
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(void)
2023-01-20 15:47:41 +01:00
{
const char *fname = "stream-bad.obs";
2023-01-20 15:47:41 +01:00
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;
ERR(stream_load(&stream, ".", fname));
err("OK");
2023-01-20 15:47:41 +01:00
}
int main(void)
{
test_ok();
test_bad();
2023-01-20 15:47:41 +01:00
return 0;
2023-01-20 15:47:41 +01:00
}