nuttx-apps/testing/testsuites/kernel/fs/cases/fs_stat_test.c
guohao15 4ac3732734 testing/fdcheck: fix fd close in other thread
Fix file descriptor leak issue where fd was stored in test_state for deferred close, but could fail when CONFIG_FDCHECK is enabled because fd cannot be closed in a different thread.

Signed-off-by: guohao15 <guohao15@xiaomi.com>
2026-01-28 10:37:52 -03:00

139 lines
3.9 KiB
C

/****************************************************************************
* apps/testing/testsuites/kernel/fs/cases/fs_stat_test.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <utime.h>
#include <time.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "fstest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_FILE "stat_test_file"
#define BUF_SIZE 1024
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_printtime
****************************************************************************/
__attribute__((unused)) static void test_nuttx_fs_printtime(struct tm *TM)
{
syslog(LOG_INFO, " tm_year: %d\n", TM->tm_year + 1900);
syslog(LOG_INFO, " tm_mon: %d\n", TM->tm_mon);
syslog(LOG_INFO, " tm_mday: %d\n", TM->tm_mday);
syslog(LOG_INFO, " tm_hour: %d\n", TM->tm_hour);
syslog(LOG_INFO, " tm_min: %d\n", TM->tm_min);
syslog(LOG_INFO, " tm_sec: %d\n", TM->tm_sec);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_fs_stat01
****************************************************************************/
void test_nuttx_fs_stat01(FAR void **state)
{
int fd;
int ret;
int ret2;
struct stat file_s;
char buf[BUF_SIZE] =
{
0
};
struct tm *tm_1 = NULL;
struct tm *tm_2 = NULL;
time_t t_1;
time_t t_2;
time_t t_diff;
/* set memory */
memset(buf, 65, BUF_SIZE);
/* open file */
fd = open(TEST_FILE, O_RDWR | O_CREAT, 0777);
assert_true(fd > 0);
/* do write */
ret2 = write(fd, buf, BUF_SIZE);
assert_int_in_range(ret2, 1, 1024);
close(fd);
/* get system time */
time(&t_1);
tm_1 = gmtime(&t_1);
assert_non_null(tm_1);
/* get file info */
ret = stat(TEST_FILE, &file_s);
assert_int_equal(ret, 0);
/* output stat struct information */
t_2 = file_s.st_mtime;
tm_2 = gmtime(&t_2);
assert_non_null(tm_2);
/* compare time */
t_diff = t_2 - t_1;
/* tolerance for 30s for worst case */
assert_int_in_range(t_diff, -30, 30);
/* compare size */
assert_int_equal(file_s.st_size, BUF_SIZE);
}