GroupU - 05012018 1543

Fork of 352 by Elec351 MMB

Committer:
mslade
Date:
Tue Jan 09 14:53:07 2018 +0000
Revision:
1:84581acd1333
Final Version - Group U

Who changed what in which revision?

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