test_vfs_append.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #include <sys/stat.h>
  18. #include "unity.h"
  19. #include "esp_vfs.h"
  20. #include "esp_vfs_fat.h"
  21. #include "esp_spiffs.h"
  22. #include "wear_levelling.h"
  23. #define TEST_PARTITION_LABEL "flash_test"
  24. #define OPEN_MODE 0
  25. #define MSG1 "Hello"
  26. #define MSG2 " "
  27. #define MSG3 "world!"
  28. static inline void test_write(int fd, const char *str, const char *msg)
  29. {
  30. TEST_ASSERT_EQUAL_MESSAGE(strlen(str), write(fd, str, strlen(str)), msg);
  31. }
  32. static inline void test_read(int fd, const char *str, const char *msg)
  33. {
  34. char buf[strlen(str)];
  35. TEST_ASSERT_EQUAL_MESSAGE(strlen(str), read(fd, buf, strlen(str)), msg);
  36. TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(str, buf, strlen(str), msg);
  37. }
  38. static inline void test_read_fails(int fd, const char *msg)
  39. {
  40. char buf;
  41. TEST_ASSERT_EQUAL_MESSAGE(0, read(fd, &buf, 1), msg);
  42. }
  43. static void test_append(const char *path)
  44. {
  45. int fd = open(path, O_RDWR | O_APPEND | O_CREAT | O_TRUNC, OPEN_MODE);
  46. TEST_ASSERT_NOT_EQUAL(-1, fd);
  47. test_write(fd, MSG1, "write MSG1");
  48. test_read_fails(fd, "read fails MSG1");
  49. lseek(fd, 0, SEEK_SET);
  50. test_read(fd, MSG1, "read MSG1");
  51. lseek(fd, 0, SEEK_SET);
  52. test_write(fd, MSG2, "write MSG2");
  53. test_read_fails(fd, "read fails MSG2"); //because write moved the pointer
  54. lseek(fd, 0, SEEK_SET);
  55. test_read(fd, MSG1 MSG2, "read MSG1 + MSG2");
  56. TEST_ASSERT_NOT_EQUAL(-1, close(fd));
  57. fd = open(path, O_RDWR | O_APPEND, OPEN_MODE);
  58. TEST_ASSERT_NOT_EQUAL(-1, fd);
  59. //after reopening the pointer should be at the beginning
  60. test_read(fd, MSG1 MSG2, "read reopening");
  61. lseek(fd, strlen(MSG1), SEEK_SET);
  62. test_read(fd, MSG2, "read MSG2");
  63. lseek(fd, strlen(MSG1), SEEK_SET);
  64. test_write(fd, MSG3, "write MSG3");
  65. test_read_fails(fd, "read fails MSG3"); //because write moved the pointer
  66. lseek(fd, strlen(MSG1), SEEK_SET);
  67. test_read(fd, MSG2 MSG3, "read MSG2 + MSG3");
  68. lseek(fd, 0, SEEK_SET);
  69. test_read(fd, MSG1 MSG2 MSG3, "read MSG1 + MSG2 + MSG3");
  70. TEST_ASSERT_NOT_EQUAL(-1, close(fd));
  71. TEST_ASSERT_NOT_EQUAL(-1, unlink(path));
  72. }
  73. TEST_CASE("open() with O_APPEND on FATFS works well", "[vfs][FATFS]")
  74. {
  75. wl_handle_t test_wl_handle;
  76. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  77. .format_if_mount_failed = true,
  78. .max_files = 2
  79. };
  80. TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &test_wl_handle));
  81. test_append("/spiflash/file.txt");
  82. TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", test_wl_handle));
  83. }
  84. TEST_CASE("open() with O_APPEND on SPIFFS works well", "[vfs][spiffs]")
  85. {
  86. esp_vfs_spiffs_conf_t conf = {
  87. .base_path = "/spiffs",
  88. .partition_label = TEST_PARTITION_LABEL,
  89. .max_files = 2,
  90. .format_if_mount_failed = true
  91. };
  92. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  93. test_append("/spiffs/file.txt");
  94. TEST_ESP_OK(esp_vfs_spiffs_unregister(TEST_PARTITION_LABEL));
  95. }