2007-04-27 Jonathan Chambers <joncham@gmail.com>
[mono.git] / eglib / test / file.c
1 #include <config.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #include <stdio.h>
9 #include "test.h"
10
11 #ifdef G_OS_WIN32
12 #include <io.h>
13 #define close _close
14 #endif
15
16 RESULT
17 test_file_get_contents ()
18 {
19         GError *error;
20         gchar *content;
21         gboolean ret;
22         gsize length;
23 #ifdef G_OS_WIN32
24         const gchar *filename = "c:\\Windows\\system.ini";
25 #else
26         const gchar *filename = "/etc/hosts";
27 #endif
28
29         /*
30         filename != NULL
31         ret = g_file_get_contents (NULL, NULL, NULL, NULL);
32         contents != NULL
33         ret = g_file_get_contents ("", NULL, NULL, NULL);
34         error no such file and fails for 'error' not being null too
35         ret = g_file_get_contents ("", &content, NULL, &error);
36         */
37
38         error = NULL;
39         ret = g_file_get_contents ("", &content, NULL, &error);
40         if (ret)
41                 return FAILED ("HAH!");
42         if (error == NULL)
43                 return FAILED ("Got nothing as error.");
44         if (content != NULL)
45                 return FAILED ("Content is uninitialized");
46
47         g_error_free (error);
48         error = NULL;
49         ret = g_file_get_contents (filename, &content, &length, &error);
50         if (!ret)
51                 return FAILED ("The error is %d %s\n", error->code, error->message);
52         if (error != NULL)
53                 return FAILED ("Got an error returning TRUE");
54         if (content == NULL)
55                 return FAILED ("Content is NULL");
56         if (strlen (content) != length)
57                 return FAILED ("length is %d but the string is %d", length, strlen (content));
58         g_free (content);
59
60         return OK;
61 }
62
63 RESULT
64 test_open_tmp ()
65 {
66         GError *error;
67         gint fd;
68         gchar *name = GINT_TO_POINTER (-1);
69
70         /*
71          * Okay, this works, but creates a .xxx file in /tmp on every run. Disabled.
72          * fd = g_file_open_tmp (NULL, NULL, NULL);
73          * if (fd < 0)
74          *      return FAILED ("Default failed.");
75          * close (fd);
76         */
77         error = NULL;
78         fd = g_file_open_tmp ("invalidtemplate", NULL, &error);
79         if (fd != -1)
80                 return FAILED ("The template was invalid and accepted");
81         if (error == NULL)
82                 return FAILED ("No error returned.");
83         g_error_free (error);
84
85         error = NULL;
86         fd = g_file_open_tmp ("i/nvalidtemplate", &name, &error);
87         if (fd != -1)
88                 return FAILED ("The template was invalid and accepted");
89         if (error == NULL)
90                 return FAILED ("No error returned.");
91         if (name == NULL)
92                 return FAILED ("'name' is not reset");
93         g_error_free (error);
94
95         error = NULL;
96         fd = g_file_open_tmp ("valid-XXXXXX", &name, &error);
97         if (fd == -1)
98                 return FAILED ("This should be valid");
99         if (error != NULL)
100                 return FAILED ("No error returned.");
101         if (name == NULL)
102                 return FAILED ("No name returned.");
103         close (fd);
104         unlink (name);
105         g_free (name);
106         return OK;
107 }
108
109 RESULT
110 test_file ()
111 {
112         gboolean res;
113         const gchar *tmp;
114         gchar *path;
115
116 #ifndef G_OS_WIN32 /* FIXME */
117         gchar *sympath;
118         gint ignored;
119 #endif
120
121         res = g_file_test (NULL, 0);
122         if (res)
123                 return FAILED ("Should return FALSE HERE");
124
125         res = g_file_test ("file.c", 0);
126         if (res)
127                 return FAILED ("Should return FALSE HERE");
128
129         tmp = g_get_tmp_dir ();
130         res = g_file_test (tmp, G_FILE_TEST_EXISTS);
131         if (!res)
132                 return FAILED ("tmp does not exist.");
133         res = g_file_test (tmp, G_FILE_TEST_IS_REGULAR);
134         if (res)
135                 return FAILED ("tmp is regular");
136
137         res = g_file_test (tmp, G_FILE_TEST_IS_DIR);
138         if (!res)
139                 return FAILED ("tmp is not a directory");
140         res = g_file_test (tmp, G_FILE_TEST_IS_EXECUTABLE);
141         if (!res)
142                 return FAILED ("tmp is not a executable");
143
144         res = g_file_test (tmp, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_SYMLINK);
145         if (!res)
146                 return FAILED ("2 tmp does not exist.");
147         res = g_file_test (tmp, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK);
148         if (res)
149                 return FAILED ("2 tmp is regular");
150
151         res = g_file_test (tmp, G_FILE_TEST_IS_DIR | G_FILE_TEST_IS_SYMLINK);
152         if (!res)
153                 return FAILED ("2 tmp is not a directory");
154         res = g_file_test (tmp, G_FILE_TEST_IS_EXECUTABLE | G_FILE_TEST_IS_SYMLINK);
155         if (!res)
156                 return FAILED ("2 tmp is not a executable");
157
158         close (g_file_open_tmp (NULL, &path, NULL)); /* create an empty file */
159         res = g_file_test (path, G_FILE_TEST_EXISTS);
160         if (!res)
161                 return FAILED ("3 %s should exist", path);
162         res = g_file_test (path, G_FILE_TEST_IS_REGULAR);
163         /* This is strange. Empty file is reported as not existing! */
164         if (!res)
165                 return FAILED ("3 %s IS_REGULAR", path);
166         res = g_file_test (path, G_FILE_TEST_IS_DIR);
167         if (res)
168                 return FAILED ("3 %s should not be a directory", path);
169         res = g_file_test (path, G_FILE_TEST_IS_EXECUTABLE);
170         if (res)
171                 return FAILED ("3 %s should not be executable", path);
172         res = g_file_test (path, G_FILE_TEST_IS_SYMLINK);
173         if (res)
174                 return FAILED ("3 %s should not be a symlink", path);
175
176 #ifndef G_OS_WIN32 /* FIXME */
177         sympath = g_strconcat (path, "-link", NULL);
178         ignored = symlink (path, sympath);
179         res = g_file_test (sympath, G_FILE_TEST_EXISTS);
180         if (!res)
181                 return FAILED ("4 %s should not exist", sympath);
182         res = g_file_test (sympath, G_FILE_TEST_IS_REGULAR);
183         if (!res)
184                 return FAILED ("4 %s should not be a regular file", sympath);
185         res = g_file_test (sympath, G_FILE_TEST_IS_DIR);
186         if (res)
187                 return FAILED ("4 %s should not be a directory", sympath);
188         res = g_file_test (sympath, G_FILE_TEST_IS_EXECUTABLE);
189         if (res)
190                 return FAILED ("4 %s should not be executable", sympath);
191         res = g_file_test (sympath, G_FILE_TEST_IS_SYMLINK);
192         if (!res)
193                 return FAILED ("4 %s should be a symlink", sympath);
194
195         unlink (path);
196
197         res = g_file_test (sympath, G_FILE_TEST_EXISTS);
198         if (res)
199                 return FAILED ("5 %s should exist", sympath);
200         res = g_file_test (sympath, G_FILE_TEST_IS_REGULAR);
201         if (res)
202                 return FAILED ("5 %s should be a regular file", sympath);
203         res = g_file_test (sympath, G_FILE_TEST_IS_DIR);
204         if (res)
205                 return FAILED ("5 %s should not be a directory", sympath);
206         res = g_file_test (sympath, G_FILE_TEST_IS_EXECUTABLE);
207         if (res)
208                 return FAILED ("5 %s should not be executable", sympath);
209         res = g_file_test (sympath, G_FILE_TEST_IS_SYMLINK);
210         if (!res)
211                 return FAILED ("5 %s should be a symlink", sympath);
212         unlink (sympath);
213         g_free (sympath);
214 #endif
215         g_free (path);
216         return OK;
217 }
218
219 static Test file_tests [] = {
220         {"g_file_get_contents", test_file_get_contents},
221         {"g_file_open_tmp", test_open_tmp},
222         {"g_file_test", test_file},
223         {NULL, NULL}
224 };
225
226 DEFINE_TEST_GROUP_INIT(file_tests_init, file_tests)
227
228