Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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