ovni/test/unit/stream.c

97 lines
1.7 KiB
C
Raw Normal View History

2024-09-17 08:27:51 +02:00
/* Copyright (c) 2021-2024 Barcelona Supercomputing Center (BSC)
2023-02-27 13:40:20 +01:00
* 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
2024-09-17 08:27:51 +02:00
static void
write_dummy_json(const char *path)
{
const char *json = "{ \"version\" : 3 }";
FILE *f = fopen(path, "w");
if (f == NULL)
die("fopen json failed:");
if (fwrite(json, strlen(json), 1, f) != 1)
die("fwrite json failed:");
fclose(f);
}
2023-01-20 15:47:41 +01:00
static void
test_ok(void)
2023-01-20 15:47:41 +01:00
{
2024-09-17 08:27:51 +02:00
OK(mkdir("ok", 0755));
const char *fname = "ok/stream.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);
2024-09-17 08:27:51 +02:00
write_dummy_json("ok/stream.json");
struct stream stream;
2024-09-17 08:27:51 +02:00
OK(stream_load(&stream, ".", "ok"));
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
{
2024-09-17 08:27:51 +02:00
OK(mkdir("bad", 0755));
const char *fname = "bad/stream.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);
2024-09-17 08:27:51 +02:00
write_dummy_json("bad/stream.json");
struct stream stream;
2024-09-17 08:27:51 +02:00
ERR(stream_load(&stream, ".", "bad"));
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
}