From c3b5d31b4526468a99602704eaea1f95cbd78e6d Mon Sep 17 00:00:00 2001
From: Eric Blake <ebb9@byu.net>
Date: Tue, 17 Jun 2008 08:43:38 -0600
Subject: [PATCH] Move c-stack test into testsuite.

* modules/c-stack-tests: New file.
* lib/c-stack.c [DEBUG]: Move test program...
* tests/test-c-stack.c: ...into this new file.  Skip rather than
fail test if sigaltstack is lacking.
* tests/test-c-stack.sh: New driver file.

Signed-off-by: Eric Blake <ebb9@byu.net>
---
 ChangeLog             |  9 ++++++
 lib/c-stack.c         | 40 -------------------------
 modules/c-stack-tests | 15 ++++++++++
 tests/test-c-stack.c  | 68 +++++++++++++++++++++++++++++++++++++++++++
 tests/test-c-stack.sh | 21 +++++++++++++
 5 files changed, 113 insertions(+), 40 deletions(-)
 create mode 100644 modules/c-stack-tests
 create mode 100644 tests/test-c-stack.c
 create mode 100755 tests/test-c-stack.sh

diff --git a/ChangeLog b/ChangeLog
index 0976ec37a2..80c51b891e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-06-17  Eric Blake  <ebb9@byu.net>
+
+	Move c-stack test into testsuite.
+	* modules/c-stack-tests: New file.
+	* lib/c-stack.c [DEBUG]: Move test program...
+	* tests/test-c-stack.c: ...into this new file.  Skip rather than
+	fail test if sigaltstack is lacking.
+	* tests/test-c-stack.sh: New driver file.
+
 2008-06-16  Eric Blake  <ebb9@byu.net>
 
 	Use raise module consistently.
diff --git a/lib/c-stack.c b/lib/c-stack.c
index a7ebafa8a4..7129036249 100644
--- a/lib/c-stack.c
+++ b/lib/c-stack.c
@@ -76,10 +76,6 @@ typedef struct sigaltstack stack_t;
 # define STDERR_FILENO 2
 #endif
 
-#if DEBUG
-# include <stdio.h>
-#endif
-
 #include "c-stack.h"
 #include "exitfail.h"
 
@@ -265,39 +261,3 @@ c_stack_action (void (*action) (int)  __attribute__ ((unused)))
 }
 
 #endif
-
-
-
-#if DEBUG
-
-int volatile exit_failure;
-
-static long
-recurse (char *p)
-{
-  char array[500];
-  array[0] = 1;
-  return *p + recurse (array);
-}
-
-char *program_name;
-
-int
-main (int argc __attribute__ ((unused)), char **argv)
-{
-  program_name = argv[0];
-  fprintf (stderr,
-	   "The last output line should contain \"stack overflow\".\n");
-  if (c_stack_action (0) == 0)
-    return recurse ("\1");
-  perror ("c_stack_action");
-  return 1;
-}
-
-#endif /* DEBUG */
-
-/*
-Local Variables:
-compile-command: "gcc -DDEBUG -g -O -Wall -W c-stack.c"
-End:
-*/
diff --git a/modules/c-stack-tests b/modules/c-stack-tests
new file mode 100644
index 0000000000..291f58d5ec
--- /dev/null
+++ b/modules/c-stack-tests
@@ -0,0 +1,15 @@
+Files:
+tests/test-c-stack.c
+tests/test-c-stack.sh
+
+Depends-on:
+exitfail
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-c-stack.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@'
+check_PROGRAMS += test-c-stack
+test_c_stack_LDADD = $(LDADD) @LIBINTL@
+MOSTLYCLEANFILES += t-c-stack.tmp
diff --git a/tests/test-c-stack.c b/tests/test-c-stack.c
new file mode 100644
index 0000000000..13c1a12560
--- /dev/null
+++ b/tests/test-c-stack.c
@@ -0,0 +1,68 @@
+/* Test of c-stack module.
+   Copyright (C) 2002, 2004, 2006, 2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include "c-stack.h"
+
+#include "exitfail.h"
+#include <stdio.h>
+#include <stdlib.h>
+#if HAVE_SETRLIMIT
+# include <sys/resource.h>
+#endif
+
+#define ASSERT(expr) \
+  do									     \
+    {									     \
+      if (!(expr))							     \
+        {								     \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);						     \
+          abort ();							     \
+        }								     \
+    }									     \
+  while (0)
+
+static long
+recurse (char *p)
+{
+  char array[500];
+  array[0] = 1;
+  return *p + recurse (array);
+}
+
+char *program_name;
+
+int
+main (int argc, char **argv)
+{
+   program_name = argv[0];
+#if HAVE_SETRLIMIT && defined RLIMIT_STACK
+   /* Before starting the endless recursion, try to be friendly to the
+      user's machine.  On some Linux 2.2.x systems, there is no stack
+      limit for user processes at all.  We don't want to kill such
+      systems.  */
+   struct rlimit rl;
+   rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
+   setrlimit (RLIMIT_STACK, &rl);
+#endif
+
+  if (c_stack_action (0) == 0)
+    return recurse ("\1");
+  perror ("c_stack_action");
+  return 77;
+}
diff --git a/tests/test-c-stack.sh b/tests/test-c-stack.sh
new file mode 100755
index 0000000000..f979065627
--- /dev/null
+++ b/tests/test-c-stack.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="t-c-stack.tmp"
+./test-c-stack${EXEEXT} 2> t-c-stack.tmp
+case $? in
+  77) cat t-c-stack.tmp >&2; (exit 77); exit 77 ;;
+  1) ;;
+  *) (exit 1); exit 1 ;;
+esac
+if grep 'stack overflow' t-c-stack.tmp >/dev/null ; then
+  :
+else
+  (exit 1); exit 1
+fi
+
+rm -fr $tmpfiles
+
+exit 0
-- 
2.30.2