test_vfs_access.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include "unity.h"
  11. #include "driver/uart.h"
  12. #include "esp_vfs.h"
  13. #include "esp_vfs_dev.h"
  14. #include "esp_vfs_fat.h"
  15. #include "wear_levelling.h"
  16. #include "sdkconfig.h"
  17. static wl_handle_t test_wl_handle;
  18. TEST_CASE("Can use access() for UART", "[vfs]")
  19. {
  20. const char *uarts[] = {
  21. "/dev/uart/0",
  22. "/dev/uart/1",
  23. #if SOC_UART_HP_NUM > 2
  24. "/dev/uart/2"
  25. #endif
  26. };
  27. uart_driver_install(UART_NUM_0, 256, 0, 0, NULL, 0);
  28. uart_driver_install(UART_NUM_1, 256, 0, 0, NULL, 0);
  29. #if SOC_UART_HP_NUM > 2
  30. uart_driver_install(UART_NUM_2, 256, 0, 0, NULL, 0);
  31. #endif
  32. for (size_t i = 0; i < sizeof(uarts)/sizeof(uarts[0]); ++i) {
  33. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], F_OK), 0, uarts[i]);
  34. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK), 0, uarts[i]);
  35. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], W_OK), 0, uarts[i]);
  36. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], X_OK), -1, uarts[i]);
  37. TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
  38. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | W_OK), 0, uarts[i]);
  39. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | X_OK), -1, uarts[i]);
  40. TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
  41. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], W_OK | X_OK), -1, uarts[i]);
  42. TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
  43. TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | W_OK | X_OK), -1, uarts[i]);
  44. TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
  45. }
  46. TEST_ASSERT_EQUAL(access("/dev/uart/3", F_OK), -1);
  47. TEST_ASSERT_EQUAL(errno, ENOENT);
  48. uart_driver_delete(UART_NUM_0);
  49. uart_driver_delete(UART_NUM_1);
  50. #if SOC_UART_HP_NUM > 2
  51. uart_driver_delete(UART_NUM_2);
  52. #endif
  53. }
  54. static inline void test_spi_flash_setup(void)
  55. {
  56. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  57. .format_if_mount_failed = true,
  58. .max_files = 5
  59. };
  60. TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &test_wl_handle));
  61. }
  62. static inline void test_spi_flash_teardown(void)
  63. {
  64. TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", test_wl_handle));
  65. }
  66. static inline void test_fatfs_create_file(const char *name)
  67. {
  68. int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC);
  69. TEST_ASSERT_NOT_EQUAL(fd, -1);
  70. TEST_ASSERT_EQUAL(0, close(fd));
  71. }
  72. static inline void test_fatfs_delete_file(const char *name)
  73. {
  74. int ret = unlink(name);
  75. TEST_ASSERT_EQUAL(ret, 0);
  76. }
  77. TEST_CASE("Can use access() for FATFS", "[vfs][fatfs][wear_levelling]")
  78. {
  79. const char *path = "/spiflash/access.txt";
  80. test_spi_flash_setup();
  81. {
  82. int ret = access(path, F_OK);
  83. if (ret != -1) {
  84. // it wasn't deleted before so we delete it now to pass the test
  85. // case the next time
  86. test_fatfs_delete_file(path);
  87. }
  88. TEST_ASSERT_EQUAL(ret, -1);
  89. TEST_ASSERT_EQUAL(errno, ENOENT);
  90. }
  91. test_fatfs_create_file(path);
  92. TEST_ASSERT_EQUAL(access(path, F_OK), 0);
  93. TEST_ASSERT_EQUAL(access(path, R_OK), 0);
  94. TEST_ASSERT_EQUAL(access(path, W_OK), 0);
  95. TEST_ASSERT_EQUAL(access(path, X_OK), 0);
  96. TEST_ASSERT_EQUAL(access(path, R_OK | W_OK), 0);
  97. TEST_ASSERT_EQUAL(access(path, R_OK | X_OK), 0);
  98. TEST_ASSERT_EQUAL(access(path, W_OK | X_OK), 0);
  99. TEST_ASSERT_EQUAL(access(path, R_OK | W_OK | X_OK), 0);
  100. //TODO f_chmod the file and re-test the access rights (this requires
  101. // f_chmod support to be implemented in VFS)
  102. test_fatfs_delete_file(path);
  103. TEST_ASSERT_EQUAL(access(path, F_OK), -1);
  104. TEST_ASSERT_EQUAL(errno, ENOENT);
  105. test_spi_flash_teardown();
  106. }