file.c 6.0 KB

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