test_file.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <stdio_ext.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include "esp_vfs.h"
  12. #include "unity.h"
  13. #include "unity_fixture.h"
  14. TEST_GROUP(file);
  15. TEST_SETUP(file)
  16. {
  17. }
  18. TEST_TEAR_DOWN(file)
  19. {
  20. }
  21. /* This test checks that st_blksize value set in struct stat correctly affects the
  22. * FILE structure:
  23. * - _blksize field should be equal to st_blksize
  24. * - buffer size should be equal to st_blksize if it is nonzero, and __BUFSIZ__ otherwise.
  25. * This is more of an integration test since some of the functions responsible for this are
  26. * in ROM, and have been built without HAVE_BLKSIZE feature for the ESP32 chip.
  27. */
  28. typedef struct {
  29. unsigned blksize;
  30. } blksize_test_ctx_t;
  31. static int blksize_test_open(void* ctx, const char * path, int flags, int mode)
  32. {
  33. return 1;
  34. }
  35. static int blksize_test_fstat(void* ctx, int fd, struct stat * st)
  36. {
  37. blksize_test_ctx_t* test_ctx = (blksize_test_ctx_t*) ctx;
  38. memset(st, 0, sizeof(*st));
  39. st->st_mode = S_IFREG;
  40. st->st_blksize = test_ctx->blksize;
  41. return 0;
  42. }
  43. static ssize_t blksize_test_write(void* ctx, int fd, const void * data, size_t size)
  44. {
  45. return size;
  46. }
  47. TEST(file, blksize)
  48. {
  49. FILE* f;
  50. blksize_test_ctx_t ctx = {};
  51. const char c = 42;
  52. const esp_vfs_t desc = {
  53. .flags = ESP_VFS_FLAG_CONTEXT_PTR,
  54. .open_p = blksize_test_open,
  55. .fstat_p = blksize_test_fstat,
  56. .write_p = blksize_test_write,
  57. };
  58. TEST_ESP_OK(esp_vfs_register("/test", &desc, &ctx));
  59. /* test with zero st_blksize (=not set) */
  60. ctx.blksize = 0;
  61. f = fopen("/test/path", "w");
  62. TEST_ASSERT_NOT_NULL(f);
  63. fwrite(&c, 1, 1, f);
  64. TEST_ASSERT_EQUAL(0, f->_blksize);
  65. TEST_ASSERT_EQUAL(__BUFSIZ__, __fbufsize(f));
  66. fclose(f);
  67. /* test with non-zero st_blksize */
  68. ctx.blksize = 4096;
  69. f = fopen("/test/path", "w");
  70. TEST_ASSERT_NOT_NULL(f);
  71. fwrite(&c, 1, 1, f);
  72. TEST_ASSERT_EQUAL(ctx.blksize, f->_blksize);
  73. TEST_ASSERT_EQUAL(ctx.blksize, __fbufsize(f));
  74. fclose(f);
  75. TEST_ESP_OK(esp_vfs_unregister("/test"));
  76. }
  77. TEST_GROUP_RUNNER(file)
  78. {
  79. RUN_TEST_CASE(file, blksize)
  80. }