summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2017-11-10 13:13:06 -0800
committerCedric BAIL <cedric@osg.samsung.com>2017-11-10 13:13:44 -0800
commitb5333edd3ffe8c0379170a0e9d08ee05af439e2c (patch)
tree2baf5d8bf4e1ccecb5576a0f18a1be6f252f698e
parentde9cf5a217557a62fb33176492218eb2f8e37e92 (diff)
evil: add tests for Windows port.
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
-rw-r--r--src/Makefile_Evil.am4
-rw-r--r--src/lib/evil/evil_stdio.c34
-rw-r--r--src/tests/evil/evil_suite.c4
-rw-r--r--src/tests/evil/evil_suite.h4
-rw-r--r--src/tests/evil/evil_test_stdio.c238
-rw-r--r--src/tests/evil/evil_test_stdlib.c317
6 files changed, 576 insertions, 25 deletions
diff --git a/src/Makefile_Evil.am b/src/Makefile_Evil.am
index 7aff77d..f93b226 100644
--- a/src/Makefile_Evil.am
+++ b/src/Makefile_Evil.am
@@ -126,7 +126,9 @@ tests_evil_evil_suite_SOURCES = \
126tests/evil/evil_suite.c \ 126tests/evil/evil_suite.c \
127tests/evil/evil_suite.h \ 127tests/evil/evil_suite.h \
128tests/evil/evil_test_dlfcn.c \ 128tests/evil/evil_test_dlfcn.c \
129tests/evil/evil_test_main.c 129tests/evil/evil_test_main.c \
130tests/evil/evil_test_stdio.c \
131tests/evil/evil_test_stdlib.c
130 132
131tests_evil_evil_suite_CPPFLAGS = \ 133tests_evil_evil_suite_CPPFLAGS = \
132-I$(top_builddir)/src/lib/efl \ 134-I$(top_builddir)/src/lib/efl \
diff --git a/src/lib/evil/evil_stdio.c b/src/lib/evil/evil_stdio.c
index 5919d0c..6b71e92 100644
--- a/src/lib/evil/evil_stdio.c
+++ b/src/lib/evil/evil_stdio.c
@@ -2,9 +2,14 @@
2# include "config.h" 2# include "config.h"
3#endif /* HAVE_CONFIG_H */ 3#endif /* HAVE_CONFIG_H */
4 4
5#include <direct.h>
6#include <sys/stat.h>
7#include <sys/types.h> 5#include <sys/types.h>
6#include <direct.h>
7
8#ifndef WIN32_LEAN_AND_MEAN
9# define WIN32_LEAN_AND_MEAN
10#endif
11#include <windows.h>
12#undef WIN32_LEAN_AND_MEAN
8 13
9#include "evil_macro.h" 14#include "evil_macro.h"
10#include "evil_stdio.h" 15#include "evil_stdio.h"
@@ -12,30 +17,19 @@
12 17
13#undef rename 18#undef rename
14 19
15int 20int
16evil_rename(const char *src, const char* dst) 21evil_rename(const char *src, const char* dst)
17{ 22{
18 struct stat st; 23 DWORD res;
19
20 if (stat(dst, &st) < 0)
21 return rename(src, dst);
22
23 if (stat(src, &st) < 0)
24 return -1;
25
26 if (S_ISDIR(st.st_mode))
27 {
28 rmdir(dst);
29 return rename(src, dst);
30 }
31 24
32 if (S_ISREG(st.st_mode)) 25 res = GetFileAttributes(dst);
26 if ((res != 0xffffffff) && (res & FILE_ATTRIBUTE_DIRECTORY))
33 { 27 {
34 unlink(dst); 28 if (!RemoveDirectory(dst))
35 return rename(src, dst); 29 return -1;
36 } 30 }
37 31
38 return -1; 32 return MoveFileEx(src, dst, MOVEFILE_REPLACE_EXISTING) ? 0 : -1;
39} 33}
40 34
41int 35int
diff --git a/src/tests/evil/evil_suite.c b/src/tests/evil/evil_suite.c
index aeae25e..d8fc492 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[] = {
37 { "Main", evil_test_main }, 37 { "Main", evil_test_main },
38 /* { "Mman", evil_test_mman }, */ 38 /* { "Mman", evil_test_mman }, */
39 /* { "Pwd", evil_test_pwd }, */ 39 /* { "Pwd", evil_test_pwd }, */
40 /* { "Stdio", evil_test_stdio }, */ 40 { "Stdio", evil_test_stdio },
41 /* { "Stdlib", evil_test_stdlib }, */ 41 { "Stdlib", evil_test_stdlib },
42 /* { "String", evil_test_string }, */ 42 /* { "String", evil_test_string }, */
43 /* { "Time", evil_test_time }, */ 43 /* { "Time", evil_test_time }, */
44 /* { "Unistd", evil_test_unistd }, */ 44 /* { "Unistd", evil_test_unistd }, */
diff --git a/src/tests/evil/evil_suite.h b/src/tests/evil/evil_suite.h
index 17222fc..c7220e0 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);
31void evil_test_main(TCase *tc); 31void evil_test_main(TCase *tc);
32/* void evil_test_mman(TCase *tc); */ 32/* void evil_test_mman(TCase *tc); */
33/* void evil_test_pwd(TCase *tc); */ 33/* void evil_test_pwd(TCase *tc); */
34/* void evil_test_stdio(TCase *tc); */ 34void evil_test_stdio(TCase *tc);
35/* void evil_test_stdlib(TCase *tc); */ 35void evil_test_stdlib(TCase *tc);
36/* void evil_test_string(TCase *tc); */ 36/* void evil_test_string(TCase *tc); */
37/* void evil_test_time(TCase *tc); */ 37/* void evil_test_time(TCase *tc); */
38/* void evil_test_unistd(TCase *tc); */ 38/* 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 0000000..eeab2d0
--- /dev/null
+++ b/src/tests/evil/evil_test_stdio.c
@@ -0,0 +1,238 @@
1/* EVIL - EFL library for Windows port
2 * Copyright (C) 2017 Vincent Torri
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <stdlib.h>
24#include <stdio.h>
25#include <direct.h>
26
27#include <Evil.h>
28
29#include "evil_suite.h"
30
31static int
32_evil_test_stdio_file_new(const char *n, const char *t)
33{
34 FILE *f;
35
36 f = fopen(n, "wb");
37 if (!f)
38 return 0;
39 if (fwrite(t, 1, strlen(t), f) != strlen(t))
40 {
41 fclose(f);
42 _unlink(n);
43 return 0;
44 }
45
46 fclose(f);
47
48 return 1;
49}
50
51static FILE *
52_evil_test_stdio_file_new_and_opened(const char *n, const char *t)
53{
54 FILE *f;
55
56 f = fopen(n, "wb");
57 if (!f)
58 return NULL;
59 if (fwrite(t, 1, strlen(t), f) != strlen(t))
60 {
61 fclose(f);
62 _unlink(n);
63 return NULL;
64 }
65
66 return f;
67}
68
69START_TEST(evil_stdio_rename_src_file_none)
70{
71 int res;
72
73 res = rename(NULL, NULL);
74 fail_if(res != -1);
75
76 res = rename("evil_foo.txt", NULL);
77 fail_if(res != -1);
78}
79END_TEST
80
81START_TEST(evil_stdio_rename_dst_file_none)
82{
83 int res1;
84 int res2;
85 int res3;
86 int res4;
87
88 res1 = _evil_test_stdio_file_new("foo.txt", "test file none");
89 fail_if(res1 == 0);
90
91 res1 = rename("foo.txt", NULL);
92 res2 = rename("foo.txt", "bar.txt");
93 res3 = _unlink("bar.txt");
94 res4 = _unlink("foo.txt");
95
96 fail_if(res1 == 0);
97 fail_if(res2 == -1);
98 fail_if(res3 == -1);
99 fail_if(res4 == 0);
100}
101END_TEST
102
103START_TEST(evil_stdio_rename_dst_file_exists)
104{
105 int res1;
106 int res2;
107 int res3;
108
109 res1 = _evil_test_stdio_file_new("foo.txt", "test file exists foo");
110 fail_if(res1 == 0);
111
112 res2 = _evil_test_stdio_file_new("bar.txt", "test file exists bar");
113 if (res2 == 0)
114 _unlink("foo.txt");
115
116 fail_if(res2 == 0);
117
118 res1 = rename("foo.txt", "bar.txt");
119 res2 = _unlink("bar.txt");
120 res3 = _unlink("foo.txt");
121
122 fail_if(res1 == -1);
123 fail_if(res2 == -1);
124 fail_if(res3 == 0);
125}
126END_TEST
127
128START_TEST(evil_stdio_rename_dst_file_used)
129{
130 FILE *f;
131 int res1;
132 int res2;
133 int res3;
134
135 f = _evil_test_stdio_file_new_and_opened("foo.txt", "test file used foo");
136 fail_if(f == NULL);
137
138 res1 = _evil_test_stdio_file_new("bar.txt", "test file used bar");
139 if (res1 == 0)
140 {
141 fclose(f);
142 _unlink("foo.txt");
143 }
144
145 fail_if(res1 == 0);
146
147 res1 = rename("foo.txt", "bar.txt");
148 res2 = _unlink("bar.txt");
149 fclose(f);
150 res3 = _unlink("foo.txt");
151
152 fail_if(res1 == 0);
153 fail_if(res2 == -1);
154 fail_if(res3 == -1);
155}
156END_TEST
157
158START_TEST(evil_stdio_rename_dst_file_move_to_dir)
159{
160 int res1;
161 int res2;
162 int res3;
163 int res4;
164
165 res1 = _evil_test_stdio_file_new("foo.txt", "test file move foo");
166 fail_if(res1 == 0);
167
168 res2 = _mkdir("foo_dir");
169 fail_if(res2 == -1);
170
171 res1 = rename("foo.txt", "foo_dir/bar.txt");
172 res2 = _unlink("foo_dir/bar.txt");
173 res3 = _rmdir("foo_dir");
174 res4 = _unlink("foo.txt");
175
176 fail_if(res1 == -1);
177 fail_if(res2 == -1);
178 fail_if(res3 == -1);
179 fail_if(res4 == 0);
180}
181END_TEST
182
183START_TEST(evil_stdio_rename_dst_dir_none)
184{
185 int res1;
186 int res2;
187 int res3;
188
189 res1 = _mkdir("foo_dir");
190 fail_if(res1 == -1);
191
192 res1 = rename("foo_dir", "bar_dir");
193 res2 = _rmdir("bar_dir");
194 res3 = _rmdir("foo_dir");
195
196 fail_if(res1 == -1);
197 fail_if(res2 == -1);
198 fail_if(res3 == 0);
199}
200END_TEST
201
202START_TEST(evil_stdio_rename_dst_dir_exists)
203{
204 int res1;
205 int res2;
206 int res3;
207
208 res1 = _mkdir("foo_dir");
209 fail_if(res1 == -1);
210
211 res1 = _mkdir("bar_dir");
212 if (res1 == -1)
213 _rmdir("foo_dir");
214
215 fail_if(res1 == -1);
216
217 res1 = rename("foo_dir", "bar_dir");
218 res2 = _rmdir("bar_dir");
219 res3 = _rmdir("foo_dir");
220
221 fail_if(res1 == -1);
222 fail_if(res2 == -1);
223 fail_if(res3 == 0);
224}
225END_TEST
226
227void evil_test_stdio(TCase *tc)
228{
229 tcase_add_test(tc, evil_stdio_rename_src_file_none);
230
231 tcase_add_test(tc, evil_stdio_rename_dst_file_none);
232 tcase_add_test(tc, evil_stdio_rename_dst_file_exists);
233 tcase_add_test(tc, evil_stdio_rename_dst_file_used);
234 tcase_add_test(tc, evil_stdio_rename_dst_file_move_to_dir);
235
236 tcase_add_test(tc, evil_stdio_rename_dst_dir_none);
237 tcase_add_test(tc, evil_stdio_rename_dst_dir_exists);
238}
diff --git a/src/tests/evil/evil_test_stdlib.c b/src/tests/evil/evil_test_stdlib.c
new file mode 100644
index 0000000..9de3a14
--- /dev/null
+++ b/src/tests/evil/evil_test_stdlib.c
@@ -0,0 +1,317 @@
1/* EVIL - EFL library for Windows port
2 * Copyright (C) 2017 Vincent Torri
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <stdlib.h>
24
25#include <Evil.h>
26
27#include "evil_suite.h"
28
29START_TEST(evil_stdlib_setenv_NULL)
30{
31 char *val;
32 int res;
33
34 res = setenv("EVIL_TEST_ENV", NULL, 1);
35 fail_if(res < 0);
36
37 val = getenv("EVIL_TEST_ENV");
38 fail_if(val != 0);
39}
40END_TEST
41
42START_TEST(evil_stdlib_setenv_NULL_after_set)
43{
44 char *val;
45 int res;
46
47 res = setenv("EVIL_TEST_ENV", "val", 1);
48 fail_if(res < 0);
49
50 val = getenv("EVIL_TEST_ENV");
51 fail_if(val == 0);
52
53 fail_if(strcmp(val, "val") != 0);
54
55 res = setenv("EVIL_TEST_ENV", NULL, 1);
56 fail_if(res < 0);
57
58 val = getenv("EVIL_TEST_ENV");
59 fail_if(val != 0);
60}
61END_TEST
62
63START_TEST(evil_stdlib_getenv_one)
64{
65 char *val;
66 int res;
67
68 res = setenv("EVIL_TEST_ENV", "val", 1);
69 fail_if(res < 0);
70
71 val = getenv("EVIL_TEST_ENV");
72 fail_if(val == 0);
73
74 fail_if(strcmp(val, "val") != 0);
75
76 res = setenv("EVIL_TEST_ENV", NULL, 1);
77 fail_if(res < 0);
78
79 val = getenv("EVIL_TEST_ENV");
80 fail_if(val != 0);
81}
82END_TEST
83
84START_TEST(evil_stdlib_getenv_two)
85{
86 char *val;
87 int res;
88
89 res = setenv("EVIL_TEST_ENV1", "val1", 1);
90 fail_if(res < 0);
91
92 res = setenv("EVIL_TEST_ENV2", "val2", 1);
93 fail_if(res < 0);
94
95 val = getenv("EVIL_TEST_ENV1");
96 fail_if(val == 0);
97
98 fail_if(strcmp(val, "val1") != 0);
99
100 val = getenv("EVIL_TEST_ENV2");
101 fail_if(val == 0);
102
103 fail_if(strcmp(val, "val2") != 0);
104
105 res = setenv("EVIL_TEST_ENV1", NULL, 1);
106 fail_if(res < 0);
107
108 val = getenv("EVIL_TEST_ENV1");
109 fail_if(val != 0);
110
111 res = setenv("EVIL_TEST_ENV2", NULL, 1);
112 fail_if(res < 0);
113
114 val = getenv("EVIL_TEST_ENV2");
115 fail_if(val != 0);
116}
117END_TEST
118
119START_TEST(evil_stdlib_getenv_two_swapped)
120{
121 char *val;
122 int res;
123
124 res = setenv("EVIL_TEST_ENV1", "val1", 1);
125 fail_if(res < 0);
126
127 res = setenv("EVIL_TEST_ENV2", "val2", 1);
128 fail_if(res < 0);
129
130 val = getenv("EVIL_TEST_ENV2");
131 fail_if(val == 0);
132
133 fail_if(strcmp(val, "val2") != 0);
134
135 val = getenv("EVIL_TEST_ENV1");
136 fail_if(val == 0);
137
138 fail_if(strcmp(val, "val1") != 0);
139
140 res = setenv("EVIL_TEST_ENV1", NULL, 1);
141 fail_if(res < 0);
142
143 val = getenv("EVIL_TEST_ENV1");
144 fail_if(val != 0);
145
146 res = setenv("EVIL_TEST_ENV2", NULL, 1);
147 fail_if(res < 0);
148
149 val = getenv("EVIL_TEST_ENV2");
150 fail_if(val != 0);
151}
152END_TEST
153
154START_TEST(evil_stdlib_unsetenv)
155{
156 char *val;
157 int res;
158
159 res = setenv("EVIL_TEST_ENV", "val", 1);
160 fail_if(res < 0);
161
162 val = getenv("EVIL_TEST_ENV");
163 fail_if(val == 0);
164
165 fail_if(unsetenv("EVIL_TEST_ENV") != 0);
166
167 val = getenv("EVIL_TEST_ENV");
168 fail_if(val != 0);
169}
170END_TEST
171
172START_TEST(evil_stdlib_mkdtemp)
173{
174 char template[] = "file_XXXXXX";
175 char *res;
176
177 res = mkdtemp(template);
178 fail_if(res == NULL);
179
180 fail_if(rmdir(res) < 0);
181}
182END_TEST
183
184START_TEST(evil_stdlib_mkdtemp_fail)
185{
186 char template[] = "file_XXX";
187 char *res;
188
189 res = mkdtemp(template);
190 fail_if(res != NULL);
191}
192END_TEST
193
194START_TEST(evil_stdlib_mkstemp)
195{
196 char template[] = "file_XXXXXX";
197 int fd;
198
199 fd = mkstemp(template);
200 fail_if(fd < 0);
201
202 fail_if(close(fd) == -1);
203
204 fail_if(unlink(template) == -1);
205}
206END_TEST
207
208START_TEST(evil_stdlib_mkstemp_fail)
209{
210 char template[] = "file_XXX";
211 int fd;
212
213 fd = mkstemp(template);
214 fail_if(fd >= 0);
215}
216END_TEST
217
218START_TEST(evil_stdlib_mkstemps)
219{
220 char template[] = "file_XXXXXX.ext";
221 int fd;
222
223 fd = mkstemps(template, 4);
224 fail_if(fd < 0);
225
226 fail_if(close(fd) == -1);
227
228 fail_if(unlink(template) == -1);
229}
230END_TEST
231
232START_TEST(evil_stdlib_mkstemps_fail_1)
233{
234 char template[] = "file_XXX.ext";
235 int fd;
236
237 fd = mkstemps(template, 4);
238 fail_if(fd >= 0);
239}
240END_TEST
241
242START_TEST(evil_stdlib_mkstemps_fail_2)
243{
244 char template[] = "file_XXX";
245 int fd;
246
247 fd = mkstemps(template, 4);
248 fail_if(fd >= 0);
249}
250END_TEST
251
252START_TEST(evil_stdlib_realpath_1)
253{
254 char buf[PATH_MAX];
255 char *filename = "C:\\Windows\\System32\\kernel32.dll";
256 char *res;
257
258 res = realpath(filename, buf);
259 fail_if(res == NULL);
260}
261END_TEST
262
263START_TEST(evil_stdlib_realpath_2)
264{
265 char buf[PATH_MAX];
266 char *filename = "C:\\Windows\\System32\\.\\kernel32.dll";
267 char *res;
268
269 res = realpath(filename, buf);
270 fail_if(res == NULL);
271}
272END_TEST
273
274START_TEST(evil_stdlib_realpath_3)
275{
276 char buf[PATH_MAX];
277 char *filename = "C:\\Windows\\System32\\..\\System32\\kernel32.dll";
278 char *res;
279
280 res = realpath(filename, buf);
281 fail_if(res == NULL);
282}
283END_TEST
284
285START_TEST(evil_stdlib_realpath_fail)
286{
287 char buf[PATH_MAX];
288 char *filename = "C:\\Windows\\System32\\System32\\kernel.dll";
289 char *res;
290
291 res = realpath(filename, buf);
292 fail_if(res != NULL);
293}
294END_TEST
295
296void evil_test_stdlib(TCase *tc)
297{
298 tcase_add_test(tc, evil_stdlib_setenv_NULL);
299 tcase_add_test(tc, evil_stdlib_setenv_NULL_after_set);
300 tcase_add_test(tc, evil_stdlib_getenv_one);
301 tcase_add_test(tc, evil_stdlib_getenv_two);
302 tcase_add_test(tc, evil_stdlib_getenv_two_swapped);
303 tcase_add_test(tc, evil_stdlib_unsetenv);
304
305 tcase_add_test(tc, evil_stdlib_mkdtemp);
306 tcase_add_test(tc, evil_stdlib_mkdtemp_fail);
307 tcase_add_test(tc, evil_stdlib_mkstemp);
308 tcase_add_test(tc, evil_stdlib_mkstemp_fail);
309 tcase_add_test(tc, evil_stdlib_mkstemps);
310 tcase_add_test(tc, evil_stdlib_mkstemps_fail_1);
311 tcase_add_test(tc, evil_stdlib_mkstemps_fail_2);
312
313 tcase_add_test(tc, evil_stdlib_realpath_1);
314 tcase_add_test(tc, evil_stdlib_realpath_2);
315 tcase_add_test(tc, evil_stdlib_realpath_3);
316 tcase_add_test(tc, evil_stdlib_realpath_fail);
317}