Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions testing/ostest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions testing/ostest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
173 changes: 173 additions & 0 deletions testing/ostest/cwd.c
Original file line number Diff line number Diff line change
@@ -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 <nuttx/config.h>

#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#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;
}
6 changes: 6 additions & 0 deletions testing/ostest/ostest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) && \
Expand Down
13 changes: 13 additions & 0 deletions testing/ostest/ostest_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading