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