diff --git a/testing/ostest/CMakeLists.txt b/testing/ostest/CMakeLists.txt index 921d1920221..8678bea896a 100644 --- a/testing/ostest/CMakeLists.txt +++ b/testing/ostest/CMakeLists.txt @@ -52,6 +52,10 @@ if(CONFIG_TESTING_OSTEST) endif() endif() + if(NOT CONFIG_DISABLE_PSEUDOFS_OPERATIONS) + list(APPEND SRCS cwd.c) + endif() + if(NOT CONFIG_STDIO_DISABLE_BUFFERING) list(APPEND SRCS setvbuf.c) endif() diff --git a/testing/ostest/Makefile b/testing/ostest/Makefile index 6b07da3a5a5..4b0769b0f84 100644 --- a/testing/ostest/Makefile +++ b/testing/ostest/Makefile @@ -63,6 +63,10 @@ CSRCS += fpu.c endif endif +ifneq ($(CONFIG_DISABLE_PSEUDOFS_OPERATIONS),y) +CSRCS += cwd.c +endif + ifneq ($(CONFIG_STDIO_DISABLE_BUFFERING),y) CSRCS += setvbuf.c endif diff --git a/testing/ostest/cwd.c b/testing/ostest/cwd.c new file mode 100644 index 00000000000..06638d2108b --- /dev/null +++ b/testing/ostest/cwd.c @@ -0,0 +1,173 @@ +/**************************************************************************** + * apps/testing/ostest/cwd.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 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ostest.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define CWD_DIR "/ostest_cwd" +#define CWD_NEW "/ostest_cwd2" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int cwd_fail(FAR const char *msg) +{ + printf("cwd_test: ERROR %s, errno=%d\n", msg, errno); + return 1; +} + +static bool cwd_is(FAR const char *expect) +{ + char buf[PATH_MAX]; + + return getcwd(buf, sizeof(buf)) != NULL && strcmp(buf, expect) == 0; +} + +static bool cwd_removed(void) +{ + char buf[PATH_MAX]; + + errno = 0; + return getcwd(buf, sizeof(buf)) == NULL && errno == ENOENT; +} + +#ifndef CONFIG_DISABLE_PTHREAD +static FAR void *cwd_thread(FAR void *arg) +{ + UNUSED(arg); + + chdir("/"); + return NULL; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int cwd_test(void) +{ + char saved[PATH_MAX]; +#ifndef CONFIG_DISABLE_PTHREAD + pthread_t thread; +#endif + int errors = 0; + + if (getcwd(saved, sizeof(saved)) == NULL) + { + strlcpy(saved, "/", sizeof(saved)); + } + + rmdir(CWD_DIR); + rmdir(CWD_NEW); + if (mkdir(CWD_DIR, 0777) < 0) + { + return cwd_fail("mkdir() failed"); + } + + if (chdir(CWD_DIR) < 0 || !cwd_is(CWD_DIR)) + { + errors += cwd_fail("getcwd() does not give the chdir() directory"); + } + +#ifndef CONFIG_DISABLE_ENVIRON + /* PWD is information for the shell, not the current directory */ + + setenv("PWD", "/", 1); + if (!cwd_is(CWD_DIR)) + { + errors += cwd_fail("setenv(\"PWD\") changed the current directory"); + } + + unsetenv("PWD"); + if (!cwd_is(CWD_DIR)) + { + errors += cwd_fail("unsetenv(\"PWD\") changed the current directory"); + } +#endif + +#ifndef CONFIG_DISABLE_PTHREAD + /* The threads of a task group share the current directory */ + + if (pthread_create(&thread, NULL, cwd_thread, NULL) != 0 || + pthread_join(thread, NULL) != 0 || !cwd_is("/")) + { + errors += cwd_fail("chdir() of a thread did not change the group"); + } + + chdir(CWD_DIR); +#endif + + /* rename() of the current directory: it keeps the directory */ + + if (rename(CWD_DIR, CWD_NEW) < 0 || !cwd_is(CWD_NEW)) + { + errors += cwd_fail("getcwd() does not give the new name"); + } + + /* rmdir() of the current directory: getcwd() fails with ENOENT, also + * after a new directory of the same name appears. + */ + + if (rmdir(CWD_NEW) < 0 || !cwd_removed()) + { + errors += cwd_fail("getcwd() does not fail after rmdir()"); + } + else if (mkdir(CWD_NEW, 0777) < 0 || !cwd_removed()) + { + errors += cwd_fail("a new directory became the current directory"); + } + + chdir(saved); +#ifndef CONFIG_DISABLE_ENVIRON + setenv("PWD", saved, 1); +#endif + rmdir(CWD_DIR); + rmdir(CWD_NEW); + + if (errors == 0) + { + printf("cwd_test: PASS\n"); + } + + return errors; +} diff --git a/testing/ostest/ostest.h b/testing/ostest/ostest.h index 12e62209433..6864a30d833 100644 --- a/testing/ostest/ostest.h +++ b/testing/ostest/ostest.h @@ -152,6 +152,12 @@ int waitpid_test(void); int chroot_test(void); #endif +/* cwd.c ********************************************************************/ + +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS +int cwd_test(void); +#endif + /* caps.c *******************************************************************/ #if defined(CONFIG_SCHED_CAPABILITIES) && defined(CONFIG_SCHED_WAITPID) && \ diff --git a/testing/ostest/ostest_main.c b/testing/ostest/ostest_main.c index 5aa67d8eef9..a1efcce3eb0 100644 --- a/testing/ostest/ostest_main.c +++ b/testing/ostest/ostest_main.c @@ -426,6 +426,19 @@ static int user_main(int argc, char *argv[]) check_test_memory_usage(); #endif +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + /* Check the current directory of the task group */ + + printf("\nuser_main: current directory test\n"); + if (cwd_test() != 0) + { + printf("user_main: ERROR current directory test failed\n"); + ASSERT(false); + } + + check_test_memory_usage(); +#endif + #if defined(CONFIG_SCHED_CAPABILITIES) && defined(CONFIG_SCHED_WAITPID) && \ !defined(CONFIG_BUILD_KERNEL) /* Check process capabilities */