test_vfs_access.c 4.1 KB

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