test_vfs_append.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include "unity.h"
  11. #include "esp_vfs.h"
  12. #include "esp_vfs_fat.h"
  13. #include "esp_spiffs.h"
  14. #include "wear_levelling.h"
  15. #define TEST_PARTITION_LABEL "flash_test"
  16. #define OPEN_MODE 0
  17. #define MSG1 "Hello"
  18. #define MSG2 " "
  19. #define MSG3 "world!"
  20. static inline void test_write(int fd, const char *str, const char *msg)
  21. {
  22. TEST_ASSERT_EQUAL_MESSAGE(strlen(str), write(fd, str, strlen(str)), msg);
  23. }
  24. static inline void test_read(int fd, const char *str, const char *msg)
  25. {
  26. char buf[strlen(str)];
  27. TEST_ASSERT_EQUAL_MESSAGE(strlen(str), read(fd, buf, strlen(str)), msg);
  28. TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(str, buf, strlen(str), msg);
  29. }
  30. static inline void test_read_fails(int fd, const char *msg)
  31. {
  32. char buf;
  33. TEST_ASSERT_EQUAL_MESSAGE(0, read(fd, &buf, 1), msg);
  34. }
  35. static void test_append(const char *path)
  36. {
  37. int fd = open(path, O_RDWR | O_APPEND | O_CREAT | O_TRUNC, OPEN_MODE);
  38. TEST_ASSERT_NOT_EQUAL(-1, fd);
  39. test_write(fd, MSG1, "write MSG1");
  40. test_read_fails(fd, "read fails MSG1");
  41. lseek(fd, 0, SEEK_SET);
  42. test_read(fd, MSG1, "read MSG1");
  43. lseek(fd, 0, SEEK_SET);
  44. test_write(fd, MSG2, "write MSG2");
  45. test_read_fails(fd, "read fails MSG2"); //because write moved the pointer
  46. lseek(fd, 0, SEEK_SET);
  47. test_read(fd, MSG1 MSG2, "read MSG1 + MSG2");
  48. TEST_ASSERT_NOT_EQUAL(-1, close(fd));
  49. fd = open(path, O_RDWR | O_APPEND, OPEN_MODE);
  50. TEST_ASSERT_NOT_EQUAL(-1, fd);
  51. //after reopening the pointer should be at the beginning
  52. test_read(fd, MSG1 MSG2, "read reopening");
  53. lseek(fd, strlen(MSG1), SEEK_SET);
  54. test_read(fd, MSG2, "read MSG2");
  55. lseek(fd, strlen(MSG1), SEEK_SET);
  56. test_write(fd, MSG3, "write MSG3");
  57. test_read_fails(fd, "read fails MSG3"); //because write moved the pointer
  58. lseek(fd, strlen(MSG1), SEEK_SET);
  59. test_read(fd, MSG2 MSG3, "read MSG2 + MSG3");
  60. lseek(fd, 0, SEEK_SET);
  61. test_read(fd, MSG1 MSG2 MSG3, "read MSG1 + MSG2 + MSG3");
  62. TEST_ASSERT_NOT_EQUAL(-1, close(fd));
  63. TEST_ASSERT_NOT_EQUAL(-1, unlink(path));
  64. }
  65. TEST_CASE("open() with O_APPEND on FATFS works well", "[vfs][FATFS]")
  66. {
  67. wl_handle_t test_wl_handle;
  68. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  69. .format_if_mount_failed = true,
  70. .max_files = 2
  71. };
  72. TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &test_wl_handle));
  73. test_append("/spiflash/file.txt");
  74. TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", test_wl_handle));
  75. }
  76. TEST_CASE("open() with O_APPEND on SPIFFS works well", "[vfs][spiffs]")
  77. {
  78. esp_vfs_spiffs_conf_t conf = {
  79. .base_path = "/spiffs",
  80. .partition_label = TEST_PARTITION_LABEL,
  81. .max_files = 2,
  82. .format_if_mount_failed = true
  83. };
  84. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  85. test_append("/spiffs/file.txt");
  86. TEST_ESP_OK(esp_vfs_spiffs_unregister(TEST_PARTITION_LABEL));
  87. }