2
0

testfile.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_file_io.h"
  17. #include "fspr_file_info.h"
  18. #include "fspr_network_io.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_general.h"
  21. #include "fspr_poll.h"
  22. #include "fspr_lib.h"
  23. #include "testutil.h"
  24. #define DIRNAME "data"
  25. #define FILENAME DIRNAME "/file_datafile.txt"
  26. #define TESTSTR "This is the file data file."
  27. #define TESTREAD_BLKSIZE 1024
  28. #define APR_BUFFERSIZE 4096 /* This should match APR's buffer size. */
  29. static void test_open_noreadwrite(abts_case *tc, void *data)
  30. {
  31. fspr_status_t rv;
  32. fspr_file_t *thefile = NULL;
  33. rv = fspr_file_open(&thefile, FILENAME,
  34. APR_CREATE | APR_EXCL,
  35. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  36. ABTS_TRUE(tc, rv != APR_SUCCESS);
  37. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EACCES(rv));
  38. ABTS_PTR_EQUAL(tc, NULL, thefile);
  39. }
  40. static void test_open_excl(abts_case *tc, void *data)
  41. {
  42. fspr_status_t rv;
  43. fspr_file_t *thefile = NULL;
  44. rv = fspr_file_open(&thefile, FILENAME,
  45. APR_CREATE | APR_EXCL | APR_WRITE,
  46. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  47. ABTS_TRUE(tc, rv != APR_SUCCESS);
  48. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EEXIST(rv));
  49. ABTS_PTR_EQUAL(tc, NULL, thefile);
  50. }
  51. static void test_open_read(abts_case *tc, void *data)
  52. {
  53. fspr_status_t rv;
  54. fspr_file_t *filetest = NULL;
  55. rv = fspr_file_open(&filetest, FILENAME,
  56. APR_READ,
  57. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  58. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  59. ABTS_PTR_NOTNULL(tc, filetest);
  60. fspr_file_close(filetest);
  61. }
  62. static void test_read(abts_case *tc, void *data)
  63. {
  64. fspr_status_t rv;
  65. fspr_size_t nbytes = 256;
  66. char *str = fspr_pcalloc(p, nbytes + 1);
  67. fspr_file_t *filetest = NULL;
  68. rv = fspr_file_open(&filetest, FILENAME,
  69. APR_READ,
  70. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  71. APR_ASSERT_SUCCESS(tc, "Opening test file " FILENAME, rv);
  72. rv = fspr_file_read(filetest, str, &nbytes);
  73. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  74. ABTS_INT_EQUAL(tc, strlen(TESTSTR), nbytes);
  75. ABTS_STR_EQUAL(tc, TESTSTR, str);
  76. fspr_file_close(filetest);
  77. }
  78. static void test_readzero(abts_case *tc, void *data)
  79. {
  80. fspr_status_t rv;
  81. fspr_size_t nbytes = 0;
  82. char *str = NULL;
  83. fspr_file_t *filetest;
  84. rv = fspr_file_open(&filetest, FILENAME, APR_READ, APR_OS_DEFAULT, p);
  85. APR_ASSERT_SUCCESS(tc, "Opening test file " FILENAME, rv);
  86. rv = fspr_file_read(filetest, str, &nbytes);
  87. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  88. ABTS_INT_EQUAL(tc, 0, nbytes);
  89. fspr_file_close(filetest);
  90. }
  91. static void test_filename(abts_case *tc, void *data)
  92. {
  93. const char *str;
  94. fspr_status_t rv;
  95. fspr_file_t *filetest = NULL;
  96. rv = fspr_file_open(&filetest, FILENAME,
  97. APR_READ,
  98. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  99. APR_ASSERT_SUCCESS(tc, "Opening test file " FILENAME, rv);
  100. rv = fspr_file_name_get(&str, filetest);
  101. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  102. ABTS_STR_EQUAL(tc, FILENAME, str);
  103. fspr_file_close(filetest);
  104. }
  105. static void test_fileclose(abts_case *tc, void *data)
  106. {
  107. char str;
  108. fspr_status_t rv;
  109. fspr_size_t one = 1;
  110. fspr_file_t *filetest = NULL;
  111. rv = fspr_file_open(&filetest, FILENAME,
  112. APR_READ,
  113. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  114. APR_ASSERT_SUCCESS(tc, "Opening test file " FILENAME, rv);
  115. rv = fspr_file_close(filetest);
  116. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  117. /* We just closed the file, so this should fail */
  118. rv = fspr_file_read(filetest, &str, &one);
  119. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EBADF(rv));
  120. }
  121. static void test_file_remove(abts_case *tc, void *data)
  122. {
  123. fspr_status_t rv;
  124. fspr_file_t *filetest = NULL;
  125. rv = fspr_file_remove(FILENAME, p);
  126. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  127. rv = fspr_file_open(&filetest, FILENAME, APR_READ,
  128. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  129. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  130. }
  131. static void test_open_write(abts_case *tc, void *data)
  132. {
  133. fspr_status_t rv;
  134. fspr_file_t *filetest = NULL;
  135. filetest = NULL;
  136. rv = fspr_file_open(&filetest, FILENAME,
  137. APR_WRITE,
  138. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  139. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  140. ABTS_PTR_EQUAL(tc, NULL, filetest);
  141. }
  142. static void test_open_writecreate(abts_case *tc, void *data)
  143. {
  144. fspr_status_t rv;
  145. fspr_file_t *filetest = NULL;
  146. filetest = NULL;
  147. rv = fspr_file_open(&filetest, FILENAME,
  148. APR_WRITE | APR_CREATE,
  149. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  150. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  151. fspr_file_close(filetest);
  152. }
  153. static void test_write(abts_case *tc, void *data)
  154. {
  155. fspr_status_t rv;
  156. fspr_size_t bytes = strlen(TESTSTR);
  157. fspr_file_t *filetest = NULL;
  158. rv = fspr_file_open(&filetest, FILENAME,
  159. APR_WRITE | APR_CREATE,
  160. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  161. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  162. rv = fspr_file_write(filetest, TESTSTR, &bytes);
  163. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  164. fspr_file_close(filetest);
  165. }
  166. static void test_open_readwrite(abts_case *tc, void *data)
  167. {
  168. fspr_status_t rv;
  169. fspr_file_t *filetest = NULL;
  170. filetest = NULL;
  171. rv = fspr_file_open(&filetest, FILENAME,
  172. APR_READ | APR_WRITE,
  173. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  174. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  175. ABTS_PTR_NOTNULL(tc, filetest);
  176. fspr_file_close(filetest);
  177. }
  178. static void test_seek(abts_case *tc, void *data)
  179. {
  180. fspr_status_t rv;
  181. fspr_off_t offset = 5;
  182. fspr_size_t nbytes = 256;
  183. char *str = fspr_pcalloc(p, nbytes + 1);
  184. fspr_file_t *filetest = NULL;
  185. rv = fspr_file_open(&filetest, FILENAME,
  186. APR_READ,
  187. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  188. APR_ASSERT_SUCCESS(tc, "Open test file " FILENAME, rv);
  189. rv = fspr_file_read(filetest, str, &nbytes);
  190. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  191. ABTS_INT_EQUAL(tc, strlen(TESTSTR), nbytes);
  192. ABTS_STR_EQUAL(tc, TESTSTR, str);
  193. memset(str, 0, nbytes + 1);
  194. rv = fspr_file_seek(filetest, SEEK_SET, &offset);
  195. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  196. rv = fspr_file_read(filetest, str, &nbytes);
  197. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  198. ABTS_INT_EQUAL(tc, strlen(TESTSTR) - 5, nbytes);
  199. ABTS_STR_EQUAL(tc, TESTSTR + 5, str);
  200. fspr_file_close(filetest);
  201. /* Test for regression of sign error bug with SEEK_END and
  202. buffered files. */
  203. rv = fspr_file_open(&filetest, FILENAME,
  204. APR_READ | APR_BUFFERED,
  205. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  206. APR_ASSERT_SUCCESS(tc, "Open test file " FILENAME, rv);
  207. offset = -5;
  208. rv = fspr_file_seek(filetest, SEEK_END, &offset);
  209. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  210. ABTS_INT_EQUAL(tc, strlen(TESTSTR) - 5, nbytes);
  211. memset(str, 0, nbytes + 1);
  212. nbytes = 256;
  213. rv = fspr_file_read(filetest, str, &nbytes);
  214. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  215. ABTS_INT_EQUAL(tc, 5, nbytes);
  216. ABTS_STR_EQUAL(tc, TESTSTR + strlen(TESTSTR) - 5, str);
  217. fspr_file_close(filetest);
  218. }
  219. static void test_userdata_set(abts_case *tc, void *data)
  220. {
  221. fspr_status_t rv;
  222. fspr_file_t *filetest = NULL;
  223. rv = fspr_file_open(&filetest, FILENAME,
  224. APR_WRITE,
  225. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  226. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  227. rv = fspr_file_data_set(filetest, "This is a test",
  228. "test", fspr_pool_cleanup_null);
  229. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  230. fspr_file_close(filetest);
  231. }
  232. static void test_userdata_get(abts_case *tc, void *data)
  233. {
  234. fspr_status_t rv;
  235. void *udata;
  236. char *teststr;
  237. fspr_file_t *filetest = NULL;
  238. rv = fspr_file_open(&filetest, FILENAME,
  239. APR_WRITE,
  240. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  241. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  242. rv = fspr_file_data_set(filetest, "This is a test",
  243. "test", fspr_pool_cleanup_null);
  244. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  245. rv = fspr_file_data_get(&udata, "test", filetest);
  246. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  247. teststr = udata;
  248. ABTS_STR_EQUAL(tc, "This is a test", teststr);
  249. fspr_file_close(filetest);
  250. }
  251. static void test_userdata_getnokey(abts_case *tc, void *data)
  252. {
  253. fspr_status_t rv;
  254. void *teststr;
  255. fspr_file_t *filetest = NULL;
  256. rv = fspr_file_open(&filetest, FILENAME,
  257. APR_WRITE,
  258. APR_UREAD | APR_UWRITE | APR_GREAD, p);
  259. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  260. rv = fspr_file_data_get(&teststr, "nokey", filetest);
  261. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  262. ABTS_PTR_EQUAL(tc, NULL, teststr);
  263. fspr_file_close(filetest);
  264. }
  265. static void test_getc(abts_case *tc, void *data)
  266. {
  267. fspr_file_t *f = NULL;
  268. fspr_status_t rv;
  269. char ch;
  270. rv = fspr_file_open(&f, FILENAME, APR_READ, 0, p);
  271. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  272. fspr_file_getc(&ch, f);
  273. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  274. ABTS_INT_EQUAL(tc, (int)TESTSTR[0], (int)ch);
  275. fspr_file_close(f);
  276. }
  277. static void test_ungetc(abts_case *tc, void *data)
  278. {
  279. fspr_file_t *f = NULL;
  280. fspr_status_t rv;
  281. char ch;
  282. rv = fspr_file_open(&f, FILENAME, APR_READ, 0, p);
  283. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  284. fspr_file_getc(&ch, f);
  285. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  286. ABTS_INT_EQUAL(tc, (int)TESTSTR[0], (int)ch);
  287. fspr_file_ungetc('X', f);
  288. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  289. fspr_file_getc(&ch, f);
  290. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  291. ABTS_INT_EQUAL(tc, 'X', (int)ch);
  292. fspr_file_close(f);
  293. }
  294. static void test_gets(abts_case *tc, void *data)
  295. {
  296. fspr_file_t *f = NULL;
  297. fspr_status_t rv;
  298. char *str = fspr_palloc(p, 256);
  299. rv = fspr_file_open(&f, FILENAME, APR_READ, 0, p);
  300. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  301. rv = fspr_file_gets(str, 256, f);
  302. /* Only one line in the test file, so APR will encounter EOF on the first
  303. * call to gets, but we should get APR_SUCCESS on this call and
  304. * APR_EOF on the next.
  305. */
  306. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  307. ABTS_STR_EQUAL(tc, TESTSTR, str);
  308. rv = fspr_file_gets(str, 256, f);
  309. ABTS_INT_EQUAL(tc, APR_EOF, rv);
  310. ABTS_STR_EQUAL(tc, "", str);
  311. fspr_file_close(f);
  312. }
  313. static void test_bigread(abts_case *tc, void *data)
  314. {
  315. fspr_file_t *f = NULL;
  316. fspr_status_t rv;
  317. char buf[APR_BUFFERSIZE * 2];
  318. fspr_size_t nbytes;
  319. /* Create a test file with known content.
  320. */
  321. rv = fspr_file_open(&f, "data/created_file",
  322. APR_CREATE | APR_WRITE | APR_TRUNCATE,
  323. APR_UREAD | APR_UWRITE, p);
  324. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  325. nbytes = APR_BUFFERSIZE;
  326. memset(buf, 0xFE, nbytes);
  327. rv = fspr_file_write(f, buf, &nbytes);
  328. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  329. ABTS_INT_EQUAL(tc, APR_BUFFERSIZE, nbytes);
  330. rv = fspr_file_close(f);
  331. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  332. f = NULL;
  333. rv = fspr_file_open(&f, "data/created_file", APR_READ, 0, p);
  334. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  335. nbytes = sizeof buf;
  336. rv = fspr_file_read(f, buf, &nbytes);
  337. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  338. ABTS_INT_EQUAL(tc, APR_BUFFERSIZE, nbytes);
  339. rv = fspr_file_close(f);
  340. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  341. rv = fspr_file_remove("data/created_file", p);
  342. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  343. }
  344. /* This is a horrible name for this function. We are testing APR, not how
  345. * Apache uses APR. And, this function tests _way_ too much stuff.
  346. */
  347. static void test_mod_neg(abts_case *tc, void *data)
  348. {
  349. fspr_status_t rv;
  350. fspr_file_t *f;
  351. const char *s;
  352. int i;
  353. fspr_size_t nbytes;
  354. char buf[8192];
  355. fspr_off_t cur;
  356. const char *fname = "data/modneg.dat";
  357. rv = fspr_file_open(&f, fname,
  358. APR_CREATE | APR_WRITE, APR_UREAD | APR_UWRITE, p);
  359. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  360. s = "body56789\n";
  361. nbytes = strlen(s);
  362. rv = fspr_file_write(f, s, &nbytes);
  363. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  364. ABTS_INT_EQUAL(tc, strlen(s), nbytes);
  365. for (i = 0; i < 7980; i++) {
  366. s = "0";
  367. nbytes = strlen(s);
  368. rv = fspr_file_write(f, s, &nbytes);
  369. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  370. ABTS_INT_EQUAL(tc, strlen(s), nbytes);
  371. }
  372. s = "end456789\n";
  373. nbytes = strlen(s);
  374. rv = fspr_file_write(f, s, &nbytes);
  375. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  376. ABTS_INT_EQUAL(tc, strlen(s), nbytes);
  377. for (i = 0; i < 10000; i++) {
  378. s = "1";
  379. nbytes = strlen(s);
  380. rv = fspr_file_write(f, s, &nbytes);
  381. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  382. ABTS_INT_EQUAL(tc, strlen(s), nbytes);
  383. }
  384. rv = fspr_file_close(f);
  385. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  386. rv = fspr_file_open(&f, fname, APR_READ, 0, p);
  387. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  388. rv = fspr_file_gets(buf, 11, f);
  389. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  390. ABTS_STR_EQUAL(tc, "body56789\n", buf);
  391. cur = 0;
  392. rv = fspr_file_seek(f, APR_CUR, &cur);
  393. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  394. ABTS_ASSERT(tc, "File Pointer Mismatch, expected 10", cur == 10);
  395. nbytes = sizeof(buf);
  396. rv = fspr_file_read(f, buf, &nbytes);
  397. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  398. ABTS_INT_EQUAL(tc, nbytes, sizeof(buf));
  399. cur = -((fspr_off_t)nbytes - 7980);
  400. rv = fspr_file_seek(f, APR_CUR, &cur);
  401. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  402. ABTS_ASSERT(tc, "File Pointer Mismatch, expected 7990", cur == 7990);
  403. rv = fspr_file_gets(buf, 11, f);
  404. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  405. ABTS_STR_EQUAL(tc, "end456789\n", buf);
  406. rv = fspr_file_close(f);
  407. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  408. rv = fspr_file_remove(fname, p);
  409. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  410. }
  411. /* Test that the contents of file FNAME are equal to data EXPECT of
  412. * length EXPECTLEN. */
  413. static void file_contents_equal(abts_case *tc,
  414. const char *fname,
  415. const void *expect,
  416. fspr_size_t expectlen)
  417. {
  418. void *actual = fspr_palloc(p, expectlen);
  419. fspr_file_t *f;
  420. APR_ASSERT_SUCCESS(tc, "open file",
  421. fspr_file_open(&f, fname, APR_READ|APR_BUFFERED,
  422. 0, p));
  423. APR_ASSERT_SUCCESS(tc, "read from file",
  424. fspr_file_read_full(f, actual, expectlen, NULL));
  425. ABTS_ASSERT(tc, "matched expected file contents",
  426. memcmp(expect, actual, expectlen) == 0);
  427. APR_ASSERT_SUCCESS(tc, "close file", fspr_file_close(f));
  428. }
  429. #define LINE1 "this is a line of text\n"
  430. #define LINE2 "this is a second line of text\n"
  431. static void test_puts(abts_case *tc, void *data)
  432. {
  433. fspr_file_t *f;
  434. const char *fname = "data/testputs.txt";
  435. APR_ASSERT_SUCCESS(tc, "open file for writing",
  436. fspr_file_open(&f, fname,
  437. APR_WRITE|APR_CREATE|APR_TRUNCATE,
  438. APR_OS_DEFAULT, p));
  439. APR_ASSERT_SUCCESS(tc, "write line to file",
  440. fspr_file_puts(LINE1, f));
  441. APR_ASSERT_SUCCESS(tc, "write second line to file",
  442. fspr_file_puts(LINE2, f));
  443. APR_ASSERT_SUCCESS(tc, "close for writing",
  444. fspr_file_close(f));
  445. file_contents_equal(tc, fname, LINE1 LINE2, strlen(LINE1 LINE2));
  446. }
  447. static void test_writev(abts_case *tc, void *data)
  448. {
  449. fspr_file_t *f;
  450. fspr_size_t nbytes;
  451. struct iovec vec[5];
  452. const char *fname = "data/testwritev.txt";
  453. APR_ASSERT_SUCCESS(tc, "open file for writing",
  454. fspr_file_open(&f, fname,
  455. APR_WRITE|APR_CREATE|APR_TRUNCATE,
  456. APR_OS_DEFAULT, p));
  457. vec[0].iov_base = LINE1;
  458. vec[0].iov_len = strlen(LINE1);
  459. APR_ASSERT_SUCCESS(tc, "writev of size 1 to file",
  460. fspr_file_writev(f, vec, 1, &nbytes));
  461. file_contents_equal(tc, fname, LINE1, strlen(LINE1));
  462. vec[0].iov_base = LINE1;
  463. vec[0].iov_len = strlen(LINE1);
  464. vec[1].iov_base = LINE2;
  465. vec[1].iov_len = strlen(LINE2);
  466. vec[2].iov_base = LINE1;
  467. vec[2].iov_len = strlen(LINE1);
  468. vec[3].iov_base = LINE1;
  469. vec[3].iov_len = strlen(LINE1);
  470. vec[4].iov_base = LINE2;
  471. vec[4].iov_len = strlen(LINE2);
  472. APR_ASSERT_SUCCESS(tc, "writev of size 5 to file",
  473. fspr_file_writev(f, vec, 5, &nbytes));
  474. APR_ASSERT_SUCCESS(tc, "close for writing",
  475. fspr_file_close(f));
  476. file_contents_equal(tc, fname, LINE1 LINE1 LINE2 LINE1 LINE1 LINE2,
  477. strlen(LINE1)*4 + strlen(LINE2)*2);
  478. }
  479. static void test_writev_full(abts_case *tc, void *data)
  480. {
  481. fspr_file_t *f;
  482. fspr_size_t nbytes;
  483. struct iovec vec[5];
  484. const char *fname = "data/testwritev_full.txt";
  485. APR_ASSERT_SUCCESS(tc, "open file for writing",
  486. fspr_file_open(&f, fname,
  487. APR_WRITE|APR_CREATE|APR_TRUNCATE,
  488. APR_OS_DEFAULT, p));
  489. vec[0].iov_base = LINE1;
  490. vec[0].iov_len = strlen(LINE1);
  491. vec[1].iov_base = LINE2;
  492. vec[1].iov_len = strlen(LINE2);
  493. vec[2].iov_base = LINE1;
  494. vec[2].iov_len = strlen(LINE1);
  495. vec[3].iov_base = LINE1;
  496. vec[3].iov_len = strlen(LINE1);
  497. vec[4].iov_base = LINE2;
  498. vec[4].iov_len = strlen(LINE2);
  499. APR_ASSERT_SUCCESS(tc, "writev_full of size 5 to file",
  500. fspr_file_writev_full(f, vec, 5, &nbytes));
  501. ABTS_INT_EQUAL(tc, strlen(LINE1)*3 + strlen(LINE2)*2, nbytes);
  502. APR_ASSERT_SUCCESS(tc, "close for writing",
  503. fspr_file_close(f));
  504. file_contents_equal(tc, fname, LINE1 LINE2 LINE1 LINE1 LINE2,
  505. strlen(LINE1)*3 + strlen(LINE2)*2);
  506. }
  507. static void test_truncate(abts_case *tc, void *data)
  508. {
  509. fspr_status_t rv;
  510. fspr_file_t *f;
  511. const char *fname = "data/testtruncate.dat";
  512. const char *s;
  513. fspr_size_t nbytes;
  514. fspr_finfo_t finfo;
  515. fspr_file_remove(fname, p);
  516. rv = fspr_file_open(&f, fname,
  517. APR_CREATE | APR_WRITE, APR_UREAD | APR_UWRITE, p);
  518. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  519. s = "some data";
  520. nbytes = strlen(s);
  521. rv = fspr_file_write(f, s, &nbytes);
  522. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  523. ABTS_INT_EQUAL(tc, strlen(s), nbytes);
  524. rv = fspr_file_close(f);
  525. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  526. rv = fspr_file_open(&f, fname,
  527. APR_TRUNCATE | APR_WRITE, APR_UREAD | APR_UWRITE, p);
  528. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  529. rv = fspr_file_close(f);
  530. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  531. rv = fspr_stat(&finfo, fname, APR_FINFO_SIZE, p);
  532. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  533. ABTS_ASSERT(tc, "File size mismatch, expected 0 (empty)", finfo.size == 0);
  534. rv = fspr_file_remove(fname, p);
  535. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  536. }
  537. static void test_bigfprintf(abts_case *tc, void *data)
  538. {
  539. fspr_file_t *f;
  540. const char *fname = "data/testbigfprintf.dat";
  541. char *to_write;
  542. int i;
  543. fspr_file_remove(fname, p);
  544. APR_ASSERT_SUCCESS(tc, "open test file",
  545. fspr_file_open(&f, fname,
  546. APR_CREATE|APR_WRITE,
  547. APR_UREAD|APR_UWRITE, p));
  548. to_write = malloc(HUGE_STRING_LEN + 3);
  549. for (i = 0; i < HUGE_STRING_LEN + 1; ++i)
  550. to_write[i] = 'A' + i%26;
  551. strcpy(to_write + HUGE_STRING_LEN, "42");
  552. i = fspr_file_printf(f, "%s", to_write);
  553. ABTS_INT_EQUAL(tc, HUGE_STRING_LEN + 2, i);
  554. fspr_file_close(f);
  555. file_contents_equal(tc, fname, to_write, HUGE_STRING_LEN + 2);
  556. free(to_write);
  557. }
  558. static void test_fail_write_flush(abts_case *tc, void *data)
  559. {
  560. fspr_file_t *f;
  561. const char *fname = "data/testflush.dat";
  562. fspr_status_t rv;
  563. char buf[APR_BUFFERSIZE];
  564. int n;
  565. fspr_file_remove(fname, p);
  566. APR_ASSERT_SUCCESS(tc, "open test file",
  567. fspr_file_open(&f, fname,
  568. APR_CREATE|APR_READ|APR_BUFFERED,
  569. APR_UREAD|APR_UWRITE, p));
  570. memset(buf, 'A', sizeof buf);
  571. /* Try three writes. One of these should fail when it exceeds the
  572. * internal buffer and actually tries to write to the file, which
  573. * was opened read-only and hence should be unwritable. */
  574. for (n = 0, rv = APR_SUCCESS; n < 4 && rv == APR_SUCCESS; n++) {
  575. fspr_size_t bytes = sizeof buf;
  576. rv = fspr_file_write(f, buf, &bytes);
  577. }
  578. ABTS_ASSERT(tc, "failed to write to read-only buffered fd",
  579. rv != APR_SUCCESS);
  580. fspr_file_close(f);
  581. }
  582. static void test_fail_read_flush(abts_case *tc, void *data)
  583. {
  584. fspr_file_t *f;
  585. const char *fname = "data/testflush.dat";
  586. fspr_status_t rv;
  587. char buf[2];
  588. fspr_file_remove(fname, p);
  589. APR_ASSERT_SUCCESS(tc, "open test file",
  590. fspr_file_open(&f, fname,
  591. APR_CREATE|APR_READ|APR_BUFFERED,
  592. APR_UREAD|APR_UWRITE, p));
  593. /* this write should be buffered. */
  594. APR_ASSERT_SUCCESS(tc, "buffered write should succeed",
  595. fspr_file_puts("hello", f));
  596. /* Now, trying a read should fail since the write must be flushed,
  597. * and should fail with something other than EOF since the file is
  598. * opened read-only. */
  599. rv = fspr_file_read_full(f, buf, 2, NULL);
  600. ABTS_ASSERT(tc, "read should flush buffered write and fail",
  601. rv != APR_SUCCESS && rv != APR_EOF);
  602. /* Likewise for gets */
  603. rv = fspr_file_gets(buf, 2, f);
  604. ABTS_ASSERT(tc, "gets should flush buffered write and fail",
  605. rv != APR_SUCCESS && rv != APR_EOF);
  606. /* Likewise for seek. */
  607. {
  608. fspr_off_t offset = 0;
  609. rv = fspr_file_seek(f, APR_SET, &offset);
  610. }
  611. ABTS_ASSERT(tc, "seek should flush buffered write and fail",
  612. rv != APR_SUCCESS && rv != APR_EOF);
  613. fspr_file_close(f);
  614. }
  615. static void test_xthread(abts_case *tc, void *data)
  616. {
  617. fspr_file_t *f;
  618. const char *fname = "data/testxthread.dat";
  619. fspr_status_t rv;
  620. fspr_int32_t flags = APR_CREATE|APR_READ|APR_WRITE|APR_APPEND|APR_XTHREAD;
  621. char buf[128] = { 0 };
  622. /* Test for bug 38438, opening file with append + xthread and seeking to
  623. the end of the file resulted in writes going to the beginning not the
  624. end. */
  625. fspr_file_remove(fname, p);
  626. APR_ASSERT_SUCCESS(tc, "open test file",
  627. fspr_file_open(&f, fname, flags,
  628. APR_UREAD|APR_UWRITE, p));
  629. APR_ASSERT_SUCCESS(tc, "write should succeed",
  630. fspr_file_puts("hello", f));
  631. fspr_file_close(f);
  632. APR_ASSERT_SUCCESS(tc, "open test file",
  633. fspr_file_open(&f, fname, flags,
  634. APR_UREAD|APR_UWRITE, p));
  635. /* Seek to the end. */
  636. {
  637. fspr_off_t offset = 0;
  638. rv = fspr_file_seek(f, APR_END, &offset);
  639. }
  640. APR_ASSERT_SUCCESS(tc, "more writes should succeed",
  641. fspr_file_puts("world", f));
  642. /* Back to the beginning. */
  643. {
  644. fspr_off_t offset = 0;
  645. rv = fspr_file_seek(f, APR_SET, &offset);
  646. }
  647. fspr_file_read_full(f, buf, sizeof(buf), NULL);
  648. ABTS_STR_EQUAL(tc, "helloworld", buf);
  649. fspr_file_close(f);
  650. }
  651. abts_suite *testfile(abts_suite *suite)
  652. {
  653. suite = ADD_SUITE(suite)
  654. abts_run_test(suite, test_open_noreadwrite, NULL);
  655. abts_run_test(suite, test_open_excl, NULL);
  656. abts_run_test(suite, test_open_read, NULL);
  657. abts_run_test(suite, test_open_readwrite, NULL);
  658. abts_run_test(suite, test_read, NULL);
  659. abts_run_test(suite, test_readzero, NULL);
  660. abts_run_test(suite, test_seek, NULL);
  661. abts_run_test(suite, test_filename, NULL);
  662. abts_run_test(suite, test_fileclose, NULL);
  663. abts_run_test(suite, test_file_remove, NULL);
  664. abts_run_test(suite, test_open_write, NULL);
  665. abts_run_test(suite, test_open_writecreate, NULL);
  666. abts_run_test(suite, test_write, NULL);
  667. abts_run_test(suite, test_userdata_set, NULL);
  668. abts_run_test(suite, test_userdata_get, NULL);
  669. abts_run_test(suite, test_userdata_getnokey, NULL);
  670. abts_run_test(suite, test_getc, NULL);
  671. abts_run_test(suite, test_ungetc, NULL);
  672. abts_run_test(suite, test_gets, NULL);
  673. abts_run_test(suite, test_puts, NULL);
  674. abts_run_test(suite, test_writev, NULL);
  675. abts_run_test(suite, test_writev_full, NULL);
  676. abts_run_test(suite, test_bigread, NULL);
  677. abts_run_test(suite, test_mod_neg, NULL);
  678. abts_run_test(suite, test_truncate, NULL);
  679. abts_run_test(suite, test_bigfprintf, NULL);
  680. abts_run_test(suite, test_fail_write_flush, NULL);
  681. abts_run_test(suite, test_fail_read_flush, NULL);
  682. abts_run_test(suite, test_xthread, NULL);
  683. return suite;
  684. }