cedric pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=b5333edd3ffe8c0379170a0e9d08ee05af439e2c

commit b5333edd3ffe8c0379170a0e9d08ee05af439e2c
Author: Vincent Torri <[email protected]>
Date:   Fri Nov 10 13:13:06 2017 -0800

    evil: add tests for Windows port.
    
    Signed-off-by: Cedric BAIL <[email protected]>
---
 src/Makefile_Evil.am              |   4 +-
 src/lib/evil/evil_stdio.c         |  34 ++--
 src/tests/evil/evil_suite.c       |   4 +-
 src/tests/evil/evil_suite.h       |   4 +-
 src/tests/evil/evil_test_stdio.c  | 238 ++++++++++++++++++++++++++++
 src/tests/evil/evil_test_stdlib.c | 317 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 576 insertions(+), 25 deletions(-)

diff --git a/src/Makefile_Evil.am b/src/Makefile_Evil.am
index 7aff77d665..f93b226f9c 100644
--- a/src/Makefile_Evil.am
+++ b/src/Makefile_Evil.am
@@ -126,7 +126,9 @@ tests_evil_evil_suite_SOURCES = \
 tests/evil/evil_suite.c \
 tests/evil/evil_suite.h \
 tests/evil/evil_test_dlfcn.c \
-tests/evil/evil_test_main.c
+tests/evil/evil_test_main.c \
+tests/evil/evil_test_stdio.c \
+tests/evil/evil_test_stdlib.c
 
 tests_evil_evil_suite_CPPFLAGS = \
 -I$(top_builddir)/src/lib/efl \
diff --git a/src/lib/evil/evil_stdio.c b/src/lib/evil/evil_stdio.c
index 5919d0c474..6b71e922bb 100644
--- a/src/lib/evil/evil_stdio.c
+++ b/src/lib/evil/evil_stdio.c
@@ -2,9 +2,14 @@
 # include "config.h"
 #endif /* HAVE_CONFIG_H */
 
-#include <direct.h>
-#include <sys/stat.h>
 #include <sys/types.h>
+#include <direct.h>
+
+#ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+#undef WIN32_LEAN_AND_MEAN
 
 #include "evil_macro.h"
 #include "evil_stdio.h"
@@ -12,30 +17,19 @@
 
 #undef rename
 
-int 
+int
 evil_rename(const char *src, const char* dst)
 {
-   struct stat st;
-
-   if (stat(dst, &st) < 0)
-        return rename(src, dst);
-
-   if (stat(src, &st) < 0)
-        return -1;
-
-   if (S_ISDIR(st.st_mode))
-     {
-        rmdir(dst);
-        return rename(src, dst);
-     }
+   DWORD res;
 
-   if (S_ISREG(st.st_mode))
+   res = GetFileAttributes(dst);
+   if ((res != 0xffffffff) && (res & FILE_ATTRIBUTE_DIRECTORY))
      {
-        unlink(dst);
-        return rename(src, dst);
+        if (!RemoveDirectory(dst))
+          return -1;
      }
 
-   return -1;
+   return MoveFileEx(src, dst, MOVEFILE_REPLACE_EXISTING) ? 0 : -1;
 }
 
 int
