SDHI_driver patch (mbedOS 5.11.5)

Committer:
tvendov
Date:
Fri Mar 29 19:47:34 2019 +0200
Revision:
2:7c75ab32d7c9
Fix: Folder structure changed. All SD Auto tests pass with OK!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tvendov 2:7c75ab32d7c9 1 /*
tvendov 2:7c75ab32d7c9 2 * mbed Microcontroller Library
tvendov 2:7c75ab32d7c9 3 * Copyright (c) 2006-2016 ARM Limited
tvendov 2:7c75ab32d7c9 4 *
tvendov 2:7c75ab32d7c9 5 * Licensed under the Apache License, Version 2.0 (the "License");
tvendov 2:7c75ab32d7c9 6 * you may not use this file except in compliance with the License.
tvendov 2:7c75ab32d7c9 7 * You may obtain a copy of the License at
tvendov 2:7c75ab32d7c9 8 *
tvendov 2:7c75ab32d7c9 9 * http://www.apache.org/licenses/LICENSE-2.0
tvendov 2:7c75ab32d7c9 10 *
tvendov 2:7c75ab32d7c9 11 * Unless required by applicable law or agreed to in writing, software
tvendov 2:7c75ab32d7c9 12 * distributed under the License is distributed on an "AS IS" BASIS,
tvendov 2:7c75ab32d7c9 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tvendov 2:7c75ab32d7c9 14 * See the License for the specific language governing permissions and
tvendov 2:7c75ab32d7c9 15 * limitations under the License.
tvendov 2:7c75ab32d7c9 16 */
tvendov 2:7c75ab32d7c9 17
tvendov 2:7c75ab32d7c9 18 /** @file fopen.cpp Test cases to POSIX file fopen() interface.
tvendov 2:7c75ab32d7c9 19 *
tvendov 2:7c75ab32d7c9 20 * Please consult the documentation under the test-case functions for
tvendov 2:7c75ab32d7c9 21 * a description of the individual test case.
tvendov 2:7c75ab32d7c9 22 */
tvendov 2:7c75ab32d7c9 23
tvendov 2:7c75ab32d7c9 24 #include "mbed.h"
tvendov 2:7c75ab32d7c9 25 #include "mbed_config.h"
tvendov 2:7c75ab32d7c9 26 #include "RZ_SDHIBlockDevice.hpp"
tvendov 2:7c75ab32d7c9 27 #include "FATFileSystem.h"
tvendov 2:7c75ab32d7c9 28 #include "fsfat_debug.h"
tvendov 2:7c75ab32d7c9 29 #include "fsfat_test.h"
tvendov 2:7c75ab32d7c9 30 #include "utest/utest.h"
tvendov 2:7c75ab32d7c9 31 #include "unity/unity.h"
tvendov 2:7c75ab32d7c9 32 #include "greentea-client/test_env.h"
tvendov 2:7c75ab32d7c9 33
tvendov 2:7c75ab32d7c9 34 #include <stdio.h>
tvendov 2:7c75ab32d7c9 35 #include <string.h>
tvendov 2:7c75ab32d7c9 36 #include <stdlib.h> /*rand()*/
tvendov 2:7c75ab32d7c9 37 #include <inttypes.h>
tvendov 2:7c75ab32d7c9 38 #include <errno.h>
tvendov 2:7c75ab32d7c9 39 /* mbed_retarget.h is included after errno.h so symbols are mapped to
tvendov 2:7c75ab32d7c9 40 * consistent values for all toolchains */
tvendov 2:7c75ab32d7c9 41 #include "platform/mbed_retarget.h"
tvendov 2:7c75ab32d7c9 42
tvendov 2:7c75ab32d7c9 43 using namespace utest::v1;
tvendov 2:7c75ab32d7c9 44
tvendov 2:7c75ab32d7c9 45 /// @cond FSFAT_DOXYGEN_DISABLE
tvendov 2:7c75ab32d7c9 46 #ifdef FSFAT_DEBUG
tvendov 2:7c75ab32d7c9 47 #define FSFAT_FOPEN_GREENTEA_TIMEOUT_S 3000
tvendov 2:7c75ab32d7c9 48 #else
tvendov 2:7c75ab32d7c9 49 #define FSFAT_FOPEN_GREENTEA_TIMEOUT_S 1000
tvendov 2:7c75ab32d7c9 50 #endif
tvendov 2:7c75ab32d7c9 51 /// @endcond
tvendov 2:7c75ab32d7c9 52
tvendov 2:7c75ab32d7c9 53
tvendov 2:7c75ab32d7c9 54 /* DEVICE_SPI
tvendov 2:7c75ab32d7c9 55 * This symbol is defined in targets.json if the target has a SPI interface, which is required for SDCard support.
tvendov 2:7c75ab32d7c9 56 *
tvendov 2:7c75ab32d7c9 57 * MBED_CONF_APP_FSFAT_SDCARD_INSTALLED
tvendov 2:7c75ab32d7c9 58 * For testing purposes, an SDCard must be installed on the target for the test cases in this file to succeed.
tvendov 2:7c75ab32d7c9 59 * If the target has an SD card installed then the MBED_CONF_APP_FSFAT_SDCARD_INSTALLED will be generated
tvendov 2:7c75ab32d7c9 60 * from the mbed_app.json, which includes the line
tvendov 2:7c75ab32d7c9 61 * {
tvendov 2:7c75ab32d7c9 62 * "config": {
tvendov 2:7c75ab32d7c9 63 * "UART_RX": "D0",
tvendov 2:7c75ab32d7c9 64 * <<< lines removed >>>
tvendov 2:7c75ab32d7c9 65 * "DEVICE_SPI": 1,
tvendov 2:7c75ab32d7c9 66 * "FSFAT_SDCARD_INSTALLED": 1
tvendov 2:7c75ab32d7c9 67 * },
tvendov 2:7c75ab32d7c9 68 * <<< lines removed >>>
tvendov 2:7c75ab32d7c9 69 */
tvendov 2:7c75ab32d7c9 70
tvendov 2:7c75ab32d7c9 71 #if DEVICE_SPI && ( defined(MBED_CONF_APP_FSFAT_SDCARD_INSTALLED) || (MBED_CONF_SD_FSFAT_SDCARD_INSTALLED))
tvendov 2:7c75ab32d7c9 72 static char fsfat_fopen_utest_msg_g[FSFAT_UTEST_MSG_BUF_SIZE];
tvendov 2:7c75ab32d7c9 73 #define FSFAT_FOPEN_TEST_MOUNT_PT_NAME "sd"
tvendov 2:7c75ab32d7c9 74 #define FSFAT_FOPEN_TEST_MOUNT_PT_PATH "/" FSFAT_FOPEN_TEST_MOUNT_PT_NAME
tvendov 2:7c75ab32d7c9 75 #define FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1 64
tvendov 2:7c75ab32d7c9 76 #define FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH 20
tvendov 2:7c75ab32d7c9 77 static const char *sd_badfile_path = "/sd/badfile.txt";
tvendov 2:7c75ab32d7c9 78 static const char *sd_testfile_path = "/sd/test.txt";
tvendov 2:7c75ab32d7c9 79
tvendov 2:7c75ab32d7c9 80 SDHIBlockDevice sd(MBED_CONF_RZ_SDHI_CH);
tvendov 2:7c75ab32d7c9 81 FATFileSystem fs("sd", &sd);
tvendov 2:7c75ab32d7c9 82
tvendov 2:7c75ab32d7c9 83 #define FSFAT_FOPEN_TEST_00 fsfat_fopen_test_00
tvendov 2:7c75ab32d7c9 84 #define FSFAT_FOPEN_TEST_01 fsfat_fopen_test_01
tvendov 2:7c75ab32d7c9 85 #define FSFAT_FOPEN_TEST_02 fsfat_fopen_test_02
tvendov 2:7c75ab32d7c9 86 #define FSFAT_FOPEN_TEST_03 fsfat_fopen_test_03
tvendov 2:7c75ab32d7c9 87 #define FSFAT_FOPEN_TEST_04 fsfat_fopen_test_04
tvendov 2:7c75ab32d7c9 88 #define FSFAT_FOPEN_TEST_05 fsfat_fopen_test_05
tvendov 2:7c75ab32d7c9 89 #define FSFAT_FOPEN_TEST_06 fsfat_fopen_test_06
tvendov 2:7c75ab32d7c9 90 #define FSFAT_FOPEN_TEST_07 fsfat_fopen_test_07
tvendov 2:7c75ab32d7c9 91 #define FSFAT_FOPEN_TEST_08 fsfat_fopen_test_08
tvendov 2:7c75ab32d7c9 92 #define FSFAT_FOPEN_TEST_09 fsfat_fopen_test_09
tvendov 2:7c75ab32d7c9 93 #define FSFAT_FOPEN_TEST_10 fsfat_fopen_test_10
tvendov 2:7c75ab32d7c9 94 #define FSFAT_FOPEN_TEST_11 fsfat_fopen_test_11
tvendov 2:7c75ab32d7c9 95 #define FSFAT_FOPEN_TEST_12 fsfat_fopen_test_12
tvendov 2:7c75ab32d7c9 96 #define FSFAT_FOPEN_TEST_13 fsfat_fopen_test_13
tvendov 2:7c75ab32d7c9 97 #define FSFAT_FOPEN_TEST_14 fsfat_fopen_test_14
tvendov 2:7c75ab32d7c9 98 #define FSFAT_FOPEN_TEST_15 fsfat_fopen_test_15
tvendov 2:7c75ab32d7c9 99 #define FSFAT_FOPEN_TEST_16 fsfat_fopen_test_16
tvendov 2:7c75ab32d7c9 100 #define FSFAT_FOPEN_TEST_17 fsfat_fopen_test_17
tvendov 2:7c75ab32d7c9 101 #define FSFAT_FOPEN_TEST_18 fsfat_fopen_test_18
tvendov 2:7c75ab32d7c9 102 #define FSFAT_FOPEN_TEST_19 fsfat_fopen_test_19
tvendov 2:7c75ab32d7c9 103 #define FSFAT_FOPEN_TEST_20 fsfat_fopen_test_20
tvendov 2:7c75ab32d7c9 104 #define FSFAT_FOPEN_TEST_21 fsfat_fopen_test_21
tvendov 2:7c75ab32d7c9 105 #define FSFAT_FOPEN_TEST_22 fsfat_fopen_test_22
tvendov 2:7c75ab32d7c9 106 #define FSFAT_FOPEN_TEST_23 fsfat_fopen_test_23
tvendov 2:7c75ab32d7c9 107 #define FSFAT_FOPEN_TEST_24 fsfat_fopen_test_24
tvendov 2:7c75ab32d7c9 108 #define FSFAT_FOPEN_TEST_25 fsfat_fopen_test_25
tvendov 2:7c75ab32d7c9 109 #define FSFAT_FOPEN_TEST_26 fsfat_fopen_test_26
tvendov 2:7c75ab32d7c9 110 #define FSFAT_FOPEN_TEST_27 fsfat_fopen_test_27
tvendov 2:7c75ab32d7c9 111 #define FSFAT_FOPEN_TEST_28 fsfat_fopen_test_28
tvendov 2:7c75ab32d7c9 112 #define FSFAT_FOPEN_TEST_29 fsfat_fopen_test_29
tvendov 2:7c75ab32d7c9 113 #define FSFAT_FOPEN_TEST_30 fsfat_fopen_test_30
tvendov 2:7c75ab32d7c9 114
tvendov 2:7c75ab32d7c9 115
tvendov 2:7c75ab32d7c9 116 /* support functions */
tvendov 2:7c75ab32d7c9 117
tvendov 2:7c75ab32d7c9 118 /*
tvendov 2:7c75ab32d7c9 119 * open tests that focus on testing fopen()
tvendov 2:7c75ab32d7c9 120 * fsfat_handle_t fopen(const char* filename, char* data, size_t* len, fsfat_key_desc_t* kdesc)
tvendov 2:7c75ab32d7c9 121 */
tvendov 2:7c75ab32d7c9 122
tvendov 2:7c75ab32d7c9 123 /* file data for test_01 */
tvendov 2:7c75ab32d7c9 124 static fsfat_kv_data_t fsfat_fopen_test_01_kv_data[] = {
tvendov 2:7c75ab32d7c9 125 { "/sd/fopentst/hello/world/animal/wobbly/dog/foot/frontlft.txt", "missing"},
tvendov 2:7c75ab32d7c9 126 { NULL, NULL},
tvendov 2:7c75ab32d7c9 127 };
tvendov 2:7c75ab32d7c9 128
tvendov 2:7c75ab32d7c9 129
tvendov 2:7c75ab32d7c9 130 /** @brief
tvendov 2:7c75ab32d7c9 131 * Split a file path into its component parts, setting '/' characters to '\0', and returning
tvendov 2:7c75ab32d7c9 132 * pointers to the file path components in the parts array. For example, if
tvendov 2:7c75ab32d7c9 133 * filepath = "/sd/fopentst/hello/world/animal/wobbly/dog/foot/frontlft.txt" then
tvendov 2:7c75ab32d7c9 134 * *parts[0] = "sd"
tvendov 2:7c75ab32d7c9 135 * *parts[1] = "fopentst"
tvendov 2:7c75ab32d7c9 136 * *parts[2] = "hello"
tvendov 2:7c75ab32d7c9 137 * *parts[3] = "world"
tvendov 2:7c75ab32d7c9 138 * *parts[4] = "animal"
tvendov 2:7c75ab32d7c9 139 * *parts[5] = "wobbly"
tvendov 2:7c75ab32d7c9 140 * *parts[6] = "dog"
tvendov 2:7c75ab32d7c9 141 * *parts[7] = "foot"
tvendov 2:7c75ab32d7c9 142 * *parts[8] = "frontlft.txt"
tvendov 2:7c75ab32d7c9 143 * parts[9] = NULL
tvendov 2:7c75ab32d7c9 144 *
tvendov 2:7c75ab32d7c9 145 * ARGUMENTS
tvendov 2:7c75ab32d7c9 146 * @param filepath IN file path string to split into component parts. Expected to start with '/'
tvendov 2:7c75ab32d7c9 147 * @param parts IN OUT array to hold pointers to parts
tvendov 2:7c75ab32d7c9 148 * @param num IN number of components available in parts
tvendov 2:7c75ab32d7c9 149 *
tvendov 2:7c75ab32d7c9 150 * @return On success, this returns the number of components in the filepath Returns number of compoee
tvendov 2:7c75ab32d7c9 151 */
tvendov 2:7c75ab32d7c9 152 static int32_t fsfat_filepath_split(char *filepath, char *parts[], uint32_t num)
tvendov 2:7c75ab32d7c9 153 {
tvendov 2:7c75ab32d7c9 154 uint32_t i = 0;
tvendov 2:7c75ab32d7c9 155 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 156 char *z = filepath;
tvendov 2:7c75ab32d7c9 157
tvendov 2:7c75ab32d7c9 158 while (i < num && *z != '\0') {
tvendov 2:7c75ab32d7c9 159 if (*z == '/') {
tvendov 2:7c75ab32d7c9 160 *z = '\0';
tvendov 2:7c75ab32d7c9 161 parts[i] = ++z;
tvendov 2:7c75ab32d7c9 162 i++;
tvendov 2:7c75ab32d7c9 163 } else {
tvendov 2:7c75ab32d7c9 164 z++;
tvendov 2:7c75ab32d7c9 165 }
tvendov 2:7c75ab32d7c9 166 }
tvendov 2:7c75ab32d7c9 167 if (*z == '\0' && i > 0) {
tvendov 2:7c75ab32d7c9 168 ret = (int32_t) i;
tvendov 2:7c75ab32d7c9 169 }
tvendov 2:7c75ab32d7c9 170 return ret;
tvendov 2:7c75ab32d7c9 171 }
tvendov 2:7c75ab32d7c9 172
tvendov 2:7c75ab32d7c9 173
tvendov 2:7c75ab32d7c9 174 /** @brief
tvendov 2:7c75ab32d7c9 175 * remove all directories and file in the given filepath
tvendov 2:7c75ab32d7c9 176 *
tvendov 2:7c75ab32d7c9 177 * ARGUMENTS
tvendov 2:7c75ab32d7c9 178 * @param filepath IN file path string to split into component parts. Expected to start with '/'
tvendov 2:7c75ab32d7c9 179 *
tvendov 2:7c75ab32d7c9 180 * @return On success, this returns 0, otherwise < 0 is returned;
tvendov 2:7c75ab32d7c9 181 */
tvendov 2:7c75ab32d7c9 182 int32_t fsfat_filepath_remove_all(char *filepath)
tvendov 2:7c75ab32d7c9 183 {
tvendov 2:7c75ab32d7c9 184 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 185 int32_t len = 0;
tvendov 2:7c75ab32d7c9 186 char *fpathbuf = NULL;
tvendov 2:7c75ab32d7c9 187 char *pos = NULL;
tvendov 2:7c75ab32d7c9 188
tvendov 2:7c75ab32d7c9 189 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 190 len = strlen(filepath);
tvendov 2:7c75ab32d7c9 191 fpathbuf = (char *) malloc(len + 1);
tvendov 2:7c75ab32d7c9 192 if (fpathbuf == NULL) {
tvendov 2:7c75ab32d7c9 193 FSFAT_DBGLOG("%s: failed to duplicate string (out of memory)\n", __func__);
tvendov 2:7c75ab32d7c9 194 return ret;
tvendov 2:7c75ab32d7c9 195 }
tvendov 2:7c75ab32d7c9 196 memset(fpathbuf, 0, len + 1);
tvendov 2:7c75ab32d7c9 197 memcpy(fpathbuf, filepath, len);
tvendov 2:7c75ab32d7c9 198
tvendov 2:7c75ab32d7c9 199 /* delete the leaf node first, and then successively parent directories. */
tvendov 2:7c75ab32d7c9 200 pos = fpathbuf + strlen(fpathbuf);
tvendov 2:7c75ab32d7c9 201 while (pos != fpathbuf) {
tvendov 2:7c75ab32d7c9 202 /* If the remaining file path is the mount point path then finish as the mount point cannot be removed */
tvendov 2:7c75ab32d7c9 203 if (strlen(fpathbuf) == strlen(FSFAT_FOPEN_TEST_MOUNT_PT_PATH)) {
tvendov 2:7c75ab32d7c9 204 if (strncmp(fpathbuf, FSFAT_FOPEN_TEST_MOUNT_PT_PATH, strlen(fpathbuf)) == 0) {
tvendov 2:7c75ab32d7c9 205 break;
tvendov 2:7c75ab32d7c9 206 }
tvendov 2:7c75ab32d7c9 207 }
tvendov 2:7c75ab32d7c9 208 ret = remove(fpathbuf);
tvendov 2:7c75ab32d7c9 209 pos = strrchr(fpathbuf, '/');
tvendov 2:7c75ab32d7c9 210 *pos = '\0';
tvendov 2:7c75ab32d7c9 211 }
tvendov 2:7c75ab32d7c9 212 if (fpathbuf) {
tvendov 2:7c75ab32d7c9 213 free(fpathbuf);
tvendov 2:7c75ab32d7c9 214 }
tvendov 2:7c75ab32d7c9 215 return ret;
tvendov 2:7c75ab32d7c9 216 }
tvendov 2:7c75ab32d7c9 217
tvendov 2:7c75ab32d7c9 218
tvendov 2:7c75ab32d7c9 219 /** @brief
tvendov 2:7c75ab32d7c9 220 * make all directories in the given filepath. Do not create the file if present at end of filepath
tvendov 2:7c75ab32d7c9 221 *
tvendov 2:7c75ab32d7c9 222 * ARGUMENTS
tvendov 2:7c75ab32d7c9 223 * @param filepath IN file path containing directories and file
tvendov 2:7c75ab32d7c9 224 * @param do_asserts IN set to true if function should assert on errors
tvendov 2:7c75ab32d7c9 225 *
tvendov 2:7c75ab32d7c9 226 * @return On success, this returns 0, otherwise < 0 is returned;
tvendov 2:7c75ab32d7c9 227 */
tvendov 2:7c75ab32d7c9 228 static int32_t fsfat_filepath_make_dirs(char *filepath, bool do_asserts)
tvendov 2:7c75ab32d7c9 229 {
tvendov 2:7c75ab32d7c9 230 int32_t i = 0;
tvendov 2:7c75ab32d7c9 231 int32_t num_parts = 0;
tvendov 2:7c75ab32d7c9 232 int32_t len = 0;
tvendov 2:7c75ab32d7c9 233 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 234 char *fpathbuf = NULL;
tvendov 2:7c75ab32d7c9 235 char *buf = NULL;
tvendov 2:7c75ab32d7c9 236 int pos = 0;
tvendov 2:7c75ab32d7c9 237 char *parts[FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH];
tvendov 2:7c75ab32d7c9 238
tvendov 2:7c75ab32d7c9 239 FSFAT_DBGLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 240 /* find the dirs to create*/
tvendov 2:7c75ab32d7c9 241 memset(parts, 0, sizeof(parts));
tvendov 2:7c75ab32d7c9 242 len = strlen(filepath);
tvendov 2:7c75ab32d7c9 243 fpathbuf = (char *) malloc(len + 1);
tvendov 2:7c75ab32d7c9 244 if (fpathbuf == NULL) {
tvendov 2:7c75ab32d7c9 245 FSFAT_DBGLOG("%s: failed to duplicate string (out of memory)\n", __func__);
tvendov 2:7c75ab32d7c9 246 return ret;
tvendov 2:7c75ab32d7c9 247 }
tvendov 2:7c75ab32d7c9 248 memset(fpathbuf, 0, len + 1);
tvendov 2:7c75ab32d7c9 249 memcpy(fpathbuf, filepath, len);
tvendov 2:7c75ab32d7c9 250 num_parts = fsfat_filepath_split(fpathbuf, parts, FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH);
tvendov 2:7c75ab32d7c9 251 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 252 "%s:Error: failed to split filepath (filename=\"%s\", num_parts=%d)\n", __func__, filepath, (int) num_parts);
tvendov 2:7c75ab32d7c9 253 TEST_ASSERT_MESSAGE(num_parts > 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 254
tvendov 2:7c75ab32d7c9 255 /* Now create the directories on the directory path.
tvendov 2:7c75ab32d7c9 256 * Skip creating dir for "/sd" which must be present */
tvendov 2:7c75ab32d7c9 257 buf = (char *) malloc(strlen(filepath) + 1);
tvendov 2:7c75ab32d7c9 258 memset(buf, 0, strlen(filepath) + 1);
tvendov 2:7c75ab32d7c9 259 pos = sprintf(buf, "/%s", parts[0]);
tvendov 2:7c75ab32d7c9 260 for (i = 1; i < num_parts - 1; i++) {
tvendov 2:7c75ab32d7c9 261 pos += sprintf(buf + pos, "/%s", parts[i]);
tvendov 2:7c75ab32d7c9 262 FSFAT_DBGLOG("mkdir(%s)\n", buf);
tvendov 2:7c75ab32d7c9 263 ret = mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
tvendov 2:7c75ab32d7c9 264 if (do_asserts == true) {
tvendov 2:7c75ab32d7c9 265 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 266 "%s:Error: failed to create directory (filepath2=\"%s\", ret=%d, errno=%d)\n", __func__, buf, (int) ret, errno);
tvendov 2:7c75ab32d7c9 267 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 268 }
tvendov 2:7c75ab32d7c9 269 }
tvendov 2:7c75ab32d7c9 270
tvendov 2:7c75ab32d7c9 271 if (buf) {
tvendov 2:7c75ab32d7c9 272 free(buf);
tvendov 2:7c75ab32d7c9 273 }
tvendov 2:7c75ab32d7c9 274 if (fpathbuf) {
tvendov 2:7c75ab32d7c9 275 free(fpathbuf);
tvendov 2:7c75ab32d7c9 276 }
tvendov 2:7c75ab32d7c9 277 return ret;
tvendov 2:7c75ab32d7c9 278 }
tvendov 2:7c75ab32d7c9 279
tvendov 2:7c75ab32d7c9 280 /** @brief
tvendov 2:7c75ab32d7c9 281 * First and last test must format the SD card to FAT FS format:
tvendov 2:7c75ab32d7c9 282 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 283 */
tvendov 2:7c75ab32d7c9 284 control_t fsfat_fopen_test_00(const size_t call_count)
tvendov 2:7c75ab32d7c9 285 {
tvendov 2:7c75ab32d7c9 286 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 287 (void) call_count;
tvendov 2:7c75ab32d7c9 288 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 289
tvendov 2:7c75ab32d7c9 290 fs.unmount();
tvendov 2:7c75ab32d7c9 291 ret = fs.format(&sd);
tvendov 2:7c75ab32d7c9 292 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 293 "%s:Error: failed to format sdcard (ret=%d)\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 294 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 295 fs.mount(&sd);
tvendov 2:7c75ab32d7c9 296
tvendov 2:7c75ab32d7c9 297 return CaseNext;
tvendov 2:7c75ab32d7c9 298 }
tvendov 2:7c75ab32d7c9 299
tvendov 2:7c75ab32d7c9 300
tvendov 2:7c75ab32d7c9 301 /* FIX ME: errno not set correctly when error occurs. This indicates a problem with the implementation. */
tvendov 2:7c75ab32d7c9 302
tvendov 2:7c75ab32d7c9 303 /** @brief
tvendov 2:7c75ab32d7c9 304 * Basic fopen test which does the following:
tvendov 2:7c75ab32d7c9 305 * - creates file and writes some data to the value blob.
tvendov 2:7c75ab32d7c9 306 * - closes the newly created file.
tvendov 2:7c75ab32d7c9 307 * - opens the file (r-only)
tvendov 2:7c75ab32d7c9 308 * - reads the file data and checks its the same as the previously created data.
tvendov 2:7c75ab32d7c9 309 * - closes the opened file
tvendov 2:7c75ab32d7c9 310 *
tvendov 2:7c75ab32d7c9 311 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 312 */
tvendov 2:7c75ab32d7c9 313 static control_t fsfat_fopen_test_01(const size_t call_count)
tvendov 2:7c75ab32d7c9 314 {
tvendov 2:7c75ab32d7c9 315 char *read_buf;
tvendov 2:7c75ab32d7c9 316 int32_t ret = 0;
tvendov 2:7c75ab32d7c9 317 size_t len = 0;
tvendov 2:7c75ab32d7c9 318 fsfat_kv_data_t *node;
tvendov 2:7c75ab32d7c9 319 FILE *fp = NULL;
tvendov 2:7c75ab32d7c9 320
tvendov 2:7c75ab32d7c9 321 FSFAT_DBGLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 322 (void) call_count;
tvendov 2:7c75ab32d7c9 323 node = fsfat_fopen_test_01_kv_data;
tvendov 2:7c75ab32d7c9 324
tvendov 2:7c75ab32d7c9 325 /* remove file and directory from a previous failed test run, if present */
tvendov 2:7c75ab32d7c9 326 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 327
tvendov 2:7c75ab32d7c9 328 /* create dirs */
tvendov 2:7c75ab32d7c9 329 ret = fsfat_filepath_make_dirs((char *) node->filename, true);
tvendov 2:7c75ab32d7c9 330 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 331 "%s:Error: failed to create dirs for filename (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret);
tvendov 2:7c75ab32d7c9 332 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 333
tvendov 2:7c75ab32d7c9 334 FSFAT_DBGLOG("%s:About to create new file (filename=\"%s\", data=\"%s\")\n", __func__, node->filename, node->value);
tvendov 2:7c75ab32d7c9 335 fp = fopen(node->filename, "w+");
tvendov 2:7c75ab32d7c9 336 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 337 "%s:Error: failed to create file (filename=\"%s\", data=\"%s\")(ret=%d, errno=%d)\n", __func__, node->filename,
tvendov 2:7c75ab32d7c9 338 node->value, (int) ret, errno);
tvendov 2:7c75ab32d7c9 339 TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 340
tvendov 2:7c75ab32d7c9 341 FSFAT_DBGLOG("%s:length of file=%d (filename=\"%s\", data=\"%s\")\n", __func__, (int) len, node->filename, node->value);
tvendov 2:7c75ab32d7c9 342 len = strlen(node->value);
tvendov 2:7c75ab32d7c9 343 ret = fwrite((const void *) node->value, len, 1, fp);
tvendov 2:7c75ab32d7c9 344 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 345 "%s:Error: failed to write file (filename=\"%s\", data=\"%s\")(ret=%d)\n", __func__, node->filename, node->value,
tvendov 2:7c75ab32d7c9 346 (int) ret);
tvendov 2:7c75ab32d7c9 347 TEST_ASSERT_MESSAGE(ret == 1, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 348
tvendov 2:7c75ab32d7c9 349 FSFAT_DBGLOG("Created file successfully (filename=\"%s\", data=\"%s\")\n", node->filename, node->value);
tvendov 2:7c75ab32d7c9 350 ret = fclose(fp);
tvendov 2:7c75ab32d7c9 351 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 352 "%s:Error: failed to close file (ret=%d, errno=%d)\n", __func__, (int) ret, errno);
tvendov 2:7c75ab32d7c9 353 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 354
tvendov 2:7c75ab32d7c9 355 /* now open the newly created key */
tvendov 2:7c75ab32d7c9 356 fp = NULL;
tvendov 2:7c75ab32d7c9 357 fp = fopen(node->filename, "r");
tvendov 2:7c75ab32d7c9 358 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 359 "%s:Error: failed to open file for reading (filename=\"%s\", data=\"%s\")(ret=%d)\n", __func__, node->filename,
tvendov 2:7c75ab32d7c9 360 node->value, (int) ret);
tvendov 2:7c75ab32d7c9 361 TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 362
tvendov 2:7c75ab32d7c9 363 len = strlen(node->value) + 1;
tvendov 2:7c75ab32d7c9 364 read_buf = (char *) malloc(len);
tvendov 2:7c75ab32d7c9 365 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 366 "%s:Error: failed to allocated read buffer \n", __func__);
tvendov 2:7c75ab32d7c9 367 TEST_ASSERT_MESSAGE(read_buf != NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 368
tvendov 2:7c75ab32d7c9 369 FSFAT_DBGLOG("Opened file successfully (filename=\"%s\", data=\"%s\")\n", node->filename, node->value);
tvendov 2:7c75ab32d7c9 370 memset(read_buf, 0, len);
tvendov 2:7c75ab32d7c9 371 ret = fread((void *) read_buf, len, 1, fp);
tvendov 2:7c75ab32d7c9 372 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 373 "%s:Error: failed to read file (filename=\"%s\", data=\"%s\", read_buf=\"%s\", ret=%d)\n", __func__, node->filename,
tvendov 2:7c75ab32d7c9 374 node->value, read_buf, (int) ret);
tvendov 2:7c75ab32d7c9 375 /* FIX ME: fread should return the number of items read, not 0 when an item is read successfully.
tvendov 2:7c75ab32d7c9 376 * This indicates a problem with the implementation, as the correct data is read. The correct assert should be:
tvendov 2:7c75ab32d7c9 377 * TEST_ASSERT_MESSAGE(ret == 1, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 378 * The following assert is curerntly used until the implementation is fixed
tvendov 2:7c75ab32d7c9 379 */
tvendov 2:7c75ab32d7c9 380 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 381
tvendov 2:7c75ab32d7c9 382 /* check read data is as expected */
tvendov 2:7c75ab32d7c9 383 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 384 "%s:Error: read value data (%s) != expected value data (filename=\"%s\", data=\"%s\", read_buf=\"%s\", ret=%d)\n",
tvendov 2:7c75ab32d7c9 385 __func__, read_buf, node->filename, node->value, read_buf, (int) ret);
tvendov 2:7c75ab32d7c9 386 TEST_ASSERT_MESSAGE(strncmp(read_buf, node->value, strlen(node->value)) == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 387
tvendov 2:7c75ab32d7c9 388 if (read_buf) {
tvendov 2:7c75ab32d7c9 389 free(read_buf);
tvendov 2:7c75ab32d7c9 390 }
tvendov 2:7c75ab32d7c9 391 ret = fclose(fp);
tvendov 2:7c75ab32d7c9 392 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 393 "%s:Error: fclose() call failed (ret=%d, errno=%d).\n", __func__, (int) ret, errno);
tvendov 2:7c75ab32d7c9 394 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 395 return CaseNext;
tvendov 2:7c75ab32d7c9 396 }
tvendov 2:7c75ab32d7c9 397
tvendov 2:7c75ab32d7c9 398 static fsfat_kv_data_t fsfat_fopen_test_02_data[] = {
tvendov 2:7c75ab32d7c9 399 FSFAT_INIT_1_TABLE_MID_NODE,
tvendov 2:7c75ab32d7c9 400 { NULL, NULL},
tvendov 2:7c75ab32d7c9 401 };
tvendov 2:7c75ab32d7c9 402
tvendov 2:7c75ab32d7c9 403 /**
tvendov 2:7c75ab32d7c9 404 * @brief test to fopen() a pre-existing key and try to write it, which should fail
tvendov 2:7c75ab32d7c9 405 * as by default pre-existing keys are opened read-only
tvendov 2:7c75ab32d7c9 406 *
tvendov 2:7c75ab32d7c9 407 * Basic open test which does the following:
tvendov 2:7c75ab32d7c9 408 * - creates file with default rw perms and writes some data to the value blob.
tvendov 2:7c75ab32d7c9 409 * - closes the newly created file.
tvendov 2:7c75ab32d7c9 410 * - opens the file with the default permissions (read-only)
tvendov 2:7c75ab32d7c9 411 * - tries to write the file data which should fail because file was not opened with write flag set.
tvendov 2:7c75ab32d7c9 412 * - closes the opened key
tvendov 2:7c75ab32d7c9 413 *
tvendov 2:7c75ab32d7c9 414 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 415 */
tvendov 2:7c75ab32d7c9 416 control_t fsfat_fopen_test_02(const size_t call_count)
tvendov 2:7c75ab32d7c9 417 {
tvendov 2:7c75ab32d7c9 418 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 419 size_t len = 0;
tvendov 2:7c75ab32d7c9 420 FILE *fp = NULL;
tvendov 2:7c75ab32d7c9 421
tvendov 2:7c75ab32d7c9 422 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 423 (void) call_count;
tvendov 2:7c75ab32d7c9 424 len = strlen(fsfat_fopen_test_02_data[0].value);
tvendov 2:7c75ab32d7c9 425 ret = fsfat_test_create(fsfat_fopen_test_02_data[0].filename, (char *) fsfat_fopen_test_02_data[0].value, len);
tvendov 2:7c75ab32d7c9 426 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 427 "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 428 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 429
tvendov 2:7c75ab32d7c9 430 /* by default, owner of key opens with read-only permissions*/
tvendov 2:7c75ab32d7c9 431 fp = fopen(fsfat_fopen_test_02_data[0].filename, "r");
tvendov 2:7c75ab32d7c9 432 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 433 "%s:Error: failed to open file (filename=\"%s\", ret=%d)\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret);
tvendov 2:7c75ab32d7c9 434 TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 435
tvendov 2:7c75ab32d7c9 436 len = strlen(fsfat_fopen_test_02_data[0].value);
tvendov 2:7c75ab32d7c9 437 ret = fwrite((const void *) fsfat_fopen_test_02_data[0].value, len, 1, fp);
tvendov 2:7c75ab32d7c9 438 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 439 "%s:Error: call to fwrite() succeeded when should have failed for read-only file (filename=\"%s\")(ret=%d).\n",
tvendov 2:7c75ab32d7c9 440 __func__, fsfat_fopen_test_02_data[0].filename, (int) ret);
tvendov 2:7c75ab32d7c9 441 TEST_ASSERT_MESSAGE(ret <= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 442
tvendov 2:7c75ab32d7c9 443 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: fclose() call failed.\n",
tvendov 2:7c75ab32d7c9 444 __func__);
tvendov 2:7c75ab32d7c9 445 TEST_ASSERT_MESSAGE(fclose(fp) == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 446
tvendov 2:7c75ab32d7c9 447 return CaseNext;
tvendov 2:7c75ab32d7c9 448 }
tvendov 2:7c75ab32d7c9 449
tvendov 2:7c75ab32d7c9 450
tvendov 2:7c75ab32d7c9 451 /**
tvendov 2:7c75ab32d7c9 452 * @brief test to fopen() a pre-existing file and try to write it, which should succeed
tvendov 2:7c75ab32d7c9 453 * because the key was opened read-write permissions explicitly
tvendov 2:7c75ab32d7c9 454 *
tvendov 2:7c75ab32d7c9 455 * Basic open test which does the following:
tvendov 2:7c75ab32d7c9 456 * - creates file with default rw perms and writes some data to the value blob.
tvendov 2:7c75ab32d7c9 457 * - closes the newly created file.
tvendov 2:7c75ab32d7c9 458 * - opens the file with the rw permissions (non default)
tvendov 2:7c75ab32d7c9 459 * - tries to write the file data which should succeeds because file was opened with write flag set.
tvendov 2:7c75ab32d7c9 460 * - closes the opened key
tvendov 2:7c75ab32d7c9 461 *
tvendov 2:7c75ab32d7c9 462 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 463 */
tvendov 2:7c75ab32d7c9 464 control_t fsfat_fopen_test_03(const size_t call_count)
tvendov 2:7c75ab32d7c9 465 {
tvendov 2:7c75ab32d7c9 466 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 467 size_t len = 0;
tvendov 2:7c75ab32d7c9 468 FILE *fp = NULL;
tvendov 2:7c75ab32d7c9 469
tvendov 2:7c75ab32d7c9 470 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 471 (void) call_count;
tvendov 2:7c75ab32d7c9 472 len = strlen(fsfat_fopen_test_02_data[0].value);
tvendov 2:7c75ab32d7c9 473 ret = fsfat_test_create(fsfat_fopen_test_02_data[0].filename, (char *) fsfat_fopen_test_02_data[0].value, len);
tvendov 2:7c75ab32d7c9 474 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 475 "%s:Error: failed to create file in store (ret=%d).\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 476 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 477
tvendov 2:7c75ab32d7c9 478 /* opens with read-write permissions*/
tvendov 2:7c75ab32d7c9 479 fp = fopen(fsfat_fopen_test_02_data[0].filename, "w+");
tvendov 2:7c75ab32d7c9 480 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 481 "%s:Error: failed to open file (filename=\"%s\")(ret=%d)\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret);
tvendov 2:7c75ab32d7c9 482 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 483
tvendov 2:7c75ab32d7c9 484 len = strlen(fsfat_fopen_test_02_data[0].value);
tvendov 2:7c75ab32d7c9 485 ret = fwrite((const void *) fsfat_fopen_test_02_data[0].value, len, 1, fp);
tvendov 2:7c75ab32d7c9 486 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 487 "%s:Error: call to fwrite() failed when should have succeeded (filename=\"%s\", ret=%d).\n", __func__,
tvendov 2:7c75ab32d7c9 488 fsfat_fopen_test_02_data[0].filename, (int) ret);
tvendov 2:7c75ab32d7c9 489 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 490
tvendov 2:7c75ab32d7c9 491 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: fclose() call failed.\n",
tvendov 2:7c75ab32d7c9 492 __func__);
tvendov 2:7c75ab32d7c9 493 TEST_ASSERT_MESSAGE(fclose(fp) >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 494
tvendov 2:7c75ab32d7c9 495 /* clean-up */
tvendov 2:7c75ab32d7c9 496 ret = remove(fsfat_fopen_test_02_data[0].filename);
tvendov 2:7c75ab32d7c9 497 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 498 "%s:Error: unable to delete file (filename=%s, ret=%d) .\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret);
tvendov 2:7c75ab32d7c9 499 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 500
tvendov 2:7c75ab32d7c9 501 return CaseNext;
tvendov 2:7c75ab32d7c9 502 }
tvendov 2:7c75ab32d7c9 503
tvendov 2:7c75ab32d7c9 504
tvendov 2:7c75ab32d7c9 505 /** @brief test to call fopen() with a filename string that exceeds the maximum length
tvendov 2:7c75ab32d7c9 506 * - chanFS supports the exFAT format which should support 255 char filenames
tvendov 2:7c75ab32d7c9 507 * - check that filenames of this length can be created
tvendov 2:7c75ab32d7c9 508 *
tvendov 2:7c75ab32d7c9 509 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 510 */
tvendov 2:7c75ab32d7c9 511 control_t fsfat_fopen_test_04(const size_t call_count)
tvendov 2:7c75ab32d7c9 512 {
tvendov 2:7c75ab32d7c9 513 char filename_good[FSFAT_FILENAME_MAX_LENGTH + 1];
tvendov 2:7c75ab32d7c9 514 char filename_bad[FSFAT_FILENAME_MAX_LENGTH + 2];
tvendov 2:7c75ab32d7c9 515 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 516 size_t len = 0;
tvendov 2:7c75ab32d7c9 517
tvendov 2:7c75ab32d7c9 518 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 519 (void) call_count;
tvendov 2:7c75ab32d7c9 520
tvendov 2:7c75ab32d7c9 521 memset(filename_good, 0, FSFAT_FILENAME_MAX_LENGTH + 1);
tvendov 2:7c75ab32d7c9 522 memset(filename_bad, 0, FSFAT_FILENAME_MAX_LENGTH + 2);
tvendov 2:7c75ab32d7c9 523 ret = fsfat_test_filename_gen(filename_good, FSFAT_FILENAME_MAX_LENGTH);
tvendov 2:7c75ab32d7c9 524 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 525 "%s:Error: unable to generate filename_good.\n", __func__);
tvendov 2:7c75ab32d7c9 526 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 527
tvendov 2:7c75ab32d7c9 528 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 529 "%s:Error: filename_good is not the correct length (filename_good=%s, len=%d, expected=%d).\n", __func__, filename_good,
tvendov 2:7c75ab32d7c9 530 (int) strlen(filename_good), (int) FSFAT_FILENAME_MAX_LENGTH);
tvendov 2:7c75ab32d7c9 531 TEST_ASSERT_MESSAGE(strlen(filename_good) == FSFAT_FILENAME_MAX_LENGTH, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 532
tvendov 2:7c75ab32d7c9 533 ret = fsfat_test_filename_gen(filename_bad, FSFAT_FILENAME_MAX_LENGTH + 1);
tvendov 2:7c75ab32d7c9 534 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 535 "%s:Error: unable to generate filename_bad.\n", __func__);
tvendov 2:7c75ab32d7c9 536 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 537 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 538 "%s:Error: filename_bad is not the correct length (len=%d, expected=%d).\n", __func__, (int) strlen(filename_bad),
tvendov 2:7c75ab32d7c9 539 (int) FSFAT_FILENAME_MAX_LENGTH + 1);
tvendov 2:7c75ab32d7c9 540 TEST_ASSERT_MESSAGE(strlen(filename_bad) == FSFAT_FILENAME_MAX_LENGTH + 1, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 541
tvendov 2:7c75ab32d7c9 542 len = strlen(filename_good);
tvendov 2:7c75ab32d7c9 543 ret = fsfat_test_create(filename_good, filename_good, len);
tvendov 2:7c75ab32d7c9 544 /* FIXME:
tvendov 2:7c75ab32d7c9 545 * The current implementation can create file with a filename with 9 chars (more than the 8 restriction of FAT32 Short File Names).
tvendov 2:7c75ab32d7c9 546 * However, the exFAT 255 char filesnames is not supported and hence the following is commented out. Find out what is
tvendov 2:7c75ab32d7c9 547 * the supported max filename length and change this testcase according.
tvendov 2:7c75ab32d7c9 548 *
tvendov 2:7c75ab32d7c9 549 * FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (filename=%s, ret=%d).\n", __func__, filename_good, (int) ret);
tvendov 2:7c75ab32d7c9 550 * TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 551 */
tvendov 2:7c75ab32d7c9 552
tvendov 2:7c75ab32d7c9 553 len = strlen(filename_bad);
tvendov 2:7c75ab32d7c9 554 ret = fsfat_test_create(filename_bad, filename_bad, len);
tvendov 2:7c75ab32d7c9 555 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 556 "%s:Error: created file in store for filename_bad when should have failed (filename=%s, ret=%d).\n", __func__,
tvendov 2:7c75ab32d7c9 557 filename_bad, (int) ret);
tvendov 2:7c75ab32d7c9 558 TEST_ASSERT_MESSAGE(ret < 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 559 return CaseNext;
tvendov 2:7c75ab32d7c9 560 }
tvendov 2:7c75ab32d7c9 561
tvendov 2:7c75ab32d7c9 562
tvendov 2:7c75ab32d7c9 563 /// @cond FSFAT_DOXYGEN_DISABLE
tvendov 2:7c75ab32d7c9 564 typedef struct fsfat_fopen_kv_name_ascii_node {
tvendov 2:7c75ab32d7c9 565 uint32_t code;
tvendov 2:7c75ab32d7c9 566 uint32_t f_allowed : 1;
tvendov 2:7c75ab32d7c9 567 } fsfat_fopen_kv_name_ascii_node;
tvendov 2:7c75ab32d7c9 568 /// @endcond
tvendov 2:7c75ab32d7c9 569
tvendov 2:7c75ab32d7c9 570 static const uint32_t fsfat_fopen_kv_name_ascii_table_code_sentinel_g = 256;
tvendov 2:7c75ab32d7c9 571
tvendov 2:7c75ab32d7c9 572 /*@brief table recording ascii character codes permitted in kv names */
tvendov 2:7c75ab32d7c9 573 static fsfat_fopen_kv_name_ascii_node fsfat_fopen_kv_name_ascii_table[] = {
tvendov 2:7c75ab32d7c9 574 {0, true}, /* code 0-33 allowed*/
tvendov 2:7c75ab32d7c9 575 {34, false}, /* '"' not allowed */
tvendov 2:7c75ab32d7c9 576 {35, true}, /* allowed */
tvendov 2:7c75ab32d7c9 577 {42, false}, /* '*' not allowed */
tvendov 2:7c75ab32d7c9 578 {43, true}, /* allowed */
tvendov 2:7c75ab32d7c9 579 {47, false}, /* '/' not allowed */
tvendov 2:7c75ab32d7c9 580 {48, true}, /* allowed */
tvendov 2:7c75ab32d7c9 581 {58, false}, /* ':' not allowed */
tvendov 2:7c75ab32d7c9 582 {59, true}, /* allowed */
tvendov 2:7c75ab32d7c9 583 {60, false}, /* '<' not allowed */
tvendov 2:7c75ab32d7c9 584 {61, true}, /* allowed */
tvendov 2:7c75ab32d7c9 585 {62, false}, /* '?', '>' not allowed */
tvendov 2:7c75ab32d7c9 586 {64, true}, /* allowed */
tvendov 2:7c75ab32d7c9 587 {92, false}, /* '\' not allowed */
tvendov 2:7c75ab32d7c9 588 {93, true}, /* allowed */
tvendov 2:7c75ab32d7c9 589 {124, false}, /* '!' not allowed */
tvendov 2:7c75ab32d7c9 590 {125, true}, /* allowed */
tvendov 2:7c75ab32d7c9 591 {127, false}, /* DEL not allowed */
tvendov 2:7c75ab32d7c9 592 {128, true}, /* allowed */
tvendov 2:7c75ab32d7c9 593 {fsfat_fopen_kv_name_ascii_table_code_sentinel_g, false}, /* sentinel */
tvendov 2:7c75ab32d7c9 594 };
tvendov 2:7c75ab32d7c9 595
tvendov 2:7c75ab32d7c9 596
tvendov 2:7c75ab32d7c9 597 /// @cond FSFAT_DOXYGEN_DISABLE
tvendov 2:7c75ab32d7c9 598 enum fsfat_fopen_kv_name_pos {
tvendov 2:7c75ab32d7c9 599 fsfat_fopen_kv_name_pos_start = 0x0,
tvendov 2:7c75ab32d7c9 600 fsfat_fopen_kv_name_pos_mid,
tvendov 2:7c75ab32d7c9 601 fsfat_fopen_kv_name_pos_end,
tvendov 2:7c75ab32d7c9 602 fsfat_fopen_kv_name_pos_max
tvendov 2:7c75ab32d7c9 603 };
tvendov 2:7c75ab32d7c9 604 /// @endcond
tvendov 2:7c75ab32d7c9 605
tvendov 2:7c75ab32d7c9 606 /** @brief test to call fopen() with filename that in includes illegal characters
tvendov 2:7c75ab32d7c9 607 * - the character(s) can be at the beginning of the filename
tvendov 2:7c75ab32d7c9 608 * - the character(s) can be at the end of the filename
tvendov 2:7c75ab32d7c9 609 * - the character(s) can be somewhere within the filename string
tvendov 2:7c75ab32d7c9 610 * - a max-length string of random characters (legal and illegal)
tvendov 2:7c75ab32d7c9 611 * - a max-length string of random illegal characters only
tvendov 2:7c75ab32d7c9 612 *
tvendov 2:7c75ab32d7c9 613 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 614 */
tvendov 2:7c75ab32d7c9 615 control_t fsfat_fopen_test_05(const size_t call_count)
tvendov 2:7c75ab32d7c9 616 {
tvendov 2:7c75ab32d7c9 617 bool f_allowed = false;
tvendov 2:7c75ab32d7c9 618 const char *mnt_pt = FSFAT_FOPEN_TEST_MOUNT_PT_PATH;
tvendov 2:7c75ab32d7c9 619 const char *basename = "goodfile";
tvendov 2:7c75ab32d7c9 620 const char *extname = "txt";
tvendov 2:7c75ab32d7c9 621 const size_t basename_len = strlen(basename);
tvendov 2:7c75ab32d7c9 622 const size_t filename_len = strlen(mnt_pt) + strlen(basename) + strlen(extname) +
tvendov 2:7c75ab32d7c9 623 2; /* extra 2 chars for '/' and '.' in "/sd/goodfile.txt" */
tvendov 2:7c75ab32d7c9 624 char filename[FSFAT_BUF_MAX_LENGTH];
tvendov 2:7c75ab32d7c9 625 size_t len = 0;
tvendov 2:7c75ab32d7c9 626 uint32_t j = 0;
tvendov 2:7c75ab32d7c9 627 int32_t ret = 0;
tvendov 2:7c75ab32d7c9 628 fsfat_fopen_kv_name_ascii_node *node = NULL;
tvendov 2:7c75ab32d7c9 629 uint32_t pos;
tvendov 2:7c75ab32d7c9 630
tvendov 2:7c75ab32d7c9 631 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 632 (void) call_count;
tvendov 2:7c75ab32d7c9 633
tvendov 2:7c75ab32d7c9 634 #ifdef FSFAT_DEBUG
tvendov 2:7c75ab32d7c9 635 /* symbol only used why debug is enabled */
tvendov 2:7c75ab32d7c9 636 const char *pos_str = NULL;
tvendov 2:7c75ab32d7c9 637 #endif
tvendov 2:7c75ab32d7c9 638
tvendov 2:7c75ab32d7c9 639 /* create bad keyname strings with invalid character code at start of keyname */
tvendov 2:7c75ab32d7c9 640 node = fsfat_fopen_kv_name_ascii_table;
tvendov 2:7c75ab32d7c9 641 memset(filename, 0, FSFAT_BUF_MAX_LENGTH);
tvendov 2:7c75ab32d7c9 642 while (node->code != fsfat_fopen_kv_name_ascii_table_code_sentinel_g) {
tvendov 2:7c75ab32d7c9 643 /* loop over range */
tvendov 2:7c75ab32d7c9 644 for (j = node->code; j < (node + 1)->code; j++) {
tvendov 2:7c75ab32d7c9 645 if ((j >= 48 && j <= 57) || (j >= 65 && j <= 90) || (j >= 97 && j <= 122)) {
tvendov 2:7c75ab32d7c9 646 FSFAT_DBGLOG("%s: skipping alpha-numeric ascii character code %d (%c).\n", __func__, (int) j, (char) j);
tvendov 2:7c75ab32d7c9 647 continue;
tvendov 2:7c75ab32d7c9 648 }
tvendov 2:7c75ab32d7c9 649
tvendov 2:7c75ab32d7c9 650 /* set the start, mid, last character of the name to the test char code */
tvendov 2:7c75ab32d7c9 651 for (pos = (uint32_t) fsfat_fopen_kv_name_pos_start; pos < (uint32_t) fsfat_fopen_kv_name_pos_max; pos++) {
tvendov 2:7c75ab32d7c9 652 len = snprintf(filename, filename_len + 1, "%s/%s.%s", mnt_pt, basename, extname);
tvendov 2:7c75ab32d7c9 653 /* overwrite a char at the pos start, mid, end of the filename with an ascii char code (both illegal and legal)*/
tvendov 2:7c75ab32d7c9 654 switch (pos) {
tvendov 2:7c75ab32d7c9 655 case fsfat_fopen_kv_name_pos_start:
tvendov 2:7c75ab32d7c9 656 filename[5] = (char) j; /* 5 so at to write the second basename char (bad chars as first char not accepted)*/
tvendov 2:7c75ab32d7c9 657 break;
tvendov 2:7c75ab32d7c9 658 case fsfat_fopen_kv_name_pos_mid:
tvendov 2:7c75ab32d7c9 659 /* create bad keyname strings with invalid character code in the middle of keyname */
tvendov 2:7c75ab32d7c9 660 filename[5 + basename_len / 2] = (char) j;
tvendov 2:7c75ab32d7c9 661 break;
tvendov 2:7c75ab32d7c9 662 case fsfat_fopen_kv_name_pos_end:
tvendov 2:7c75ab32d7c9 663 /* create bad keyname strings with invalid character code at end of keyname */
tvendov 2:7c75ab32d7c9 664 filename[5 + basename_len - 1] = (char) j;
tvendov 2:7c75ab32d7c9 665 break;
tvendov 2:7c75ab32d7c9 666 default:
tvendov 2:7c75ab32d7c9 667 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 668 "%s:Error: unexpected value of pos (pos=%d).\n", __func__, (int) pos);
tvendov 2:7c75ab32d7c9 669 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 670 break;
tvendov 2:7c75ab32d7c9 671 }
tvendov 2:7c75ab32d7c9 672
tvendov 2:7c75ab32d7c9 673 #ifdef FSFAT_DEBUG
tvendov 2:7c75ab32d7c9 674 /* processing only required when debug trace enabled */
tvendov 2:7c75ab32d7c9 675 switch (pos) {
tvendov 2:7c75ab32d7c9 676 case fsfat_fopen_kv_name_pos_start:
tvendov 2:7c75ab32d7c9 677 pos_str = "start";
tvendov 2:7c75ab32d7c9 678 break;
tvendov 2:7c75ab32d7c9 679 case fsfat_fopen_kv_name_pos_mid:
tvendov 2:7c75ab32d7c9 680 pos_str = "middle";
tvendov 2:7c75ab32d7c9 681 break;
tvendov 2:7c75ab32d7c9 682 case fsfat_fopen_kv_name_pos_end:
tvendov 2:7c75ab32d7c9 683 pos_str = "end";
tvendov 2:7c75ab32d7c9 684 break;
tvendov 2:7c75ab32d7c9 685 default:
tvendov 2:7c75ab32d7c9 686 break;
tvendov 2:7c75ab32d7c9 687 }
tvendov 2:7c75ab32d7c9 688 #endif
tvendov 2:7c75ab32d7c9 689 ret = fsfat_test_create(filename, (const char *) filename, len);
tvendov 2:7c75ab32d7c9 690
tvendov 2:7c75ab32d7c9 691 /* special cases */
tvendov 2:7c75ab32d7c9 692 switch (j) {
tvendov 2:7c75ab32d7c9 693 //case 0 :
tvendov 2:7c75ab32d7c9 694 //case 46 :
tvendov 2:7c75ab32d7c9 695 // switch(pos)
tvendov 2:7c75ab32d7c9 696 // {
tvendov 2:7c75ab32d7c9 697 // /* for code = 0 (null terminator). permitted at mid and end of string */
tvendov 2:7c75ab32d7c9 698 // /* for code = 46 ('.'). permitted at mid and end of string but not at start */
tvendov 2:7c75ab32d7c9 699 // case fsfat_fopen_kv_name_pos_start:
tvendov 2:7c75ab32d7c9 700 // f_allowed = false;
tvendov 2:7c75ab32d7c9 701 // break;
tvendov 2:7c75ab32d7c9 702 // case fsfat_fopen_kv_name_pos_mid:
tvendov 2:7c75ab32d7c9 703 // case fsfat_fopen_kv_name_pos_end:
tvendov 2:7c75ab32d7c9 704 // default:
tvendov 2:7c75ab32d7c9 705 // f_allowed = true;
tvendov 2:7c75ab32d7c9 706 // break;
tvendov 2:7c75ab32d7c9 707 // }
tvendov 2:7c75ab32d7c9 708 // break;
tvendov 2:7c75ab32d7c9 709 default:
tvendov 2:7c75ab32d7c9 710 f_allowed = node->f_allowed;
tvendov 2:7c75ab32d7c9 711 break;
tvendov 2:7c75ab32d7c9 712 }
tvendov 2:7c75ab32d7c9 713 if (f_allowed == true) {
tvendov 2:7c75ab32d7c9 714 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 715 "%s:Error: failed to create file in store when filename contains valid characters (code=%d, ret=%d).\n", __func__,
tvendov 2:7c75ab32d7c9 716 (int) j, (int) ret);
tvendov 2:7c75ab32d7c9 717 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 718 /* revert FSFAT_LOG for more trace */
tvendov 2:7c75ab32d7c9 719 FSFAT_DBGLOG("Successfully created a file with valid keyname containing ascii character code %d (%c) at the %s of the keyname.\n",
tvendov 2:7c75ab32d7c9 720 (int) j, (int) j, pos_str);
tvendov 2:7c75ab32d7c9 721 FSFAT_LOG("%c", '.');
tvendov 2:7c75ab32d7c9 722
tvendov 2:7c75ab32d7c9 723 ret = fsfat_test_delete(filename);
tvendov 2:7c75ab32d7c9 724 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 725 "%s:Error: failed to delete file previously created (code=%d, ret=%d).\n", __func__, (int) j, (int) ret);
tvendov 2:7c75ab32d7c9 726 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 727 } else {
tvendov 2:7c75ab32d7c9 728 /*node->f_allowed == false => not allowed to create kv name with ascii code */
tvendov 2:7c75ab32d7c9 729 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 730 "%s:Error: created file in store when filename contains an invalid character (code=%d, ret=%d).\n", __func__, (int) j,
tvendov 2:7c75ab32d7c9 731 (int) ret);
tvendov 2:7c75ab32d7c9 732 TEST_ASSERT_MESSAGE(ret < 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 733 /* revert FSFAT_LOG for more trace */
tvendov 2:7c75ab32d7c9 734 FSFAT_DBGLOG("Successfully failed to create a file with an invalid keyname containing ascii character code %d at the %s of the keyname.\n",
tvendov 2:7c75ab32d7c9 735 (int) j, pos_str);
tvendov 2:7c75ab32d7c9 736 FSFAT_LOG("%c", '.');
tvendov 2:7c75ab32d7c9 737 }
tvendov 2:7c75ab32d7c9 738 }
tvendov 2:7c75ab32d7c9 739 }
tvendov 2:7c75ab32d7c9 740 node++;
tvendov 2:7c75ab32d7c9 741 }
tvendov 2:7c75ab32d7c9 742
tvendov 2:7c75ab32d7c9 743 FSFAT_LOG("%c", '\n');
tvendov 2:7c75ab32d7c9 744 return CaseNext;
tvendov 2:7c75ab32d7c9 745 }
tvendov 2:7c75ab32d7c9 746
tvendov 2:7c75ab32d7c9 747
tvendov 2:7c75ab32d7c9 748 static const char fsfat_fopen_ascii_illegal_buf_g[] = "\"�'*+,./:;<=>?[\\]|";
tvendov 2:7c75ab32d7c9 749
tvendov 2:7c75ab32d7c9 750 /** @brief test to call fopen() with filename that in includes
tvendov 2:7c75ab32d7c9 751 * illegal characters
tvendov 2:7c75ab32d7c9 752 * - a max-length string of random illegal characters only
tvendov 2:7c75ab32d7c9 753 *
tvendov 2:7c75ab32d7c9 754 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 755 */
tvendov 2:7c75ab32d7c9 756 control_t fsfat_fopen_test_06(const size_t call_count)
tvendov 2:7c75ab32d7c9 757 {
tvendov 2:7c75ab32d7c9 758 const char *mnt_pt = FSFAT_FOPEN_TEST_MOUNT_PT_PATH;
tvendov 2:7c75ab32d7c9 759 const char *extname = "txt";
tvendov 2:7c75ab32d7c9 760 const size_t filename_len = strlen(mnt_pt) + FSFAT_MAX_FILE_BASENAME + strlen(extname) +
tvendov 2:7c75ab32d7c9 761 2; /* extra 2 chars for '/' and '.' in "/sd/goodfile.txt" */
tvendov 2:7c75ab32d7c9 762 char filename[FSFAT_BUF_MAX_LENGTH];
tvendov 2:7c75ab32d7c9 763 int32_t i = 0;
tvendov 2:7c75ab32d7c9 764 int32_t j = 0;
tvendov 2:7c75ab32d7c9 765 uint32_t pos = 0;
tvendov 2:7c75ab32d7c9 766 uint32_t len = 0;
tvendov 2:7c75ab32d7c9 767 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 768 size_t buf_data_max = 0;
tvendov 2:7c75ab32d7c9 769
tvendov 2:7c75ab32d7c9 770 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 771 (void) call_count;
tvendov 2:7c75ab32d7c9 772
tvendov 2:7c75ab32d7c9 773 memset(filename, 0, FSFAT_BUF_MAX_LENGTH);
tvendov 2:7c75ab32d7c9 774 /* create bad keyname strings with invalid character code at start of keyname */
tvendov 2:7c75ab32d7c9 775 buf_data_max = strlen(fsfat_fopen_ascii_illegal_buf_g);
tvendov 2:7c75ab32d7c9 776
tvendov 2:7c75ab32d7c9 777 /* generate a number of illegal filenames */
tvendov 2:7c75ab32d7c9 778 for (j = 0; i < FSFAT_MAX_FILE_BASENAME; j++) {
tvendov 2:7c75ab32d7c9 779 /* generate a kv name of illegal chars*/
tvendov 2:7c75ab32d7c9 780 len = snprintf(filename, filename_len + 1, "%s/", mnt_pt);
tvendov 2:7c75ab32d7c9 781 for (i = 0; i < FSFAT_MAX_FILE_BASENAME; i++) {
tvendov 2:7c75ab32d7c9 782 pos = rand() % (buf_data_max + 1);
tvendov 2:7c75ab32d7c9 783 len += snprintf(filename + len, filename_len + 1, "%c", fsfat_fopen_ascii_illegal_buf_g[pos]);
tvendov 2:7c75ab32d7c9 784
tvendov 2:7c75ab32d7c9 785 }
tvendov 2:7c75ab32d7c9 786 len += snprintf(filename + len, filename_len + 1, ".%s", extname);
tvendov 2:7c75ab32d7c9 787 ret = fsfat_test_create(filename, filename, len);
tvendov 2:7c75ab32d7c9 788 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 789 "%s:Error: created file when filename contains invalid characters (filename=%s, ret=%d).\n", __func__, filename,
tvendov 2:7c75ab32d7c9 790 (int) ret);
tvendov 2:7c75ab32d7c9 791 TEST_ASSERT_MESSAGE(ret < 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 792 }
tvendov 2:7c75ab32d7c9 793 return CaseNext;
tvendov 2:7c75ab32d7c9 794 }
tvendov 2:7c75ab32d7c9 795
tvendov 2:7c75ab32d7c9 796
tvendov 2:7c75ab32d7c9 797 /** @brief test for errno reporting on a failed fopen()call
tvendov 2:7c75ab32d7c9 798 *
tvendov 2:7c75ab32d7c9 799 * This test does the following:
tvendov 2:7c75ab32d7c9 800 * - tries to open a file that does not exist for reading, and checks that a NULL pointer is returned.
tvendov 2:7c75ab32d7c9 801 * - checks that errno is not 0 as there is an error.
tvendov 2:7c75ab32d7c9 802 * - checks that ferror() returns 1 indicating an error exists.
tvendov 2:7c75ab32d7c9 803 *
tvendov 2:7c75ab32d7c9 804 * Note: see NOTE_1 below.
tvendov 2:7c75ab32d7c9 805 *
tvendov 2:7c75ab32d7c9 806 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 807 */
tvendov 2:7c75ab32d7c9 808 control_t fsfat_fopen_test_07(const size_t call_count)
tvendov 2:7c75ab32d7c9 809 {
tvendov 2:7c75ab32d7c9 810 FILE *f = NULL;
tvendov 2:7c75ab32d7c9 811 int ret = -1;
tvendov 2:7c75ab32d7c9 812 int errno_val = 0;
tvendov 2:7c75ab32d7c9 813 const char *filename = sd_badfile_path;
tvendov 2:7c75ab32d7c9 814
tvendov 2:7c75ab32d7c9 815 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 816 (void) call_count;
tvendov 2:7c75ab32d7c9 817
tvendov 2:7c75ab32d7c9 818 errno = 0;
tvendov 2:7c75ab32d7c9 819 /* this is expect to fail as the file doesnt exist */
tvendov 2:7c75ab32d7c9 820 f = fopen(filename, "r");
tvendov 2:7c75ab32d7c9 821
tvendov 2:7c75ab32d7c9 822 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 823 "%s:Error: opened non-existent file for reading (filename=%s, f=%p).\n", __func__, filename, f);
tvendov 2:7c75ab32d7c9 824 TEST_ASSERT_MESSAGE(f == NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 825
tvendov 2:7c75ab32d7c9 826 /* check errno is set correctly */
tvendov 2:7c75ab32d7c9 827 #if ! defined(__ARMCC_VERSION) && defined(__GNUC__)
tvendov 2:7c75ab32d7c9 828 /* Store errno so the current value set is not changed by new function call */
tvendov 2:7c75ab32d7c9 829 errno_val = errno;
tvendov 2:7c75ab32d7c9 830 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 831 "%s:Error: errno has unexpected value (errno != 0 expected) (filename=%s, errno=%d).\n", __func__, filename, errno);
tvendov 2:7c75ab32d7c9 832 TEST_ASSERT_MESSAGE(errno_val != 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 833 #endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */
tvendov 2:7c75ab32d7c9 834 return CaseNext;
tvendov 2:7c75ab32d7c9 835 }
tvendov 2:7c75ab32d7c9 836
tvendov 2:7c75ab32d7c9 837
tvendov 2:7c75ab32d7c9 838 /** @brief test for operation of clearerr() and ferror()
tvendov 2:7c75ab32d7c9 839 *
tvendov 2:7c75ab32d7c9 840 * The test does the following:
tvendov 2:7c75ab32d7c9 841 * - opens and then closes a file, but keeps a copy of the FILE pointer fp.
tvendov 2:7c75ab32d7c9 842 * - set errno to 0.
tvendov 2:7c75ab32d7c9 843 * - write to the close file with fwrite(fp) which should return 0 (no writes) and set the errno.
tvendov 2:7c75ab32d7c9 844 * - check the error condition is set with ferror().
tvendov 2:7c75ab32d7c9 845 * - clear the error with clearerr().
tvendov 2:7c75ab32d7c9 846 * - check the error condition is reset with ferror().
tvendov 2:7c75ab32d7c9 847 *
tvendov 2:7c75ab32d7c9 848 * NOTE_1: GCC/ARMCC support for setting errno
tvendov 2:7c75ab32d7c9 849 * - Documentation (e.g. fwrite() man page) does not explicity say fwrite() sets errno
tvendov 2:7c75ab32d7c9 850 * (e.g. for an fwrite() on a read-only file).
tvendov 2:7c75ab32d7c9 851 * - GCC libc fwrite() appears to set errno as expected.
tvendov 2:7c75ab32d7c9 852 * - ARMCC & IAR libc fwrite() appears not to set errno.
tvendov 2:7c75ab32d7c9 853 *
tvendov 2:7c75ab32d7c9 854 * The following ARMCC documents are silent on whether fwrite() sets errno:
tvendov 2:7c75ab32d7c9 855 * - "ARM C and C++ Libraries and Floating-Point Support".
tvendov 2:7c75ab32d7c9 856 * - "RL-ARM User Guide fwrite() section".
tvendov 2:7c75ab32d7c9 857 *
tvendov 2:7c75ab32d7c9 858 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 859 */
tvendov 2:7c75ab32d7c9 860 control_t fsfat_fopen_test_08(const size_t call_count)
tvendov 2:7c75ab32d7c9 861 {
tvendov 2:7c75ab32d7c9 862 FILE *fp = NULL;
tvendov 2:7c75ab32d7c9 863 int ret = -1;
tvendov 2:7c75ab32d7c9 864 int ret_ferror = -1;
tvendov 2:7c75ab32d7c9 865 const char *filename = sd_testfile_path;
tvendov 2:7c75ab32d7c9 866
tvendov 2:7c75ab32d7c9 867 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 868 (void) call_count;
tvendov 2:7c75ab32d7c9 869
tvendov 2:7c75ab32d7c9 870 errno = 0;
tvendov 2:7c75ab32d7c9 871 fp = fopen(filename, "w+");
tvendov 2:7c75ab32d7c9 872 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 873 "%s:Error: failed to open file (filename=%s, f=%p).\n", __func__, filename, fp);
tvendov 2:7c75ab32d7c9 874 TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 875
tvendov 2:7c75ab32d7c9 876 /* close the fp but then try to read or write it */
tvendov 2:7c75ab32d7c9 877 ret = fclose(fp);
tvendov 2:7c75ab32d7c9 878 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 879 "%s:Error: failed to close file (ret=%d, errno=%d)\n", __func__, (int) ret, errno);
tvendov 2:7c75ab32d7c9 880 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 881
tvendov 2:7c75ab32d7c9 882 /* open file */
tvendov 2:7c75ab32d7c9 883 errno = 0;
tvendov 2:7c75ab32d7c9 884 fp = fopen(filename, "r");
tvendov 2:7c75ab32d7c9 885 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 886 "%s:Error: failed to open file for reading (filename=\"%s\", ret=%d)\n", __func__, filename, (int) ret);
tvendov 2:7c75ab32d7c9 887 TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 888
tvendov 2:7c75ab32d7c9 889 /* Perform fwrite() operation that will fail. */
tvendov 2:7c75ab32d7c9 890 errno = 0;
tvendov 2:7c75ab32d7c9 891 ret = fwrite("42!", 4, 1, fp);
tvendov 2:7c75ab32d7c9 892
tvendov 2:7c75ab32d7c9 893 ret_ferror = ferror(fp);
tvendov 2:7c75ab32d7c9 894 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 895 "%s:Error: ferror() failed to report error (filename=%s, ret_ferror=%d).\n", __func__, filename, (int) ret_ferror);
tvendov 2:7c75ab32d7c9 896 TEST_ASSERT_MESSAGE(ret_ferror != 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 897
tvendov 2:7c75ab32d7c9 898 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 899 "%s:Error: fwrite successfully wrote to read-only file (filename=%s, ret=%d).\n", __func__, filename, (int) ret);
tvendov 2:7c75ab32d7c9 900 /* the fwrite() should fail and return 0. */
tvendov 2:7c75ab32d7c9 901 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 902
tvendov 2:7c75ab32d7c9 903 #if ! defined(__ARMCC_VERSION) && defined(__GNUC__)
tvendov 2:7c75ab32d7c9 904 /* check that errno is set. ARMCC appears not to set errno for fwrite() failure. */
tvendov 2:7c75ab32d7c9 905 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 906 "%s:Error: unexpected zero value for errno (filename=%s, ret=%d, errno=%d).\n", __func__, filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 907 TEST_ASSERT_MESSAGE(errno != 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 908
tvendov 2:7c75ab32d7c9 909 /* check that errno is set to the expected value (this may change differ for different libc's) */
tvendov 2:7c75ab32d7c9 910 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 911 "%s:Error: errno != EBADF (filename=%s, ret=%d, errno=%d).\n", __func__, filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 912 TEST_ASSERT_MESSAGE(errno == EBADF, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 913 #endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */
tvendov 2:7c75ab32d7c9 914
tvendov 2:7c75ab32d7c9 915 /* check clearerr() return clears the error */
tvendov 2:7c75ab32d7c9 916 clearerr(fp);
tvendov 2:7c75ab32d7c9 917 ret = ferror(fp);
tvendov 2:7c75ab32d7c9 918 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 919 "%s:Error: ferror() did not return zero value when error has been cleared (filename=%s, ret=%d).\n", __func__, filename,
tvendov 2:7c75ab32d7c9 920 (int) ret);
tvendov 2:7c75ab32d7c9 921 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 922
tvendov 2:7c75ab32d7c9 923 fclose(fp);
tvendov 2:7c75ab32d7c9 924 return CaseNext;
tvendov 2:7c75ab32d7c9 925 }
tvendov 2:7c75ab32d7c9 926
tvendov 2:7c75ab32d7c9 927
tvendov 2:7c75ab32d7c9 928 /** @brief test for operation of ftell()
tvendov 2:7c75ab32d7c9 929 *
tvendov 2:7c75ab32d7c9 930 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 931 */
tvendov 2:7c75ab32d7c9 932 control_t fsfat_fopen_test_09(const size_t call_count)
tvendov 2:7c75ab32d7c9 933 {
tvendov 2:7c75ab32d7c9 934 FILE *fp = NULL;
tvendov 2:7c75ab32d7c9 935 int ret = -1;
tvendov 2:7c75ab32d7c9 936 int32_t len = 0;
tvendov 2:7c75ab32d7c9 937
tvendov 2:7c75ab32d7c9 938 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 939 (void) call_count;
tvendov 2:7c75ab32d7c9 940
tvendov 2:7c75ab32d7c9 941 /* create a file of a certain length */
tvendov 2:7c75ab32d7c9 942 len = strlen(fsfat_fopen_test_02_data[0].value);
tvendov 2:7c75ab32d7c9 943 ret = fsfat_test_create(fsfat_fopen_test_02_data[0].filename, (char *) fsfat_fopen_test_02_data[0].value, len);
tvendov 2:7c75ab32d7c9 944
tvendov 2:7c75ab32d7c9 945 errno = 0;
tvendov 2:7c75ab32d7c9 946 /* Open the file for reading so the file is not truncated to 0 length. */
tvendov 2:7c75ab32d7c9 947 fp = fopen(fsfat_fopen_test_02_data[0].filename, "r");
tvendov 2:7c75ab32d7c9 948 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 949 "%s:Error: failed to open file (filename=%s, fp=%p, errno=%d).\n", __func__, fsfat_fopen_test_02_data[0].filename, fp,
tvendov 2:7c75ab32d7c9 950 errno);
tvendov 2:7c75ab32d7c9 951 TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 952
tvendov 2:7c75ab32d7c9 953 errno = 0;
tvendov 2:7c75ab32d7c9 954 ret = fseek(fp, 0, SEEK_END);
tvendov 2:7c75ab32d7c9 955 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 956 "%s:Error: fseek() failed to SEEK_END (filename=%s, ret=%d, errno=%d).\n", __func__,
tvendov 2:7c75ab32d7c9 957 fsfat_fopen_test_02_data[0].filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 958 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 959
tvendov 2:7c75ab32d7c9 960 errno = 0;
tvendov 2:7c75ab32d7c9 961 ret = ftell(fp);
tvendov 2:7c75ab32d7c9 962 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 963 "%s:Error: ftell() failed to report correct offset value (filename=%s, ret=%d, errno=%d).\n", __func__,
tvendov 2:7c75ab32d7c9 964 fsfat_fopen_test_02_data[0].filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 965 TEST_ASSERT_MESSAGE(ret == len, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 966
tvendov 2:7c75ab32d7c9 967 errno = 0;
tvendov 2:7c75ab32d7c9 968 ret = fclose(fp);
tvendov 2:7c75ab32d7c9 969 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 970 "%s:Error: failed to close file (ret=%d, errno=%d)\n", __func__, (int) ret, errno);
tvendov 2:7c75ab32d7c9 971 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 972
tvendov 2:7c75ab32d7c9 973 return CaseNext;
tvendov 2:7c75ab32d7c9 974 }
tvendov 2:7c75ab32d7c9 975
tvendov 2:7c75ab32d7c9 976
tvendov 2:7c75ab32d7c9 977 /* file data for test_10 */
tvendov 2:7c75ab32d7c9 978 static fsfat_kv_data_t fsfat_fopen_test_10_kv_data[] = {
tvendov 2:7c75ab32d7c9 979 { "/sd/test_10/testfile.txt", "test_data"},
tvendov 2:7c75ab32d7c9 980 { NULL, NULL},
tvendov 2:7c75ab32d7c9 981 };
tvendov 2:7c75ab32d7c9 982
tvendov 2:7c75ab32d7c9 983 /** @brief test for operation of remove()
tvendov 2:7c75ab32d7c9 984 *
tvendov 2:7c75ab32d7c9 985 * Performs the following tests:
tvendov 2:7c75ab32d7c9 986 * 1. test remove() on a file that exists. This should succeed.
tvendov 2:7c75ab32d7c9 987 * 2. test remove() on a dir that exists. This should succeed.
tvendov 2:7c75ab32d7c9 988 * 3. test remove() on a file that doesnt exist. This should fail. check errno set.
tvendov 2:7c75ab32d7c9 989 * 4. test remove() on a dir that doesnt exist. This should fail. check errno set.
tvendov 2:7c75ab32d7c9 990 *
tvendov 2:7c75ab32d7c9 991 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 992 */
tvendov 2:7c75ab32d7c9 993 control_t fsfat_fopen_test_10(const size_t call_count)
tvendov 2:7c75ab32d7c9 994 {
tvendov 2:7c75ab32d7c9 995 char buf[FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1];
tvendov 2:7c75ab32d7c9 996 char *pos = NULL;
tvendov 2:7c75ab32d7c9 997 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 998 size_t len = 0;
tvendov 2:7c75ab32d7c9 999 fsfat_kv_data_t *node = fsfat_fopen_test_10_kv_data;
tvendov 2:7c75ab32d7c9 1000
tvendov 2:7c75ab32d7c9 1001 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 1002 (void) call_count;
tvendov 2:7c75ab32d7c9 1003
tvendov 2:7c75ab32d7c9 1004 TEST_ASSERT(strlen(node->filename) < FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1);
tvendov 2:7c75ab32d7c9 1005
tvendov 2:7c75ab32d7c9 1006 /* start from a known state i.e. directory to be created in not present */
tvendov 2:7c75ab32d7c9 1007 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1008
tvendov 2:7c75ab32d7c9 1009 /* (1) */
tvendov 2:7c75ab32d7c9 1010 errno = 0;
tvendov 2:7c75ab32d7c9 1011 ret = fsfat_filepath_make_dirs((char *) node->filename, false);
tvendov 2:7c75ab32d7c9 1012 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1013 "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1014 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1015
tvendov 2:7c75ab32d7c9 1016 len = strlen(node->value);
tvendov 2:7c75ab32d7c9 1017 ret = fsfat_test_create(node->filename, (char *) node->value, len);
tvendov 2:7c75ab32d7c9 1018 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1019 "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 1020 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1021
tvendov 2:7c75ab32d7c9 1022 ret = remove(node->filename);
tvendov 2:7c75ab32d7c9 1023 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1024 "%s:Error: delete file operation failed (filename=%s, ret=%d) .\n", __func__, node->filename, (int) ret);
tvendov 2:7c75ab32d7c9 1025 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1026
tvendov 2:7c75ab32d7c9 1027 /* (3) */
tvendov 2:7c75ab32d7c9 1028 ret = remove(node->filename);
tvendov 2:7c75ab32d7c9 1029 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1030 "%s:Error: deleted a file that doesn't exist (filename=%s, ret=%d, errno=%d) .\n", __func__, node->filename, (int) ret,
tvendov 2:7c75ab32d7c9 1031 errno);
tvendov 2:7c75ab32d7c9 1032 TEST_ASSERT_MESSAGE(ret != 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1033
tvendov 2:7c75ab32d7c9 1034 /* (2) */
tvendov 2:7c75ab32d7c9 1035 memset(buf, 0, FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1);
tvendov 2:7c75ab32d7c9 1036 memcpy(buf, node->filename, strlen(node->filename));
tvendov 2:7c75ab32d7c9 1037 pos = strrchr(buf, '/');
tvendov 2:7c75ab32d7c9 1038 *pos = '\0';
tvendov 2:7c75ab32d7c9 1039 ret = remove(buf);
tvendov 2:7c75ab32d7c9 1040 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1041 "%s:Error: delete directory operation failed (directory name=%s, ret=%d, errno=%d).\n", __func__, buf, (int) ret,
tvendov 2:7c75ab32d7c9 1042 errno);
tvendov 2:7c75ab32d7c9 1043 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1044
tvendov 2:7c75ab32d7c9 1045 /* (4) */
tvendov 2:7c75ab32d7c9 1046 ret = remove(buf);
tvendov 2:7c75ab32d7c9 1047 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1048 "%s:Error: deleted a directory that doesn't exist (directory name=%s, ret=%d, errno=%d).\n", __func__, buf, (int) ret,
tvendov 2:7c75ab32d7c9 1049 errno);
tvendov 2:7c75ab32d7c9 1050 TEST_ASSERT_MESSAGE(ret != 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1051
tvendov 2:7c75ab32d7c9 1052 return CaseNext;
tvendov 2:7c75ab32d7c9 1053 }
tvendov 2:7c75ab32d7c9 1054
tvendov 2:7c75ab32d7c9 1055
tvendov 2:7c75ab32d7c9 1056 /* file data for test_11 */
tvendov 2:7c75ab32d7c9 1057 static fsfat_kv_data_t fsfat_fopen_test_11_kv_data[] = {
tvendov 2:7c75ab32d7c9 1058 { "/sd/test_11/step0.txt", "test_data"},
tvendov 2:7c75ab32d7c9 1059 { "/sd/test_11/step1.txt", "test_data"},
tvendov 2:7c75ab32d7c9 1060 { "/sd/test_11/subdir/step3.txt", "test_data"},
tvendov 2:7c75ab32d7c9 1061 { NULL, NULL},
tvendov 2:7c75ab32d7c9 1062 };
tvendov 2:7c75ab32d7c9 1063
tvendov 2:7c75ab32d7c9 1064 /** @brief test for operation of rename()
tvendov 2:7c75ab32d7c9 1065 *
tvendov 2:7c75ab32d7c9 1066 * This test does the following:
tvendov 2:7c75ab32d7c9 1067 * 1) test rename() on a file that exists to a new filename within the same directory.
tvendov 2:7c75ab32d7c9 1068 * 2) test rename() on a file that exists to a new filename within a different directory.
tvendov 2:7c75ab32d7c9 1069 *
tvendov 2:7c75ab32d7c9 1070 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 1071 */
tvendov 2:7c75ab32d7c9 1072 control_t fsfat_fopen_test_11(const size_t call_count)
tvendov 2:7c75ab32d7c9 1073 {
tvendov 2:7c75ab32d7c9 1074 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 1075 size_t len = 0;
tvendov 2:7c75ab32d7c9 1076 fsfat_kv_data_t *node = fsfat_fopen_test_11_kv_data;
tvendov 2:7c75ab32d7c9 1077
tvendov 2:7c75ab32d7c9 1078 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 1079 (void) call_count;
tvendov 2:7c75ab32d7c9 1080
tvendov 2:7c75ab32d7c9 1081 TEST_ASSERT(strlen(node->filename) < FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1);
tvendov 2:7c75ab32d7c9 1082
tvendov 2:7c75ab32d7c9 1083 /* start from a known state i.e. directory to be created in not present, files not present */
tvendov 2:7c75ab32d7c9 1084 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1085 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1086 node++;
tvendov 2:7c75ab32d7c9 1087 }
tvendov 2:7c75ab32d7c9 1088
tvendov 2:7c75ab32d7c9 1089 /* create file and directories ready for rename() tests */
tvendov 2:7c75ab32d7c9 1090 errno = 0;
tvendov 2:7c75ab32d7c9 1091 node = fsfat_fopen_test_11_kv_data;
tvendov 2:7c75ab32d7c9 1092 ret = fsfat_filepath_make_dirs((char *) node->filename, false);
tvendov 2:7c75ab32d7c9 1093 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1094 "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1095 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1096
tvendov 2:7c75ab32d7c9 1097 len = strlen(node->value);
tvendov 2:7c75ab32d7c9 1098 ret = fsfat_test_create(node->filename, (char *) node->value, len);
tvendov 2:7c75ab32d7c9 1099 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1100 "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 1101 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1102
tvendov 2:7c75ab32d7c9 1103 errno = 0;
tvendov 2:7c75ab32d7c9 1104 node = &fsfat_fopen_test_11_kv_data[2];
tvendov 2:7c75ab32d7c9 1105 ret = fsfat_filepath_make_dirs((char *) node->filename, false);
tvendov 2:7c75ab32d7c9 1106 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1107 "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1108 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1109
tvendov 2:7c75ab32d7c9 1110 /* (1) */
tvendov 2:7c75ab32d7c9 1111 ret = rename(fsfat_fopen_test_11_kv_data[0].filename, fsfat_fopen_test_11_kv_data[1].filename);
tvendov 2:7c75ab32d7c9 1112 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1113 "%s:Error: unable to rename file from (%s) to (%s) (ret=%d, errno=%d).\n", __func__,
tvendov 2:7c75ab32d7c9 1114 fsfat_fopen_test_11_kv_data[0].filename, fsfat_fopen_test_11_kv_data[1].filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1115 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1116
tvendov 2:7c75ab32d7c9 1117 /* (2) */
tvendov 2:7c75ab32d7c9 1118 ret = rename(fsfat_fopen_test_11_kv_data[1].filename, fsfat_fopen_test_11_kv_data[2].filename);
tvendov 2:7c75ab32d7c9 1119 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1120 "%s:Error: unable to rename file from (%s) to (%s) (ret=%d, errno=%d).\n", __func__,
tvendov 2:7c75ab32d7c9 1121 fsfat_fopen_test_11_kv_data[1].filename, fsfat_fopen_test_11_kv_data[2].filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1122 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1123
tvendov 2:7c75ab32d7c9 1124 return CaseNext;
tvendov 2:7c75ab32d7c9 1125 }
tvendov 2:7c75ab32d7c9 1126
tvendov 2:7c75ab32d7c9 1127
tvendov 2:7c75ab32d7c9 1128 /* file data for test_12 */
tvendov 2:7c75ab32d7c9 1129 static fsfat_kv_data_t fsfat_fopen_test_12_kv_data[] = {
tvendov 2:7c75ab32d7c9 1130 { "/sd/test_12/subdir/testfil1.txt", "testfil1.txt"},
tvendov 2:7c75ab32d7c9 1131 { "/sd/test_12/testfil2.txt", "testfil2.txt"},
tvendov 2:7c75ab32d7c9 1132 { "/sd/test_12/testfil3.txt", "testfil3.txt"},
tvendov 2:7c75ab32d7c9 1133 { "/sd/test_12/testfil4.txt", "testfil4.txt"},
tvendov 2:7c75ab32d7c9 1134 { "/sd/test_12/testfil5.txt", "testfil5.txt"},
tvendov 2:7c75ab32d7c9 1135 { NULL, NULL},
tvendov 2:7c75ab32d7c9 1136 };
tvendov 2:7c75ab32d7c9 1137
tvendov 2:7c75ab32d7c9 1138 /** @brief test for operation of readdir().
tvendov 2:7c75ab32d7c9 1139 *
tvendov 2:7c75ab32d7c9 1140 * Note, rewinddir(), telldir() and seekdir() dont appear to work reliably.
tvendov 2:7c75ab32d7c9 1141 * opendir() not available on ARM/IAR toolchains.
tvendov 2:7c75ab32d7c9 1142 *
tvendov 2:7c75ab32d7c9 1143 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 1144 */
tvendov 2:7c75ab32d7c9 1145 control_t fsfat_fopen_test_12(const size_t call_count)
tvendov 2:7c75ab32d7c9 1146 {
tvendov 2:7c75ab32d7c9 1147 char buf[FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1];
tvendov 2:7c75ab32d7c9 1148 char *pos = NULL;
tvendov 2:7c75ab32d7c9 1149 int32_t count = 0;
tvendov 2:7c75ab32d7c9 1150 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 1151 size_t len = 0;
tvendov 2:7c75ab32d7c9 1152 DIR *dir;
tvendov 2:7c75ab32d7c9 1153 struct dirent *dp;
tvendov 2:7c75ab32d7c9 1154 fsfat_kv_data_t *node = fsfat_fopen_test_12_kv_data;
tvendov 2:7c75ab32d7c9 1155
tvendov 2:7c75ab32d7c9 1156 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 1157 (void) call_count;
tvendov 2:7c75ab32d7c9 1158
tvendov 2:7c75ab32d7c9 1159 #if ! defined(__ARMCC_VERSION) && defined(__GNUC__)
tvendov 2:7c75ab32d7c9 1160
tvendov 2:7c75ab32d7c9 1161 /* start from a known state i.e. directory to be created in not present */
tvendov 2:7c75ab32d7c9 1162 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1163 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1164 node++;
tvendov 2:7c75ab32d7c9 1165 }
tvendov 2:7c75ab32d7c9 1166
tvendov 2:7c75ab32d7c9 1167 /* create a file */
tvendov 2:7c75ab32d7c9 1168 node = fsfat_fopen_test_12_kv_data;
tvendov 2:7c75ab32d7c9 1169 errno = 0;
tvendov 2:7c75ab32d7c9 1170 ret = fsfat_filepath_make_dirs((char *) node->filename, false);
tvendov 2:7c75ab32d7c9 1171 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1172 "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1173 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1174
tvendov 2:7c75ab32d7c9 1175 node = fsfat_fopen_test_12_kv_data;
tvendov 2:7c75ab32d7c9 1176 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1177 len = strlen(node->value);
tvendov 2:7c75ab32d7c9 1178 ret = fsfat_test_create(node->filename, (char *) node->value, len);
tvendov 2:7c75ab32d7c9 1179 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1180 "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 1181 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1182 node++;
tvendov 2:7c75ab32d7c9 1183 }
tvendov 2:7c75ab32d7c9 1184
tvendov 2:7c75ab32d7c9 1185 node = fsfat_fopen_test_12_kv_data;
tvendov 2:7c75ab32d7c9 1186 memset(buf, 0, FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1);
tvendov 2:7c75ab32d7c9 1187 memcpy(buf, node->filename, strlen(node->filename));
tvendov 2:7c75ab32d7c9 1188 pos = strrchr(buf, '/');
tvendov 2:7c75ab32d7c9 1189 *pos = '\0';
tvendov 2:7c75ab32d7c9 1190 dir = opendir(buf);
tvendov 2:7c75ab32d7c9 1191
tvendov 2:7c75ab32d7c9 1192 while ((dp = readdir(dir)) != NULL) {
tvendov 2:7c75ab32d7c9 1193 FSFAT_DBGLOG("%s: filename: \"%s\"\n", __func__, dp->d_name);
tvendov 2:7c75ab32d7c9 1194 TEST_ASSERT_MESSAGE(dp != 0, "Error: readdir() failed\n");
tvendov 2:7c75ab32d7c9 1195 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1196 "%s:Error: unexpected object name (name=%s, expected=%s).\n", __func__, dp->d_name,
tvendov 2:7c75ab32d7c9 1197 fsfat_fopen_test_12_kv_data[count].value);
tvendov 2:7c75ab32d7c9 1198 TEST_ASSERT_MESSAGE(strncmp(dp->d_name, fsfat_fopen_test_12_kv_data[count].value,
tvendov 2:7c75ab32d7c9 1199 strlen(fsfat_fopen_test_12_kv_data[count].value)) == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1200 count++;
tvendov 2:7c75ab32d7c9 1201 }
tvendov 2:7c75ab32d7c9 1202 closedir(dir);
tvendov 2:7c75ab32d7c9 1203
tvendov 2:7c75ab32d7c9 1204 /* cleanup */
tvendov 2:7c75ab32d7c9 1205 node = fsfat_fopen_test_12_kv_data;
tvendov 2:7c75ab32d7c9 1206 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1207 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1208 node++;
tvendov 2:7c75ab32d7c9 1209 }
tvendov 2:7c75ab32d7c9 1210 #endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */
tvendov 2:7c75ab32d7c9 1211 return CaseNext;
tvendov 2:7c75ab32d7c9 1212 }
tvendov 2:7c75ab32d7c9 1213
tvendov 2:7c75ab32d7c9 1214
tvendov 2:7c75ab32d7c9 1215 /* file data for test_13 */
tvendov 2:7c75ab32d7c9 1216 static fsfat_kv_data_t fsfat_fopen_test_13_kv_data[] = {
tvendov 2:7c75ab32d7c9 1217 /* a file is included in the filepath even though its not created by the test,
tvendov 2:7c75ab32d7c9 1218 * as the fsfat_filepath_make_dirs() works with it present. */
tvendov 2:7c75ab32d7c9 1219 { "/sd/test_13/dummy.txt", "testdir"},
tvendov 2:7c75ab32d7c9 1220 { NULL, NULL},
tvendov 2:7c75ab32d7c9 1221 };
tvendov 2:7c75ab32d7c9 1222 /** @brief test for operation of mkdir()/remove()
tvendov 2:7c75ab32d7c9 1223 *
tvendov 2:7c75ab32d7c9 1224 * This test checks that:
tvendov 2:7c75ab32d7c9 1225 * - The mkdir() function successfully creates a directory that is not already present.
tvendov 2:7c75ab32d7c9 1226 * - The mkdir() function returns EEXIST when trying to create a directory thats already present.
tvendov 2:7c75ab32d7c9 1227 * - The remove() function successfully removes a directory that is present.
tvendov 2:7c75ab32d7c9 1228 *
tvendov 2:7c75ab32d7c9 1229 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 1230 */
tvendov 2:7c75ab32d7c9 1231 control_t fsfat_fopen_test_13(const size_t call_count)
tvendov 2:7c75ab32d7c9 1232 {
tvendov 2:7c75ab32d7c9 1233 int32_t ret = 0;
tvendov 2:7c75ab32d7c9 1234
tvendov 2:7c75ab32d7c9 1235 FSFAT_DBGLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 1236 (void) call_count;
tvendov 2:7c75ab32d7c9 1237
tvendov 2:7c75ab32d7c9 1238 /* start from a known state i.e. directory to be created in not present */
tvendov 2:7c75ab32d7c9 1239 fsfat_filepath_remove_all((char *) fsfat_fopen_test_13_kv_data[0].filename);
tvendov 2:7c75ab32d7c9 1240
tvendov 2:7c75ab32d7c9 1241 errno = 0;
tvendov 2:7c75ab32d7c9 1242 ret = fsfat_filepath_make_dirs((char *) fsfat_fopen_test_13_kv_data[0].filename, false);
tvendov 2:7c75ab32d7c9 1243 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1244 "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, fsfat_fopen_test_13_kv_data[0].filename,
tvendov 2:7c75ab32d7c9 1245 (int) ret, errno);
tvendov 2:7c75ab32d7c9 1246 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1247
tvendov 2:7c75ab32d7c9 1248 /* check that get a suitable error when try to create it again.*/
tvendov 2:7c75ab32d7c9 1249 errno = 0;
tvendov 2:7c75ab32d7c9 1250 ret = fsfat_filepath_make_dirs((char *) fsfat_fopen_test_13_kv_data[0].filename, false);
tvendov 2:7c75ab32d7c9 1251 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1252 "%s:Error: permitted to create directory when already exists (dirname=%s, ret=%d, errno=%d)\n", __func__,
tvendov 2:7c75ab32d7c9 1253 fsfat_fopen_test_13_kv_data[0].filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1254 TEST_ASSERT_MESSAGE(ret != 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1255
tvendov 2:7c75ab32d7c9 1256 /* check errno is as expected */
tvendov 2:7c75ab32d7c9 1257 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1258 "%s:Error: errno != EEXIST (dirname=%s, ret=%d, errno=%d)\n", __func__, fsfat_fopen_test_13_kv_data[0].filename,
tvendov 2:7c75ab32d7c9 1259 (int) ret, errno);
tvendov 2:7c75ab32d7c9 1260 TEST_ASSERT_MESSAGE(errno == EEXIST, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1261
tvendov 2:7c75ab32d7c9 1262 ret = fsfat_filepath_remove_all((char *) fsfat_fopen_test_13_kv_data[0].filename);
tvendov 2:7c75ab32d7c9 1263 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1264 "%s:Error: failed to remove directory (dirname=%s, ret=%d, errno=%d)\n", __func__,
tvendov 2:7c75ab32d7c9 1265 fsfat_fopen_test_13_kv_data[0].filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1266 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1267
tvendov 2:7c75ab32d7c9 1268 return CaseNext;
tvendov 2:7c75ab32d7c9 1269 }
tvendov 2:7c75ab32d7c9 1270
tvendov 2:7c75ab32d7c9 1271 /* file data for test_14 */
tvendov 2:7c75ab32d7c9 1272 static fsfat_kv_data_t fsfat_fopen_test_14_kv_data[] = {
tvendov 2:7c75ab32d7c9 1273 /* a file is included in the filepath even though its not created by the test,
tvendov 2:7c75ab32d7c9 1274 * as the fsfat_filepath_make_dirs() works with it present. */
tvendov 2:7c75ab32d7c9 1275 { "/sd/test_14/testfile.txt", "testdata"},
tvendov 2:7c75ab32d7c9 1276 { NULL, NULL},
tvendov 2:7c75ab32d7c9 1277 };
tvendov 2:7c75ab32d7c9 1278
tvendov 2:7c75ab32d7c9 1279 /** @brief test for operation of stat()
tvendov 2:7c75ab32d7c9 1280 *
tvendov 2:7c75ab32d7c9 1281 * stat() is currently no supported by ARMCC and IAR toolchains libc.
tvendov 2:7c75ab32d7c9 1282 *
tvendov 2:7c75ab32d7c9 1283 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 1284 */
tvendov 2:7c75ab32d7c9 1285 control_t fsfat_fopen_test_14(const size_t call_count)
tvendov 2:7c75ab32d7c9 1286 {
tvendov 2:7c75ab32d7c9 1287 #if ! defined(__ARMCC_VERSION) && defined(__GNUC__)
tvendov 2:7c75ab32d7c9 1288
tvendov 2:7c75ab32d7c9 1289 char buf[FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1];
tvendov 2:7c75ab32d7c9 1290 char *pos = NULL;
tvendov 2:7c75ab32d7c9 1291 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 1292 size_t len = 0;
tvendov 2:7c75ab32d7c9 1293 struct stat file_stat;
tvendov 2:7c75ab32d7c9 1294 fsfat_kv_data_t *node = fsfat_fopen_test_14_kv_data;
tvendov 2:7c75ab32d7c9 1295
tvendov 2:7c75ab32d7c9 1296 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 1297 (void) call_count;
tvendov 2:7c75ab32d7c9 1298
tvendov 2:7c75ab32d7c9 1299 TEST_ASSERT(strlen(node->filename) < FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1);
tvendov 2:7c75ab32d7c9 1300
tvendov 2:7c75ab32d7c9 1301 /* start from a known state i.e. directory to be created in not present */
tvendov 2:7c75ab32d7c9 1302 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1303
tvendov 2:7c75ab32d7c9 1304 /* Create file in a directory. */
tvendov 2:7c75ab32d7c9 1305 errno = 0;
tvendov 2:7c75ab32d7c9 1306 ret = fsfat_filepath_make_dirs((char *) node->filename, false);
tvendov 2:7c75ab32d7c9 1307 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1308 "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno);
tvendov 2:7c75ab32d7c9 1309 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1310
tvendov 2:7c75ab32d7c9 1311 len = strlen(node->value);
tvendov 2:7c75ab32d7c9 1312 ret = fsfat_test_create(node->filename, (char *) node->value, len);
tvendov 2:7c75ab32d7c9 1313 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1314 "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 1315 TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1316
tvendov 2:7c75ab32d7c9 1317 /* Test stat() on the file returns the correct attribute set */
tvendov 2:7c75ab32d7c9 1318 memset(&file_stat, 0, sizeof(file_stat));
tvendov 2:7c75ab32d7c9 1319 ret = stat(node->filename, &file_stat);
tvendov 2:7c75ab32d7c9 1320 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1321 "%s:Error: stat() operation on file failed (filename=%s, ret=%d, errno=%d).\n", __func__, node->filename, (int) ret,
tvendov 2:7c75ab32d7c9 1322 errno);
tvendov 2:7c75ab32d7c9 1323 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1324
tvendov 2:7c75ab32d7c9 1325 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1326 "%s:Error: expected st_mode S_IFREG flag not set (filename=%s).\n", __func__, node->filename);
tvendov 2:7c75ab32d7c9 1327 TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFREG) == S_IFREG, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1328
tvendov 2:7c75ab32d7c9 1329 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1330 "%s:Error: unexpected st_mode S_IFDIR flag set (filename=%s).\n", __func__, node->filename);
tvendov 2:7c75ab32d7c9 1331 TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFDIR) != S_IFDIR, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1332
tvendov 2:7c75ab32d7c9 1333 /* Test stat() on the directory returns the correct attribute set */
tvendov 2:7c75ab32d7c9 1334 memset(&file_stat, 0, sizeof(file_stat));
tvendov 2:7c75ab32d7c9 1335 memset(buf, 0, FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1);
tvendov 2:7c75ab32d7c9 1336 memcpy(buf, node->filename, strlen(node->filename));
tvendov 2:7c75ab32d7c9 1337 pos = strrchr(buf, '/');
tvendov 2:7c75ab32d7c9 1338 *pos = '\0';
tvendov 2:7c75ab32d7c9 1339 ret = stat(buf, &file_stat);
tvendov 2:7c75ab32d7c9 1340 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1341 "%s:Error: stat() operation on directory failed (directory name=%s, ret=%d, errno=%d).\n", __func__, buf, (int) ret,
tvendov 2:7c75ab32d7c9 1342 errno);
tvendov 2:7c75ab32d7c9 1343 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1344
tvendov 2:7c75ab32d7c9 1345 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1346 "%s:Error: unexpected st_mode S_IFREG flag set (directory name=%s).\n", __func__, buf);
tvendov 2:7c75ab32d7c9 1347 TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFREG) != S_IFREG, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1348
tvendov 2:7c75ab32d7c9 1349 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1350 "%s:Error: expected st_mode S_IFDIR flag not set (directory name=%s).\n", __func__, buf);
tvendov 2:7c75ab32d7c9 1351 TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFDIR) == S_IFDIR, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1352
tvendov 2:7c75ab32d7c9 1353 /* clean up after successful test */
tvendov 2:7c75ab32d7c9 1354 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1355
tvendov 2:7c75ab32d7c9 1356 #endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */
tvendov 2:7c75ab32d7c9 1357 return CaseNext;
tvendov 2:7c75ab32d7c9 1358 }
tvendov 2:7c75ab32d7c9 1359
tvendov 2:7c75ab32d7c9 1360 /** @brief test for operation of SDFileSystem::format()
tvendov 2:7c75ab32d7c9 1361 *
tvendov 2:7c75ab32d7c9 1362 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 1363 */
tvendov 2:7c75ab32d7c9 1364 control_t fsfat_fopen_test_15(const size_t call_count)
tvendov 2:7c75ab32d7c9 1365 {
tvendov 2:7c75ab32d7c9 1366
tvendov 2:7c75ab32d7c9 1367 FSFAT_FENTRYLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 1368 (void) call_count;
tvendov 2:7c75ab32d7c9 1369 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 1370
tvendov 2:7c75ab32d7c9 1371 /* the allocation_unit of 0 means chanFS will use the default for the card (varies according to capacity). */
tvendov 2:7c75ab32d7c9 1372 fs.unmount();
tvendov 2:7c75ab32d7c9 1373 ret = fs.format(&sd);
tvendov 2:7c75ab32d7c9 1374 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1375 "%s:Error: failed to format sdcard (ret=%d)\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 1376 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1377 fs.mount(&sd);
tvendov 2:7c75ab32d7c9 1378 return CaseNext;
tvendov 2:7c75ab32d7c9 1379 }
tvendov 2:7c75ab32d7c9 1380
tvendov 2:7c75ab32d7c9 1381
tvendov 2:7c75ab32d7c9 1382 /* @brief test utility function to create a file of a given size.
tvendov 2:7c75ab32d7c9 1383 *
tvendov 2:7c75ab32d7c9 1384 * A reference data table is used of so that the data file can be later be
tvendov 2:7c75ab32d7c9 1385 * checked with fsfat_test_check_data_file().
tvendov 2:7c75ab32d7c9 1386 *
tvendov 2:7c75ab32d7c9 1387 * @param filename name of the file including path
tvendov 2:7c75ab32d7c9 1388 * @param data data to store in file
tvendov 2:7c75ab32d7c9 1389 * @param len number of bytes of data present in the data buffer.
tvendov 2:7c75ab32d7c9 1390 */
tvendov 2:7c75ab32d7c9 1391 int32_t fsfat_test_create_data_file(const char *filename, size_t len)
tvendov 2:7c75ab32d7c9 1392 {
tvendov 2:7c75ab32d7c9 1393 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 1394 FILE *fp = NULL;
tvendov 2:7c75ab32d7c9 1395 size_t write_len = 0;
tvendov 2:7c75ab32d7c9 1396 size_t written_len = 0;
tvendov 2:7c75ab32d7c9 1397 int32_t exp = 0;
tvendov 2:7c75ab32d7c9 1398 const int32_t exp_max = 8; /* so as not to exceed FSFAT_TEST_BYTE_DATA_TABLE_SIZE/2 */
tvendov 2:7c75ab32d7c9 1399
tvendov 2:7c75ab32d7c9 1400 FSFAT_FENTRYLOG("%s:entered (filename=%s, len=%d).\n", __func__, filename, (int) len);
tvendov 2:7c75ab32d7c9 1401 TEST_ASSERT(len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE == 0);
tvendov 2:7c75ab32d7c9 1402 fp = fopen(filename, "a");
tvendov 2:7c75ab32d7c9 1403 if (fp == NULL) {
tvendov 2:7c75ab32d7c9 1404 return ret;
tvendov 2:7c75ab32d7c9 1405 }
tvendov 2:7c75ab32d7c9 1406
tvendov 2:7c75ab32d7c9 1407 while (written_len < len) {
tvendov 2:7c75ab32d7c9 1408 /* write fsfat_test_byte_data_table or part thereof, in 9 writes of sizes
tvendov 2:7c75ab32d7c9 1409 * 1, 2, 4, 8, 16, 32, 64, 128, 1, totalling 256 bytes len permitting. */
tvendov 2:7c75ab32d7c9 1410 for (exp = 0; (exp <= exp_max) && (written_len < len); exp++) {
tvendov 2:7c75ab32d7c9 1411 write_len = 0x1 << (exp % exp_max);
tvendov 2:7c75ab32d7c9 1412 write_len = len - written_len > write_len ? write_len : len - written_len;
tvendov 2:7c75ab32d7c9 1413 ret = fwrite((const void *) &fsfat_test_byte_data_table[written_len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE], write_len, 1,
tvendov 2:7c75ab32d7c9 1414 fp);
tvendov 2:7c75ab32d7c9 1415 written_len += write_len;
tvendov 2:7c75ab32d7c9 1416 if (ret != 1) {
tvendov 2:7c75ab32d7c9 1417 FSFAT_DBGLOG("%s:Error: fwrite() failed (ret=%d)\n", __func__, (int) ret);
tvendov 2:7c75ab32d7c9 1418 ret = -1;
tvendov 2:7c75ab32d7c9 1419 goto out0;
tvendov 2:7c75ab32d7c9 1420 }
tvendov 2:7c75ab32d7c9 1421 }
tvendov 2:7c75ab32d7c9 1422 }
tvendov 2:7c75ab32d7c9 1423 if (written_len == len) {
tvendov 2:7c75ab32d7c9 1424 ret = 0;
tvendov 2:7c75ab32d7c9 1425 } else {
tvendov 2:7c75ab32d7c9 1426 ret = -1;
tvendov 2:7c75ab32d7c9 1427 }
tvendov 2:7c75ab32d7c9 1428 out0:
tvendov 2:7c75ab32d7c9 1429 fclose(fp);
tvendov 2:7c75ab32d7c9 1430 return ret;
tvendov 2:7c75ab32d7c9 1431 }
tvendov 2:7c75ab32d7c9 1432
tvendov 2:7c75ab32d7c9 1433
tvendov 2:7c75ab32d7c9 1434 /* @brief test utility function to check the data in the specified file is correct.
tvendov 2:7c75ab32d7c9 1435 *
tvendov 2:7c75ab32d7c9 1436 * The data read from the file is check that it agrees with the data written by
tvendov 2:7c75ab32d7c9 1437 * fsfat_test_create_data_file().
tvendov 2:7c75ab32d7c9 1438 *
tvendov 2:7c75ab32d7c9 1439 * @param filename name of the file including path
tvendov 2:7c75ab32d7c9 1440 * @param data data to store in file
tvendov 2:7c75ab32d7c9 1441 * @param len number of bytes of data present in the data buffer.
tvendov 2:7c75ab32d7c9 1442 */
tvendov 2:7c75ab32d7c9 1443 int32_t fsfat_test_check_data_file(const char *filename, size_t len)
tvendov 2:7c75ab32d7c9 1444 {
tvendov 2:7c75ab32d7c9 1445 int32_t ret = -1;
tvendov 2:7c75ab32d7c9 1446 FILE *fp = NULL;
tvendov 2:7c75ab32d7c9 1447 size_t read_len = 0;
tvendov 2:7c75ab32d7c9 1448 uint8_t buf[FSFAT_TEST_BYTE_DATA_TABLE_SIZE];
tvendov 2:7c75ab32d7c9 1449
tvendov 2:7c75ab32d7c9 1450 FSFAT_FENTRYLOG("%s:entered (filename=%s, len=%d).\n", __func__, filename, (int) len);
tvendov 2:7c75ab32d7c9 1451 TEST_ASSERT(len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE == 0);
tvendov 2:7c75ab32d7c9 1452 fp = fopen(filename, "r");
tvendov 2:7c75ab32d7c9 1453 if (fp == NULL) {
tvendov 2:7c75ab32d7c9 1454 return ret;
tvendov 2:7c75ab32d7c9 1455 }
tvendov 2:7c75ab32d7c9 1456
tvendov 2:7c75ab32d7c9 1457 while (read_len < len) {
tvendov 2:7c75ab32d7c9 1458 ret = fread((void *) buf, FSFAT_TEST_BYTE_DATA_TABLE_SIZE, 1, fp);
tvendov 2:7c75ab32d7c9 1459 read_len += FSFAT_TEST_BYTE_DATA_TABLE_SIZE;
tvendov 2:7c75ab32d7c9 1460 if (ret == 0) {
tvendov 2:7c75ab32d7c9 1461 /* end of read*/
tvendov 2:7c75ab32d7c9 1462 FSFAT_DBGLOG("%s:unable to read data\n", __func__);
tvendov 2:7c75ab32d7c9 1463 break;
tvendov 2:7c75ab32d7c9 1464 }
tvendov 2:7c75ab32d7c9 1465 if (memcmp(buf, fsfat_test_byte_data_table, FSFAT_TEST_BYTE_DATA_TABLE_SIZE) != 0) {
tvendov 2:7c75ab32d7c9 1466 FSFAT_DBGLOG("%s:Error: read data not as expected (0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x\n",
tvendov 2:7c75ab32d7c9 1467 __func__,
tvendov 2:7c75ab32d7c9 1468 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
tvendov 2:7c75ab32d7c9 1469 buf[14], buf[15]);
tvendov 2:7c75ab32d7c9 1470 ret = -1;
tvendov 2:7c75ab32d7c9 1471 goto out0;
tvendov 2:7c75ab32d7c9 1472 }
tvendov 2:7c75ab32d7c9 1473 }
tvendov 2:7c75ab32d7c9 1474 if (read_len == len) {
tvendov 2:7c75ab32d7c9 1475 ret = 0;
tvendov 2:7c75ab32d7c9 1476 }
tvendov 2:7c75ab32d7c9 1477 out0:
tvendov 2:7c75ab32d7c9 1478 fclose(fp);
tvendov 2:7c75ab32d7c9 1479 return ret;
tvendov 2:7c75ab32d7c9 1480 }
tvendov 2:7c75ab32d7c9 1481
tvendov 2:7c75ab32d7c9 1482 /* file data for test_16 */
tvendov 2:7c75ab32d7c9 1483 static fsfat_kv_data_t fsfat_fopen_test_16_kv_data[] = {
tvendov 2:7c75ab32d7c9 1484 { "/sd/tst16_0/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1485 { "/sd/tst16_1/subdir0/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1486 { "/sd/tst16_2/subdir0/subdir1/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1487 { "/sd/tst16_3/subdir0/subdir1/subdir2/subdir3/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1488 { "/sd/tst16_4/subdir0/subdir1/subdir2/subdir3/subdir4/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1489 { "/sd/tst16_5/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1490 { "/sd/tst16_6/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1491 { "/sd/tst16_7/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1492 { "/sd/tst16_8/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/subdir8/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1493 { "/sd/tst16_9/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/subdir8/subdir9/testfil0.txt", "dummy_data"},
tvendov 2:7c75ab32d7c9 1494 { NULL, NULL},
tvendov 2:7c75ab32d7c9 1495 };
tvendov 2:7c75ab32d7c9 1496
tvendov 2:7c75ab32d7c9 1497
tvendov 2:7c75ab32d7c9 1498 /** @brief stress test to write data to fs
tvendov 2:7c75ab32d7c9 1499 *
tvendov 2:7c75ab32d7c9 1500 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
tvendov 2:7c75ab32d7c9 1501 */
tvendov 2:7c75ab32d7c9 1502 control_t fsfat_fopen_test_16(const size_t call_count)
tvendov 2:7c75ab32d7c9 1503 {
tvendov 2:7c75ab32d7c9 1504 int32_t ret = 0;
tvendov 2:7c75ab32d7c9 1505 fsfat_kv_data_t *node = fsfat_fopen_test_16_kv_data;
tvendov 2:7c75ab32d7c9 1506 const int32_t num_blocks = 100; /* each file ~25kB */
tvendov 2:7c75ab32d7c9 1507
tvendov 2:7c75ab32d7c9 1508 FSFAT_DBGLOG("%s:entered\n", __func__);
tvendov 2:7c75ab32d7c9 1509 (void) call_count;
tvendov 2:7c75ab32d7c9 1510
tvendov 2:7c75ab32d7c9 1511 /* remove file and directory from a previous failed test run, if present */
tvendov 2:7c75ab32d7c9 1512 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1513 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1514 node++;
tvendov 2:7c75ab32d7c9 1515 }
tvendov 2:7c75ab32d7c9 1516
tvendov 2:7c75ab32d7c9 1517 /* create dirs */
tvendov 2:7c75ab32d7c9 1518 node = fsfat_fopen_test_16_kv_data;
tvendov 2:7c75ab32d7c9 1519 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1520 ret = fsfat_filepath_make_dirs((char *) node->filename, true);
tvendov 2:7c75ab32d7c9 1521 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1522 "%s:Error: failed to create dirs for filename (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret);
tvendov 2:7c75ab32d7c9 1523 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1524 node++;
tvendov 2:7c75ab32d7c9 1525 }
tvendov 2:7c75ab32d7c9 1526
tvendov 2:7c75ab32d7c9 1527 /* create the data files */
tvendov 2:7c75ab32d7c9 1528 node = fsfat_fopen_test_16_kv_data;
tvendov 2:7c75ab32d7c9 1529 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1530 ret = fsfat_test_create_data_file(node->filename, num_blocks * FSFAT_TEST_BYTE_DATA_TABLE_SIZE);
tvendov 2:7c75ab32d7c9 1531 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1532 "%s:Error: failed to create data file (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret);
tvendov 2:7c75ab32d7c9 1533 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1534 node++;
tvendov 2:7c75ab32d7c9 1535 }
tvendov 2:7c75ab32d7c9 1536
tvendov 2:7c75ab32d7c9 1537 /* read the data back and check its as expected */
tvendov 2:7c75ab32d7c9 1538 node = fsfat_fopen_test_16_kv_data;
tvendov 2:7c75ab32d7c9 1539 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1540 ret = fsfat_test_check_data_file(node->filename, num_blocks * FSFAT_TEST_BYTE_DATA_TABLE_SIZE);
tvendov 2:7c75ab32d7c9 1541 FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE,
tvendov 2:7c75ab32d7c9 1542 "%s:Error: failed to check data file (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret);
tvendov 2:7c75ab32d7c9 1543 TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g);
tvendov 2:7c75ab32d7c9 1544 node++;
tvendov 2:7c75ab32d7c9 1545 }
tvendov 2:7c75ab32d7c9 1546
tvendov 2:7c75ab32d7c9 1547 /* clean up */
tvendov 2:7c75ab32d7c9 1548 node = fsfat_fopen_test_16_kv_data;
tvendov 2:7c75ab32d7c9 1549 while (node->filename != NULL) {
tvendov 2:7c75ab32d7c9 1550 fsfat_filepath_remove_all((char *) node->filename);
tvendov 2:7c75ab32d7c9 1551 node++;
tvendov 2:7c75ab32d7c9 1552 }
tvendov 2:7c75ab32d7c9 1553 return CaseNext;
tvendov 2:7c75ab32d7c9 1554 }
tvendov 2:7c75ab32d7c9 1555
tvendov 2:7c75ab32d7c9 1556
tvendov 2:7c75ab32d7c9 1557 #else
tvendov 2:7c75ab32d7c9 1558
tvendov 2:7c75ab32d7c9 1559 #define FSFAT_FOPEN_TEST_00 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1560 #define FSFAT_FOPEN_TEST_01 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1561 #define FSFAT_FOPEN_TEST_02 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1562 #define FSFAT_FOPEN_TEST_03 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1563 #define FSFAT_FOPEN_TEST_04 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1564 #define FSFAT_FOPEN_TEST_05 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1565 #define FSFAT_FOPEN_TEST_06 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1566 #define FSFAT_FOPEN_TEST_07 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1567 #define FSFAT_FOPEN_TEST_08 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1568 #define FSFAT_FOPEN_TEST_09 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1569 #define FSFAT_FOPEN_TEST_10 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1570 #define FSFAT_FOPEN_TEST_11 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1571 #define FSFAT_FOPEN_TEST_12 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1572 #define FSFAT_FOPEN_TEST_13 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1573 #define FSFAT_FOPEN_TEST_14 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1574 #define FSFAT_FOPEN_TEST_15 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1575 #define FSFAT_FOPEN_TEST_16 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1576 #define FSFAT_FOPEN_TEST_17 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1577 #define FSFAT_FOPEN_TEST_18 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1578 #define FSFAT_FOPEN_TEST_19 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1579 #define FSFAT_FOPEN_TEST_20 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1580 #define FSFAT_FOPEN_TEST_21 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1581 #define FSFAT_FOPEN_TEST_22 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1582 #define FSFAT_FOPEN_TEST_23 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1583 #define FSFAT_FOPEN_TEST_24 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1584 #define FSFAT_FOPEN_TEST_25 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1585 #define FSFAT_FOPEN_TEST_26 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1586 #define FSFAT_FOPEN_TEST_27 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1587 #define FSFAT_FOPEN_TEST_28 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1588 #define FSFAT_FOPEN_TEST_29 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1589 #define FSFAT_FOPEN_TEST_30 fsfat_fopen_test_dummy
tvendov 2:7c75ab32d7c9 1590
tvendov 2:7c75ab32d7c9 1591 /** @brief fsfat_fopen_test_dummy Dummy test case for testing when platform doesnt have an SDCard installed.
tvendov 2:7c75ab32d7c9 1592 *
tvendov 2:7c75ab32d7c9 1593 * @return success always
tvendov 2:7c75ab32d7c9 1594 */
tvendov 2:7c75ab32d7c9 1595 static control_t fsfat_fopen_test_dummy()
tvendov 2:7c75ab32d7c9 1596 {
tvendov 2:7c75ab32d7c9 1597 printf("Null test\n");
tvendov 2:7c75ab32d7c9 1598 return CaseNext;
tvendov 2:7c75ab32d7c9 1599 }
tvendov 2:7c75ab32d7c9 1600
tvendov 2:7c75ab32d7c9 1601 #endif
tvendov 2:7c75ab32d7c9 1602
tvendov 2:7c75ab32d7c9 1603
tvendov 2:7c75ab32d7c9 1604 /// @cond FSFAT_DOXYGEN_DISABLE
tvendov 2:7c75ab32d7c9 1605 utest::v1::status_t greentea_setup(const size_t number_of_cases)
tvendov 2:7c75ab32d7c9 1606 {
tvendov 2:7c75ab32d7c9 1607 GREENTEA_SETUP(FSFAT_FOPEN_GREENTEA_TIMEOUT_S, "default_auto");
tvendov 2:7c75ab32d7c9 1608 return greentea_test_setup_handler(number_of_cases);
tvendov 2:7c75ab32d7c9 1609 }
tvendov 2:7c75ab32d7c9 1610
tvendov 2:7c75ab32d7c9 1611 Case cases[] = {
tvendov 2:7c75ab32d7c9 1612 /* 1 2 3 4 5 6 7 */
tvendov 2:7c75ab32d7c9 1613 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */
tvendov 2:7c75ab32d7c9 1614 Case("FSFAT_FOPEN_TEST_00: format sd card to FAT FS.", FSFAT_FOPEN_TEST_00),
tvendov 2:7c75ab32d7c9 1615 Case("FSFAT_FOPEN_TEST_01: fopen()/fwrite()/fclose() directories/file in multi-dir filepath.", FSFAT_FOPEN_TEST_01),
tvendov 2:7c75ab32d7c9 1616 Case("FSFAT_FOPEN_TEST_02: fopen(r) pre-existing file try to write it.", FSFAT_FOPEN_TEST_02),
tvendov 2:7c75ab32d7c9 1617 Case("FSFAT_FOPEN_TEST_03: fopen(w+) pre-existing file try to write it.", FSFAT_FOPEN_TEST_03),
tvendov 2:7c75ab32d7c9 1618 Case("FSFAT_FOPEN_TEST_04: fopen() with a filename exceeding the maximum length.", FSFAT_FOPEN_TEST_04),
tvendov 2:7c75ab32d7c9 1619 #ifdef FOPEN_EXTENDED_TESTING
tvendov 2:7c75ab32d7c9 1620 Case("FSFAT_FOPEN_TEST_05: fopen() with bad filenames (extended).", FSFAT_FOPEN_TEST_05),
tvendov 2:7c75ab32d7c9 1621 #endif
tvendov 2:7c75ab32d7c9 1622 Case("FSFAT_FOPEN_TEST_06: fopen() with bad filenames (minimal).", FSFAT_FOPEN_TEST_06),
tvendov 2:7c75ab32d7c9 1623 Case("FSFAT_FOPEN_TEST_07: fopen()/errno handling.", FSFAT_FOPEN_TEST_07),
tvendov 2:7c75ab32d7c9 1624 Case("FSFAT_FOPEN_TEST_08: ferror()/clearerr()/errno handling.", FSFAT_FOPEN_TEST_08),
tvendov 2:7c75ab32d7c9 1625 Case("FSFAT_FOPEN_TEST_09: ftell() handling.", FSFAT_FOPEN_TEST_09),
tvendov 2:7c75ab32d7c9 1626 Case("FSFAT_FOPEN_TEST_10: remove() test.", FSFAT_FOPEN_TEST_10),
tvendov 2:7c75ab32d7c9 1627 Case("FSFAT_FOPEN_TEST_11: rename().", FSFAT_FOPEN_TEST_11),
tvendov 2:7c75ab32d7c9 1628 Case("FSFAT_FOPEN_TEST_12: opendir(), readdir(), closedir() test.", FSFAT_FOPEN_TEST_12),
tvendov 2:7c75ab32d7c9 1629 Case("FSFAT_FOPEN_TEST_13: mkdir() test.", FSFAT_FOPEN_TEST_13),
tvendov 2:7c75ab32d7c9 1630 Case("FSFAT_FOPEN_TEST_14: stat() test.", FSFAT_FOPEN_TEST_14),
tvendov 2:7c75ab32d7c9 1631 Case("FSFAT_FOPEN_TEST_15: format() test.", FSFAT_FOPEN_TEST_15),
tvendov 2:7c75ab32d7c9 1632 Case("FSFAT_FOPEN_TEST_16: write/check n x 25kB data files.", FSFAT_FOPEN_TEST_16),
tvendov 2:7c75ab32d7c9 1633 };
tvendov 2:7c75ab32d7c9 1634
tvendov 2:7c75ab32d7c9 1635
tvendov 2:7c75ab32d7c9 1636 /* Declare your test specification with a custom setup handler */
tvendov 2:7c75ab32d7c9 1637 Specification specification(greentea_setup, cases);
tvendov 2:7c75ab32d7c9 1638
tvendov 2:7c75ab32d7c9 1639 int main()
tvendov 2:7c75ab32d7c9 1640 {
tvendov 2:7c75ab32d7c9 1641 return !Harness::run(specification);
tvendov 2:7c75ab32d7c9 1642 }
tvendov 2:7c75ab32d7c9 1643 /// @endcond