1/*
2 Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12
13/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
14
15/* quiet windows compiler warnings */
16#define _CRT_NONSTDC_NO_WARNINGS
17
18#include <stdlib.h>
19
20#ifndef _MSC_VER
21#include <unistd.h>
22#endif
23
24#include "SDL.h"
25
26
27#include <stdio.h>
28
29/* WARNING ! those 2 files will be destroyed by this test program */
30
31#ifdef __IPHONEOS__
32#define FBASENAME1 "../Documents/sdldata1" /* this file will be created during tests */
33#define FBASENAME2 "../Documents/sdldata2" /* this file should not exist before starting test */
34#else
35#define FBASENAME1 "sdldata1" /* this file will be created during tests */
36#define FBASENAME2 "sdldata2" /* this file should not exist before starting test */
37#endif
38
39#ifndef NULL
40#define NULL ((void *)0)
41#endif
42
43static void
44cleanup(void)
45{
46 unlink(FBASENAME1);
47 unlink(FBASENAME2);
48}
49
50static void
51rwops_error_quit(unsigned line, SDL_RWops * rwops)
52{
53 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
54 if (rwops) {
55 rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
56 }
57 cleanup();
58 exit(1); /* quit with rwops error (test failed) */
59}
60
61#define RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) )
62
63
64
65int
66main(int argc, char *argv[])
67{
68 SDL_RWops *rwops = NULL;
69 char test_buf[30];
70
71 /* Enable standard application logging */
72 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
73
74 cleanup();
75
76/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
77
78 rwops = SDL_RWFromFile(NULL, NULL);
79 if (rwops)
80 RWOP_ERR_QUIT(rwops);
81 rwops = SDL_RWFromFile(NULL, "ab+");
82 if (rwops)
83 RWOP_ERR_QUIT(rwops);
84 rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
85 if (rwops)
86 RWOP_ERR_QUIT(rwops);
87 rwops = SDL_RWFromFile("something", "");
88 if (rwops)
89 RWOP_ERR_QUIT(rwops);
90 rwops = SDL_RWFromFile("something", NULL);
91 if (rwops)
92 RWOP_ERR_QUIT(rwops);
93 SDL_Log("test1 OK\n");
94
95/* test 2 : check that inexistent file is not successfully opened/created when required */
96/* modes : r, r+ imply that file MUST exist
97 modes : a, a+, w, w+ checks that it succeeds (file may not exists)
98
99 */
100 rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
101 if (rwops)
102 RWOP_ERR_QUIT(rwops);
103 rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
104 if (rwops)
105 RWOP_ERR_QUIT(rwops);
106 rwops = SDL_RWFromFile(FBASENAME2, "wb");
107 if (!rwops)
108 RWOP_ERR_QUIT(rwops);
109 rwops->close(rwops);
110 unlink(FBASENAME2);
111 rwops = SDL_RWFromFile(FBASENAME2, "wb+");
112 if (!rwops)
113 RWOP_ERR_QUIT(rwops);
114 rwops->close(rwops);
115 unlink(FBASENAME2);
116 rwops = SDL_RWFromFile(FBASENAME2, "ab");
117 if (!rwops)
118 RWOP_ERR_QUIT(rwops);
119 rwops->close(rwops);
120 unlink(FBASENAME2);
121 rwops = SDL_RWFromFile(FBASENAME2, "ab+");
122 if (!rwops)
123 RWOP_ERR_QUIT(rwops);
124 rwops->close(rwops);
125 unlink(FBASENAME2);
126 SDL_Log("test2 OK\n");
127
128/* test 3 : creation, writing , reading, seeking,
129 test : w mode, r mode, w+ mode
130 */
131 rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */
132 if (!rwops)
133 RWOP_ERR_QUIT(rwops);
134 if (1 != rwops->write(rwops, "1234567890", 10, 1))
135 RWOP_ERR_QUIT(rwops);
136 if (10 != rwops->write(rwops, "1234567890", 1, 10))
137 RWOP_ERR_QUIT(rwops);
138 if (7 != rwops->write(rwops, "1234567", 1, 7))
139 RWOP_ERR_QUIT(rwops);
140 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
141 RWOP_ERR_QUIT(rwops);
142 if (0 != rwops->read(rwops, test_buf, 1, 1))
143 RWOP_ERR_QUIT(rwops); /* we are in write only mode */
144 rwops->close(rwops);
145
146 rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */
147 if (!rwops)
148 RWOP_ERR_QUIT(rwops);
149 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
150 RWOP_ERR_QUIT(rwops);
151 if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
152 RWOP_ERR_QUIT(rwops);
153 if (7 != rwops->read(rwops, test_buf, 1, 7))
154 RWOP_ERR_QUIT(rwops);
155 if (SDL_memcmp(test_buf, "1234567", 7))
156 RWOP_ERR_QUIT(rwops);
157 if (0 != rwops->read(rwops, test_buf, 1, 1))
158 RWOP_ERR_QUIT(rwops);
159 if (0 != rwops->read(rwops, test_buf, 10, 100))
160 RWOP_ERR_QUIT(rwops);
161 if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
162 RWOP_ERR_QUIT(rwops);
163 if (2 != rwops->read(rwops, test_buf, 10, 3))
164 RWOP_ERR_QUIT(rwops);
165 if (SDL_memcmp(test_buf, "12345678901234567890", 20))
166 RWOP_ERR_QUIT(rwops);
167 if (0 != rwops->write(rwops, test_buf, 1, 1))
168 RWOP_ERR_QUIT(rwops); /* readonly mode */
169 rwops->close(rwops);
170
171/* test 3: same with w+ mode */
172 rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
173 if (!rwops)
174 RWOP_ERR_QUIT(rwops);
175 if (1 != rwops->write(rwops, "1234567890", 10, 1))
176 RWOP_ERR_QUIT(rwops);
177 if (10 != rwops->write(rwops, "1234567890", 1, 10))
178 RWOP_ERR_QUIT(rwops);
179 if (7 != rwops->write(rwops, "1234567", 1, 7))
180 RWOP_ERR_QUIT(rwops);
181 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
182 RWOP_ERR_QUIT(rwops);
183 if (1 != rwops->read(rwops, test_buf, 1, 1))
184 RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
185 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
186 RWOP_ERR_QUIT(rwops);
187 if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
188 RWOP_ERR_QUIT(rwops);
189 if (7 != rwops->read(rwops, test_buf, 1, 7))
190 RWOP_ERR_QUIT(rwops);
191 if (SDL_memcmp(test_buf, "1234567", 7))
192 RWOP_ERR_QUIT(rwops);
193 if (0 != rwops->read(rwops, test_buf, 1, 1))
194 RWOP_ERR_QUIT(rwops);
195 if (0 != rwops->read(rwops, test_buf, 10, 100))
196 RWOP_ERR_QUIT(rwops);
197 if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
198 RWOP_ERR_QUIT(rwops);
199 if (2 != rwops->read(rwops, test_buf, 10, 3))
200 RWOP_ERR_QUIT(rwops);
201 if (SDL_memcmp(test_buf, "12345678901234567890", 20))
202 RWOP_ERR_QUIT(rwops);
203 rwops->close(rwops);
204 SDL_Log("test3 OK\n");
205
206/* test 4: same in r+ mode */
207 rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
208 if (!rwops)
209 RWOP_ERR_QUIT(rwops);
210 if (1 != rwops->write(rwops, "1234567890", 10, 1))
211 RWOP_ERR_QUIT(rwops);
212 if (10 != rwops->write(rwops, "1234567890", 1, 10))
213 RWOP_ERR_QUIT(rwops);
214 if (7 != rwops->write(rwops, "1234567", 1, 7))
215 RWOP_ERR_QUIT(rwops);
216 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
217 RWOP_ERR_QUIT(rwops);
218 if (1 != rwops->read(rwops, test_buf, 1, 1))
219 RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
220 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
221 RWOP_ERR_QUIT(rwops);
222 if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
223 RWOP_ERR_QUIT(rwops);
224 if (7 != rwops->read(rwops, test_buf, 1, 7))
225 RWOP_ERR_QUIT(rwops);
226 if (SDL_memcmp(test_buf, "1234567", 7))
227 RWOP_ERR_QUIT(rwops);
228 if (0 != rwops->read(rwops, test_buf, 1, 1))
229 RWOP_ERR_QUIT(rwops);
230 if (0 != rwops->read(rwops, test_buf, 10, 100))
231 RWOP_ERR_QUIT(rwops);
232 if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
233 RWOP_ERR_QUIT(rwops);
234 if (2 != rwops->read(rwops, test_buf, 10, 3))
235 RWOP_ERR_QUIT(rwops);
236 if (SDL_memcmp(test_buf, "12345678901234567890", 20))
237 RWOP_ERR_QUIT(rwops);
238 rwops->close(rwops);
239 SDL_Log("test4 OK\n");
240
241/* test5 : append mode */
242 rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */
243 if (!rwops)
244 RWOP_ERR_QUIT(rwops);
245 if (1 != rwops->write(rwops, "1234567890", 10, 1))
246 RWOP_ERR_QUIT(rwops);
247 if (10 != rwops->write(rwops, "1234567890", 1, 10))
248 RWOP_ERR_QUIT(rwops);
249 if (7 != rwops->write(rwops, "1234567", 1, 7))
250 RWOP_ERR_QUIT(rwops);
251 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
252 RWOP_ERR_QUIT(rwops);
253
254 if (1 != rwops->read(rwops, test_buf, 1, 1))
255 RWOP_ERR_QUIT(rwops);
256 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
257 RWOP_ERR_QUIT(rwops);
258
259 if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
260 RWOP_ERR_QUIT(rwops);
261 if (7 != rwops->read(rwops, test_buf, 1, 7))
262 RWOP_ERR_QUIT(rwops);
263 if (SDL_memcmp(test_buf, "1234567", 7))
264 RWOP_ERR_QUIT(rwops);
265 if (0 != rwops->read(rwops, test_buf, 1, 1))
266 RWOP_ERR_QUIT(rwops);
267 if (0 != rwops->read(rwops, test_buf, 10, 100))
268 RWOP_ERR_QUIT(rwops);
269
270 if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
271 RWOP_ERR_QUIT(rwops);
272
273 if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
274 RWOP_ERR_QUIT(rwops);
275 if (3 != rwops->read(rwops, test_buf, 10, 3))
276 RWOP_ERR_QUIT(rwops);
277 if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
278 RWOP_ERR_QUIT(rwops);
279 rwops->close(rwops);
280 SDL_Log("test5 OK\n");
281 cleanup();
282 return 0; /* all ok */
283}
284