@@ -71,6 +71,7 @@ static char fsfat_fopen_utest_msg_g[FSFAT_UTEST_MSG_BUF_SIZE];
71
71
#define FSFAT_FOPEN_TEST_MOUNT_PT_NAME " sd"
72
72
#define FSFAT_FOPEN_TEST_MOUNT_PT_PATH " /" FSFAT_FOPEN_TEST_MOUNT_PT_NAME
73
73
#define FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1 64
74
+ #define FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH 20
74
75
static const char *sd_badfile_path = " /sd/badfile.txt" ;
75
76
static const char *sd_testfile_path = " /sd/test.txt" ;
76
77
@@ -297,7 +298,7 @@ static int32_t fsfat_filepath_make_dirs(char* filepath, bool do_asserts)
297
298
char *fpathbuf = NULL ;
298
299
char *buf = NULL ;
299
300
int pos = 0 ;
300
- char *parts[10 ];
301
+ char *parts[FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH ];
301
302
302
303
FSFAT_DBGLOG (" %s:entered\n " , __func__);
303
304
/* find the dirs to create*/
@@ -310,7 +311,7 @@ static int32_t fsfat_filepath_make_dirs(char* filepath, bool do_asserts)
310
311
}
311
312
memset (fpathbuf, 0 , len+1 );
312
313
memcpy (fpathbuf, filepath, len);
313
- num_parts = fsfat_filepath_split (fpathbuf, parts, 10 );
314
+ num_parts = fsfat_filepath_split (fpathbuf, parts, FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH );
314
315
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);
315
316
TEST_ASSERT_MESSAGE (num_parts > 0 , fsfat_fopen_utest_msg_g);
316
317
@@ -1332,6 +1333,175 @@ control_t fsfat_fopen_test_15(const size_t call_count)
1332
1333
}
1333
1334
1334
1335
1336
+ /* @brief test utility function to create a file of a given size.
1337
+ *
1338
+ * A reference data table is used of so that the data file can be later be
1339
+ * checked with fsfat_test_check_data_file().
1340
+ *
1341
+ * @param filename name of the file including path
1342
+ * @param data data to store in file
1343
+ * @param len number of bytes of data present in the data buffer.
1344
+ */
1345
+ int32_t fsfat_test_create_data_file (const char * filename, size_t len)
1346
+ {
1347
+ int32_t ret = -1 ;
1348
+ FILE *fp = NULL ;
1349
+ size_t write_len = 0 ;
1350
+ size_t written_len = 0 ;
1351
+ int32_t exp = 0 ;
1352
+ const int32_t exp_max = 8 ; /* so as not to exceed FSFAT_TEST_BYTE_DATA_TABLE_SIZE/2 */
1353
+
1354
+ FSFAT_FENTRYLOG (" %s:entered (filename=%s, len=%d).\n " , __func__, filename, (int ) len);
1355
+ TEST_ASSERT (len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE == 0 );
1356
+ fp = fopen (filename, " a" );
1357
+ if (fp == NULL ){
1358
+ return ret;
1359
+ }
1360
+
1361
+ while (written_len < len) {
1362
+ /* write fsfat_test_byte_data_table or part thereof, in 9 writes of sizes
1363
+ * 1, 2, 4, 8, 16, 32, 64, 128, 1, totalling 256 bytes len permitting. */
1364
+ for (exp = 0 ; (exp <= exp_max) && (written_len < len); exp++){
1365
+ write_len = 0x1 << (exp % exp_max);
1366
+ write_len = len - written_len > write_len ? write_len : len - written_len;
1367
+ ret = fwrite ((const void *) &fsfat_test_byte_data_table[written_len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE], write_len, 1 , fp);
1368
+ written_len += write_len;
1369
+ if (ret != 1 ){
1370
+ FSFAT_DBGLOG (" %s:Error: fwrite() failed (ret=%d)\n " , __func__, (int ) ret);
1371
+ ret = -1 ;
1372
+ goto out0;
1373
+ }
1374
+ }
1375
+ }
1376
+ if (written_len == len) {
1377
+ ret = 0 ;
1378
+ } else {
1379
+ ret = -1 ;
1380
+ }
1381
+ out0:
1382
+ fclose (fp);
1383
+ return ret;
1384
+ }
1385
+
1386
+
1387
+ /* @brief test utility function to check the data in the specified file is correct.
1388
+ *
1389
+ * The data read from the file is check that it agrees with the data written by
1390
+ * fsfat_test_create_data_file().
1391
+ *
1392
+ * @param filename name of the file including path
1393
+ * @param data data to store in file
1394
+ * @param len number of bytes of data present in the data buffer.
1395
+ */
1396
+ int32_t fsfat_test_check_data_file (const char * filename, size_t len)
1397
+ {
1398
+ int32_t ret = -1 ;
1399
+ FILE *fp = NULL ;
1400
+ size_t read_len = 0 ;
1401
+ uint8_t buf[FSFAT_TEST_BYTE_DATA_TABLE_SIZE];
1402
+
1403
+ FSFAT_FENTRYLOG (" %s:entered (filename=%s, len=%d).\n " , __func__, filename, (int ) len);
1404
+ TEST_ASSERT (len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE == 0 );
1405
+ fp = fopen (filename, " r" );
1406
+ if (fp == NULL ){
1407
+ return ret;
1408
+ }
1409
+
1410
+ while (read_len < len) {
1411
+ ret = fread ((void *) buf, FSFAT_TEST_BYTE_DATA_TABLE_SIZE, 1 , fp);
1412
+ read_len += FSFAT_TEST_BYTE_DATA_TABLE_SIZE;
1413
+ if (ret == 0 ){
1414
+ /* end of read*/
1415
+ FSFAT_DBGLOG (" %s:unable to read data\n " , __func__);
1416
+ break ;
1417
+ }
1418
+ if (memcmp (buf, fsfat_test_byte_data_table, FSFAT_TEST_BYTE_DATA_TABLE_SIZE) != 0 ) {
1419
+ 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__,
1420
+ 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 ]);
1421
+ ret = -1 ;
1422
+ goto out0;
1423
+ }
1424
+ }
1425
+ if (read_len == len) {
1426
+ ret = 0 ;
1427
+ }
1428
+ out0:
1429
+ fclose (fp);
1430
+ return ret;
1431
+ }
1432
+
1433
+ /* file data for test_16 */
1434
+ static fsfat_kv_data_t fsfat_fopen_test_16_kv_data[] = {
1435
+ { " /sd/tst16_0/testfil0.txt" , " dummy_data" },
1436
+ { " /sd/tst16_1/subdir0/testfil0.txt" , " dummy_data" },
1437
+ { " /sd/tst16_2/subdir0/subdir1/testfil0.txt" , " dummy_data" },
1438
+ { " /sd/tst16_3/subdir0/subdir1/subdir2/subdir3/testfil0.txt" , " dummy_data" },
1439
+ { " /sd/tst16_4/subdir0/subdir1/subdir2/subdir3/subdir4/testfil0.txt" , " dummy_data" },
1440
+ { " /sd/tst16_5/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/testfil0.txt" , " dummy_data" },
1441
+ { " /sd/tst16_6/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/testfil0.txt" , " dummy_data" },
1442
+ { " /sd/tst16_7/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/testfil0.txt" , " dummy_data" },
1443
+ { " /sd/tst16_8/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/subdir8/testfil0.txt" , " dummy_data" },
1444
+ { " /sd/tst16_9/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/subdir8/subdir9/testfil0.txt" , " dummy_data" },
1445
+ { NULL , NULL },
1446
+ };
1447
+
1448
+
1449
+ /* * @brief stress test to write data to fs
1450
+ *
1451
+ * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
1452
+ */
1453
+ control_t fsfat_fopen_test_16 (const size_t call_count)
1454
+ {
1455
+ int32_t ret = 0 ;
1456
+ fsfat_kv_data_t *node = fsfat_fopen_test_16_kv_data;
1457
+ const int32_t num_blocks = 100 ; /* each file ~25kB */
1458
+
1459
+ FSFAT_DBGLOG (" %s:entered\n " , __func__);
1460
+ (void ) call_count;
1461
+
1462
+ /* remove file and directory from a previous failed test run, if present */
1463
+ while (node->filename != NULL ) {
1464
+ fsfat_filepath_remove_all ((char *) node->filename );
1465
+ node++;
1466
+ }
1467
+
1468
+ /* create dirs */
1469
+ node = fsfat_fopen_test_16_kv_data;
1470
+ while (node->filename != NULL ) {
1471
+ ret = fsfat_filepath_make_dirs ((char *) node->filename , true );
1472
+ 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);
1473
+ TEST_ASSERT_MESSAGE (ret == 0 , fsfat_fopen_utest_msg_g);
1474
+ node++;
1475
+ }
1476
+
1477
+ /* create the data files */
1478
+ node = fsfat_fopen_test_16_kv_data;
1479
+ while (node->filename != NULL ) {
1480
+ ret = fsfat_test_create_data_file (node->filename , num_blocks * FSFAT_TEST_BYTE_DATA_TABLE_SIZE);
1481
+ 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);
1482
+ TEST_ASSERT_MESSAGE (ret == 0 , fsfat_fopen_utest_msg_g);
1483
+ node++;
1484
+ }
1485
+
1486
+ /* read the data back and check its as expected */
1487
+ node = fsfat_fopen_test_16_kv_data;
1488
+ while (node->filename != NULL ) {
1489
+ ret = fsfat_test_check_data_file (node->filename , num_blocks * FSFAT_TEST_BYTE_DATA_TABLE_SIZE);
1490
+ 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);
1491
+ TEST_ASSERT_MESSAGE (ret == 0 , fsfat_fopen_utest_msg_g);
1492
+ node++;
1493
+ }
1494
+
1495
+ /* clean up */
1496
+ node = fsfat_fopen_test_16_kv_data;
1497
+ while (node->filename != NULL ) {
1498
+ fsfat_filepath_remove_all ((char *) node->filename );
1499
+ node++;
1500
+ }
1501
+ return CaseNext;
1502
+ }
1503
+
1504
+
1335
1505
#else
1336
1506
1337
1507
@@ -1406,7 +1576,7 @@ Case cases[] = {
1406
1576
Case (" FSFAT_FOPEN_TEST_13: mkdir() test." , FSFAT_FOPEN_TEST_13),
1407
1577
Case (" FSFAT_FOPEN_TEST_14: stat() test." , FSFAT_FOPEN_TEST_14),
1408
1578
Case (" FSFAT_FOPEN_TEST_15: format() test." , FSFAT_FOPEN_TEST_15),
1409
-
1579
+ Case ( " FSFAT_FOPEN_TEST_16: write/check n x 25kB data files. " , FSFAT_FOPEN_TEST_16),
1410
1580
};
1411
1581
1412
1582
0 commit comments