This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new ee3d67104 testing/libc/mktemp: Add a test of mktemp() with a name too 
long.
ee3d67104 is described below

commit ee3d67104d117bb3d932b29b6068f513d04109bf
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 8 10:55:43 2026 +0100

    testing/libc/mktemp: Add a test of mktemp() with a name too long.
    
    mktemp(), mkstemp() and mkdtemp() must return an error at once when
    stat() fails with an error other than ENOENT, because that error is
    the same for each name that they try.  The test gives them a name
    longer than NAME_MAX and expects ENAMETOOLONG.  The calls run in a
    thread: if they do not return in 10 seconds, the test fails and ends.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/libc/mktemp/CMakeLists.txt |  35 +++++++++
 testing/libc/mktemp/Kconfig        |  31 ++++++++
 testing/libc/mktemp/Make.defs      |  25 ++++++
 testing/libc/mktemp/Makefile       |  36 +++++++++
 testing/libc/mktemp/mktemp_main.c  | 151 +++++++++++++++++++++++++++++++++++++
 5 files changed, 278 insertions(+)

diff --git a/testing/libc/mktemp/CMakeLists.txt 
b/testing/libc/mktemp/CMakeLists.txt
new file mode 100644
index 000000000..acf30046e
--- /dev/null
+++ b/testing/libc/mktemp/CMakeLists.txt
@@ -0,0 +1,35 @@
+# 
##############################################################################
+# apps/testing/libc/mktemp/CMakeLists.txt
+#
+# 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.
+#
+# 
##############################################################################
+
+if(CONFIG_TESTING_MKTEMP)
+  nuttx_add_application(
+    NAME
+    ${CONFIG_TESTING_MKTEMP_PROGNAME}
+    PRIORITY
+    ${CONFIG_TESTING_MKTEMP_PRIORITY}
+    STACKSIZE
+    ${CONFIG_TESTING_MKTEMP_STACKSIZE}
+    MODULE
+    ${CONFIG_TESTING_MKTEMP}
+    SRCS
+    mktemp_main.c)
+endif()
diff --git a/testing/libc/mktemp/Kconfig b/testing/libc/mktemp/Kconfig
new file mode 100644
index 000000000..29812b1aa
--- /dev/null
+++ b/testing/libc/mktemp/Kconfig
@@ -0,0 +1,31 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config TESTING_MKTEMP
+       tristate "mktemp() test"
+       default n
+       depends on !DISABLE_PTHREAD
+       ---help---
+               Check that mktemp(), mkstemp() and mkdtemp() return an error at
+               once when stat() fails with an error other than ENOENT.
+
+if TESTING_MKTEMP
+
+config TESTING_MKTEMP_PROGNAME
+       string "Program name"
+       default "mktemp_test"
+       ---help---
+               This is the name of the program that will be used when the NSH 
ELF
+               program is installed.
+
+config TESTING_MKTEMP_PRIORITY
+       int "mktemp_test task priority"
+       default 100
+
+config TESTING_MKTEMP_STACKSIZE
+       int "mktemp_test stack size"
+       default DEFAULT_TASK_STACKSIZE
+
+endif
diff --git a/testing/libc/mktemp/Make.defs b/testing/libc/mktemp/Make.defs
new file mode 100644
index 000000000..02b35adea
--- /dev/null
+++ b/testing/libc/mktemp/Make.defs
@@ -0,0 +1,25 @@
+############################################################################
+# apps/testing/libc/mktemp/Make.defs
+#
+# 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.
+#
+############################################################################
+
+ifneq ($(CONFIG_TESTING_MKTEMP),)
+CONFIGURED_APPS += $(APPDIR)/testing/libc/mktemp
+endif
diff --git a/testing/libc/mktemp/Makefile b/testing/libc/mktemp/Makefile
new file mode 100644
index 000000000..6a6212033
--- /dev/null
+++ b/testing/libc/mktemp/Makefile
@@ -0,0 +1,36 @@
+############################################################################
+# apps/testing/libc/mktemp/Makefile
+#
+# 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.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# mktemp() test built-in application info
+
+PROGNAME  = $(CONFIG_TESTING_MKTEMP_PROGNAME)
+PRIORITY  = $(CONFIG_TESTING_MKTEMP_PRIORITY)
+STACKSIZE = $(CONFIG_TESTING_MKTEMP_STACKSIZE)
+MODULE    = $(CONFIG_TESTING_MKTEMP)
+
+# mktemp test files
+
+MAINSRC = mktemp_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/testing/libc/mktemp/mktemp_main.c 
b/testing/libc/mktemp/mktemp_main.c
new file mode 100644
index 000000000..87caa9c44
--- /dev/null
+++ b/testing/libc/mktemp/mktemp_main.c
@@ -0,0 +1,151 @@
+/****************************************************************************
+ * apps/testing/libc/mktemp/mktemp_main.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 <errno.h>
+#include <limits.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The calls must return at once.  Wait this many seconds for them. */
+
+#define MKTEMP_TEST_TIMEOUT 10
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Static: the thread can outlive main() when the calls hang */
+
+static sem_t g_done;
+static int g_nerrors;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Make a template with a name longer than NAME_MAX: stat() fails with
+ * ENAMETOOLONG for each name that mktemp() makes from it.
+ */
+
+static void make_template(FAR char *buf)
+{
+  buf[0] = '/';
+  memset(&buf[1], 'a', NAME_MAX + 1);
+  strcpy(&buf[NAME_MAX + 2], "XXXXXX");
+}
+
+static void check(FAR const char *name, bool failed, int errcode)
+{
+  if (!failed || errcode != ENAMETOOLONG)
+    {
+      printf("ERROR: %s: failed %d errno %d (expected ENAMETOOLONG)\n",
+             name, failed, errcode);
+      g_nerrors++;
+    }
+  else
+    {
+      printf("%s: OK\n", name);
+    }
+}
+
+static FAR void *mktemp_thread(FAR void *arg)
+{
+  char path[NAME_MAX + 16];
+  FAR char *ptr;
+  int fd;
+
+  make_template(path);
+  errno = 0;
+  ptr = mktemp(path);
+  check("mktemp", ptr == NULL, errno);
+
+  make_template(path);
+  errno = 0;
+  fd = mkstemp(path);
+  check("mkstemp", fd < 0, errno);
+  if (fd >= 0)
+    {
+      close(fd);
+    }
+
+  make_template(path);
+  errno = 0;
+  ptr = mkdtemp(path);
+  check("mkdtemp", ptr == NULL, errno);
+
+  sem_post(&g_done);
+  return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  struct timespec abstime;
+  pthread_t thread;
+  int ret;
+
+  printf("mktemp test: a template that stat() refuses\n");
+
+  sem_init(&g_done, 0, 0);
+  ret = pthread_create(&thread, NULL, mktemp_thread, NULL);
+  if (ret != 0)
+    {
+      printf("ERROR: pthread_create() failed: %d\n", ret);
+      return EXIT_FAILURE;
+    }
+
+  /* If the calls do not return, the exit of the task ends the thread */
+
+  clock_gettime(CLOCK_REALTIME, &abstime);
+  abstime.tv_sec += MKTEMP_TEST_TIMEOUT;
+  while ((ret = sem_timedwait(&g_done, &abstime)) < 0 && errno == EINTR);
+
+  if (ret < 0)
+    {
+      printf("ERROR: the calls did not return in %d seconds\n",
+             MKTEMP_TEST_TIMEOUT);
+      return EXIT_FAILURE;
+    }
+
+  pthread_join(thread, NULL);
+  sem_destroy(&g_done);
+
+  printf("mktemp test: %d errors\n", g_nerrors);
+  return g_nerrors > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}

Reply via email to