ocarina/tests/core/file.c

246 lines
7.3 KiB
C

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/file.h>
#include <core/version.h>
#include <tests/test.h>
#include <stdlib.h>
static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp)
{
g_assert_null(file->f_file);
g_assert_cmpuint(data_file_version(file), ==, OCARINA_MINOR_VERSION);
g_assert_cmpuint(file->f_mode, ==, CLOSED);
g_assert_cmpstr_free(file_path(file), ==, fpath);
g_assert_cmpstr_free(file_write_path(file), ==, ftmp);
}
static void test_invalid_file(gconstpointer path)
{
struct file file;
file_init_data(&file, "", (gchar *)path, 0);
test_verify_constructor(&file, "", "");
g_assert_false(data_file_open(&file, OPEN_READ));
g_assert_null(file.f_file);
g_assert_false(data_file_open(&file, OPEN_WRITE));
g_assert_null(file.f_file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_false(file_exists(&file));
}
static void __test_file_subprocess()
{
struct file file = FILE_INIT_DATA("", "file.txt", 0);
gchar *basepath, *filepath, *realpath;
basepath = g_strjoin("/", g_get_user_data_dir(), OCARINA_NAME, NULL);
filepath = g_strjoin("/", basepath, "file.txt", NULL);
realpath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
test_verify_constructor(&file, filepath, realpath);
g_assert_false(file_exists(&file));
g_assert_false(data_file_open(&file, OPEN_READ));
g_assert_false(data_file_open(&file, CLOSED));
g_assert_true(data_file_open(&file, OPEN_WRITE));
g_assert_nonnull(file.f_file);
g_assert_cmpuint(file.f_mode, ==, OPEN_WRITE);
g_assert_false(data_file_open(&file, OPEN_WRITE));
g_assert_false(file_exists(&file));
file_close(&file);
g_assert_null(file.f_file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_true(file_exists(&file));
g_chmod(filepath, 0444);
g_assert_false(data_file_open(&file, OPEN_WRITE));
g_chmod(filepath, 0200);
g_assert_false(data_file_open(&file, OPEN_READ));
g_chmod(filepath, 0644);
g_assert_false(data_file_open(&file, CLOSED));
g_assert_true(data_file_open(&file, OPEN_READ));
g_assert_nonnull(file.f_file);
g_assert_cmpuint(file.f_mode, ==, OPEN_READ);
g_assert_false(data_file_open(&file, OPEN_READ));
g_assert_false(file_remove(&file));
g_assert_true(file_exists(&file));
file_close(&file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_true(file_remove(&file));
g_assert_false(file_exists(&file));
g_free(filepath);
}
static void test_file()
{
if (g_test_subprocess()) {
__test_file_subprocess();
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*file.txt: Permission denied*");
}
static void test_io()
{
struct file fout = FILE_INIT_DATA("", "file.txt", 0);
struct file fin = FILE_INIT_DATA("", "file.txt", 1);
char *res = NULL;
unsigned int i;
fout.f_version = 1;
g_assert_true(data_file_open(&fout, OPEN_WRITE));
data_file_writef(&fout, "1 ABCDE\n");
data_file_writef(&fout, "2 FGHIJ KLMNO\n");
data_file_writef(&fout, "3 \n");
data_file_writef(&fout, "4 5 PQRST\n");
file_close(&fout);
g_assert_true(file_exists(&fout));
g_assert_true(data_file_open(&fin, OPEN_READ));
g_assert_cmpuint(data_file_version(&fin), ==, 1);
g_assert_cmpuint(data_file_readf(&fin, "%u %ms\n", &i, &res), ==, 2);
g_assert_cmpuint(i, ==, 1);
g_assert_cmpstr_free(res, ==, "ABCDE");
g_assert_cmpuint(data_file_readf(&fin, "%u %m[^\n]\n", &i, &res), ==, 2);
g_assert_cmpuint(i, ==, 2);
g_assert_cmpstr_free(res, ==, "FGHIJ KLMNO");
g_assert_cmpuint(data_file_readf(&fin, "%u", &i), ==, 1);
res = data_file_readl(&fin);
g_assert_cmpuint(i, ==, 3);
g_assert_cmpstr_free(res, ==, "");
g_assert_cmpuint(data_file_readf(&fin, "%u %m[^\n]", &i, &res), ==, 2);
g_assert_cmpuint(i, ==, 4);
g_assert_cmpstr_free(res, ==, "5 PQRST");
file_close(&fin);
g_assert_cmpuint(data_file_version(&fin), ==, OCARINA_MINOR_VERSION);
}
static void __test_versioning_subprocess(unsigned int out, unsigned int in)
{
struct file fout = FILE_INIT_DATA("", "file.txt", out);
struct file fin = FILE_INIT_DATA("", "file.txt", in);
fout.f_version = out;
fin.f_version = in;
g_assert_true(data_file_open(&fout, OPEN_WRITE));
data_file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
file_close(&fout);
g_assert_true(file_exists(&fout));
g_assert_false(data_file_open(&fin, OPEN_READ));
g_assert_null(fin.f_file);
}
static void test_versioning_old()
{
if (g_test_subprocess()) {
__test_versioning_subprocess(0, 1);
return;
};
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*file.txt: File too old to be upgraded.*");
}
static void test_versioning_new()
{
if (g_test_subprocess()) {
__test_versioning_subprocess(1, 0);
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*file.txt: File too new to be opened.*");
}
static void test_cache()
{
gchar *basepath, *filepath, *writepath;
struct file file, copy;
file_init_cache(&file, "dir", "file.txt");
file_init_cache(&copy, "dir", "copy.txt");
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, "dir", NULL);
filepath = g_strjoin("/", basepath, "file.txt", NULL);
writepath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
g_assert_null(file.f_file);
g_assert_cmpstr(file.f_name, ==, "file.txt");
g_assert_cmpstr(file.f_subdir, ==, "dir");
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_cmpstr_free(file_path(&file), ==, filepath);
g_assert_cmpstr_free(file_write_path(&file), ==, writepath);
/* Test writing data to a cache file. */
g_assert_false(file_exists(&file));
g_assert_false(cache_file_open(&file, OPEN_READ));
g_assert_false(cache_file_open(&file, CLOSED));
g_assert_true(cache_file_open(&file, OPEN_WRITE));
g_assert_nonnull(file.f_file);
g_assert_cmpuint(file.f_mode, ==, OPEN_WRITE);
g_assert_false(cache_file_open(&file, OPEN_WRITE));
g_assert_false(file_exists(&file));
g_assert_cmpuint(cache_file_write(&file, "abcde", 5), ==, 5);
file_close(&file);
g_assert_null(file.f_file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_true(file_exists(&file));
g_assert_false(cache_file_open(&file, OPEN_READ));
/* Test importing a file into the cache. */
g_assert_false(file_exists(&copy));
g_assert_false(cache_file_import(&copy, filepath));
g_assert_false(file_exists(&copy));
g_assert_true(cache_file_open(&copy, OPEN_WRITE));
g_assert_false(cache_file_import(&copy, NULL));
g_assert_true(cache_file_import(&copy, filepath));
g_assert_false(file_exists(&copy));
file_close(&copy);
g_assert_true(file_exists(&copy));
/* Test removing cache files. */
g_assert_true(file_remove(&copy));
g_assert_false(file_exists(&copy));
g_assert_true(file_exists(&file));
g_assert_true(g_file_test(basepath, G_FILE_TEST_EXISTS));
g_assert_true(file_remove(&file));
g_assert_false(file_exists(&file));
g_assert_false(g_file_test(basepath, G_FILE_TEST_EXISTS));
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_data_func("/Core/File/Path = NULL", NULL, test_invalid_file);
g_test_add_data_func("/Core/File/Path = \"\"", "", test_invalid_file);
g_test_add_func("/Core/File/Path = \"file.txt\"", test_file);
g_test_add_func("/Core/File/IO", test_io);
g_test_add_func("/Core/File/Versioning/Too Old", test_versioning_old);
g_test_add_func("/Core/File/Versioning/Too New", test_versioning_new);
g_test_add_func("/Core/File/Cache", test_cache);
return g_test_run();
}