2010-04-16 Sebastien Pouliot <sebastien@ximian.com>
[mono/afaerber.git] / eglib / test / file.c
blob411c945296684b97d819f9e0affbc561dc97a01b
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"
11 #ifdef G_OS_WIN32
12 #include <io.h>
13 #define close _close
14 #endif
16 RESULT
17 test_file_get_contents ()
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
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);
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");
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);
60 return OK;
63 RESULT
64 test_open_tmp ()
66 GError *error;
67 gint fd;
68 gchar *name = GINT_TO_POINTER (-1);
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);
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);
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);
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;
109 RESULT
110 test_file ()
112 gboolean res;
113 const gchar *tmp;
114 gchar *path;
116 #ifndef G_OS_WIN32 /* FIXME */
117 gchar *sympath;
118 gint ignored;
119 #endif
121 res = g_file_test (NULL, 0);
122 if (res)
123 return FAILED ("Should return FALSE HERE");
125 res = g_file_test ("file.c", 0);
126 if (res)
127 return FAILED ("Should return FALSE HERE");
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");
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");
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");
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");
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);
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);
195 unlink (path);
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;
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}
226 DEFINE_TEST_GROUP_INIT(file_tests_init, file_tests)