efl

Форк
0
/
evil_test_stdio.c 
238 строк · 4.8 Кб
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_private.h>
28

29
#include "evil_suite.h"
30

31
static 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

51
static 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

69
EFL_START_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
}
79
EFL_END_TEST
80

81
EFL_START_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
}
101
EFL_END_TEST
102

103
EFL_START_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
}
126
EFL_END_TEST
127

128
EFL_START_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
}
156
EFL_END_TEST
157

158
EFL_START_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
}
181
EFL_END_TEST
182

183
EFL_START_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
}
200
EFL_END_TEST
201

202
EFL_START_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
}
225
EFL_END_TEST
226

227
void 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
}
239

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.