Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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