file.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "test.h"
  20. #include <pjlib.h>
  21. #if INCLUDE_FILE_TEST
  22. #define FILENAME "testfil1.txt"
  23. #define NEWNAME "testfil2.txt"
  24. #define INCLUDE_FILE_TIME_TEST 0
  25. static char buffer[11] = {'H', 'e', 'l', 'l', 'o', ' ',
  26. 'W', 'o', 'r', 'l', 'd' };
  27. static int file_test_internal(void)
  28. {
  29. enum { FILE_MAX_AGE = 1000 };
  30. pj_oshandle_t fd = 0;
  31. pj_status_t status;
  32. char readbuf[sizeof(buffer)+16];
  33. pj_file_stat stat;
  34. pj_time_val start_time;
  35. pj_ssize_t size;
  36. pj_off_t pos;
  37. PJ_LOG(3,("", "..file io test.."));
  38. /* Get time. */
  39. pj_gettimeofday(&start_time);
  40. /* Delete original file if exists. */
  41. if (pj_file_exists(FILENAME))
  42. pj_file_delete(FILENAME);
  43. /*
  44. * Write data to the file.
  45. */
  46. status = pj_file_open(NULL, FILENAME, PJ_O_WRONLY, &fd);
  47. if (status != PJ_SUCCESS) {
  48. app_perror("...file_open() error", status);
  49. return -10;
  50. }
  51. size = sizeof(buffer);
  52. status = pj_file_write(fd, buffer, &size);
  53. if (status != PJ_SUCCESS) {
  54. app_perror("...file_write() error", status);
  55. pj_file_close(fd);
  56. return -20;
  57. }
  58. if (size != sizeof(buffer))
  59. return -25;
  60. status = pj_file_close(fd);
  61. if (status != PJ_SUCCESS) {
  62. app_perror("...file_close() error", status);
  63. return -30;
  64. }
  65. /* Check the file existance and size. */
  66. if (!pj_file_exists(FILENAME))
  67. return -40;
  68. if (pj_file_size(FILENAME) != sizeof(buffer))
  69. return -50;
  70. /* Get file stat. */
  71. status = pj_file_getstat(FILENAME, &stat);
  72. if (status != PJ_SUCCESS)
  73. return -60;
  74. /* Check stat size. */
  75. if (stat.size != sizeof(buffer))
  76. return -70;
  77. #if INCLUDE_FILE_TIME_TEST
  78. /* Check file creation time >= start_time. */
  79. if (!PJ_TIME_VAL_GTE(stat.ctime, start_time))
  80. return -80;
  81. /* Check file creation time is not much later. */
  82. PJ_TIME_VAL_SUB(stat.ctime, start_time);
  83. if (stat.ctime.sec > FILE_MAX_AGE)
  84. return -90;
  85. /* Check file modification time >= start_time. */
  86. if (!PJ_TIME_VAL_GTE(stat.mtime, start_time))
  87. return -80;
  88. /* Check file modification time is not much later. */
  89. PJ_TIME_VAL_SUB(stat.mtime, start_time);
  90. if (stat.mtime.sec > FILE_MAX_AGE)
  91. return -90;
  92. /* Check file access time >= start_time. */
  93. if (!PJ_TIME_VAL_GTE(stat.atime, start_time))
  94. return -80;
  95. /* Check file access time is not much later. */
  96. PJ_TIME_VAL_SUB(stat.atime, start_time);
  97. if (stat.atime.sec > FILE_MAX_AGE)
  98. return -90;
  99. #endif
  100. /*
  101. * Re-open the file and read data.
  102. */
  103. status = pj_file_open(NULL, FILENAME, PJ_O_RDONLY, &fd);
  104. if (status != PJ_SUCCESS) {
  105. app_perror("...file_open() error", status);
  106. return -100;
  107. }
  108. size = 0;
  109. while (size < (pj_ssize_t)sizeof(readbuf)) {
  110. pj_ssize_t read;
  111. read = 1;
  112. status = pj_file_read(fd, &readbuf[size], &read);
  113. if (status != PJ_SUCCESS) {
  114. PJ_LOG(3,("", "...error reading file after %ld bytes "
  115. "(error follows)", size));
  116. app_perror("...error", status);
  117. return -110;
  118. }
  119. if (read == 0) {
  120. // EOF
  121. break;
  122. }
  123. size += read;
  124. }
  125. if (size != sizeof(buffer))
  126. return -120;
  127. /*
  128. if (!pj_file_eof(fd, PJ_O_RDONLY))
  129. return -130;
  130. */
  131. if (pj_memcmp(readbuf, buffer, size) != 0)
  132. return -140;
  133. /* Seek test. */
  134. status = pj_file_setpos(fd, 4, PJ_SEEK_SET);
  135. if (status != PJ_SUCCESS) {
  136. app_perror("...file_setpos() error", status);
  137. return -141;
  138. }
  139. /* getpos test. */
  140. status = pj_file_getpos(fd, &pos);
  141. if (status != PJ_SUCCESS) {
  142. app_perror("...file_getpos() error", status);
  143. return -142;
  144. }
  145. if (pos != 4)
  146. return -143;
  147. status = pj_file_close(fd);
  148. if (status != PJ_SUCCESS) {
  149. app_perror("...file_close() error", status);
  150. return -150;
  151. }
  152. /*
  153. * Rename test.
  154. */
  155. status = pj_file_move(FILENAME, NEWNAME);
  156. if (status != PJ_SUCCESS) {
  157. app_perror("...file_move() error", status);
  158. return -160;
  159. }
  160. if (pj_file_exists(FILENAME))
  161. return -170;
  162. if (!pj_file_exists(NEWNAME))
  163. return -180;
  164. if (pj_file_size(NEWNAME) != sizeof(buffer))
  165. return -190;
  166. /* Delete test. */
  167. status = pj_file_delete(NEWNAME);
  168. if (status != PJ_SUCCESS) {
  169. app_perror("...file_delete() error", status);
  170. return -200;
  171. }
  172. if (pj_file_exists(NEWNAME))
  173. return -210;
  174. PJ_LOG(3,("", "...success"));
  175. return PJ_SUCCESS;
  176. }
  177. int file_test(void)
  178. {
  179. int rc = file_test_internal();
  180. /* Delete test file if exists. */
  181. if (pj_file_exists(FILENAME))
  182. pj_file_delete(FILENAME);
  183. return rc;
  184. }
  185. #else
  186. int dummy_file_test;
  187. #endif