2022-02-21 16:09:24 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-05-22 04:47:30 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2019-10-08 05:55:12 -04:00
|
|
|
#include <stdlib.h>
|
2019-01-14 20:59:09 -05:00
|
|
|
#include <sys/mman.h>
|
2019-10-08 05:55:12 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2019-01-14 20:59:09 -05:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
2019-10-08 05:55:12 -04:00
|
|
|
#include <unistd.h>
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
#include "Mockqueue.h"
|
|
|
|
|
2018-05-22 04:47:30 -04:00
|
|
|
#include "esp_partition.h"
|
|
|
|
#include "spiffs.h"
|
|
|
|
#include "spiffs_nucleus.h"
|
|
|
|
#include "spiffs_api.h"
|
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
#include "unity.h"
|
|
|
|
#include "unity_fixture.h"
|
|
|
|
|
|
|
|
TEST_GROUP(spiffs);
|
|
|
|
|
|
|
|
TEST_SETUP(spiffs)
|
|
|
|
{
|
|
|
|
// CMock init for spiffs xSemaphore* use
|
|
|
|
xQueueSemaphoreTake_IgnoreAndReturn(0);
|
|
|
|
xQueueGenericSend_IgnoreAndReturn(0);
|
|
|
|
}
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_TEAR_DOWN(spiffs)
|
|
|
|
{
|
|
|
|
}
|
2018-05-26 02:58:01 -04:00
|
|
|
|
2019-01-14 20:59:09 -05:00
|
|
|
static void init_spiffs(spiffs *fs, uint32_t max_files)
|
2018-05-22 04:47:30 -04:00
|
|
|
{
|
2022-08-25 09:23:59 -04:00
|
|
|
spiffs_config cfg = {};
|
2019-01-14 20:59:09 -05:00
|
|
|
s32_t spiffs_res;
|
2022-08-25 09:23:59 -04:00
|
|
|
u32_t flash_sector_size;
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, "storage");
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_NOT_NULL(partition);
|
2018-05-26 02:58:01 -04:00
|
|
|
|
2018-05-22 04:47:30 -04:00
|
|
|
// Configure objects needed by SPIFFS
|
2022-08-25 09:23:59 -04:00
|
|
|
esp_spiffs_t *user_data = (esp_spiffs_t *) calloc(1, sizeof(*user_data));
|
2019-01-14 20:59:09 -05:00
|
|
|
user_data->partition = partition;
|
2022-08-25 09:23:59 -04:00
|
|
|
fs->user_data = (void *)user_data;
|
|
|
|
|
|
|
|
flash_sector_size = 4096;
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
cfg.hal_erase_f = spiffs_api_erase;
|
|
|
|
cfg.hal_read_f = spiffs_api_read;
|
|
|
|
cfg.hal_write_f = spiffs_api_write;
|
2022-08-25 09:23:59 -04:00
|
|
|
cfg.log_block_size = flash_sector_size;
|
2018-05-22 04:47:30 -04:00
|
|
|
cfg.log_page_size = CONFIG_SPIFFS_PAGE_SIZE;
|
|
|
|
cfg.phys_addr = 0;
|
2022-08-25 09:23:59 -04:00
|
|
|
cfg.phys_erase_block = flash_sector_size;
|
2018-05-26 02:58:01 -04:00
|
|
|
cfg.phys_size = partition->size;
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2019-01-14 20:59:09 -05:00
|
|
|
uint32_t work_sz = cfg.log_page_size * 2;
|
2022-08-25 09:23:59 -04:00
|
|
|
uint8_t *work = (uint8_t *) malloc(work_sz);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
uint32_t fds_sz = max_files * sizeof(spiffs_fd);
|
2022-08-25 09:23:59 -04:00
|
|
|
uint8_t *fds = (uint8_t *) malloc(fds_sz);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
#if CONFIG_SPIFFS_CACHE
|
2018-05-22 04:47:30 -04:00
|
|
|
uint32_t cache_sz = sizeof(spiffs_cache) + max_files * (sizeof(spiffs_cache_page)
|
2022-08-25 09:23:59 -04:00
|
|
|
+ cfg.log_page_size);
|
|
|
|
uint8_t *cache = (uint8_t *) malloc(cache_sz);
|
2019-01-14 20:59:09 -05:00
|
|
|
#else
|
|
|
|
uint32_t cache_sz = 0;
|
|
|
|
uint8_t cache = NULL;
|
|
|
|
#endif
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
// Special mounting procedure: mount, format, mount as per
|
|
|
|
// https://github.com/pellepl/spiffs/wiki/Using-spiffs
|
2019-01-14 20:59:09 -05:00
|
|
|
spiffs_res = SPIFFS_mount(fs, &cfg, work, fds, fds_sz,
|
2022-08-25 09:23:59 -04:00
|
|
|
cache, cache_sz, spiffs_api_check);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2019-01-14 20:59:09 -05:00
|
|
|
if (spiffs_res == SPIFFS_ERR_NOT_A_FS) {
|
|
|
|
spiffs_res = SPIFFS_format(fs);
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res >= SPIFFS_OK);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
spiffs_res = SPIFFS_mount(fs, &cfg, work, fds, fds_sz,
|
2022-08-25 09:23:59 -04:00
|
|
|
cache, cache_sz, spiffs_api_check);
|
2019-01-14 20:59:09 -05:00
|
|
|
}
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res >= SPIFFS_OK);
|
2019-01-14 20:59:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void deinit_spiffs(spiffs *fs)
|
|
|
|
{
|
|
|
|
SPIFFS_unmount(fs);
|
|
|
|
|
|
|
|
free(fs->work);
|
|
|
|
free(fs->user_data);
|
|
|
|
free(fs->fd_space);
|
|
|
|
|
|
|
|
#if CONFIG_SPIFFS_CACHE
|
|
|
|
free(fs->cache);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
static void check_spiffs_files(spiffs *fs, const char *base_path, char *cur_path)
|
2019-01-14 20:59:09 -05:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
size_t len = strlen(cur_path);
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
strcpy(cur_path, base_path);
|
|
|
|
len = strlen(base_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = opendir(cur_path);
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(dir != 0);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
|
|
char *name = entry->d_name;
|
2019-10-08 05:55:12 -04:00
|
|
|
|
|
|
|
char path[PATH_MAX] = { 0 };
|
|
|
|
|
|
|
|
// Read the file from host FS
|
|
|
|
strcpy(path, cur_path);
|
|
|
|
strcat(path, "/");
|
|
|
|
strcat(path, name);
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
stat(path, &sb);
|
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
2022-08-25 09:23:59 -04:00
|
|
|
if (!strcmp(name, ".") || !strcmp(name, "..")) {
|
2019-01-14 20:59:09 -05:00
|
|
|
continue;
|
2022-08-25 09:23:59 -04:00
|
|
|
}
|
2019-01-14 20:59:09 -05:00
|
|
|
cur_path[len] = '/';
|
|
|
|
strcpy(cur_path + len + 1, name);
|
|
|
|
check_spiffs_files(fs, base_path, cur_path);
|
|
|
|
cur_path[len] = '\0';
|
|
|
|
} else {
|
2022-08-25 09:23:59 -04:00
|
|
|
FILE *f = fopen(path, "r");
|
|
|
|
TEST_ASSERT_NOT_NULL(f);
|
2019-01-14 20:59:09 -05:00
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
long sz = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
char *f_contents = (char *) malloc(sz);
|
2023-01-16 22:44:14 -05:00
|
|
|
TEST_ASSERT(fread(f_contents, 1, sz, f) == sz);
|
2019-01-14 20:59:09 -05:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
s32_t spiffs_res;
|
|
|
|
|
|
|
|
// Read the file from SPIFFS
|
|
|
|
char *spiffs_path = path + strlen(base_path);
|
|
|
|
spiffs_res = SPIFFS_open(fs, spiffs_path, SPIFFS_RDONLY, 0);
|
2019-10-08 05:55:12 -04:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res > SPIFFS_OK);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
spiffs_file fd = spiffs_res;
|
|
|
|
|
|
|
|
spiffs_stat stat;
|
|
|
|
spiffs_res = SPIFFS_stat(fs, spiffs_path, &stat);
|
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
char *spiffs_f_contents = (char *) malloc(stat.size);
|
2019-01-14 20:59:09 -05:00
|
|
|
spiffs_res = SPIFFS_read(fs, fd, spiffs_f_contents, stat.size);
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res == stat.size);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
// Compare the contents
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(sz == stat.size);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
bool same = memcmp(f_contents, spiffs_f_contents, sz) == 0;
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(same);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
free(f_contents);
|
|
|
|
free(spiffs_f_contents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST(spiffs, format_disk_open_file_write_and_read_file)
|
2019-01-14 20:59:09 -05:00
|
|
|
{
|
|
|
|
spiffs fs;
|
|
|
|
s32_t spiffs_res;
|
|
|
|
|
|
|
|
init_spiffs(&fs, 5);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
// Open test file
|
2019-01-14 20:59:09 -05:00
|
|
|
spiffs_res = SPIFFS_open(&fs, "test.txt", SPIFFS_O_CREAT | SPIFFS_O_RDWR, 0);
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res >= SPIFFS_OK);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
// Generate data
|
2018-05-22 04:47:30 -04:00
|
|
|
spiffs_file file = spiffs_res;
|
|
|
|
|
2023-06-22 09:52:06 -04:00
|
|
|
uint32_t data_count = 5000;
|
|
|
|
uint32_t data_size = data_count * sizeof(uint32_t);
|
2018-05-26 02:58:01 -04:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
char *data = (char *) malloc(data_size);
|
|
|
|
char *read = (char *) malloc(data_size);
|
2018-05-26 02:58:01 -04:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
for (uint32_t i = 0; i < data_size; i += sizeof(i)) {
|
|
|
|
*((uint32_t *)(data + i)) = i;
|
2018-05-26 02:58:01 -04:00
|
|
|
}
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
// Write data to file
|
2022-08-25 09:23:59 -04:00
|
|
|
spiffs_res = SPIFFS_write(&fs, file, (void *)data, data_size);
|
|
|
|
TEST_ASSERT_TRUE(spiffs_res >= SPIFFS_OK);
|
|
|
|
TEST_ASSERT_TRUE(spiffs_res == data_size);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
// Set the file object pointer to the beginning
|
|
|
|
spiffs_res = SPIFFS_lseek(&fs, file, 0, SPIFFS_SEEK_SET);
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res >= SPIFFS_OK);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
2018-05-26 02:58:01 -04:00
|
|
|
// Read the file
|
2022-08-25 09:23:59 -04:00
|
|
|
spiffs_res = SPIFFS_read(&fs, file, (void *)read, data_size);
|
|
|
|
TEST_ASSERT_TRUE(spiffs_res >= SPIFFS_OK);
|
|
|
|
TEST_ASSERT_TRUE(spiffs_res == data_size);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
// Close the test file
|
|
|
|
spiffs_res = SPIFFS_close(&fs, file);
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res >= SPIFFS_OK);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(memcmp(data, read, data_size) == 0);
|
2018-05-26 02:58:01 -04:00
|
|
|
|
2019-01-14 20:59:09 -05:00
|
|
|
deinit_spiffs(&fs);
|
2018-05-22 04:47:30 -04:00
|
|
|
|
|
|
|
free(read);
|
2018-05-26 02:58:01 -04:00
|
|
|
free(data);
|
|
|
|
}
|
2019-01-14 20:59:09 -05:00
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST(spiffs, can_read_spiffs_image)
|
2019-01-14 20:59:09 -05:00
|
|
|
{
|
|
|
|
spiffs fs;
|
|
|
|
s32_t spiffs_res;
|
|
|
|
|
|
|
|
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, "storage");
|
2023-03-31 03:04:54 -04:00
|
|
|
TEST_ASSERT_NOT_NULL(partition);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
// Write the contents of the image file to partition
|
2023-03-31 03:04:54 -04:00
|
|
|
FILE *img_file = fopen(BUILD_DIR "/image.bin", "r");
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_NOT_NULL(img_file);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
fseek(img_file, 0, SEEK_END);
|
|
|
|
long img_size = ftell(img_file);
|
|
|
|
fseek(img_file, 0, SEEK_SET);
|
|
|
|
|
2022-08-25 09:23:59 -04:00
|
|
|
char *img = (char *) malloc(img_size);
|
2023-01-16 22:44:14 -05:00
|
|
|
TEST_ASSERT(fread(img, 1, img_size, img_file) == img_size);
|
2019-01-14 20:59:09 -05:00
|
|
|
fclose(img_file);
|
2023-03-31 03:04:54 -04:00
|
|
|
TEST_ASSERT_EQUAL(partition->size, img_size);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
esp_partition_erase_range(partition, 0, partition->size);
|
|
|
|
esp_partition_write(partition, 0, img, img_size);
|
|
|
|
|
|
|
|
free(img);
|
|
|
|
|
|
|
|
// Mount the spiffs partition and init filesystem, using the contents of
|
|
|
|
// the image file
|
|
|
|
init_spiffs(&fs, 1024);
|
|
|
|
|
|
|
|
// Check spiffs consistency
|
|
|
|
spiffs_res = SPIFFS_check(&fs);
|
2022-08-25 09:23:59 -04:00
|
|
|
TEST_ASSERT_TRUE(spiffs_res == SPIFFS_OK);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
2023-01-20 11:58:52 -05:00
|
|
|
char path_buf[PATH_MAX] = {0};
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
// The image is created from the spiffs source directory. Compare the files in that
|
|
|
|
// directory to the files read from the SPIFFS image.
|
2023-03-31 03:04:54 -04:00
|
|
|
check_spiffs_files(&fs, BUILD_DIR "/../../spiffs", path_buf);
|
2019-01-14 20:59:09 -05:00
|
|
|
|
|
|
|
deinit_spiffs(&fs);
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|
2022-08-25 09:23:59 -04:00
|
|
|
|
|
|
|
TEST_GROUP_RUNNER(spiffs)
|
|
|
|
{
|
|
|
|
RUN_TEST_CASE(spiffs, format_disk_open_file_write_and_read_file);
|
|
|
|
RUN_TEST_CASE(spiffs, can_read_spiffs_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void run_all_tests(void)
|
|
|
|
{
|
|
|
|
RUN_TEST_GROUP(spiffs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
UNITY_MAIN_FUNC(run_all_tests);
|
|
|
|
return 0;
|
|
|
|
}
|