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>
|
2023-03-22 16:01:55 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-01-20 15:47:41 +01:00
|
|
|
#include <unistd.h>
|
2023-07-25 16:26:31 +02:00
|
|
|
#include <sys/stat.h>
|
2023-03-22 16:01:55 +01:00
|
|
|
#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
|
2023-07-25 16:26:31 +02:00
|
|
|
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)
|
2023-03-21 16:09:01 +01:00
|
|
|
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)
|
2023-03-21 16:09:01 +01:00
|
|
|
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");
|
|
|
|
|
2023-02-01 12:33:19 +01:00
|
|
|
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)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("stream is active");
|
|
|
|
|
|
|
|
err("OK");
|
2023-01-20 15:47:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-07-25 16:26:31 +02:00
|
|
|
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)
|
2023-03-21 16:09:01 +01:00
|
|
|
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)
|
2023-03-21 16:09:01 +01:00
|
|
|
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");
|
|
|
|
|
2023-02-01 12:33:19 +01:00
|
|
|
struct stream stream;
|
2024-09-17 08:27:51 +02:00
|
|
|
ERR(stream_load(&stream, ".", "bad"));
|
2023-03-21 16:09:01 +01:00
|
|
|
|
|
|
|
err("OK");
|
2023-01-20 15:47:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2023-07-25 16:26:31 +02:00
|
|
|
test_ok();
|
|
|
|
test_bad();
|
2023-01-20 15:47:41 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
return 0;
|
2023-01-20 15:47:41 +01:00
|
|
|
}
|