testfilesystem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple test of filesystem functions. */
  11. #include <SDL3/SDL.h>
  12. #include <SDL3/SDL_main.h>
  13. #include <SDL3/SDL_test.h>
  14. static SDL_EnumerationResult SDLCALL enum_callback(void *userdata, const char *origdir, const char *fname)
  15. {
  16. SDL_PathInfo info;
  17. char *fullpath = NULL;
  18. if (SDL_asprintf(&fullpath, "%s%s", origdir, fname) < 0) {
  19. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  20. return SDL_ENUM_FAILURE;
  21. }
  22. if (!SDL_GetPathInfo(fullpath, &info)) {
  23. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't stat '%s': %s", fullpath, SDL_GetError());
  24. } else {
  25. const char *type;
  26. if (info.type == SDL_PATHTYPE_FILE) {
  27. type = "FILE";
  28. } else if (info.type == SDL_PATHTYPE_DIRECTORY) {
  29. type = "DIRECTORY";
  30. } else {
  31. type = "OTHER";
  32. }
  33. SDL_Log("DIRECTORY %s (type=%s, size=%" SDL_PRIu64 ", create=%" SDL_PRIu64 ", mod=%" SDL_PRIu64 ", access=%" SDL_PRIu64 ")",
  34. fullpath, type, info.size, info.modify_time, info.create_time, info.access_time);
  35. if (info.type == SDL_PATHTYPE_DIRECTORY) {
  36. if (!SDL_EnumerateDirectory(fullpath, enum_callback, userdata)) {
  37. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Enumeration failed!");
  38. }
  39. }
  40. }
  41. SDL_free(fullpath);
  42. return SDL_ENUM_CONTINUE; /* keep going */
  43. }
  44. static SDL_EnumerationResult SDLCALL enum_storage_callback(void *userdata, const char *origdir, const char *fname)
  45. {
  46. SDL_Storage *storage = (SDL_Storage *) userdata;
  47. SDL_PathInfo info;
  48. char *fullpath = NULL;
  49. if (SDL_asprintf(&fullpath, "%s%s", origdir, fname) < 0) {
  50. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  51. return SDL_ENUM_FAILURE;
  52. }
  53. if (!SDL_GetStoragePathInfo(storage, fullpath, &info)) {
  54. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't stat '%s': %s", fullpath, SDL_GetError());
  55. } else {
  56. const char *type;
  57. if (info.type == SDL_PATHTYPE_FILE) {
  58. type = "FILE";
  59. } else if (info.type == SDL_PATHTYPE_DIRECTORY) {
  60. type = "DIRECTORY";
  61. } else {
  62. type = "OTHER";
  63. }
  64. SDL_Log("STORAGE %s (type=%s, size=%" SDL_PRIu64 ", create=%" SDL_PRIu64 ", mod=%" SDL_PRIu64 ", access=%" SDL_PRIu64 ")",
  65. fullpath, type, info.size, info.modify_time, info.create_time, info.access_time);
  66. if (info.type == SDL_PATHTYPE_DIRECTORY) {
  67. if (!SDL_EnumerateStorageDirectory(storage, fullpath, enum_storage_callback, userdata)) {
  68. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Enumeration failed!");
  69. }
  70. }
  71. }
  72. SDL_free(fullpath);
  73. return SDL_ENUM_CONTINUE; /* keep going */
  74. }
  75. int main(int argc, char *argv[])
  76. {
  77. SDLTest_CommonState *state;
  78. char *pref_path;
  79. char *curdir;
  80. const char *base_path;
  81. /* Initialize test framework */
  82. state = SDLTest_CommonCreateState(argv, 0);
  83. if (!state) {
  84. return 1;
  85. }
  86. /* Parse commandline */
  87. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  88. return 1;
  89. }
  90. if (!SDL_Init(0)) {
  91. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s", SDL_GetError());
  92. return 1;
  93. }
  94. base_path = SDL_GetBasePath();
  95. if (!base_path) {
  96. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find base path: %s",
  97. SDL_GetError());
  98. } else {
  99. SDL_Log("base path: '%s'", base_path);
  100. }
  101. pref_path = SDL_GetPrefPath("libsdl", "test_filesystem");
  102. if (!pref_path) {
  103. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path: %s",
  104. SDL_GetError());
  105. } else {
  106. SDL_Log("pref path: '%s'", pref_path);
  107. }
  108. SDL_free(pref_path);
  109. pref_path = SDL_GetPrefPath(NULL, "test_filesystem");
  110. if (!pref_path) {
  111. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path without organization: %s",
  112. SDL_GetError());
  113. } else {
  114. SDL_Log("pref path: '%s'", pref_path);
  115. }
  116. SDL_free(pref_path);
  117. curdir = SDL_GetCurrentDirectory();
  118. if (!curdir) {
  119. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find current directory: %s",
  120. SDL_GetError());
  121. } else {
  122. SDL_Log("current directory: '%s'", curdir);
  123. }
  124. SDL_free(curdir);
  125. if (base_path) {
  126. char **globlist;
  127. SDL_Storage *storage = NULL;
  128. SDL_IOStream *stream;
  129. const char *text = "foo\n";
  130. SDL_PathInfo pathinfo;
  131. if (!SDL_EnumerateDirectory(base_path, enum_callback, NULL)) {
  132. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Base path enumeration failed!");
  133. }
  134. globlist = SDL_GlobDirectory(base_path, "*/test*/T?st*", SDL_GLOB_CASEINSENSITIVE, NULL);
  135. if (!globlist) {
  136. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Base path globbing failed!");
  137. } else {
  138. int i;
  139. for (i = 0; globlist[i]; i++) {
  140. SDL_Log("DIRECTORY GLOB[%d]: '%s'", i, globlist[i]);
  141. }
  142. SDL_free(globlist);
  143. }
  144. /* !!! FIXME: put this in a subroutine and make it test more thoroughly (and put it in testautomation). */
  145. if (!SDL_CreateDirectory("testfilesystem-test")) {
  146. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test') failed: %s", SDL_GetError());
  147. } else if (!SDL_CreateDirectory("testfilesystem-test/1")) {
  148. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
  149. } else if (!SDL_CreateDirectory("testfilesystem-test/1")) { /* THIS SHOULD NOT FAIL! Making a directory that already exists should succeed here. */
  150. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/1') failed: %s", SDL_GetError());
  151. } else if (!SDL_CreateDirectory("testfilesystem-test/3/4/5/6")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  152. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateDirectory('testfilesystem-test/3/4/5/6') failed: %s", SDL_GetError());
  153. } else if (!SDL_RemovePath("testfilesystem-test/3/4/5/6")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  154. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3/4/5/6') failed: %s", SDL_GetError());
  155. } else if (!SDL_RemovePath("testfilesystem-test/3/4/5")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  156. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3/4/5') failed: %s", SDL_GetError());
  157. } else if (!SDL_RemovePath("testfilesystem-test/3/4")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  158. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3/4') failed: %s", SDL_GetError());
  159. } else if (!SDL_RemovePath("testfilesystem-test/3")) { /* THIS SHOULD NOT FAIL! Making a directory with missing parents succeed here. */
  160. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/3') failed: %s", SDL_GetError());
  161. } else if (!SDL_RenamePath("testfilesystem-test/1", "testfilesystem-test/2")) {
  162. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RenamePath('testfilesystem-test/1', 'testfilesystem-test/2') failed: %s", SDL_GetError());
  163. } else if (!SDL_RemovePath("testfilesystem-test/2")) {
  164. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test/2') failed: %s", SDL_GetError());
  165. } else if (!SDL_RemovePath("testfilesystem-test")) {
  166. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test') failed: %s", SDL_GetError());
  167. } else if (!SDL_RemovePath("testfilesystem-test")) { /* THIS SHOULD NOT FAIL! Removing a directory that is already gone should succeed here. */
  168. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-test') failed: %s", SDL_GetError());
  169. }
  170. stream = SDL_IOFromFile("testfilesystem-A", "wb");
  171. if (stream) {
  172. SDL_WriteIO(stream, text, SDL_strlen(text));
  173. SDL_CloseIO(stream);
  174. if (!SDL_RenamePath("testfilesystem-A", "testfilesystem-B")) {
  175. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RenamePath('testfilesystem-A', 'testfilesystem-B') failed: %s", SDL_GetError());
  176. } else if (!SDL_CopyFile("testfilesystem-B", "testfilesystem-A")) {
  177. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CopyFile('testfilesystem-B', 'testfilesystem-A') failed: %s", SDL_GetError());
  178. } else {
  179. size_t sizeA, sizeB;
  180. char *textA, *textB;
  181. textA = (char *)SDL_LoadFile("testfilesystem-A", &sizeA);
  182. if (!textA || sizeA != SDL_strlen(text) || SDL_strcmp(textA, text) != 0) {
  183. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Contents of testfilesystem-A didn't match, expected %s, got %s", text, textA);
  184. }
  185. SDL_free(textA);
  186. textB = (char *)SDL_LoadFile("testfilesystem-B", &sizeB);
  187. if (!textB || sizeB != SDL_strlen(text) || SDL_strcmp(textB, text) != 0) {
  188. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Contents of testfilesystem-B didn't match, expected %s, got %s", text, textB);
  189. }
  190. SDL_free(textB);
  191. }
  192. if (!SDL_RemovePath("testfilesystem-A")) {
  193. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-A') failed: %s", SDL_GetError());
  194. }
  195. if (!SDL_RemovePath("testfilesystem-B")) {
  196. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_RemovePath('testfilesystem-B') failed: %s", SDL_GetError());
  197. }
  198. } else {
  199. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_IOFromFile('testfilesystem-A', 'w') failed: %s", SDL_GetError());
  200. }
  201. storage = SDL_OpenFileStorage(base_path);
  202. if (!storage) {
  203. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open base path storage object: %s", SDL_GetError());
  204. } else {
  205. if (!SDL_EnumerateStorageDirectory(storage, "CMakeFiles", enum_storage_callback, storage)) {
  206. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Storage Base path enumeration failed!");
  207. }
  208. globlist = SDL_GlobStorageDirectory(storage, "", "C*/test*/T?st*", SDL_GLOB_CASEINSENSITIVE, NULL);
  209. if (!globlist) {
  210. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Base path globbing failed!");
  211. } else {
  212. int i;
  213. for (i = 0; globlist[i]; i++) {
  214. SDL_Log("STORAGE GLOB[%d]: '%s'", i, globlist[i]);
  215. }
  216. SDL_free(globlist);
  217. }
  218. /* these should fail: */
  219. if (!SDL_GetStoragePathInfo(storage, "CMakeFiles/../testsprite.c", &pathinfo)) {
  220. SDL_Log("Storage access on path with internal '..' refused correctly.");
  221. } else {
  222. SDL_Log("Storage access on path with internal '..' accepted INCORRECTLY.");
  223. }
  224. if (!SDL_GetStoragePathInfo(storage, "CMakeFiles/./TargetDirectories.txt", &pathinfo)) {
  225. SDL_Log("Storage access on path with internal '.' refused correctly.");
  226. } else {
  227. SDL_Log("Storage access on path with internal '.' accepted INCORRECTLY.");
  228. }
  229. if (!SDL_GetStoragePathInfo(storage, "../test", &pathinfo)) {
  230. SDL_Log("Storage access on path with leading '..' refused correctly.");
  231. } else {
  232. SDL_Log("Storage access on path with leading '..' accepted INCORRECTLY.");
  233. }
  234. if (!SDL_GetStoragePathInfo(storage, "./CMakeFiles", &pathinfo)) {
  235. SDL_Log("Storage access on path with leading '.' refused correctly.");
  236. } else {
  237. SDL_Log("Storage access on path with leading '.' accepted INCORRECTLY.");
  238. }
  239. if (!SDL_GetStoragePathInfo(storage, "CMakeFiles/..", &pathinfo)) {
  240. SDL_Log("Storage access on path with trailing '..' refused correctly.");
  241. } else {
  242. SDL_Log("Storage access on path with trailing '..' accepted INCORRECTLY.");
  243. }
  244. if (!SDL_GetStoragePathInfo(storage, "CMakeFiles/.", &pathinfo)) {
  245. SDL_Log("Storage access on path with trailing '.' refused correctly.");
  246. } else {
  247. SDL_Log("Storage access on path with trailing '.' accepted INCORRECTLY.");
  248. }
  249. if (!SDL_GetStoragePathInfo(storage, "..", &pathinfo)) {
  250. SDL_Log("Storage access on path '..' refused correctly.");
  251. } else {
  252. SDL_Log("Storage access on path '..' accepted INCORRECTLY.");
  253. }
  254. if (!SDL_GetStoragePathInfo(storage, ".", &pathinfo)) {
  255. SDL_Log("Storage access on path '.' refused correctly.");
  256. } else {
  257. SDL_Log("Storage access on path '.' accepted INCORRECTLY.");
  258. }
  259. if (!SDL_GetStoragePathInfo(storage, "CMakeFiles\\TargetDirectories.txt", &pathinfo)) {
  260. SDL_Log("Storage access on path with Windows separator refused correctly.");
  261. } else {
  262. SDL_Log("Storage access on path with Windows separator accepted INCORRECTLY.");
  263. }
  264. SDL_CloseStorage(storage);
  265. }
  266. }
  267. SDL_Quit();
  268. SDLTest_CommonDestroyState(state);
  269. return 0;
  270. }