|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 OTA keys S.A. |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @{ |
| 11 | + * |
| 12 | + * @file |
| 13 | + */ |
| 14 | +#include "fs/spiffs_fs.h" |
| 15 | +#include "vfs.h" |
| 16 | +#include "mtdi.h" |
| 17 | +#include "native_mtdi.h" |
| 18 | + |
| 19 | +#include <fcntl.h> |
| 20 | + |
| 21 | +#include "embUnit/embUnit.h" |
| 22 | + |
| 23 | +#include "tests-spiffs.h" |
| 24 | + |
| 25 | +static mtdi_dev_t _dev = { |
| 26 | + .driver = &native_flash_driver, |
| 27 | +}; |
| 28 | + |
| 29 | +static struct spiffs_desc spiffs_desc = { |
| 30 | + .lock = MUTEX_INIT, |
| 31 | + .dev = &_dev, |
| 32 | +}; |
| 33 | + |
| 34 | +static void test_spiffs_mount_umount(void) |
| 35 | +{ |
| 36 | + int res = vfs_mount(&spiffs_file_system, "/", &spiffs_desc); |
| 37 | + TEST_ASSERT(res >= 0); |
| 38 | + |
| 39 | + res = vfs_umount(res); |
| 40 | + TEST_ASSERT(res >= 0); |
| 41 | +} |
| 42 | + |
| 43 | +static void tests_spiffs_open_close(void) |
| 44 | +{ |
| 45 | + int mp = vfs_mount(&spiffs_file_system, "/", &spiffs_desc); |
| 46 | + TEST_ASSERT(mp >= 0); |
| 47 | + |
| 48 | + int res = vfs_open("/test.txt", O_CREAT | O_RDWR, 0); |
| 49 | + TEST_ASSERT(res >= 0); |
| 50 | + |
| 51 | + res = vfs_close(res); |
| 52 | + TEST_ASSERT(res == 0); |
| 53 | + |
| 54 | + res = vfs_umount(mp); |
| 55 | + TEST_ASSERT(res >= 0); |
| 56 | +} |
| 57 | + |
| 58 | +static void tests_spiffs_write(void) |
| 59 | +{ |
| 60 | + const char buf[] = "TESTSTRING"; |
| 61 | + char r_buf[2 * sizeof(buf)]; |
| 62 | + |
| 63 | + int mp = vfs_mount(&spiffs_file_system, "/", &spiffs_desc); |
| 64 | + TEST_ASSERT(mp >= 0); |
| 65 | + |
| 66 | + int fd = vfs_open("/test.txt", O_CREAT | O_RDWR, 0); |
| 67 | + TEST_ASSERT(fd >= 0); |
| 68 | + |
| 69 | + int res = vfs_write(fd, buf, sizeof(buf)); |
| 70 | + TEST_ASSERT_EQUAL_INT(sizeof(buf), res); |
| 71 | + |
| 72 | + res = vfs_lseek(fd, 0, SEEK_SET); |
| 73 | + TEST_ASSERT(res == 0); |
| 74 | + |
| 75 | + res = vfs_read(fd, r_buf, sizeof(r_buf)); |
| 76 | + TEST_ASSERT(res == sizeof(buf)); |
| 77 | + //TEST_ASSERT_EQUAL_STRING(buf, r_buf); |
| 78 | + printf("read buf: %s\n", r_buf); |
| 79 | + |
| 80 | + res = vfs_close(fd); |
| 81 | + TEST_ASSERT(res == 0); |
| 82 | + |
| 83 | + res = vfs_umount(mp); |
| 84 | + TEST_ASSERT(res >= 0); |
| 85 | +} |
| 86 | + |
| 87 | +static void tests_spiffs_unlink(void) |
| 88 | +{ |
| 89 | + const char buf[] = "TESTSTRING"; |
| 90 | + |
| 91 | + int mp = vfs_mount(&spiffs_file_system, "/", &spiffs_desc); |
| 92 | + TEST_ASSERT(mp >= 0); |
| 93 | + |
| 94 | + int fd = vfs_open("/test.txt", O_CREAT | O_RDWR, 0); |
| 95 | + TEST_ASSERT(fd >= 0); |
| 96 | + |
| 97 | + int res = vfs_write(fd, buf, sizeof(buf)); |
| 98 | + TEST_ASSERT(res == sizeof(buf)); |
| 99 | + |
| 100 | + res = vfs_close(fd); |
| 101 | + TEST_ASSERT(res == 0); |
| 102 | + |
| 103 | + res = vfs_unlink("/test.txt"); |
| 104 | + TEST_ASSERT(res == 0); |
| 105 | + |
| 106 | + res = vfs_umount(mp); |
| 107 | + TEST_ASSERT(res >= 0); |
| 108 | +} |
| 109 | + |
| 110 | +Test *tests_spiffs_tests(void) |
| 111 | +{ |
| 112 | + EMB_UNIT_TESTFIXTURES(fixtures) { |
| 113 | + new_TestFixture(test_spiffs_mount_umount), |
| 114 | + new_TestFixture(tests_spiffs_open_close), |
| 115 | + new_TestFixture(tests_spiffs_write), |
| 116 | + new_TestFixture(tests_spiffs_unlink), |
| 117 | + }; |
| 118 | + |
| 119 | + EMB_UNIT_TESTCALLER(spiffs_tests, NULL, NULL, fixtures); |
| 120 | + |
| 121 | + return (Test *)&spiffs_tests; |
| 122 | +} |
| 123 | + |
| 124 | +void tests_spiffs(void) |
| 125 | +{ |
| 126 | + TESTS_RUN(tests_spiffs_tests()); |
| 127 | +} |
| 128 | +/** @} */ |
0 commit comments