diff --git a/src/tests/evil/evil_suite.c b/src/tests/evil/evil_suite.c
index aeae25ebe0..d8fc492a9a 100644
--- a/src/tests/evil/evil_suite.c
+++ b/src/tests/evil/evil_suite.c
@@ -37,8 +37,8 @@ static const Efl_Test_Case etc[] = {
    { "Main", evil_test_main },
    /* { "Mman", evil_test_mman }, */
    /* { "Pwd", evil_test_pwd }, */
-   /* { "Stdio", evil_test_stdio }, */
-   /* { "Stdlib", evil_test_stdlib }, */
+   { "Stdio", evil_test_stdio },
+   { "Stdlib", evil_test_stdlib },
    /* { "String", evil_test_string }, */
    /* { "Time", evil_test_time }, */
    /* { "Unistd", evil_test_unistd }, */
diff --git a/src/tests/evil/evil_suite.h b/src/tests/evil/evil_suite.h
index 17222fcfec..c7220e0b80 100644
--- a/src/tests/evil/evil_suite.h
+++ b/src/tests/evil/evil_suite.h
@@ -31,8 +31,8 @@ void evil_test_dlfcn(TCase *tc);
 void evil_test_main(TCase *tc);
 /* void evil_test_mman(TCase *tc); */
 /* void evil_test_pwd(TCase *tc); */
-/* void evil_test_stdio(TCase *tc); */
-/* void evil_test_stdlib(TCase *tc); */
+void evil_test_stdio(TCase *tc);
+void evil_test_stdlib(TCase *tc);
 /* void evil_test_string(TCase *tc); */
 /* void evil_test_time(TCase *tc); */
 /* void evil_test_unistd(TCase *tc); */
diff --git a/src/tests/evil/evil_test_stdio.c b/src/tests/evil/evil_test_stdio.c
new file mode 100644
index 0000000000..eeab2d08e5
--- /dev/null
+++ b/src/tests/evil/evil_test_stdio.c
@@ -0,0 +1,238 @@
+/* EVIL - EFL library for Windows port
+ * Copyright (C) 2017 Vincent Torri
+ *
+ * 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
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <direct.h>
+
+#include <Evil.h>
+
+#include "evil_suite.h"
+
+static int
+_evil_test_stdio_file_new(const char *n, const char *t)
+{
+  FILE *f;
+
+  f = fopen(n, "wb");
+  if (!f)
+    return 0;
+  if (fwrite(t, 1, strlen(t), f) != strlen(t))
+    {
+      fclose(f);
+      _unlink(n);
+      return 0;
+    }
+
+  fclose(f);
+
+  return 1;
+}
+
+static FILE *
+_evil_test_stdio_file_new_and_opened(const char *n, const char *t)
+{
+  FILE *f;
+
+  f = fopen(n, "wb");
+  if (!f)
+    return NULL;
+  if (fwrite(t, 1, strlen(t), f) != strlen(t))
+    {
+      fclose(f);
+      _unlink(n);
+      return NULL;
+    }
+
+  return f;
+}
+
+START_TEST(evil_stdio_rename_src_file_none)
+{
+   int res;
+
+   res = rename(NULL, NULL);
+   fail_if(res != -1);
+
+   res = rename("evil_foo.txt", NULL);
+   fail_if(res != -1);
+}
+END_TEST
+
+START_TEST(evil_stdio_rename_dst_file_none)
+{
+   int res1;
+   int res2;
+   int res3;
+   int res4;
+
+   res1 = _evil_test_stdio_file_new("foo.txt", "test file none");
+   fail_if(res1 == 0);
+
+   res1 = rename("foo.txt", NULL);
+   res2 = rename("foo.txt", "bar.txt");
+   res3 = _unlink("bar.txt");
+   res4 = _unlink("foo.txt");
+
+   fail_if(res1 == 0);
+   fail_if(res2 == -1);
+   fail_if(res3 == -1);
+   fail_if(res4 == 0);
+}
+END_TEST
+
+START_TEST(evil_stdio_rename_dst_file_exists)
+{
+   int res1;
+   int res2;
+   int res3;
+
+   res1 = _evil_test_stdio_file_new("foo.txt", "test file exists foo");
+   fail_if(res1 == 0);
+
+   res2 = _evil_test_stdio_file_new("bar.txt", "test file exists bar");
+   if (res2 == 0)
+     _unlink("foo.txt");
+
+   fail_if(res2 == 0);
+
+   res1 = rename("foo.txt", "bar.txt");
+   res2 = _unlink("bar.txt");
+   res3 = _unlink("foo.txt");
+
+   fail_if(res1 == -1);
+   fail_if(res2 == -1);
+   fail_if(res3 == 0);
+}
+END_TEST
+
+START_TEST(evil_stdio_rename_dst_file_used)
+{
+   FILE *f;
+   int res1;
+   int res2;
+   int res3;
+
+   f = _evil_test_stdio_file_new_and_opened("foo.txt", "test file used foo");
+   fail_if(f == NULL);
+
+   res1 = _evil_test_stdio_file_new("bar.txt", "test file used bar");
+   if (res1 == 0)
+     {
+        fclose(f);
+        _unlink("foo.txt");
+     }
+
+   fail_if(res1 == 0);
+
+   res1 = rename("foo.txt", "bar.txt");
+   res2 = _unlink("bar.txt");
+   fclose(f);
+   res3 = _unlink("foo.txt");
+
+   fail_if(res1 == 0);
+   fail_if(res2 == -1);
+   fail_if(res3 == -1);
+}
+END_TEST
+
+START_TEST(evil_stdio_rename_dst_file_move_to_dir)
+{
+   int res1;
+   int res2;
+   int res3;
+   int res4;
+
+   res1 = _evil_test_stdio_file_new("foo.txt", "test file move foo");
+   fail_if(res1 == 0);
+
+   res2 = _mkdir("foo_dir");
+   fail_if(res2 == -1);
+
+   res1 = rename("foo.txt", "foo_dir/bar.txt");
+   res2 = _unlink("foo_dir/bar.txt");
+   res3 = _rmdir("foo_dir");
+   res4 = _unlink("foo.txt");
+
+   fail_if(res1 == -1);
+   fail_if(res2 == -1);
+   fail_if(res3 == -1);
+   fail_if(res4 == 0);
+}
+END_TEST
+
+START_TEST(evil_stdio_rename_dst_dir_none)
+{
+   int res1;
+   int res2;
+   int res3;
+
+   res1 = _mkdir("foo_dir");
+   fail_if(res1 == -1);
+
+   res1 = rename("foo_dir", "bar_dir");
+   res2 = _rmdir("bar_dir");
+   res3 = _rmdir("foo_dir");
+
+   fail_if(res1 == -1);
+   fail_if(res2 == -1);
+   fail_if(res3 == 0);
+}
+END_TEST
+
+START_TEST(evil_stdio_rename_dst_dir_exists)
+{
+   int res1;
+   int res2;
+   int res3;
+
+   res1 = _mkdir("foo_dir");
+   fail_if(res1 == -1);
+
+   res1 = _mkdir("bar_dir");
+   if (res1 == -1)
+     _rmdir("foo_dir");
+
+   fail_if(res1 == -1);
+
+   res1 = rename("foo_dir", "bar_dir");
+   res2 = _rmdir("bar_dir");
+   res3 = _rmdir("foo_dir");
+
+   fail_if(res1 == -1);
+   fail_if(res2 == -1);
+   fail_if(res3 == 0);
+}
+END_TEST
+
+void evil_test_stdio(TCase *tc)
+{
+   tcase_add_test(tc, evil_stdio_rename_src_file_none);
+
+   tcase_add_test(tc, evil_stdio_rename_dst_file_none);
+   tcase_add_test(tc, evil_stdio_rename_dst_file_exists);
+   tcase_add_test(tc, evil_stdio_rename_dst_file_used);
+   tcase_add_test(tc, evil_stdio_rename_dst_file_move_to_dir);
+
+   tcase_add_test(tc, evil_stdio_rename_dst_dir_none);
+   tcase_add_test(tc, evil_stdio_rename_dst_dir_exists);
+}
diff --git a/src/tests/evil/evil_test_stdlib.c 
b/src/tests/evil/evil_test_stdlib.c
new file mode 100644
index 0000000000..9de3a149ff
--- /dev/null
+++ b/src/tests/evil/evil_test_stdlib.c
@@ -0,0 +1,317 @@
+/* EVIL - EFL library for Windows port
+ * Copyright (C) 2017 Vincent Torri
+ *
+ * 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
+
+#include <stdlib.h>
+
+#include <Evil.h>
+
+#include "evil_suite.h"
+
+START_TEST(evil_stdlib_setenv_NULL)
+{
+   char *val;
+   int   res;
+
+   res = setenv("EVIL_TEST_ENV", NULL, 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV");
+   fail_if(val != 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_setenv_NULL_after_set)
+{
+   char *val;
+   int   res;
+
+   res = setenv("EVIL_TEST_ENV", "val", 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV");
+   fail_if(val == 0);
+
+   fail_if(strcmp(val, "val") != 0);
+
+   res = setenv("EVIL_TEST_ENV", NULL, 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV");
+   fail_if(val != 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_getenv_one)
+{
+   char *val;
+   int   res;
+
+   res = setenv("EVIL_TEST_ENV", "val", 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV");
+   fail_if(val == 0);
+
+   fail_if(strcmp(val, "val") != 0);
+
+   res = setenv("EVIL_TEST_ENV", NULL, 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV");
+   fail_if(val != 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_getenv_two)
+{
+   char *val;
+   int   res;
+
+   res = setenv("EVIL_TEST_ENV1", "val1", 1);
+   fail_if(res < 0);
+
+   res = setenv("EVIL_TEST_ENV2", "val2", 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV1");
+   fail_if(val == 0);
+
+   fail_if(strcmp(val, "val1") != 0);
+
+   val = getenv("EVIL_TEST_ENV2");
+   fail_if(val == 0);
+
+   fail_if(strcmp(val, "val2") != 0);
+
+   res = setenv("EVIL_TEST_ENV1", NULL, 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV1");
+   fail_if(val != 0);
+
+   res = setenv("EVIL_TEST_ENV2", NULL, 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV2");
+   fail_if(val != 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_getenv_two_swapped)
+{
+   char *val;
+   int   res;
+
+   res = setenv("EVIL_TEST_ENV1", "val1", 1);
+   fail_if(res < 0);
+
+   res = setenv("EVIL_TEST_ENV2", "val2", 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV2");
+   fail_if(val == 0);
+
+   fail_if(strcmp(val, "val2") != 0);
+
+   val = getenv("EVIL_TEST_ENV1");
+   fail_if(val == 0);
+
+   fail_if(strcmp(val, "val1") != 0);
+
+   res = setenv("EVIL_TEST_ENV1", NULL, 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV1");
+   fail_if(val != 0);
+
+   res = setenv("EVIL_TEST_ENV2", NULL, 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV2");
+   fail_if(val != 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_unsetenv)
+{
+   char *val;
+   int   res;
+
+   res = setenv("EVIL_TEST_ENV", "val", 1);
+   fail_if(res < 0);
+
+   val = getenv("EVIL_TEST_ENV");
+   fail_if(val == 0);
+
+   fail_if(unsetenv("EVIL_TEST_ENV") != 0);
+
+   val = getenv("EVIL_TEST_ENV");
+   fail_if(val != 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_mkdtemp)
+{
+   char template[] = "file_XXXXXX";
+   char *res;
+
+   res = mkdtemp(template);
+   fail_if(res == NULL);
+
+   fail_if(rmdir(res) < 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_mkdtemp_fail)
+{
+   char template[] = "file_XXX";
+   char *res;
+
+   res = mkdtemp(template);
+   fail_if(res != NULL);
+}
+END_TEST
+
+START_TEST(evil_stdlib_mkstemp)
+{
+   char template[] = "file_XXXXXX";
+   int fd;
+
+   fd = mkstemp(template);
+   fail_if(fd < 0);
+
+   fail_if(close(fd) == -1);
+
+   fail_if(unlink(template) == -1);
+}
+END_TEST
+
+START_TEST(evil_stdlib_mkstemp_fail)
+{
+   char template[] = "file_XXX";
+   int fd;
+
+   fd = mkstemp(template);
+   fail_if(fd >= 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_mkstemps)
+{
+   char template[] = "file_XXXXXX.ext";
+   int fd;
+
+   fd = mkstemps(template, 4);
+   fail_if(fd < 0);
+
+   fail_if(close(fd) == -1);
+
+   fail_if(unlink(template) == -1);
+}
+END_TEST
+
+START_TEST(evil_stdlib_mkstemps_fail_1)
+{
+   char template[] = "file_XXX.ext";
+   int fd;
+
+   fd = mkstemps(template, 4);
+   fail_if(fd >= 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_mkstemps_fail_2)
+{
+   char template[] = "file_XXX";
+   int fd;
+
+   fd = mkstemps(template, 4);
+   fail_if(fd >= 0);
+}
+END_TEST
+
+START_TEST(evil_stdlib_realpath_1)
+{
+   char buf[PATH_MAX];
+   char *filename = "C:\\Windows\\System32\\kernel32.dll";
+   char *res;
+
+   res = realpath(filename, buf);
+   fail_if(res == NULL);
+}
+END_TEST
+
+START_TEST(evil_stdlib_realpath_2)
+{
+   char buf[PATH_MAX];
+   char *filename = "C:\\Windows\\System32\\.\\kernel32.dll";
+   char *res;
+
+   res = realpath(filename, buf);
+   fail_if(res == NULL);
+}
+END_TEST
+
+START_TEST(evil_stdlib_realpath_3)
+{
+   char buf[PATH_MAX];
+   char *filename = "C:\\Windows\\System32\\..\\System32\\kernel32.dll";
+   char *res;
+
+   res = realpath(filename, buf);
+   fail_if(res == NULL);
+}
+END_TEST
+
+START_TEST(evil_stdlib_realpath_fail)
+{
+   char buf[PATH_MAX];
+   char *filename = "C:\\Windows\\System32\\System32\\kernel.dll";
+   char *res;
+
+   res = realpath(filename, buf);
+   fail_if(res != NULL);
+}
+END_TEST
+
+void evil_test_stdlib(TCase *tc)
+{
+   tcase_add_test(tc, evil_stdlib_setenv_NULL);
+   tcase_add_test(tc, evil_stdlib_setenv_NULL_after_set);
+   tcase_add_test(tc, evil_stdlib_getenv_one);
+   tcase_add_test(tc, evil_stdlib_getenv_two);
+   tcase_add_test(tc, evil_stdlib_getenv_two_swapped);
+   tcase_add_test(tc, evil_stdlib_unsetenv);
+
+   tcase_add_test(tc, evil_stdlib_mkdtemp);
+   tcase_add_test(tc, evil_stdlib_mkdtemp_fail);
+   tcase_add_test(tc, evil_stdlib_mkstemp);
+   tcase_add_test(tc, evil_stdlib_mkstemp_fail);
+   tcase_add_test(tc, evil_stdlib_mkstemps);
+   tcase_add_test(tc, evil_stdlib_mkstemps_fail_1);
+   tcase_add_test(tc, evil_stdlib_mkstemps_fail_2);
+
+   tcase_add_test(tc, evil_stdlib_realpath_1);
+   tcase_add_test(tc, evil_stdlib_realpath_2);
+   tcase_add_test(tc, evil_stdlib_realpath_3);
+   tcase_add_test(tc, evil_stdlib_realpath_fail);
+}

-- 


Reply via email to