2011-01-16 09:55:41 -08:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2008 Cedric Bail
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-08-13 01:31:41 -07:00
|
|
|
|
2011-01-16 09:55:41 -08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2013-03-27 06:40:28 -07:00
|
|
|
#include <unistd.h>
|
2013-08-13 01:31:41 -07:00
|
|
|
#include <fcntl.h>
|
2011-01-16 09:55:41 -08:00
|
|
|
|
2018-01-29 05:51:07 -08:00
|
|
|
#ifdef _WIN32
|
2019-06-20 04:50:35 -07:00
|
|
|
# include <evil_private.h> /* mkdir */
|
2018-01-29 05:51:07 -08:00
|
|
|
#endif
|
|
|
|
|
2016-02-04 05:44:58 -08:00
|
|
|
#include <Eina.h>
|
2011-01-16 09:55:41 -08:00
|
|
|
#include "eina_safety_checks.h"
|
2015-05-18 01:52:54 -07:00
|
|
|
#include "eina_file_common.h"
|
2011-01-16 09:55:41 -08:00
|
|
|
|
2016-02-04 05:44:58 -08:00
|
|
|
#include "eina_suite.h"
|
|
|
|
|
2015-08-10 03:49:41 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
# define PATH_SEP_C '\\'
|
|
|
|
#else
|
|
|
|
# define PATH_SEP_C '/'
|
|
|
|
#endif
|
|
|
|
|
2015-12-03 02:42:08 -08:00
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2013-03-27 06:40:28 -07:00
|
|
|
static int default_dir_rights = 0777;
|
2013-08-13 01:31:41 -07:00
|
|
|
const int file_min_offset = 1;
|
2013-03-27 06:40:28 -07:00
|
|
|
|
2012-01-20 20:44:17 -08:00
|
|
|
#ifdef EINA_SAFETY_CHECKS
|
|
|
|
struct log_ctx {
|
|
|
|
const char *msg;
|
|
|
|
const char *fnc;
|
|
|
|
Eina_Bool did;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* tests should not output on success, just uncomment this for debugging */
|
|
|
|
//#define SHOW_LOG 1
|
|
|
|
|
|
|
|
static void
|
2012-10-05 13:09:47 -07:00
|
|
|
_eina_test_safety_print_cb(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args EINA_UNUSED)
|
2012-01-20 20:44:17 -08:00
|
|
|
{
|
|
|
|
struct log_ctx *ctx = data;
|
|
|
|
va_list cp_args;
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
va_copy(cp_args, args);
|
|
|
|
str = va_arg(cp_args, const char *);
|
|
|
|
va_end(cp_args);
|
|
|
|
|
|
|
|
ck_assert_int_eq(level, EINA_LOG_LEVEL_ERR);
|
|
|
|
ck_assert_str_eq(fmt, "%s");
|
|
|
|
ck_assert_str_eq(ctx->msg, str);
|
|
|
|
ck_assert_str_eq(ctx->fnc, fnc);
|
|
|
|
ctx->did = EINA_TRUE;
|
|
|
|
|
|
|
|
#ifdef SHOW_LOG
|
|
|
|
eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
|
|
|
|
#else
|
|
|
|
(void)d;
|
|
|
|
(void)file;
|
|
|
|
(void)line;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_file_split_simple)
|
2011-01-16 09:55:41 -08:00
|
|
|
{
|
|
|
|
Eina_Array *ea;
|
|
|
|
|
|
|
|
#ifdef EINA_SAFETY_CHECKS
|
2012-01-20 20:44:17 -08:00
|
|
|
#ifdef SHOW_LOG
|
2011-01-16 09:55:41 -08:00
|
|
|
fprintf(stderr, "you should have a safety check failure below:\n");
|
2012-01-20 20:44:17 -08:00
|
|
|
#endif
|
|
|
|
struct log_ctx ctx;
|
|
|
|
|
|
|
|
#define TEST_MAGIC_SAFETY(fn, _msg) \
|
|
|
|
ctx.msg = _msg; \
|
|
|
|
ctx.fnc = fn; \
|
|
|
|
ctx.did = EINA_FALSE
|
|
|
|
|
|
|
|
eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
|
|
|
|
|
|
|
|
TEST_MAGIC_SAFETY("eina_file_split", "safety check failed: path == NULL");
|
2011-01-16 09:55:41 -08:00
|
|
|
ea = eina_file_split(NULL);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(ea);
|
|
|
|
ck_assert(ctx.did);
|
2012-01-20 20:44:17 -08:00
|
|
|
|
|
|
|
eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
|
|
|
|
#undef TEST_MAGIC_SAFETY
|
2011-01-16 09:55:41 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2015-04-28 22:52:31 -07:00
|
|
|
ea = eina_file_split(strdup("\\this/is\\a/small/test"));
|
2011-01-16 09:55:41 -08:00
|
|
|
#else
|
|
|
|
ea = eina_file_split(strdup("/this/is/a/small/test"));
|
|
|
|
#endif
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!ea);
|
|
|
|
ck_assert(eina_array_count(ea) != 5);
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 0), "this"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 1), "is"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 2), "a"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 3), "small"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 4), "test"));
|
2011-01-16 09:55:41 -08:00
|
|
|
|
|
|
|
eina_array_free(ea);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
ea =
|
|
|
|
eina_file_split(strdup(
|
2015-04-28 22:52:31 -07:00
|
|
|
"this\\/\\is\\//\\\\a \\more/\\complex///case\\\\\\"));
|
2011-01-16 09:55:41 -08:00
|
|
|
#else
|
|
|
|
ea = eina_file_split(strdup("this//is///a /more/complex///case///"));
|
|
|
|
#endif
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!ea);
|
|
|
|
ck_assert(eina_array_count(ea) != 6);
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 0), "this"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 1), "is"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 2), "a "));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 3), "more"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 4), "complex"));
|
|
|
|
ck_assert(strcmp(eina_array_data_get(ea, 5), "case"));
|
2011-01-16 09:55:41 -08:00
|
|
|
|
|
|
|
eina_array_free(ea);
|
|
|
|
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2011-01-16 09:55:41 -08:00
|
|
|
|
2013-03-27 06:40:28 -07:00
|
|
|
Eina_Tmpstr*
|
|
|
|
get_full_path(const char* tmpdirname, const char* filename)
|
|
|
|
{
|
|
|
|
char full_path[PATH_MAX] = "";
|
2015-08-10 03:49:41 -07:00
|
|
|
eina_str_join(full_path, sizeof(full_path), PATH_SEP_C, tmpdirname, filename);
|
2013-03-27 06:40:28 -07:00
|
|
|
return eina_tmpstr_add(full_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Tmpstr*
|
|
|
|
get_eina_test_file_tmp_dir()
|
|
|
|
{
|
|
|
|
Eina_Tmpstr *tmp_dir;
|
|
|
|
|
|
|
|
Eina_Bool created = eina_file_mkdtemp("EinaFileTestXXXXXX", &tmp_dir);
|
|
|
|
|
|
|
|
if (!created)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp_dir;
|
|
|
|
}
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_file_direct_ls_simple)
|
2013-03-27 06:40:28 -07:00
|
|
|
{
|
2015-08-10 03:49:41 -07:00
|
|
|
/*
|
|
|
|
* Windows: naming conventions
|
|
|
|
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
|
|
|
|
* 1) Do not end a directory with a period
|
|
|
|
* 2) '*' (asterisk) is a reserved character
|
|
|
|
* 3) ':' (colon) is a reserved character
|
2016-01-18 22:19:51 -08:00
|
|
|
*
|
|
|
|
* Note: UTF-8 symbol U+03BC is greek lower mu
|
2015-08-10 03:49:41 -07:00
|
|
|
*/
|
|
|
|
|
2013-03-27 06:40:28 -07:00
|
|
|
const char *good_dirs[] =
|
|
|
|
{
|
|
|
|
"eina_file_direct_ls_simple_dir",
|
2015-08-10 03:49:41 -07:00
|
|
|
#ifndef _WIN32
|
2013-03-27 06:40:28 -07:00
|
|
|
"a.",
|
2015-08-10 03:49:41 -07:00
|
|
|
#endif
|
2013-03-27 06:40:28 -07:00
|
|
|
"$a$b",
|
2015-08-10 03:49:41 -07:00
|
|
|
#ifndef _WIN32
|
2013-03-27 06:40:28 -07:00
|
|
|
"~$a@:-*$b!{}"
|
2015-08-10 03:49:41 -07:00
|
|
|
#else
|
2016-01-18 22:19:51 -08:00
|
|
|
"~$a@\u03bc-#$b!{}"
|
2015-08-10 03:49:41 -07:00
|
|
|
#endif
|
2013-03-27 06:40:28 -07:00
|
|
|
};
|
|
|
|
const int good_dirs_count = sizeof(good_dirs) / sizeof(const char *);
|
|
|
|
Eina_Tmpstr *test_dirname = get_eina_test_file_tmp_dir();
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(test_dirname == NULL);
|
2013-03-27 06:40:28 -07:00
|
|
|
|
|
|
|
for (int i = 0; i != good_dirs_count; ++i)
|
|
|
|
{
|
|
|
|
Eina_Tmpstr *dirname = get_full_path(test_dirname, good_dirs[i]);
|
|
|
|
// clean old test directories
|
|
|
|
rmdir(dirname);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(mkdir(dirname, default_dir_rights) != 0);
|
2013-03-27 06:40:28 -07:00
|
|
|
|
|
|
|
Eina_File_Direct_Info *dir_info;
|
|
|
|
Eina_Iterator *it = eina_file_direct_ls(test_dirname);
|
|
|
|
Eina_Bool found_dir = EINA_FALSE;
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!eina_iterator_container_get(it));
|
2013-10-25 03:57:57 -07:00
|
|
|
EINA_ITERATOR_FOREACH(it, dir_info)
|
2013-03-27 06:40:28 -07:00
|
|
|
{
|
|
|
|
if (!strcmp(dir_info->path, dirname))
|
|
|
|
{
|
|
|
|
found_dir = EINA_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eina_iterator_free(it);
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!found_dir);
|
|
|
|
ck_assert(rmdir(dirname) != 0);
|
2013-03-27 06:40:28 -07:00
|
|
|
|
|
|
|
eina_tmpstr_del(dirname);
|
|
|
|
}
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(rmdir(test_dirname) != 0);
|
2013-03-27 06:40:28 -07:00
|
|
|
eina_tmpstr_del(test_dirname);
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2013-03-27 06:40:28 -07:00
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_file_ls_simple)
|
2013-03-27 06:40:28 -07:00
|
|
|
{
|
2015-08-10 03:49:41 -07:00
|
|
|
/*
|
|
|
|
* Windows: naming conventions
|
|
|
|
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
|
|
|
|
* 1) Do not end a directory with a period
|
|
|
|
* 2) '*' (asterisk) is a reserved character
|
|
|
|
* 3) ':' (colon) is a reserved character
|
|
|
|
*/
|
|
|
|
|
2013-03-27 06:40:28 -07:00
|
|
|
const char *good_dirs[] =
|
|
|
|
{
|
|
|
|
"eina_file_ls_simple_dir",
|
2015-08-10 03:49:41 -07:00
|
|
|
#ifndef _WIN32
|
2013-03-27 06:40:28 -07:00
|
|
|
"b.",
|
2015-08-10 03:49:41 -07:00
|
|
|
#endif
|
2013-03-27 06:40:28 -07:00
|
|
|
"$b$a",
|
2015-08-10 03:49:41 -07:00
|
|
|
#ifndef _WIN32
|
2013-03-27 06:40:28 -07:00
|
|
|
"~$b@:-*$a!{}"
|
2015-08-10 03:49:41 -07:00
|
|
|
#else
|
2016-01-18 22:19:51 -08:00
|
|
|
"~$b@\u03bc-#$a!{}"
|
2015-08-10 03:49:41 -07:00
|
|
|
#endif
|
2013-03-27 06:40:28 -07:00
|
|
|
};
|
|
|
|
const int good_dirs_count = sizeof(good_dirs) / sizeof(const char *);
|
|
|
|
Eina_Tmpstr *test_dirname = get_eina_test_file_tmp_dir();
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(test_dirname == NULL);
|
2013-03-27 06:40:28 -07:00
|
|
|
|
|
|
|
for (int i = 0; i != good_dirs_count; ++i)
|
|
|
|
{
|
|
|
|
Eina_Tmpstr *dirname = get_full_path(test_dirname, good_dirs[i]);
|
|
|
|
// clean old test directories
|
|
|
|
rmdir(dirname);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(mkdir(dirname, default_dir_rights) != 0);
|
2013-03-27 06:40:28 -07:00
|
|
|
|
|
|
|
char *filename;
|
|
|
|
Eina_Iterator *it = eina_file_ls(test_dirname);
|
|
|
|
Eina_Bool found_dir = EINA_FALSE;
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!eina_iterator_container_get(it));
|
2013-10-25 03:57:57 -07:00
|
|
|
EINA_ITERATOR_FOREACH(it, filename)
|
2013-03-27 06:40:28 -07:00
|
|
|
{
|
|
|
|
if (!strcmp(filename, dirname))
|
|
|
|
{
|
|
|
|
found_dir = EINA_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eina_iterator_free(it);
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!found_dir);
|
|
|
|
ck_assert(rmdir(dirname) != 0);
|
2013-03-27 06:40:28 -07:00
|
|
|
|
|
|
|
eina_tmpstr_del(dirname);
|
|
|
|
}
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(rmdir(test_dirname) != 0);
|
2013-03-27 06:40:28 -07:00
|
|
|
eina_tmpstr_del(test_dirname);
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2013-03-27 06:40:28 -07:00
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_file_map_new_test)
|
2013-08-13 01:31:41 -07:00
|
|
|
{
|
2020-02-06 01:49:19 -08:00
|
|
|
const char *eina_map_test_string = "Hello. I'm the eina map test string.";
|
2013-08-13 01:31:41 -07:00
|
|
|
const char *test_file_name_part = "/example.txt";
|
|
|
|
const char *test_file2_name_part = "/example_big.txt";
|
|
|
|
char *test_file_path, *test_file2_path;
|
|
|
|
char *big_buffer;
|
|
|
|
const char *template = "abcdefghijklmnopqrstuvwxyz";
|
2013-11-10 00:26:12 -08:00
|
|
|
int template_size = strlen(template);
|
|
|
|
int memory_page_size = eina_cpu_page_size();
|
2013-08-13 01:31:41 -07:00
|
|
|
const int big_buffer_size = memory_page_size * 1.5;
|
|
|
|
const int iteration_number = big_buffer_size / template_size;
|
|
|
|
int test_string_length = strlen(eina_map_test_string);
|
|
|
|
int test_full_filename_size;
|
|
|
|
int test_full_filename2_size;
|
|
|
|
Eina_File *e_file, *e_file2;
|
2020-02-06 01:49:19 -08:00
|
|
|
void *file_map, *file2_map;
|
2013-08-13 01:31:41 -07:00
|
|
|
int fd, correct_file_open_check, correct_map_check, map_offset, map_length, file_length, file2_length;
|
|
|
|
int test_file_name_part_size = strlen(test_file_name_part);
|
|
|
|
int test_file2_name_part_size = strlen(test_file2_name_part);
|
|
|
|
int test_dirname_size;
|
|
|
|
int start_point_final, last_chunk_size;
|
2018-11-08 08:40:51 -08:00
|
|
|
size_t size;
|
2013-08-13 01:31:41 -07:00
|
|
|
|
|
|
|
Eina_Tmpstr *test_dirname = get_eina_test_file_tmp_dir();
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(test_dirname == NULL);
|
2013-08-13 01:31:41 -07:00
|
|
|
test_dirname_size = strlen((char *)test_dirname);
|
2020-02-06 01:49:19 -08:00
|
|
|
|
2013-08-13 01:31:41 -07:00
|
|
|
// memory allocation
|
|
|
|
test_full_filename_size = test_file_name_part_size + test_dirname_size + 1;
|
|
|
|
test_file_path = (char *)malloc(test_full_filename_size);
|
|
|
|
|
|
|
|
test_full_filename2_size = test_file2_name_part_size + test_dirname_size + 1;
|
|
|
|
test_file2_path = (char *)malloc(test_full_filename2_size);
|
|
|
|
|
|
|
|
// Creating big buffer 1.5 * (memory page size)
|
|
|
|
big_buffer = (char *)malloc(big_buffer_size);
|
|
|
|
// Set first symbol == '/0' to strcat corret work
|
|
|
|
big_buffer[0] = '\0';
|
|
|
|
// iteration_number - quantity of templates that can be fully put in big_buff
|
|
|
|
for (int i = 0; i < iteration_number; i++)
|
|
|
|
strcat (big_buffer, template);
|
|
|
|
// calculating last chunk of data that < template_size
|
|
|
|
last_chunk_size = big_buffer_size - iteration_number * template_size - file_min_offset;
|
|
|
|
//calculating start point for final iteration_number
|
|
|
|
start_point_final = iteration_number * template_size;
|
2018-11-08 08:40:51 -08:00
|
|
|
size = strlen(template);
|
|
|
|
if (size > (size_t)last_chunk_size) size = last_chunk_size;
|
|
|
|
memcpy((big_buffer + start_point_final), template, size);
|
2013-08-13 01:31:41 -07:00
|
|
|
// set last element of big_buffer in '\0' - end of string
|
|
|
|
big_buffer[big_buffer_size - file_min_offset] = '\0';
|
|
|
|
// check big_buffer valid length
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert((int)strlen(big_buffer) != (big_buffer_size - file_min_offset));
|
2013-08-13 01:31:41 -07:00
|
|
|
|
|
|
|
// generating file 1 full name
|
|
|
|
strcpy(test_file_path, (char *)test_dirname);
|
|
|
|
strcat(test_file_path, test_file_name_part);
|
|
|
|
// generating file 2 full name
|
|
|
|
strcpy(test_file2_path, (char *)test_dirname);
|
|
|
|
strcat(test_file2_path, test_file2_name_part);
|
2020-02-06 01:49:19 -08:00
|
|
|
|
2015-12-03 02:42:08 -08:00
|
|
|
fd = open(test_file_path, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd <= 0);
|
|
|
|
ck_assert(write(fd, eina_map_test_string, strlen(eina_map_test_string)) != (ssize_t) strlen(eina_map_test_string));
|
2013-08-13 01:31:41 -07:00
|
|
|
close(fd);
|
|
|
|
|
2015-12-03 02:42:08 -08:00
|
|
|
fd = open(test_file2_path, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd <= 0);
|
|
|
|
ck_assert(write(fd, big_buffer, big_buffer_size - file_min_offset) != big_buffer_size - file_min_offset);
|
2013-08-13 01:31:41 -07:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
e_file = eina_file_open(test_file_path, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!e_file);
|
2013-08-13 01:31:41 -07:00
|
|
|
file_length = eina_file_size_get(e_file);
|
2020-02-06 01:49:19 -08:00
|
|
|
correct_file_open_check = file_length - test_string_length;
|
2013-08-13 01:31:41 -07:00
|
|
|
// check size of eina_map_test_string == size of file
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(correct_file_open_check != 0);
|
2013-10-25 03:50:14 -07:00
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(eina_file_refresh(e_file));
|
2013-08-13 01:31:41 -07:00
|
|
|
|
|
|
|
e_file2 = eina_file_open(test_file2_path, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!e_file2);
|
2013-08-13 01:31:41 -07:00
|
|
|
file2_length = eina_file_size_get(e_file2);
|
2020-02-06 01:49:19 -08:00
|
|
|
correct_file_open_check = file2_length - (big_buffer_size - file_min_offset);
|
2013-08-13 01:31:41 -07:00
|
|
|
// check size of big_buffer == size of file
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(correct_file_open_check != 0);
|
2013-08-13 01:31:41 -07:00
|
|
|
|
|
|
|
// test : offset > file -> length => return NULL
|
|
|
|
map_offset = test_string_length + file_min_offset;
|
|
|
|
map_length = file_min_offset;
|
2020-02-06 01:49:19 -08:00
|
|
|
file_map = eina_file_map_new(e_file, EINA_FILE_WILLNEED, map_offset, map_length);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(file_map);
|
2013-08-13 01:31:41 -07:00
|
|
|
|
|
|
|
// test : offset + length > file -> length => return NULL
|
|
|
|
map_offset = file_min_offset;
|
|
|
|
map_length = test_string_length;
|
2020-02-06 01:49:19 -08:00
|
|
|
file_map = eina_file_map_new(e_file, EINA_FILE_WILLNEED, map_offset, map_length);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(file_map);
|
|
|
|
ck_assert(eina_file_map_faulted(e_file, file_map));
|
2013-08-13 01:31:41 -07:00
|
|
|
|
|
|
|
// test : offset = 0 AND length = file->length - use eina_file_map_all
|
|
|
|
map_offset = 0;
|
|
|
|
map_length = test_string_length;
|
2020-02-06 01:49:19 -08:00
|
|
|
file_map = eina_file_map_new(e_file, EINA_FILE_WILLNEED, map_offset, map_length);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!file_map);
|
2020-02-06 01:49:19 -08:00
|
|
|
correct_map_check= strcmp((char*) file_map, eina_map_test_string);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(correct_map_check != 0);
|
2013-08-13 01:31:41 -07:00
|
|
|
|
|
|
|
// test : offset = memory_page_size AND length = file->length - memory_page_size => correct partly map
|
|
|
|
map_offset = memory_page_size;
|
|
|
|
map_length = big_buffer_size - memory_page_size - file_min_offset;
|
2020-02-06 01:49:19 -08:00
|
|
|
file2_map = eina_file_map_new(e_file2, EINA_FILE_WILLNEED, map_offset, map_length);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!file2_map);
|
2020-02-06 01:49:19 -08:00
|
|
|
correct_map_check = strcmp((char*)file2_map, big_buffer + memory_page_size);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(correct_map_check != 0);
|
2015-03-13 00:14:50 -07:00
|
|
|
|
2017-09-14 23:56:17 -07:00
|
|
|
// test no crash with eina_file_map_populate()
|
|
|
|
eina_file_map_populate(e_file, EINA_FILE_POPULATE, file_map, 0, 0);
|
|
|
|
eina_file_map_populate(e_file, EINA_FILE_POPULATE, file_map, file_length / 2, 0);
|
|
|
|
eina_file_map_populate(e_file, EINA_FILE_POPULATE, file_map, 0, file_length * 2);
|
|
|
|
eina_file_map_populate(e_file, EINA_FILE_POPULATE, file_map, file_length / 2, big_buffer_size);
|
|
|
|
eina_file_map_populate(e_file, EINA_FILE_POPULATE, file_map, big_buffer_size + 1, file_length);
|
|
|
|
|
2015-03-13 00:14:50 -07:00
|
|
|
eina_file_map_free(e_file, file_map);
|
2017-09-14 23:56:17 -07:00
|
|
|
eina_file_map_free(e_file, file_map); // test no crash
|
2015-03-13 00:14:50 -07:00
|
|
|
eina_file_map_free(e_file2, file2_map);
|
|
|
|
eina_file_close(e_file);
|
|
|
|
eina_file_close(e_file2);
|
|
|
|
|
2013-08-13 01:31:41 -07:00
|
|
|
unlink(test_file_path);
|
|
|
|
unlink(test_file2_path);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(rmdir(test_dirname) != 0);
|
2015-03-13 00:14:50 -07:00
|
|
|
|
2013-08-13 01:31:41 -07:00
|
|
|
free(test_file_path);
|
|
|
|
free(test_file2_path);
|
2019-05-02 04:40:31 -07:00
|
|
|
free(big_buffer);
|
2013-08-13 01:31:41 -07:00
|
|
|
eina_tmpstr_del(test_dirname);
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2013-08-13 01:31:41 -07:00
|
|
|
|
2013-10-25 03:50:14 -07:00
|
|
|
static const char *virtual_file_data = "this\n"
|
2015-03-27 02:50:00 -07:00
|
|
|
"is a test for the sake of testing\r\n"
|
2013-10-25 03:50:14 -07:00
|
|
|
"it should detect all the line of this\n"
|
|
|
|
"\n"
|
|
|
|
"\r\n"
|
|
|
|
"file !\n"
|
|
|
|
"without any issue !";
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_virtualize)
|
2013-10-25 03:50:14 -07:00
|
|
|
{
|
|
|
|
Eina_File *f;
|
|
|
|
Eina_File *tmp;
|
|
|
|
Eina_Iterator *it;
|
|
|
|
Eina_File_Line *ln;
|
|
|
|
void *map;
|
2015-03-27 14:36:45 -07:00
|
|
|
unsigned int i = 0;
|
2013-10-25 03:50:14 -07:00
|
|
|
|
|
|
|
f = eina_file_virtualize("gloubi", virtual_file_data, strlen(virtual_file_data), EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!f);
|
2013-10-25 03:50:14 -07:00
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!eina_file_virtual(f));
|
2013-10-25 03:50:14 -07:00
|
|
|
|
|
|
|
tmp = eina_file_dup(f);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!tmp);
|
2013-10-25 03:50:14 -07:00
|
|
|
eina_file_close(tmp);
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(strcmp("gloubi", eina_file_filename_get(f)));
|
2013-10-25 03:50:14 -07:00
|
|
|
|
|
|
|
map = eina_file_map_new(f, EINA_FILE_WILLNEED, 7, 7);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(map != (virtual_file_data + 7));
|
2013-10-25 03:50:14 -07:00
|
|
|
eina_file_map_free(f, map);
|
|
|
|
|
|
|
|
it = eina_file_map_lines(f);
|
|
|
|
EINA_ITERATOR_FOREACH(it, ln)
|
|
|
|
{
|
|
|
|
i++;
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(ln->index != i);
|
2015-03-27 14:36:45 -07:00
|
|
|
|
|
|
|
if (i == 4 || i == 5)
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(ln->length != 0);
|
2013-10-25 03:50:14 -07:00
|
|
|
}
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(eina_iterator_container_get(it) != f);
|
2013-10-25 03:50:14 -07:00
|
|
|
eina_iterator_free(it);
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(i != 7);
|
2013-10-25 03:50:14 -07:00
|
|
|
|
|
|
|
eina_file_close(f);
|
|
|
|
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2013-10-25 03:50:14 -07:00
|
|
|
|
2013-11-20 01:19:16 -08:00
|
|
|
static void *
|
|
|
|
_eina_test_file_thread(void *data EINA_UNUSED, Eina_Thread t EINA_UNUSED)
|
|
|
|
{
|
2020-05-26 14:45:31 -07:00
|
|
|
#ifdef _WIN32
|
2020-06-24 06:32:12 -07:00
|
|
|
char filename[MAX_PATH];
|
|
|
|
size_t len;
|
|
|
|
const char test_file[] = "cmd.exe";
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!GetSystemDirectoryA(filename, MAX_PATH));
|
2020-06-24 06:32:12 -07:00
|
|
|
|
|
|
|
len = strlen(filename);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the buffer size.
|
|
|
|
* The system path length + path separator + length of the test_file + null terminator
|
|
|
|
* Must fit in MAX_PATH.
|
|
|
|
*/
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(MAX_PATH < len + 1 + sizeof(test_file));
|
2020-06-24 06:32:12 -07:00
|
|
|
|
|
|
|
// append trailing directory separator if there isn't one
|
|
|
|
if (filename[len - 1] != '\\' && filename[len - 1] != '/')
|
|
|
|
{
|
|
|
|
filename[len] = '\\';
|
|
|
|
filename[len + 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
strncat(filename, test_file, MAX_PATH - len - 2);
|
2020-05-26 14:45:31 -07:00
|
|
|
#else
|
|
|
|
const char *filename = "/bin/sh";
|
|
|
|
#endif
|
2013-11-20 01:19:16 -08:00
|
|
|
Eina_File *f;
|
|
|
|
unsigned int i;
|
|
|
|
|
2018-03-30 12:01:04 -07:00
|
|
|
for (i = 0; i < 100; ++i)
|
2013-11-20 01:19:16 -08:00
|
|
|
{
|
2020-05-26 14:45:31 -07:00
|
|
|
f = eina_file_open(filename, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!f);
|
2013-11-20 01:19:16 -08:00
|
|
|
eina_file_close(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_thread)
|
2013-11-20 01:19:16 -08:00
|
|
|
{
|
|
|
|
Eina_Thread th[4];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!(eina_thread_create(&th[i], EINA_THREAD_NORMAL, -1, _eina_test_file_thread, NULL)));
|
2013-11-20 01:19:16 -08:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(eina_thread_join(th[i]) != NULL);
|
2013-11-20 01:19:16 -08:00
|
|
|
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2013-11-20 01:19:16 -08:00
|
|
|
|
2015-02-05 06:01:12 -08:00
|
|
|
static const struct {
|
|
|
|
const char *test;
|
|
|
|
const char *result;
|
|
|
|
} sanitize[] = {
|
2020-06-23 08:21:10 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
{ "C:\\home\\mydir\\..\\myfile", "C:/home/myfile" },
|
|
|
|
{ "C:/home/mydir/../myfile", "C:/home/myfile" },
|
|
|
|
{ "\\home\\mydir\\..\\myfile", "/home/myfile" },
|
|
|
|
#endif
|
2015-02-05 06:01:12 -08:00
|
|
|
{ "/home/mydir/../myfile", "/home/myfile" }
|
|
|
|
};
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_path)
|
2015-02-05 06:01:12 -08:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (sanitize) / sizeof (sanitize[0]); i++)
|
|
|
|
{
|
|
|
|
path = eina_file_path_sanitize(sanitize[i].test);
|
2020-06-23 08:21:10 -07:00
|
|
|
ck_assert_str_eq(path, sanitize[i].result);
|
2019-05-02 04:47:28 -07:00
|
|
|
free(path);
|
2015-02-05 06:01:12 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2015-02-05 06:01:12 -08:00
|
|
|
|
2015-04-22 05:21:36 -07:00
|
|
|
#ifdef XATTR_TEST_DIR
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_xattr)
|
2015-04-22 05:21:36 -07:00
|
|
|
{
|
|
|
|
Eina_File *ef;
|
|
|
|
char *filename = "tmpfile";
|
|
|
|
const char *attribute[] =
|
|
|
|
{
|
|
|
|
"user.comment1",
|
|
|
|
"user.comment2",
|
|
|
|
"user.comment3"
|
|
|
|
};
|
|
|
|
const char *data[] =
|
|
|
|
{
|
|
|
|
"This is a test file",
|
|
|
|
"This line is a comment",
|
|
|
|
"This file has extra attributes"
|
|
|
|
};
|
|
|
|
char *ret_str;
|
|
|
|
unsigned int i;
|
|
|
|
Eina_Bool ret;
|
|
|
|
Eina_Tmpstr *test_file_path;
|
|
|
|
Eina_Iterator *it;
|
2015-05-05 23:32:04 -07:00
|
|
|
int fd, count=0;
|
2015-04-22 05:21:36 -07:00
|
|
|
Eina_Xattr *xattr;
|
|
|
|
|
|
|
|
test_file_path = get_full_path(XATTR_TEST_DIR, filename);
|
|
|
|
|
2015-12-03 02:42:08 -08:00
|
|
|
fd = open(test_file_path, O_RDONLY | O_BINARY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd == 0);
|
2015-04-22 05:21:36 -07:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(attribute) / sizeof(attribute[0]); ++i)
|
|
|
|
{
|
|
|
|
ret = eina_xattr_set(test_file_path, attribute[i], data[i], strlen(data[i]), EINA_XATTR_INSERT);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(ret != EINA_TRUE);
|
2015-04-22 05:21:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ef = eina_file_open(test_file_path, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!ef);
|
2015-04-22 05:21:36 -07:00
|
|
|
|
|
|
|
it = eina_file_xattr_get(ef);
|
|
|
|
EINA_ITERATOR_FOREACH(it, ret_str)
|
|
|
|
{
|
|
|
|
for (i = 0; i < sizeof (attribute) / sizeof (attribute[0]); i++)
|
|
|
|
if (strcmp(attribute[i], ret_str) == 0)
|
2015-05-05 23:32:04 -07:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-22 05:21:36 -07:00
|
|
|
}
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(count != sizeof (attribute) / sizeof (attribute[0]));
|
2015-04-22 05:21:36 -07:00
|
|
|
eina_iterator_free(it);
|
|
|
|
|
2015-05-05 23:32:04 -07:00
|
|
|
count = 0;
|
2015-04-22 05:21:36 -07:00
|
|
|
it = eina_file_xattr_value_get(ef);
|
|
|
|
EINA_ITERATOR_FOREACH(it, xattr)
|
|
|
|
{
|
|
|
|
for (i = 0; i < sizeof (data) / sizeof (data[0]); i++)
|
|
|
|
if (strcmp(attribute[i], xattr->name) == 0 &&
|
|
|
|
strcmp(data[i], xattr->value) == 0)
|
2015-05-05 23:32:04 -07:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-22 05:21:36 -07:00
|
|
|
}
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(count != sizeof (data) / sizeof (data[0]));
|
2015-04-22 05:21:36 -07:00
|
|
|
eina_iterator_free(it);
|
|
|
|
|
|
|
|
unlink(test_file_path);
|
|
|
|
eina_tmpstr_del(test_file_path);
|
|
|
|
eina_file_close(ef);
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2015-04-22 05:21:36 -07:00
|
|
|
#endif
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_copy)
|
2015-05-18 01:52:54 -07:00
|
|
|
{
|
|
|
|
const char *test_file1_name = "EinaCopyFromXXXXXX.txt";
|
|
|
|
const char *test_file2_name = "EinaCopyToXXXXXX.txt";
|
|
|
|
Eina_Tmpstr *test_file1_path, *test_file2_path;
|
|
|
|
const char *data = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
Eina_File *e_file1, *e_file2;
|
|
|
|
int fd1, fd2, rval;
|
|
|
|
size_t file1_len, file2_len;
|
|
|
|
Eina_Bool ret;
|
|
|
|
void *content1, *content2;
|
|
|
|
|
|
|
|
fd1 = eina_file_mkstemp(test_file1_name, &test_file1_path);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd1 <= 0);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
|
|
|
fd2 = eina_file_mkstemp(test_file2_name, &test_file2_path);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd2 <= 0);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(write(fd1, data, strlen(data)) != (ssize_t) strlen(data));
|
2015-05-18 01:52:54 -07:00
|
|
|
|
|
|
|
close(fd1);
|
|
|
|
close(fd2);
|
|
|
|
|
|
|
|
//Copy file data
|
|
|
|
ret = eina_file_copy(test_file1_path, test_file2_path, EINA_FILE_COPY_DATA, NULL, NULL);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(ret != EINA_TRUE);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
|
|
|
e_file1 = eina_file_open(test_file1_path, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!e_file1);
|
2015-05-18 01:52:54 -07:00
|
|
|
file1_len = eina_file_size_get(e_file1);
|
|
|
|
|
|
|
|
e_file2 = eina_file_open(test_file2_path, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!e_file2);
|
2015-05-18 01:52:54 -07:00
|
|
|
file2_len = eina_file_size_get(e_file2);
|
|
|
|
|
|
|
|
//Check if both the files are same
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(e_file1 == e_file2);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(file1_len != strlen(data));
|
|
|
|
ck_assert(file1_len != file2_len);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
|
|
|
//Check the contents of both the file
|
|
|
|
content1 = eina_file_map_all(e_file1, EINA_FILE_POPULATE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(content1 == NULL);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
|
|
|
content2 = eina_file_map_all(e_file2, EINA_FILE_POPULATE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(content2 == NULL);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
|
|
|
rval = memcmp(content1, content2, strlen(data));
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(rval != 0);
|
2015-05-18 01:52:54 -07:00
|
|
|
|
|
|
|
eina_file_map_free(e_file1, content1);
|
|
|
|
eina_file_map_free(e_file2, content2);
|
|
|
|
eina_file_close(e_file1);
|
|
|
|
eina_file_close(e_file2);
|
|
|
|
unlink(test_file1_path);
|
|
|
|
unlink(test_file2_path);
|
|
|
|
eina_tmpstr_del(test_file1_path);
|
|
|
|
eina_tmpstr_del(test_file2_path);
|
|
|
|
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2015-05-18 01:52:54 -07:00
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_statat)
|
2015-11-09 16:26:21 -08:00
|
|
|
{
|
|
|
|
Eina_Tmpstr *test_file1_path, *test_file2_path;
|
|
|
|
Eina_Iterator *it;
|
|
|
|
Eina_Stat st;
|
|
|
|
Eina_File_Direct_Info *info;
|
|
|
|
const char *template = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
int template_size = strlen(template);
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
Eina_Tmpstr *test_dirname = get_eina_test_file_tmp_dir();
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(test_dirname == NULL);
|
2015-11-09 16:26:21 -08:00
|
|
|
|
|
|
|
test_file1_path = get_full_path(test_dirname, "example1.txt");
|
|
|
|
test_file2_path = get_full_path(test_dirname, "example2.txt");
|
|
|
|
|
2015-12-03 02:42:08 -08:00
|
|
|
fd = open(test_file1_path, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd == 0);
|
|
|
|
ck_assert(write(fd, template, template_size) != template_size);
|
2015-11-09 16:26:21 -08:00
|
|
|
close(fd);
|
|
|
|
|
2015-12-03 02:42:08 -08:00
|
|
|
fd = open(test_file2_path, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd == 0);
|
|
|
|
ck_assert(write(fd, template, template_size) != template_size);
|
2015-11-09 16:26:21 -08:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
it = eina_file_stat_ls(test_dirname);
|
|
|
|
fprintf(stderr, "file=%s\n", test_dirname);
|
|
|
|
EINA_ITERATOR_FOREACH(it, info)
|
|
|
|
{
|
|
|
|
ret = eina_file_statat(eina_iterator_container_get(it), info, &st);
|
|
|
|
fprintf(stderr, "ret=%d\n", ret);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(ret != 0);
|
|
|
|
ck_assert(st.size != (unsigned int)template_size);
|
2015-11-09 16:26:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
unlink(test_file1_path);
|
|
|
|
unlink(test_file2_path);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(rmdir(test_dirname) != 0);
|
2015-11-09 16:26:21 -08:00
|
|
|
eina_tmpstr_del(test_file1_path);
|
|
|
|
eina_tmpstr_del(test_file2_path);
|
|
|
|
eina_tmpstr_del(test_dirname);
|
|
|
|
|
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2015-11-09 16:26:21 -08:00
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_mktemp)
|
2016-01-19 00:12:19 -08:00
|
|
|
{
|
|
|
|
Eina_Tmpstr *tmpfile, *tmpdir = NULL;
|
2019-06-20 04:50:35 -07:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
#ifndef _WIN32
|
|
|
|
char fmt[256];
|
|
|
|
#endif
|
2016-01-19 00:12:19 -08:00
|
|
|
Eina_File_Direct_Info *info;
|
|
|
|
Eina_Iterator *it;
|
|
|
|
Eina_File *file;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
static const char *patterns[] = {
|
|
|
|
"XXXXXX",
|
|
|
|
"XXXXXX.txt",
|
|
|
|
"eina_file_test_XXXXXX",
|
|
|
|
"eina_file_test_XXXXXX.txt",
|
|
|
|
"./eina_file_test_XXXXXX.txt",
|
|
|
|
};
|
|
|
|
|
|
|
|
/* test NULL */
|
2020-02-06 01:49:19 -08:00
|
|
|
EXPECT_ERROR_START;
|
2016-01-19 00:12:19 -08:00
|
|
|
fd = eina_file_mkstemp(NULL, NULL);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd >= 0);
|
2020-02-06 01:49:19 -08:00
|
|
|
EXPECT_ERROR_END;
|
2016-01-19 00:12:19 -08:00
|
|
|
fd = eina_file_mkstemp(patterns[0], NULL);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd < 0);
|
2016-01-19 00:12:19 -08:00
|
|
|
|
|
|
|
/* here's an attempt at removing the file, without knowing its path */
|
|
|
|
#ifdef F_GETPATH
|
|
|
|
/* most likely Mac OS */
|
|
|
|
if (fcntl(fd, F_GETPATH, buf) != -1)
|
|
|
|
unlink(buf);
|
|
|
|
#elif !defined _WIN32
|
|
|
|
sprintf(fmt, "/proc/self/fd/%d", fd);
|
|
|
|
if (readlink(fmt, buf, sizeof(buf)))
|
|
|
|
unlink(buf);
|
|
|
|
#else
|
2018-01-29 05:51:07 -08:00
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
HANDLE h;
|
|
|
|
DWORD l;
|
|
|
|
|
|
|
|
h = (HANDLE)_get_osfhandle(fd);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(h == INVALID_HANDLE_VALUE);
|
2018-01-29 05:51:07 -08:00
|
|
|
l = GetFinalPathNameByHandle(h, buf, sizeof(buf), 0);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(l == 0);
|
2018-01-29 05:51:07 -08:00
|
|
|
/* GetFinalPathNameByHandle() preprends path with \\?\ */
|
2018-01-31 04:35:55 -08:00
|
|
|
unlink(buf + 4);
|
2018-01-29 05:51:07 -08:00
|
|
|
}
|
2016-01-19 00:12:19 -08:00
|
|
|
#endif
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
for (unsigned int k = 0; k < sizeof(patterns) / sizeof(patterns[0]); k++)
|
|
|
|
{
|
|
|
|
tmpfile = NULL;
|
|
|
|
fd = eina_file_mkstemp(patterns[k], &tmpfile);
|
2018-06-25 12:18:23 -07:00
|
|
|
ck_assert(fd >= 0);
|
|
|
|
ck_assert(!!tmpfile);
|
2016-01-19 00:12:19 -08:00
|
|
|
file = eina_file_open(tmpfile, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!file);
|
2016-01-19 00:12:19 -08:00
|
|
|
eina_file_close(file);
|
|
|
|
unlink(tmpfile);
|
|
|
|
eina_tmpstr_del(tmpfile);
|
2016-01-19 01:15:20 -08:00
|
|
|
close(fd);
|
2016-01-19 00:12:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* temp directory */
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!eina_file_mkdtemp("eina_file_test_XXXXXX", &tmpdir) || !tmpdir);
|
2016-01-19 00:12:19 -08:00
|
|
|
|
|
|
|
it = eina_file_direct_ls(tmpdir);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!it);
|
2016-01-19 00:12:19 -08:00
|
|
|
|
|
|
|
/* should be empty! */
|
|
|
|
EINA_ITERATOR_FOREACH(it, info)
|
|
|
|
fail();
|
|
|
|
|
|
|
|
eina_iterator_free(it);
|
|
|
|
|
|
|
|
/* file inside that directory */
|
|
|
|
eina_file_path_join(buf, sizeof(buf), tmpdir, "eina_file_test_XXXXXX.txt");
|
|
|
|
|
|
|
|
fd = eina_file_mkstemp(buf, &tmpfile);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert((fd < 0) || !tmpfile);
|
|
|
|
ck_assert(close(fd));
|
2016-01-19 00:12:19 -08:00
|
|
|
|
|
|
|
it = eina_file_direct_ls(tmpdir);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!it);
|
2016-01-19 00:12:19 -08:00
|
|
|
|
|
|
|
/* should be empty! */
|
|
|
|
EINA_ITERATOR_FOREACH(it, info)
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(strcmp(info->path, tmpfile));
|
2016-01-19 00:12:19 -08:00
|
|
|
|
|
|
|
eina_iterator_free(it);
|
|
|
|
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(unlink(tmpfile));
|
|
|
|
ck_assert(rmdir(tmpdir));
|
2020-08-24 09:52:43 -07:00
|
|
|
eina_tmpstr_del(tmpdir);
|
2016-01-19 00:12:19 -08:00
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2016-01-19 00:12:19 -08:00
|
|
|
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
int create_file_not_empty(const char *file_name, Eina_Tmpstr **test_file_path, Eina_Bool close_file)
|
|
|
|
{
|
|
|
|
const char *data = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
int data_size = strlen(data);
|
|
|
|
int wr_size;
|
|
|
|
int fd = eina_file_mkstemp(file_name, test_file_path);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd <= 0);
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
wr_size = write(fd, data, data_size);
|
|
|
|
if(close_file == EINA_TRUE)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
fd = 0;
|
|
|
|
}
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(wr_size != data_size);
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(eina_test_file_unlink)
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
Eina_Tmpstr *test_file_path;
|
2020-08-01 08:11:07 -07:00
|
|
|
Eina_File* test_file;
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
const char *tmpfile = "eina_file_test_XXXXXX";
|
|
|
|
|
|
|
|
/*If file was not opened as 'eina'*/
|
|
|
|
fd = create_file_not_empty(tmpfile, &test_file_path, EINA_TRUE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd != 0);
|
|
|
|
ck_assert(!eina_file_unlink(test_file_path));
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
|
|
|
|
/*If file was opened as 'eina'*/
|
|
|
|
fd = create_file_not_empty(tmpfile, &test_file_path, EINA_TRUE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(fd != 0);
|
2020-08-01 08:11:07 -07:00
|
|
|
|
|
|
|
test_file = eina_file_open(test_file_path, EINA_FALSE);
|
2023-08-07 10:01:59 -07:00
|
|
|
ck_assert(!test_file);
|
|
|
|
ck_assert(!eina_file_unlink(test_file_path));
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
|
2020-08-01 08:11:07 -07:00
|
|
|
eina_file_close(test_file);
|
|
|
|
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
|
2011-01-16 09:55:41 -08:00
|
|
|
void
|
|
|
|
eina_test_file(TCase *tc)
|
|
|
|
{
|
|
|
|
tcase_add_test(tc, eina_file_split_simple);
|
2013-03-27 06:40:28 -07:00
|
|
|
tcase_add_test(tc, eina_file_direct_ls_simple);
|
|
|
|
tcase_add_test(tc, eina_file_ls_simple);
|
2013-08-13 01:31:41 -07:00
|
|
|
tcase_add_test(tc, eina_file_map_new_test);
|
2013-10-25 03:50:14 -07:00
|
|
|
tcase_add_test(tc, eina_test_file_virtualize);
|
2013-11-20 01:19:16 -08:00
|
|
|
tcase_add_test(tc, eina_test_file_thread);
|
2015-02-05 06:01:12 -08:00
|
|
|
tcase_add_test(tc, eina_test_file_path);
|
2015-04-22 05:21:36 -07:00
|
|
|
#ifdef XATTR_TEST_DIR
|
|
|
|
tcase_add_test(tc, eina_test_file_xattr);
|
|
|
|
#endif
|
2015-05-18 01:52:54 -07:00
|
|
|
tcase_add_test(tc, eina_test_file_copy);
|
2015-11-09 16:26:21 -08:00
|
|
|
tcase_add_test(tc, eina_test_file_statat);
|
2016-01-19 00:12:19 -08:00
|
|
|
tcase_add_test(tc, eina_test_file_mktemp);
|
eina_file: add eina_file_unlink
Test Plan: split D4423 - unlink
Reviewers: raster, vtorri, an.kroitor, jpeg, reutskiy.v.v, NikaWhite, cedric
Reviewed By: cedric
Subscribers: artem.popov, cedric, vtorri, jpeg
Tags: #windows, #efl
Differential Revision: https://phab.enlightenment.org/D4485
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
2017-02-07 14:50:32 -08:00
|
|
|
tcase_add_test(tc, eina_test_file_unlink);
|
|
|
|
|
2011-01-16 09:55:41 -08:00
|
|
|
}
|