test_vfs_access.c 4.2 KB

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