summaryrefslogtreecommitdiff
path: root/src/tests/ecore/ecore_test_ecore_file.c
blob: cd06d20c0f65e28b836639e5b17dd9cd114215f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <Ecore_File.h>
#include <Ecore.h>
#include <Eina.h>

#include "ecore_suite.h"

#define MAXSIZE 256

void
_writeToFile(char *filePath, char *text)
{
   FILE *f = fopen(filePath, "r+");
   if (f == NULL)
     f = fopen(filePath, "w");
   fail_if(f == NULL);
   fprintf(f, "%s\n", text);
   fclose(f);
}

static void
file_monitor_cb(void *data EINA_UNUSED, Ecore_File_Monitor *em EINA_UNUSED,
                Ecore_File_Event event, const char *path)
{
   switch (event)
     {
      case ECORE_FILE_EVENT_NONE:
      case ECORE_FILE_EVENT_CREATED_FILE:
         fprintf(stderr, "File created in %s", path);
         break;
      case ECORE_FILE_EVENT_DELETED_FILE:
         fprintf(stderr, "File deleted in %s", path);
         break;
      case ECORE_FILE_EVENT_MODIFIED:
         fprintf(stderr, "File modified in %s", path);
         break;
      case ECORE_FILE_EVENT_CLOSED:
         fprintf(stderr, "File closed in %s", path);
         break;
      case ECORE_FILE_EVENT_DELETED_DIRECTORY:
         fprintf(stderr, "Directory deleted in %s", path);
         break;
      case ECORE_FILE_EVENT_CREATED_DIRECTORY:
         fprintf(stderr, "Directory created in %s", path);
         break;
      case ECORE_FILE_EVENT_DELETED_SELF:
         fprintf(stderr, "Path %s deleted", path);
         break;
     }
}
 
START_TEST(ecore_test_ecore_file_init)
{
   int ret;

   ret = ecore_file_init();
   fail_if(ret != 1);

   ret = ecore_file_shutdown();
   fail_if(ret != 0);
}
END_TEST

