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>
|
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-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
|
|
|
|
|
|
|
static void
|
|
|
|
test_ok(char *fname)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2023-02-01 12:33:19 +01:00
|
|
|
struct stream stream;
|
2023-01-20 15:47:41 +01:00
|
|
|
const char *relpath = &fname[5];
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(stream_load(&stream, "/tmp", relpath));
|
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
|
|
|
|
test_bad(char *fname)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2023-02-01 12:33:19 +01:00
|
|
|
struct stream stream;
|
2023-01-20 15:47:41 +01:00
|
|
|
const char *relpath = &fname[5];
|
2023-03-21 16:09:01 +01:00
|
|
|
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)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("mkstemp failed:");
|
2023-01-20 15:47:41 +01:00
|
|
|
|
|
|
|
test_ok(fname);
|
|
|
|
test_bad(fname);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
return 0;
|
2023-01-20 15:47:41 +01:00
|
|
|
}
|