test_dev.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * test_dev.c
  3. *
  4. * Created on: Jul 14, 2013
  5. * Author: petera
  6. */
  7. #include "testrunner.h"
  8. #include "test_spiffs.h"
  9. #include "spiffs_nucleus.h"
  10. #include "spiffs.h"
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <dirent.h>
  15. #include <unistd.h>
  16. SUITE(dev_tests)
  17. void setup() {
  18. _setup();
  19. }
  20. void teardown() {
  21. _teardown();
  22. }
  23. TEST(interrupted_write) {
  24. char *name = "interrupt";
  25. char *name2 = "interrupt2";
  26. int res;
  27. spiffs_file fd;
  28. const u32_t sz = SPIFFS_CFG_LOG_PAGE_SZ(FS)*8;
  29. u8_t *buf = malloc(sz);
  30. memrand(buf, sz);
  31. printf(" create reference file\n");
  32. fd = SPIFFS_open(FS, name, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
  33. TEST_CHECK(fd > 0);
  34. clear_flash_ops_log();
  35. res = SPIFFS_write(FS, fd, buf, sz);
  36. TEST_CHECK(res >= 0);
  37. SPIFFS_close(FS, fd);
  38. u32_t written = get_flash_ops_log_write_bytes();
  39. printf(" written bytes: %i\n", written);
  40. printf(" create error file\n");
  41. fd = SPIFFS_open(FS, name2, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
  42. TEST_CHECK(fd > 0);
  43. clear_flash_ops_log();
  44. invoke_error_after_write_bytes(written/2, 0);
  45. res = SPIFFS_write(FS, fd, buf, sz);
  46. SPIFFS_close(FS, fd);
  47. TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_TEST);
  48. clear_flash_ops_log();
  49. #if SPIFFS_CACHE
  50. // delete all cache
  51. spiffs_cache *cache = spiffs_get_cache(FS);
  52. cache->cpage_use_map = 0;
  53. #endif
  54. printf(" read error file\n");
  55. fd = SPIFFS_open(FS, name2, SPIFFS_RDONLY, 0);
  56. TEST_CHECK(fd > 0);
  57. spiffs_stat s;
  58. res = SPIFFS_fstat(FS, fd, &s);
  59. TEST_CHECK(res >= 0);
  60. printf(" file size: %i\n", s.size);
  61. if (s.size > 0) {
  62. u8_t *buf2 = malloc(s.size);
  63. res = SPIFFS_read(FS, fd, buf2, s.size);
  64. TEST_CHECK(res >= 0);
  65. u32_t ix = 0;
  66. for (ix = 0; ix < s.size; ix += 16) {
  67. int i;
  68. printf(" ");
  69. for (i = 0; i < 16; i++) {
  70. printf("%02x", buf[ix+i]);
  71. }
  72. printf(" ");
  73. for (i = 0; i < 16; i++) {
  74. printf("%02x", buf2[ix+i]);
  75. }
  76. printf("\n");
  77. }
  78. free(buf2);
  79. }
  80. SPIFFS_close(FS, fd);
  81. printf(" FS check\n");
  82. SPIFFS_check(FS);
  83. printf(" read error file again\n");
  84. fd = SPIFFS_open(FS, name2, SPIFFS_APPEND | SPIFFS_RDWR, 0);
  85. TEST_CHECK(fd > 0);
  86. res = SPIFFS_fstat(FS, fd, &s);
  87. TEST_CHECK(res >= 0);
  88. printf(" file size: %i\n", s.size);
  89. printf(" write file\n");
  90. res = SPIFFS_write(FS, fd, buf, sz);
  91. TEST_CHECK(res >= 0);
  92. SPIFFS_close(FS, fd);
  93. free(buf);
  94. return TEST_RES_OK;
  95. } TEST_END(interrupted_write)
  96. SUITE_END(dev_tests)