START_TEST(ecore_test_ecore_file_operations)
{
   const char *srcDir = "/tmp/rootDir";
   const char* dirs[] = {"b", "b/c", "b/c/d", "d"}; 
   char *srcFile = "/tmp/EcoreFileSrc.txt";
   const char *destFile = "/tmp/EcoreFileDest.txt";
   char *randomText = "This is random test String";
   char dir[MAXSIZE] = {'\0'};
   unsigned int ret;
   Eina_Bool res;
   Eina_List *list;

   char unqFile[MAXSIZE], unqDir[MAXSIZE];
   char *ptr;

   ret = ecore_file_init();
   fail_if(ret != 1);
   
   ptr = tmpnam(unqFile);
   fail_if(!ptr);

   ptr = tmpnam(unqDir);
   fail_if(!ptr);

   res = ecore_file_exists(unqFile);
   fail_if(res != EINA_FALSE);

   res = ecore_file_is_dir(unqFile);
   fail_if(res != EINA_FALSE);
   res = ecore_file_is_dir(unqDir);
   fail_if(res != EINA_FALSE);
   
   res = ecore_file_mkdir(srcDir);
   fail_if(res != EINA_TRUE);
   res = ecore_file_is_dir(srcDir);
   fail_if(res != EINA_TRUE);
   ret = ecore_file_dir_is_empty(srcDir);
   fail_if(res != 1);
   res = ecore_file_remove(srcDir);
   fail_if(res != EINA_TRUE);

   ret = ecore_file_mksubdirs(srcDir, dirs);
   fail_if(ret != 4);
   list = ecore_file_ls(srcDir);
   fail_if(!list);
   fail_if(eina_list_count(list) != 2);
   res = ecore_file_recursive_rm(srcDir);
   fail_if(res != EINA_TRUE);

   strcat(dir, srcDir);
   strcat(dir, "/");
   strcat(dir, dirs[2]);
   res = ecore_file_mkpath(dir);
   fail_if(res != EINA_TRUE);
   res = ecore_file_recursive_rm(srcDir);
   fail_if(res != EINA_TRUE);

   _writeToFile(srcFile, randomText);

   ret = ecore_file_size(srcFile);
   fail_if(ret != strlen(randomText)+1);
   ret = ecore_file_is_dir(srcFile);
   fail_if(ret != EINA_FALSE);

   res = ecore_file_cp(srcFile, destFile);
   fail_if(res != EINA_TRUE);
   res = ecore_file_exists(destFile);
   fail_if(res != EINA_TRUE);
   fail_if(ecore_file_size(srcFile) != ecore_file_size(destFile));
   res = ecore_file_remove(destFile);
   fail_if(res != EINA_TRUE);
  
   res = ecore_file_symlink(srcFile, destFile);
   fail_if(res != EINA_TRUE);
   ck_assert_str_eq(ecore_file_readlink(destFile), srcFile);
   ck_assert_str_eq(ecore_file_realpath(destFile), srcFile);
   res = ecore_file_unlink(destFile);
   fail_if(res != EINA_TRUE);

   res = ecore_file_mv(srcFile, destFile);
   fail_if(res != EINA_TRUE);
   res = ecore_file_exists(destFile);
   fail_if(res != EINA_TRUE);
   res = ecore_file_exists(srcFile);
   fail_if(res != EINA_FALSE);

   ck_assert_str_eq(ecore_file_file_get(destFile), "EcoreFileDest.txt");
   ck_assert_str_eq(ecore_file_dir_get(destFile), "/tmp");
   ck_assert_str_eq(ecore_file_realpath(destFile), destFile);
   fail_if(ecore_file_mod_time(destFile) == 0);
   fail_if(ecore_file_can_read(destFile) != EINA_TRUE);
   fail_if(ecore_file_can_write(destFile) != EINA_TRUE);
   fail_if(ecore_file_can_exec(destFile) != EINA_FALSE);
   res = ecore_file_remove(destFile);
   fail_if(res != EINA_TRUE);
 
   ret = ecore_file_shutdown();
   fail_if(ret != 0);

}
END_TEST

START_TEST(ecore_test_ecore_file_monitor)
{
   Ecore_File_Monitor *mon;
   char *path = "/tmp/rootDir/";
   char *file = "/tmp/rootDir/EcoreFileDest.txt";
   const char *subDir[] = {"subdir"};
   char dirName[MAXSIZE] = {'\0'};
   char *randomText = "This is random test String";
   char *realp;
   Eina_Bool res;
   int ret;

   ret = ecore_file_init();
   fail_if(ret != 1);

   res = ecore_file_mkdir(path);
   fail_if(res != EINA_TRUE);

   realp = ecore_file_realpath(path);
   fail_if(!realp);
   mon = ecore_file_monitor_add(realp, file_monitor_cb, NULL);
   fail_if(mon == NULL);

   _writeToFile(file, randomText);
   _writeToFile(file, randomText);

   ck_assert_str_eq(ecore_file_monitor_path_get(mon), realp);

   ret = ecore_file_mksubdirs(path, subDir);
   fail_if(ret != 1);

   res = ecore_file_remove(file);
   fail_if(res != EINA_TRUE);

   strcat(dirName, path);
   strcat(dirName, subDir[0]);
   res = ecore_file_rmdir(dirName);
   fail_if(res != EINA_TRUE);
   
   res = ecore_file_recursive_rm(path);
   fail_if(res != EINA_TRUE);

   ecore_file_monitor_del(mon);
   
   ret = ecore_file_shutdown();
   fail_if(ret != 0);

}
END_TEST

void ecore_test_ecore_file(TCase *tc)
{
   tcase_add_test(tc, ecore_test_ecore_file_init);
   tcase_add_test(tc, ecore_test_ecore_file_operations);
   tcase_add_test(tc, ecore_test_ecore_file_monitor);
}