Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | /*
* Copyright (c) 2019 Peter Bigot Consulting, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Basic littlefs operations:
* * create
* * write
* * stat
* * read
* * seek
* * tell
* * truncate
* * unlink
* * sync
*/
#include <string.h>
#include <ztest.h>
#include "testfs_tests.h"
#include "testfs_lfs.h"
#include <lfs.h>
#include <fs/littlefs.h>
#define HELLO "hello"
#define GOODBYE "goodbye"
static int mount(struct fs_mount_t *mp)
{
TC_PRINT("mounting %s\n", mp->mnt_point);
zassert_equal(fs_mount(mp), 0,
"mount failed");
return TC_PASS;
}
static int clear_partition(struct fs_mount_t *mp)
{
TC_PRINT("clearing partition %s\n", mp->mnt_point);
zassert_equal(testfs_lfs_wipe_partition(mp),
TC_PASS,
"failed to wipe partition");
return TC_PASS;
}
static int clean_statvfs(const struct fs_mount_t *mp)
{
struct fs_statvfs stat;
TC_PRINT("checking clean statvfs of %s\n", mp->mnt_point);
zassert_equal(fs_statvfs(mp->mnt_point, &stat), 0,
"statvfs failed");
TC_PRINT("%s: bsize %lu ; frsize %lu ; blocks %lu ; bfree %lu\n",
mp->mnt_point,
stat.f_bsize, stat.f_frsize, stat.f_blocks, stat.f_bfree);
zassert_equal(stat.f_bsize, 16,
"bsize fail");
zassert_equal(stat.f_frsize, 4096,
"frsize fail");
zassert_equal(stat.f_blocks, 16,
"blocks fail");
zassert_equal(stat.f_bfree, stat.f_blocks - 2U,
"bfree fail");
return TC_PASS;
}
static int create_write_hello(const struct fs_mount_t *mp)
{
struct testfs_path path;
struct fs_file_t file;
TC_PRINT("creating and writing file\n");
zassert_equal(fs_open(&file,
testfs_path_init(&path, mp,
HELLO,
TESTFS_PATH_END)),
0,
"open hello failed");
struct fs_dirent stat;
zassert_equal(fs_stat(path.path, &stat),
0,
"stat new hello failed");
zassert_equal(stat.type, FS_DIR_ENTRY_FILE,
"stat new hello not file");
zassert_equal(strcmp(stat.name, HELLO), 0,
"stat new hello not hello");
zassert_equal(stat.size, 0,
"stat new hello not empty");
zassert_equal(testfs_write_incrementing(&file, 0, TESTFS_BUFFER_SIZE),
TESTFS_BUFFER_SIZE,
"write constant failed");
zassert_equal(fs_stat(path.path, &stat),
0,
"stat written hello failed");
zassert_equal(stat.type, FS_DIR_ENTRY_FILE,
"stat written hello not file");
zassert_equal(strcmp(stat.name, HELLO), 0,
"stat written hello not hello");
/* Anomalous behavior requiring upstream response */
if (true) {
/* VARIATION POINT: littlefs does not update the file size of
* an open file. See upstream issue #250.
*/
zassert_equal(stat.size, 0,
"stat written hello bad size");
}
zassert_equal(fs_close(&file), 0,
"close hello failed");
zassert_equal(fs_stat(path.path, &stat),
0,
"stat closed hello failed");
zassert_equal(stat.type, FS_DIR_ENTRY_FILE,
"stat closed hello not file");
zassert_equal(strcmp(stat.name, HELLO), 0,
"stat closed hello not hello");
zassert_equal(stat.size, TESTFS_BUFFER_SIZE,
"stat closed hello badsize");
return TC_PASS;
}
static int verify_hello(const struct fs_mount_t *mp)
{
struct testfs_path path;
struct fs_file_t file;
TC_PRINT("opening and verifying file\n");
zassert_equal(fs_open(&file,
testfs_path_init(&path, mp,
HELLO,
TESTFS_PATH_END)),
0,
"verify hello open failed");
zassert_equal(fs_tell(&file), 0U,
"verify hello open tell failed");
zassert_equal(testfs_verify_incrementing(&file, 0, TESTFS_BUFFER_SIZE),
TESTFS_BUFFER_SIZE,
"verify hello at start failed");
zassert_equal(fs_tell(&file), TESTFS_BUFFER_SIZE,
"verify hello read tell failed");
zassert_equal(fs_close(&file), 0,
"verify close hello failed");
return TC_PASS;
}
static int seek_within_hello(const struct fs_mount_t *mp)
{
struct testfs_path path;
struct fs_file_t file;
TC_PRINT("seek and tell in file\n");
zassert_equal(fs_open(&file,
testfs_path_init(&path, mp,
HELLO,
TESTFS_PATH_END)),
0,
"verify hello open failed");
zassert_equal(fs_tell(&file), 0U,
"verify hello open tell failed");
struct fs_dirent stat;
zassert_equal(fs_stat(path.path, &stat),
0,
"stat old hello failed");
zassert_equal(stat.size, TESTFS_BUFFER_SIZE,
"stat old hello bad size");
off_t pos = stat.size / 4;
zassert_equal(fs_seek(&file, pos, FS_SEEK_SET),
0,
"verify hello seek near mid failed");
zassert_equal(fs_tell(&file), pos,
"verify hello tell near mid failed");
zassert_equal(testfs_verify_incrementing(&file, pos, TESTFS_BUFFER_SIZE),
TESTFS_BUFFER_SIZE - pos,
"verify hello at middle failed");
zassert_equal(fs_tell(&file), stat.size,
"verify hello read middle tell failed");
zassert_equal(fs_seek(&file, -stat.size, FS_SEEK_CUR),
0,
"verify hello seek back from cur failed");
zassert_equal(fs_tell(&file), 0U,
"verify hello tell back from cur failed");
zassert_equal(fs_seek(&file, -pos, FS_SEEK_END),
0,
"verify hello seek from end failed");
zassert_equal(fs_tell(&file), stat.size - pos,
"verify hello tell from end failed");
zassert_equal(testfs_verify_incrementing(&file, stat.size - pos,
TESTFS_BUFFER_SIZE),
pos,
"verify hello at post middle failed");
zassert_equal(fs_close(&file), 0,
"verify close hello failed");
return TC_PASS;
}
static int truncate_hello(const struct fs_mount_t *mp)
{
struct testfs_path path;
struct fs_file_t file;
TC_PRINT("truncate in file\n");
zassert_equal(fs_open(&file,
testfs_path_init(&path, mp,
HELLO,
TESTFS_PATH_END)),
0,
"verify hello open failed");
struct fs_dirent stat;
zassert_equal(fs_stat(path.path, &stat),
0,
"stat old hello failed");
zassert_equal(stat.size, TESTFS_BUFFER_SIZE,
"stat old hello bad size");
off_t pos = 3 * stat.size / 4;
zassert_equal(fs_tell(&file), 0U,
"truncate initial tell failed");
zassert_equal(fs_truncate(&file, pos),
0,
"truncate 3/4 failed");
zassert_equal(fs_tell(&file), 0U,
"truncate post tell failed");
zassert_equal(fs_stat(path.path, &stat),
0,
"stat open 3/4 failed");
/* Anomalous behavior requiring upstream response */
if (true) {
/* VARIATION POINT: littlefs does not update the file size of
* an open file. See upstream issue #250.
*/
zassert_equal(stat.size, TESTFS_BUFFER_SIZE,
"stat open 3/4 bad size");
}
zassert_equal(testfs_verify_incrementing(&file, 0, 64),
48,
"post truncate content unexpected");
zassert_equal(fs_close(&file), 0,
"post truncate close failed");
/* After close size is correct. */
zassert_equal(fs_stat(path.path, &stat),
0,
"stat closed truncated failed");
zassert_equal(stat.size, pos,
"stat closed truncated bad size");
return TC_PASS;
}
static int unlink_hello(const struct fs_mount_t *mp)
{
struct testfs_path path;
TC_PRINT("unlink hello\n");
testfs_path_init(&path, mp,
HELLO,
TESTFS_PATH_END);
struct fs_dirent stat;
zassert_equal(fs_stat(path.path, &stat),
0,
"stat existing hello failed");
zassert_equal(fs_unlink(path.path),
0,
"unlink hello failed");
zassert_equal(fs_stat(path.path, &stat),
-ENOENT,
"stat existing hello failed");
return TC_PASS;
}
static int sync_goodbye(const struct fs_mount_t *mp)
{
struct testfs_path path;
struct fs_file_t file;
TC_PRINT("sync goodbye\n");
zassert_equal(fs_open(&file,
testfs_path_init(&path, mp,
GOODBYE,
TESTFS_PATH_END)),
0,
"sync goodbye failed");
struct fs_dirent stat;
zassert_equal(fs_stat(path.path, &stat),
0,
"stat existing hello failed");
zassert_equal(stat.size, 0,
"stat new goodbye not empty");
zassert_equal(testfs_write_incrementing(&file, 0, TESTFS_BUFFER_SIZE),
TESTFS_BUFFER_SIZE,
"write goodbye failed");
zassert_equal(fs_tell(&file), TESTFS_BUFFER_SIZE,
"tell goodbye failed");
if (true) {
/* Upstream issue #250 */
zassert_equal(stat.size, 0,
"stat new goodbye not empty");
}
zassert_equal(fs_sync(&file), 0,
"sync goodbye failed");
zassert_equal(fs_tell(&file), TESTFS_BUFFER_SIZE,
"tell synced moved");
zassert_equal(fs_stat(path.path, &stat),
0,
"stat existing hello failed");
printk("sync size %u\n", (u32_t)stat.size);
zassert_equal(stat.size, TESTFS_BUFFER_SIZE,
"stat synced goodbye not correct");
zassert_equal(fs_close(&file), 0,
"post sync close failed");
/* After close size is correct. */
zassert_equal(fs_stat(path.path, &stat),
0,
"stat sync failed");
zassert_equal(stat.size, TESTFS_BUFFER_SIZE,
"stat sync bad size");
return TC_PASS;
}
static int verify_goodbye(const struct fs_mount_t *mp)
{
struct testfs_path path;
struct fs_file_t file;
TC_PRINT("verify goodbye\n");
zassert_equal(fs_open(&file,
testfs_path_init(&path, mp,
GOODBYE,
TESTFS_PATH_END)),
0,
"verify goodbye failed");
zassert_equal(testfs_verify_incrementing(&file, 0, TESTFS_BUFFER_SIZE),
TESTFS_BUFFER_SIZE,
"write goodbye failed");
zassert_equal(fs_close(&file), 0,
"post sync close failed");
return TC_PASS;
}
static int check_medium(void)
{
struct fs_mount_t *mp = &testfs_medium_mnt;
struct fs_statvfs stat;
zassert_equal(clear_partition(mp), TC_PASS,
"clear partition failed");
zassert_equal(fs_mount(mp), 0,
"medium mount failed");
zassert_equal(fs_statvfs(mp->mnt_point, &stat), 0,
"statvfs failed");
TC_PRINT("%s: bsize %lu ; frsize %lu ; blocks %lu ; bfree %lu\n",
mp->mnt_point,
stat.f_bsize, stat.f_frsize, stat.f_blocks, stat.f_bfree);
zassert_equal(stat.f_bsize, MEDIUM_IO_SIZE,
"bsize fail");
zassert_equal(stat.f_frsize, 4096,
"frsize fail");
zassert_equal(stat.f_blocks, 240,
"blocks fail");
zassert_equal(stat.f_bfree, stat.f_blocks - 2U,
"bfree fail");
zassert_equal(fs_unmount(mp), 0,
"medium unmount failed");
return TC_PASS;
}
static int check_large(void)
{
struct fs_mount_t *mp = &testfs_large_mnt;
struct fs_statvfs stat;
zassert_equal(clear_partition(mp), TC_PASS,
"clear partition failed");
zassert_equal(fs_mount(mp), 0,
"large mount failed");
zassert_equal(fs_statvfs(mp->mnt_point, &stat), 0,
"statvfs failed");
TC_PRINT("%s: bsize %lu ; frsize %lu ; blocks %lu ; bfree %lu\n",
mp->mnt_point,
stat.f_bsize, stat.f_frsize, stat.f_blocks, stat.f_bfree);
zassert_equal(stat.f_bsize, LARGE_IO_SIZE,
"bsize fail");
zassert_equal(stat.f_frsize, 32768,
"frsize fail");
zassert_equal(stat.f_blocks, 96,
"blocks fail");
zassert_equal(stat.f_bfree, stat.f_blocks - 2U,
"bfree fail");
zassert_equal(fs_unmount(mp), 0,
"large unmount failed");
return TC_PASS;
}
void test_lfs_basic(void)
{
struct fs_mount_t *mp = &testfs_small_mnt;
zassert_equal(clear_partition(mp), TC_PASS,
"clear partition failed");
zassert_equal(mount(mp), TC_PASS,
"clean mount failed");
zassert_equal(clean_statvfs(mp), TC_PASS,
"clean statvfs failed");
zassert_equal(create_write_hello(mp), TC_PASS,
"write hello failed");
zassert_equal(verify_hello(mp), TC_PASS,
"verify hello failed");
zassert_equal(seek_within_hello(mp), TC_PASS,
"seek within hello failed");
zassert_equal(truncate_hello(mp), TC_PASS,
"truncate hello failed");
zassert_equal(unlink_hello(mp), TC_PASS,
"unlink hello failed");
zassert_equal(sync_goodbye(mp), TC_PASS,
"sync goodbye failed");
TC_PRINT("unmounting %s\n", mp->mnt_point);
zassert_equal(fs_unmount(mp), 0,
"unmount small failed");
k_sleep(K_MSEC(100)); /* flush log messages */
TC_PRINT("checking double unmount diagnoses\n");
zassert_equal(fs_unmount(mp), -EINVAL,
"unmount unmounted failed");
zassert_equal(mount(mp), TC_PASS,
"remount failed");
zassert_equal(verify_goodbye(mp), TC_PASS,
"verify goodbye failed");
zassert_equal(fs_unmount(mp), 0,
"unmount2 small failed");
zassert_equal(check_medium(), TC_PASS,
"check medium failed");
zassert_equal(check_large(), TC_PASS,
"check large failed");
}
|