2026-01-08 16:59:37 +01:00
|
|
|
#include <criterion/criterion.h>
|
|
|
|
|
#include <criterion/new/assert.h>
|
|
|
|
|
#include <io_backend/io_backend.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
TestSuite(IO_Backend);
|
|
|
|
|
|
|
|
|
|
// IOB Init
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, init_null)
|
|
|
|
|
{
|
2026-01-13 11:08:36 +01:00
|
|
|
struct iob_context ctx =
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
.mode = IOB_MODE_NULL,
|
|
|
|
|
.args = NULL
|
|
|
|
|
};
|
|
|
|
|
int actual = iob_init(&ctx);
|
2026-01-13 11:08:36 +01:00
|
|
|
int expected = IOB_ERROR_BAD_ARG;
|
|
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, init_stdin)
|
|
|
|
|
{
|
2026-01-13 11:08:36 +01:00
|
|
|
struct iob_context ctx =
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
.mode = IOB_MODE_STDIN,
|
|
|
|
|
.args = NULL
|
|
|
|
|
};
|
|
|
|
|
int actual = iob_init(&ctx);
|
2026-01-13 11:08:36 +01:00
|
|
|
int expected = 0;
|
|
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
|
|
|
|
iob_close();
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WARNING: this one could fail because of iob_close in the previous test
|
|
|
|
|
// Same applies for other tests
|
|
|
|
|
Test(IO_Backend, init_script)
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
char *script_name = "script.tmp";
|
|
|
|
|
struct iob_context ctx = {
|
|
|
|
|
.mode = IOB_MODE_SCRIPT,
|
|
|
|
|
.args = script_name
|
|
|
|
|
};
|
|
|
|
|
// Create file
|
|
|
|
|
FILE *f = fopen(script_name, "w");
|
|
|
|
|
fclose(f);
|
2026-01-08 16:59:37 +01:00
|
|
|
|
2026-01-24 16:54:26 +01:00
|
|
|
int actual = iob_init(&ctx);
|
2026-01-13 11:08:36 +01:00
|
|
|
int expected = 0;
|
|
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
|
|
|
|
iob_close();
|
|
|
|
|
remove(script_name);
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, init_script_not_a_file)
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
char *script_name = "not_a_file.tmp";
|
|
|
|
|
struct iob_context ctx = {
|
|
|
|
|
.mode = IOB_MODE_SCRIPT,
|
|
|
|
|
.args = script_name
|
|
|
|
|
};
|
|
|
|
|
int actual = iob_init(&ctx);
|
2026-01-13 11:08:36 +01:00
|
|
|
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
|
|
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, init_script_null)
|
|
|
|
|
{
|
2026-01-13 11:08:36 +01:00
|
|
|
struct iob_context ctx =
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
.mode = IOB_MODE_SCRIPT,
|
|
|
|
|
.args = NULL
|
|
|
|
|
};
|
|
|
|
|
int actual = iob_init(&ctx);
|
2026-01-13 11:08:36 +01:00
|
|
|
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
|
|
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, init_cmd)
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
char *cmd = "iamacommand --yesido";
|
|
|
|
|
struct iob_context ctx = {
|
|
|
|
|
.mode = IOB_MODE_CMD,
|
|
|
|
|
.args = cmd
|
|
|
|
|
};
|
|
|
|
|
int actual = iob_init(&ctx);
|
2026-01-13 11:08:36 +01:00
|
|
|
int expected = 0;
|
|
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
|
|
|
|
iob_close();
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, init_cmd_null)
|
|
|
|
|
{
|
2026-01-13 11:08:36 +01:00
|
|
|
struct iob_context ctx =
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
.mode = IOB_MODE_CMD,
|
|
|
|
|
.args = NULL
|
|
|
|
|
};
|
|
|
|
|
int actual = iob_init(&ctx);
|
2026-01-13 11:08:36 +01:00
|
|
|
int expected = IOB_ERROR_BAD_ARG;
|
|
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, init_already_init)
|
|
|
|
|
{
|
2026-01-24 16:54:26 +01:00
|
|
|
char *cmd = "iamacommand --yesido";
|
|
|
|
|
struct iob_context ctx = {
|
|
|
|
|
.mode = IOB_MODE_CMD,
|
|
|
|
|
.args = cmd
|
|
|
|
|
};
|
|
|
|
|
iob_init(&ctx);
|
|
|
|
|
int actual = iob_init(&ctx);
|
|
|
|
|
int expected = IOB_ERROR_MODULE_ALREADY_INITIALIZED;
|
2026-01-13 11:08:36 +01:00
|
|
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
|
|
|
|
iob_close();
|
2026-01-08 16:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Test(IO_Backend, close_not_init)
|
|
|
|
|
{
|
|
|
|
|
iob_close(); // Shouldn't do anything
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IOB Stream
|
|
|
|
|
// TODO
|