test_vfs_append.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include "test_utils.h"
  16. #define TEST_PARTITION_LABEL "flash_test"
  17. #define OPEN_MODE 0
  18. #define MSG1 "Hello"
  19. #define MSG2 " "
  20. #define MSG3 "world!"
  21. static inline void test_write(int fd, const char *str, const char *msg)
  22. {
  23. TEST_ASSERT_EQUAL_MESSAGE(strlen(str), write(fd, str, strlen(str)), msg);
  24. }
  25. static inline void test_read(int fd, const char *str, const char *msg)
  26. {
  27. char buf[strlen(str)];
  28. TEST_ASSERT_EQUAL_MESSAGE(strlen(str), read(fd, buf, strlen(str)), msg);
  29. TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(str, buf, strlen(str), msg);
  30. }
  31. static inline void test_read_fails(int fd, const char *msg)
  32. {
  33. char buf;
  34. TEST_ASSERT_EQUAL_MESSAGE(0, read(fd, &buf, 1), msg);
  35. }
  36. static void test_append(const char *path)
  37. {
  38. int fd = open(path, O_RDWR | O_APPEND | O_CREAT | O_TRUNC, OPEN_MODE);
  39. TEST_ASSERT_NOT_EQUAL(-1, fd);
  40. test_write(fd, MSG1, "write MSG1");
  41. test_read_fails(fd, "read fails MSG1");
  42. lseek(fd, 0, SEEK_SET);
  43. test_read(fd, MSG1, "read MSG1");
  44. lseek(fd, 0, SEEK_SET);
  45. test_write(fd, MSG2, "write MSG2");
  46. test_read_fails(fd, "read fails MSG2"); //because write moved the pointer
  47. lseek(fd, 0, SEEK_SET);
  48. test_read(fd, MSG1 MSG2, "read MSG1 + MSG2");
  49. TEST_ASSERT_NOT_EQUAL(-1, close(fd));
  50. fd = open(path, O_RDWR | O_APPEND, OPEN_MODE);
  51. TEST_ASSERT_NOT_EQUAL(-1, fd);
  52. //after reopening the pointer should be at the beginning
  53. test_read(fd, MSG1 MSG2, "read reopening");
  54. lseek(fd, strlen(MSG1), SEEK_SET);
  55. test_read(fd, MSG2, "read MSG2");
  56. lseek(fd, strlen(MSG1), SEEK_SET);
  57. test_write(fd, MSG3, "write MSG3");
  58. test_read_fails(fd, "read fails MSG3"); //because write moved the pointer
  59. lseek(fd, strlen(MSG1), SEEK_SET);
  60. test_read(fd, MSG2 MSG3, "read MSG2 + MSG3");
  61. lseek(fd, 0, SEEK_SET);
  62. test_read(fd, MSG1 MSG2 MSG3, "read MSG1 + MSG2 + MSG3");
  63. TEST_ASSERT_NOT_EQUAL(-1, close(fd));
  64. TEST_ASSERT_NOT_EQUAL(-1, unlink(path));
  65. }
  66. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  67. //IDF-5139
  68. TEST_CASE("open() with O_APPEND on FATFS works well", "[vfs][FATFS]")
  69. {
  70. wl_handle_t test_wl_handle;
  71. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  72. .format_if_mount_failed = true,
  73. .max_files = 2
  74. };
  75. TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &test_wl_handle));
  76. test_append("/spiflash/file.txt");
  77. TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", test_wl_handle));
  78. }
  79. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  80. TEST_CASE("open() with O_APPEND on SPIFFS works well", "[vfs][spiffs]")
  81. {
  82. esp_vfs_spiffs_conf_t conf = {
  83. .base_path = "/spiffs",
  84. .partition_label = TEST_PARTITION_LABEL,
  85. .max_files = 2,
  86. .format_if_mount_failed = true
  87. };
  88. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  89. test_append("/spiffs/file.txt");
  90. TEST_ESP_OK(esp_vfs_spiffs_unregister(TEST_PARTITION_LABEL));
  91. }