2014-03-02 14:44:24 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/file.h>
|
2016-03-30 10:11:02 -04:00
|
|
|
#include <core/version.h>
|
2015-10-09 13:55:34 -04:00
|
|
|
#include <tests/test.h>
|
2015-10-08 11:16:38 -04:00
|
|
|
#include <stdlib.h>
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp)
|
2014-04-26 13:31:22 -04:00
|
|
|
{
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_null(file->f_file);
|
|
|
|
g_assert_cmpuint(file_version(file), ==, 0);
|
|
|
|
g_assert_cmpuint(file->f_mode, ==, OPEN_READ);
|
|
|
|
g_assert_cmpstr_free(file_path(file), ==, fpath);
|
|
|
|
g_assert_cmpstr_free(file_write_path(file), ==, ftmp);
|
2015-09-10 10:33:24 -04:00
|
|
|
}
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
static void test_invalid_file(gconstpointer path)
|
2015-09-10 10:33:24 -04:00
|
|
|
{
|
2016-03-06 10:23:11 -05:00
|
|
|
struct file file;
|
2015-09-10 09:12:48 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
file_init(&file, (gchar *)path, 0, 0);
|
|
|
|
test_verify_constructor(&file, "", "");
|
2015-08-24 08:31:00 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_open(&file, OPEN_READ));
|
|
|
|
g_assert_null(file.f_file);
|
|
|
|
g_assert_false(file_open(&file, OPEN_WRITE));
|
|
|
|
g_assert_null(file.f_file);
|
|
|
|
g_assert_cmpuint(file.f_mode, ==, OPEN_READ);
|
2015-10-09 09:55:23 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_exists(&file));
|
2015-10-09 09:55:23 -04:00
|
|
|
}
|
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
static void __test_file_subprocess()
|
2014-04-26 13:31:22 -04:00
|
|
|
{
|
2016-03-30 11:25:14 -04:00
|
|
|
struct file file = FILE_INIT("file.txt", 0, 0);
|
2016-03-30 10:11:02 -04:00
|
|
|
gchar *basepath, *filepath, *realpath;
|
2014-04-26 13:31:22 -04:00
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
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);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_exists(&file));
|
2014-03-02 14:44:24 -05:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_open(&file, OPEN_READ));
|
|
|
|
g_assert_true(file_open(&file, OPEN_WRITE));
|
|
|
|
g_assert_nonnull(file.f_file);
|
|
|
|
g_assert_cmpuint(file.f_mode, ==, OPEN_WRITE);
|
|
|
|
g_assert_false(file_open(&file, OPEN_WRITE));
|
2015-09-10 10:33:24 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_exists(&file));
|
2015-09-10 10:33:24 -04:00
|
|
|
file_close(&file);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_null(file.f_file);
|
|
|
|
g_assert_cmpuint(file.f_mode, ==, OPEN_WRITE);
|
|
|
|
g_assert_true(file_exists(&file));
|
2015-09-10 10:33:24 -04:00
|
|
|
|
2015-10-09 10:07:08 -04:00
|
|
|
g_chmod(filepath, 0444);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_open(&file, OPEN_WRITE));
|
2015-10-09 10:07:08 -04:00
|
|
|
g_chmod(filepath, 0200);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_open(&file, OPEN_READ));
|
2015-10-09 10:07:08 -04:00
|
|
|
g_chmod(filepath, 0644);
|
2015-09-10 10:33:24 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(file_open(&file, OPEN_READ));
|
|
|
|
g_assert_nonnull(file.f_file);
|
|
|
|
g_assert_cmpuint(file.f_mode, ==, OPEN_READ);
|
|
|
|
g_assert_false(file_open(&file, OPEN_READ));
|
2016-04-01 08:15:07 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(file_remove(&file));
|
|
|
|
g_assert_true(file_exists(&file));
|
2015-09-10 10:33:24 -04:00
|
|
|
file_close(&file);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(file_remove(&file));
|
|
|
|
g_assert_false(file_exists(&file));
|
2015-09-10 10:33:24 -04:00
|
|
|
|
|
|
|
g_free(filepath);
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
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*");
|
|
|
|
}
|
|
|
|
|
2014-04-26 14:06:20 -04:00
|
|
|
static void test_io()
|
2014-03-02 14:44:24 -05:00
|
|
|
{
|
2016-03-30 11:25:14 -04:00
|
|
|
struct file fout = FILE_INIT("file.txt", 1, 1);
|
2016-08-28 10:05:51 -04:00
|
|
|
struct file fin = FILE_INIT("file.txt", 2, 1);
|
2015-10-08 11:16:38 -04:00
|
|
|
char *res = NULL;
|
|
|
|
unsigned int i;
|
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(file_open(&fout, OPEN_WRITE));
|
2015-10-08 11:16:38 -04:00
|
|
|
file_writef(&fout, "1 ABCDE\n");
|
|
|
|
file_writef(&fout, "2 FGHIJ KLMNO\n");
|
|
|
|
file_writef(&fout, "3 \n");
|
|
|
|
file_writef(&fout, "4 5 PQRST\n");
|
|
|
|
file_close(&fout);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(file_exists(&fout));
|
2015-10-08 11:16:38 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(file_open(&fin, OPEN_READ));
|
|
|
|
g_assert_cmpuint(file_version(&fin), ==, 1);
|
2015-10-08 11:16:38 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_cmpuint(file_readf(&fin, "%u %ms\n", &i, &res), ==, 2);
|
|
|
|
g_assert_cmpuint(i, ==, 1);
|
|
|
|
g_assert_cmpstr_free(res, ==, "ABCDE");
|
2015-10-08 11:16:38 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_cmpuint(file_readf(&fin, "%u %m[^\n]\n", &i, &res), ==, 2);
|
|
|
|
g_assert_cmpuint(i, ==, 2);
|
|
|
|
g_assert_cmpstr_free(res, ==, "FGHIJ KLMNO");
|
2015-10-08 11:16:38 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_cmpuint(file_readf(&fin, "%u", &i), ==, 1);
|
2015-10-09 13:09:47 -04:00
|
|
|
res = file_readl(&fin);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_cmpuint(i, ==, 3);
|
|
|
|
g_assert_cmpstr_free(res, ==, "");
|
2015-10-08 11:16:38 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_cmpuint(file_readf(&fin, "%u %m[^\n]", &i, &res), ==, 2);
|
|
|
|
g_assert_cmpuint(i, ==, 4);
|
|
|
|
g_assert_cmpstr_free(res, ==, "5 PQRST");
|
2015-10-08 11:16:38 -04:00
|
|
|
|
|
|
|
file_close(&fin);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_cmpuint(file_version(&fin), ==, 2);
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
static void __test_versioning_subprocess(unsigned int out, unsigned int in)
|
2016-03-30 11:25:14 -04:00
|
|
|
{
|
2016-03-06 10:23:11 -05:00
|
|
|
struct file fout = FILE_INIT("file.txt", out, out);
|
|
|
|
struct file fin = FILE_INIT("file.txt", in, in);
|
2016-03-30 11:25:14 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(file_open(&fout, OPEN_WRITE));
|
2016-03-30 11:25:14 -04:00
|
|
|
file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
|
|
|
|
file_close(&fout);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(file_exists(&fout));
|
|
|
|
|
|
|
|
g_assert_false(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.*");
|
|
|
|
}
|
2016-03-30 11:25:14 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
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.*");
|
|
|
|
}
|
2016-03-30 11:25:14 -04:00
|
|
|
|
2016-03-31 08:08:33 -04:00
|
|
|
static void test_cache()
|
|
|
|
{
|
|
|
|
struct cache_file file = CACHE_FILE_INIT("dir", "file.txt");
|
2016-07-12 09:08:45 -04:00
|
|
|
struct cache_file copy = CACHE_FILE_INIT("dir", "copy.txt");
|
2016-03-31 09:25:49 -04:00
|
|
|
gchar *basepath, *filepath, *writepath;
|
2016-03-31 08:52:59 -04:00
|
|
|
|
2016-03-31 09:25:49 -04:00
|
|
|
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
|
|
|
|
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
|
|
|
|
writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", NULL);
|
2016-03-31 08:08:33 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_null(file.cf_file);
|
|
|
|
g_assert_cmpstr(file.cf_name, ==, "file.txt");
|
|
|
|
g_assert_cmpstr(file.cf_subdir, ==, "dir");
|
2016-03-31 08:52:59 -04:00
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_cmpstr_free(cache_file_path(&file), ==, filepath);
|
|
|
|
g_assert_cmpstr_free(cache_file_write_path(&file), ==, writepath);
|
2016-03-31 09:31:55 -04:00
|
|
|
|
2016-07-12 09:08:45 -04:00
|
|
|
/* Test writing data to a cache file. */
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(cache_file_exists(&file));
|
|
|
|
g_assert_false(cache_file_open(&file, OPEN_READ));
|
|
|
|
g_assert_true(cache_file_open(&file, OPEN_WRITE));
|
|
|
|
g_assert_nonnull(file.cf_file);
|
|
|
|
g_assert_false(cache_file_open(&file, OPEN_WRITE));
|
|
|
|
|
|
|
|
g_assert_false(cache_file_exists(&file));
|
|
|
|
g_assert_cmpuint(cache_file_write(&file, "abcde", 5), ==, 5);
|
2016-03-31 10:15:17 -04:00
|
|
|
cache_file_close(&file);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_null(file.cf_file);
|
|
|
|
g_assert_true(cache_file_exists(&file));
|
2016-07-12 09:08:45 -04:00
|
|
|
|
|
|
|
/* Test importing a file into the cache. */
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_false(cache_file_exists(©));
|
|
|
|
g_assert_false(cache_file_import(©, filepath));
|
|
|
|
g_assert_false(cache_file_exists(©));
|
|
|
|
g_assert_true(cache_file_open(©, OPEN_WRITE));
|
|
|
|
g_assert_false(cache_file_import(©, NULL));
|
|
|
|
g_assert_true(cache_file_import(©, filepath));
|
|
|
|
g_assert_false(cache_file_exists(©));
|
2016-07-12 09:08:45 -04:00
|
|
|
cache_file_close(©);
|
2016-03-06 10:23:11 -05:00
|
|
|
g_assert_true(cache_file_exists(©));
|
2016-03-31 08:08:33 -04:00
|
|
|
}
|
|
|
|
|
2016-03-06 10:23:11 -05:00
|
|
|
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();
|
|
|
|
}
|