path.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <config.h>
  2. #include <glib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #ifdef HAVE_UNISTD_H
  6. #include <unistd.h>
  7. #endif
  8. #ifdef G_OS_UNIX
  9. #include <pthread.h>
  10. #endif
  11. #include "test.h"
  12. #ifdef G_OS_WIN32
  13. #include <direct.h>
  14. #define chdir _chdir
  15. #endif
  16. /* This test is just to be used with valgrind */
  17. RESULT
  18. test_buildpath ()
  19. {
  20. char *s;
  21. char *buffer = "var/private";
  22. char *dir = "/";
  23. s = g_build_path ("/", "hola///", "//mundo", NULL);
  24. if (strcmp (s, "hola/mundo") != 0)
  25. return FAILED ("1 Got wrong result, got: %s", s);
  26. g_free (s);
  27. s = g_build_path ("/", "hola/", "/mundo", NULL);
  28. if (strcmp (s, "hola/mundo") != 0)
  29. return FAILED ("2 Got wrong result, got: %s", s);
  30. g_free (s);
  31. s = g_build_path ("/", "hola/", "mundo", NULL);
  32. if (strcmp (s, "hola/mundo") != 0)
  33. return FAILED ("3 Got wrong result, got: %s", s);
  34. g_free (s);
  35. s = g_build_path ("/", "hola", "/mundo", NULL);
  36. if (strcmp (s, "hola/mundo") != 0)
  37. return FAILED ("4 Got wrong result, got: %s", s);
  38. g_free (s);
  39. s = g_build_path ("/", "/hello", "world/", NULL);
  40. if (strcmp (s, "/hello/world/") != 0)
  41. return FAILED ("5 Got wrong result, got: %s", s);
  42. g_free (s);
  43. /* Now test multi-char-separators */
  44. s = g_build_path ("**", "hello", "world", NULL);
  45. if (strcmp (s, "hello**world") != 0)
  46. return FAILED ("6 Got wrong result, got: %s", s);
  47. g_free (s);
  48. s = g_build_path ("**", "hello**", "world", NULL);
  49. if (strcmp (s, "hello**world") != 0)
  50. return FAILED ("7 Got wrong result, got: %s", s);
  51. g_free (s);
  52. s = g_build_path ("**", "hello**", "**world", NULL);
  53. if (strcmp (s, "hello**world") != 0)
  54. return FAILED ("8 Got wrong result, got: %s", s);
  55. g_free (s);
  56. s = g_build_path ("**", "hello**", "**world", NULL);
  57. if (strcmp (s, "hello**world") != 0)
  58. return FAILED ("9 Got wrong result, got: %s", s);
  59. g_free (s);
  60. s = g_build_path ("1234567890", "hello", "world", NULL);
  61. if (strcmp (s, "hello1234567890world") != 0)
  62. return FAILED ("10 Got wrong result, got: %s", s);
  63. g_free (s);
  64. s = g_build_path ("1234567890", "hello1234567890", "1234567890world", NULL);
  65. if (strcmp (s, "hello1234567890world") != 0)
  66. return FAILED ("11 Got wrong result, got: %s", s);
  67. g_free (s);
  68. s = g_build_path ("1234567890", "hello12345678901234567890", "1234567890world", NULL);
  69. if (strcmp (s, "hello1234567890world") != 0)
  70. return FAILED ("12 Got wrong result, got: %s", s);
  71. g_free (s);
  72. /* Multiple */
  73. s = g_build_path ("/", "a", "b", "c", "d", NULL);
  74. if (strcmp (s, "a/b/c/d") != 0)
  75. return FAILED ("13 Got wrong result, got: %s", s);
  76. g_free (s);
  77. s = g_build_path ("/", "/a", "", "/c/", NULL);
  78. if (strcmp (s, "/a/c/") != 0)
  79. return FAILED ("14 Got wrong result, got: %s", s);
  80. g_free (s);
  81. /* Null */
  82. s = g_build_path ("/", NULL, NULL);
  83. if (s == NULL)
  84. return FAILED ("must get a non-NULL return");
  85. if (s [0] != 0)
  86. return FAILED ("must get an empty string");
  87. // This is to test the regression introduced by Levi for the Windows support
  88. // that code errouneously read below the allowed area (in this case dir [-1]).
  89. // and caused all kinds of random errors.
  90. dir = "//";
  91. dir++;
  92. s = g_build_filename (dir, buffer, NULL);
  93. if (s [0] != '/')
  94. return FAILED ("Must have a '/' at the start");
  95. g_free (s);
  96. return OK;
  97. }
  98. RESULT
  99. test_buildfname ()
  100. {
  101. char *s;
  102. s = g_build_filename ("a", "b", "c", "d", NULL);
  103. #ifdef G_OS_WIN32
  104. if (strcmp (s, "a\\b\\c\\d") != 0)
  105. #else
  106. if (strcmp (s, "a/b/c/d") != 0)
  107. #endif
  108. return FAILED ("1 Got wrong result, got: %s", s);
  109. g_free (s);
  110. #ifdef G_OS_WIN32
  111. s = g_build_filename ("C:\\", "a", NULL);
  112. if (strcmp (s, "C:\\a") != 0)
  113. #else
  114. s = g_build_filename ("/", "a", NULL);
  115. if (strcmp (s, "/a") != 0)
  116. #endif
  117. return FAILED ("1 Got wrong result, got: %s", s);
  118. #ifndef G_OS_WIN32
  119. s = g_build_filename ("/", "foo", "/bar", "tolo/", "/meo/", NULL);
  120. if (strcmp (s, "/foo/bar/tolo/meo/") != 0)
  121. return FAILED ("1 Got wrong result, got: %s", s);
  122. #endif
  123. return OK;
  124. }
  125. char *
  126. test_dirname ()
  127. {
  128. char *s;
  129. #ifdef G_OS_WIN32
  130. s = g_path_get_dirname ("c:\\home\\miguel");
  131. if (strcmp (s, "c:\\home") != 0)
  132. return FAILED ("Expected c:\\home, got %s", s);
  133. g_free (s);
  134. s = g_path_get_dirname ("c:/home/miguel");
  135. if (strcmp (s, "c:/home") != 0)
  136. return FAILED ("Expected c:/home, got %s", s);
  137. g_free (s);
  138. s = g_path_get_dirname ("c:\\home\\dingus\\");
  139. if (strcmp (s, "c:\\home\\dingus") != 0)
  140. return FAILED ("Expected c:\\home\\dingus, got %s", s);
  141. g_free (s);
  142. s = g_path_get_dirname ("dir.c");
  143. if (strcmp (s, ".") != 0)
  144. return FAILED ("Expected `.', got %s", s);
  145. g_free (s);
  146. s = g_path_get_dirname ("c:\\index.html");
  147. if (strcmp (s, "c:") != 0)
  148. return FAILED ("Expected [c:], got [%s]", s);
  149. #else
  150. s = g_path_get_dirname ("/home/miguel");
  151. if (strcmp (s, "/home") != 0)
  152. return FAILED ("Expected /home, got %s", s);
  153. g_free (s);
  154. s = g_path_get_dirname ("/home/dingus/");
  155. if (strcmp (s, "/home/dingus") != 0)
  156. return FAILED ("Expected /home/dingus, got %s", s);
  157. g_free (s);
  158. s = g_path_get_dirname ("dir.c");
  159. if (strcmp (s, ".") != 0)
  160. return FAILED ("Expected `.', got %s", s);
  161. g_free (s);
  162. s = g_path_get_dirname ("/index.html");
  163. if (strcmp (s, "/") != 0)
  164. return FAILED ("Expected [/], got [%s]", s);
  165. #endif
  166. return OK;
  167. }
  168. char *
  169. test_basename ()
  170. {
  171. char *s;
  172. #ifdef G_OS_WIN32
  173. s = g_path_get_basename ("");
  174. if (strcmp (s, ".") != 0)
  175. return FAILED ("Expected `.', got %s", s);
  176. g_free (s);
  177. s = g_path_get_basename ("c:\\home\\dingus\\");
  178. if (strcmp (s, "dingus") != 0)
  179. return FAILED ("1 Expected dingus, got %s", s);
  180. g_free (s);
  181. s = g_path_get_basename ("c:/home/dingus/");
  182. if (strcmp (s, "dingus") != 0)
  183. return FAILED ("1 Expected dingus, got %s", s);
  184. g_free (s);
  185. s = g_path_get_basename ("c:\\home\\dingus");
  186. if (strcmp (s, "dingus") != 0)
  187. return FAILED ("2 Expected dingus, got %s", s);
  188. g_free (s);
  189. s = g_path_get_basename ("c:/home/dingus");
  190. if (strcmp (s, "dingus") != 0)
  191. return FAILED ("2 Expected dingus, got %s", s);
  192. g_free (s);
  193. #else
  194. s = g_path_get_basename ("");
  195. if (strcmp (s, ".") != 0)
  196. return FAILED ("Expected `.', got %s", s);
  197. g_free (s);
  198. s = g_path_get_basename ("/home/dingus/");
  199. if (strcmp (s, "dingus") != 0)
  200. return FAILED ("1 Expected dingus, got %s", s);
  201. g_free (s);
  202. s = g_path_get_basename ("/home/dingus");
  203. if (strcmp (s, "dingus") != 0)
  204. return FAILED ("2 Expected dingus, got %s", s);
  205. g_free (s);
  206. #endif
  207. return OK;
  208. }
  209. gchar *
  210. test_ppath ()
  211. {
  212. char *s;
  213. #ifdef G_OS_WIN32
  214. const gchar *searchfor = "explorer.exe";
  215. #else
  216. const gchar *searchfor = "ls";
  217. #endif
  218. s = g_find_program_in_path (searchfor);
  219. if (s == NULL)
  220. return FAILED ("No %s on this system?", searchfor);
  221. g_free (s);
  222. return OK;
  223. }
  224. gchar *
  225. test_ppath2 ()
  226. {
  227. char *s;
  228. const char *path = g_getenv ("PATH");
  229. #ifdef G_OS_WIN32
  230. const gchar *searchfor = "test_eglib.exe";
  231. #else
  232. const gchar *searchfor = "test-glib";
  233. #endif
  234. g_setenv ("PATH", "", TRUE);
  235. s = g_find_program_in_path ("ls");
  236. if (s != NULL) {
  237. g_setenv ("PATH", path, TRUE);
  238. return FAILED ("Found something interesting here: %s", s);
  239. }
  240. g_free (s);
  241. s = g_find_program_in_path (searchfor);
  242. if (s == NULL) {
  243. g_setenv ("PATH", path, TRUE);
  244. return FAILED ("It should find '%s' in the current directory.", searchfor);
  245. }
  246. g_free (s);
  247. g_setenv ("PATH", path, TRUE);
  248. return OK;
  249. }
  250. #ifndef DISABLE_FILESYSTEM_TESTS
  251. gchar *
  252. test_cwd ()
  253. {
  254. char *dir = g_get_current_dir ();
  255. #ifdef G_OS_WIN32
  256. const gchar *newdir = "C:\\Windows";
  257. #else
  258. const gchar *newdir = "/bin";
  259. #endif
  260. if (dir == NULL)
  261. return FAILED ("No current directory?");
  262. g_free (dir);
  263. if (chdir (newdir) == -1)
  264. return FAILED ("No %s?", newdir);
  265. dir = g_get_current_dir ();
  266. if (strcmp (dir, newdir) != 0)
  267. return FAILED("Did not go to %s?", newdir);
  268. g_free (dir);
  269. return OK;
  270. }
  271. #else
  272. gchar *
  273. test_cwd ()
  274. {
  275. return OK;
  276. }
  277. #endif
  278. gchar *
  279. test_misc ()
  280. {
  281. const char *home = g_get_home_dir ();
  282. const char *tmp = g_get_tmp_dir ();
  283. if (home == NULL)
  284. return FAILED ("Where did my home go?");
  285. if (tmp == NULL)
  286. return FAILED ("Where did my /tmp go?");
  287. return OK;
  288. }
  289. static Test path_tests [] = {
  290. {"g_build_filename", test_buildfname},
  291. {"g_buildpath", test_buildpath},
  292. {"g_path_get_dirname", test_dirname},
  293. {"g_path_get_basename", test_basename},
  294. {"g_find_program_in_path", test_ppath},
  295. {"g_find_program_in_path2", test_ppath2},
  296. {"test_cwd", test_cwd },
  297. {"test_misc", test_misc },
  298. {NULL, NULL}
  299. };
  300. DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)