Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | /* * Copyright (c) 2018 Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ #include "test_fat.h" #include <stdio.h> extern int test_file_write(void); extern int test_file_close(void); static int test_rmdir(void); static int test_mkdir(void) { int res; TC_PRINT("\nmkdir tests:\n"); if (check_file_dir_exists(TEST_DIR)) { TC_PRINT("[%s] exists, delete it\n", TEST_DIR); if (test_rmdir()) { TC_PRINT("Error deleting dir %s\n", TEST_DIR); return TC_FAIL; } } else { TC_PRINT("Creating new dir %s\n", TEST_DIR); } /* Verify fs_mkdir() */ res = fs_mkdir(TEST_DIR); if (res) { TC_PRINT("Error creating dir[%d]\n", res); return res; } res = fs_open(&filep, TEST_DIR_FILE); if (res) { TC_PRINT("Failed opening file [%d]\n", res); return res; } res = test_file_write(); if (res) { return res; } res = fs_close(&filep); if (res) { TC_PRINT("Error closing file [%d]\n", res); return res; } TC_PRINT("Created dir %s!\n", TEST_DIR); return res; } static int test_lsdir(const char *path) { int res; struct fs_dir_t dirp; struct fs_dirent entry; TC_PRINT("\nlsdir tests:\n"); /* Verify fs_opendir() */ res = fs_opendir(&dirp, path); if (res) { TC_PRINT("Error opening dir %s [%d]\n", path, res); return res; } TC_PRINT("\nListing dir %s:\n", path); for (;;) { /* Verify fs_readdir() */ res = fs_readdir(&dirp, &entry); /* entry.name[0] == 0 means end-of-dir */ if (res || entry.name[0] == 0) { break; } if (entry.type == FS_DIR_ENTRY_DIR) { TC_PRINT("[DIR ] %s\n", entry.name); } else { TC_PRINT("[FILE] %s (size = %zu)\n", entry.name, entry.size); } } /* Verify fs_closedir() */ fs_closedir(&dirp); return res; } static int test_rmdir(void) { int res; struct fs_dir_t dirp; static struct fs_dirent entry; char file_path[80]; TC_PRINT("\nrmdir tests:\n"); if (!check_file_dir_exists(TEST_DIR)) { TC_PRINT("%s doesn't exist\n", TEST_DIR); return TC_FAIL; } res = fs_opendir(&dirp, TEST_DIR); if (res) { TC_PRINT("Error opening dir[%d]\n", res); return res; } TC_PRINT("\nRemoving files and sub directories in %s\n", TEST_DIR); for (;;) { res = fs_readdir(&dirp, &entry); /* entry.name[0] == 0 means end-of-dir */ if (res || entry.name[0] == 0) { break; } /* Delete file or sub directory */ sprintf(file_path, "%s/%s", TEST_DIR, entry.name); TC_PRINT("Removing %s\n", file_path); res = fs_unlink(file_path); if (res) { TC_PRINT("Error deleting file/dir [%d]\n", res); fs_closedir(&dirp); return res; } } fs_closedir(&dirp); /* Verify fs_unlink() */ res = fs_unlink(TEST_DIR); if (res) { TC_PRINT("Error removing dir [%d]\n", res); return res; } TC_PRINT("Removed dir %s!\n", TEST_DIR); return res; } void test_fat_mkdir(void) { zassert_true(test_mkdir() == TC_PASS, NULL); } void test_fat_readdir(void) { zassert_true(test_lsdir(FATFS_MNTP) == TC_PASS, NULL); } void test_fat_rmdir(void) { zassert_true(test_rmdir() == TC_PASS, NULL); } |