Create test cases for the new format functions for local address writing.
authorKen Hornstein <kenh@pobox.com>
Wed, 15 Feb 2012 20:54:47 +0000 (15:54 -0500)
committerKen Hornstein <kenh@pobox.com>
Wed, 15 Feb 2012 20:54:47 +0000 (15:54 -0500)
.gitignore
Makefile.am
test/common.sh.in
test/format/test-localmbox [new file with mode: 0755]
test/format/test-myhost [new file with mode: 0755]
test/format/test-mymbox [new file with mode: 0755]
test/format/test-myname [new file with mode: 0755]
test/getfullname.c [new file with mode: 0644]

index b65e540..87e31cb 100644 (file)
@@ -96,6 +96,7 @@ a.out.DSYM/
 /uip/whatnow
 /uip/whom
 /uip/*.exe
+/test/getfullname
 
 # Removed by mostlyclean:
 *.o
index 84bf58f..5b473c3 100644 (file)
@@ -42,7 +42,10 @@ TESTS_ENVIRONMENT = MH_TEST_DIR=`pwd`/test/testdir \
 ## Important note: the "cleanup" test should always be last
 ##
 TESTS = test/bad-input/test-header test/folder/test-create \
-       test/folder/test-total test/inc/test-deb359167 test/inc/test-eom-align \
+       test/folder/test-total test/format/test-localmbox \
+       test/format/test-myname test/format/test-myhost \
+       test/format/test-mymbox \
+       test/inc/test-deb359167 test/inc/test-eom-align \
        test/manpages/test-manpages test/mhbuild/test-forw \
        test/mhshow/test-cte-binary test/mhshow/test-qp \
        test/mhshow/test-subpart test/new/test-basic test/pick/test-stderr \
@@ -51,6 +54,7 @@ TESTS = test/bad-input/test-header test/folder/test-create \
        test/whatnow/test-ls test/cleanup
 
 check_SCRIPTS = test/common.sh
+check_PROGRAMS = test/getfullname
 
 ##
 ## Stuff that should be cleaned via "make clean"
@@ -341,6 +345,9 @@ uip_spost_SOURCES = uip/spost.c uip/aliasbr.c
 uip_viamail_SOURCES = uip/viamail.c uip/mhmisc.c uip/mhoutsbr.c uip/sendsbr.c \
                      uip/annosbr.c uip/distsbr.c
 
+test_getfullname_SOURCES = test/getfullname.c
+test_getfullname_LDADD =
+
 ##
 ## Our rebuild rules for files that aren't built via the normal mechanisms
 ##
index 34767f4..d5a9d7e 100644 (file)
@@ -64,6 +64,7 @@ setup_test ()
   export MH_INST_DIR=${MH_TEST_DIR}/inst
   export MHMTSCONF=${MH_INST_DIR}${sysconfdir}/mts.conf
   export PATH=${MH_INST_DIR}${bindir}:${PATH}
+  export MH_LIB_DIR=${MH_INST_DIR}${auxexecdir}
 
   if [ -z "${srcdir}" ]; then
     echo "srcdir not set; aborting"
@@ -84,7 +85,7 @@ setup_test ()
   mkdir $MH_TEST_DIR/Mail || exit 1
   cat > $MH <<EOF || exit 1
 Path: ${MH_TEST_DIR}/Mail
-mhlproc: ${MH_INST_DIR}${auxexecdir}/mhl
+mhlproc: ${MH_LIB_DIR}/mhl
 EOF
 
   for f in MailAliases components digestcomps distcomps forwcomps mhl.body \
diff --git a/test/format/test-localmbox b/test/format/test-localmbox
new file mode 100755 (executable)
index 0000000..196b03b
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# Test that we can retrieve the local mailbox name from the profile via
+# component functions
+#
+
+if [ -z "${MH_TEST_COMMON}" ]; then
+       echo "MH_TEST_COMMON not set; try running via 'make check'"
+fi
+
+. ${MH_TEST_COMMON}
+
+setup_test
+
+testname="Some User <some@domain.com>"
+
+echo "Local-Mailbox: ${testname}" >> ${MH}
+
+# We can use "ap" to get the output of format commands
+
+testoutput=$(${MH_LIB_DIR}/ap -format "%(profile Local-Mailbox)" ignore)
+
+if [ x"${testname}" != x"${testoutput}" ]; then
+       echo "Expected ${testname}, got ${testoutput}"
+       exit 1
+fi
+
+exit 0
diff --git a/test/format/test-myhost b/test/format/test-myhost
new file mode 100755 (executable)
index 0000000..d303a8f
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# Test that the %(myhost) function returns the local hostname and
+# the localname entry out of the mts configuration file.
+#
+
+if [ -z "${MH_TEST_COMMON}" ]; then
+       echo "MH_TEST_COMMON not set; try running via 'make check'"
+fi
+
+. ${MH_TEST_COMMON}
+
+setup_test
+
+runtest()
+{
+       testoutput=$(${MH_LIB_DIR}/ap -format "%(myhost)" ignore)
+
+       if [ x"$1" != x"${testoutput}" ]; then
+               echo "For $2, expected $1 but got ${testoutput}"
+               exit 1
+       fi
+}
+
+runtest "$(hostname)" "local hostname test"
+
+cp ${MHMTSCONF} ${MH_TEST_DIR}/Mail/mts.conf || exit 1
+export MHMTSCONF="${MH_TEST_DIR}/Mail/mts.conf"
+
+echo "localname: some.random.name" >> ${MHMTSCONF}
+
+runtest "some.random.name" "mts.conf localname test"
+
+exit 0
diff --git a/test/format/test-mymbox b/test/format/test-mymbox
new file mode 100755 (executable)
index 0000000..82f0879
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/sh
+#
+# Test that the %(mymbox) function correctly determines whether or not
+# a particular email address is "mine" or not
+#
+
+if [ -z "${MH_TEST_COMMON}" ]; then
+       echo "MH_TEST_COMMON not set; try running via 'make check'"
+fi
+
+. ${MH_TEST_COMMON}
+
+setup_test
+
+runtest()
+{
+       testoutput=$(${MH_LIB_DIR}/ap -format "%(mymbox{text})" "$1")
+
+       if [ -z "${testoutput}" ]; then
+               echo "Test program did not return anything for $3"
+               exit 1
+       fi
+
+       if [ "${testoutput}" -ne $2 ]; then
+               echo "Did not get expected output for $3"
+               exit 1
+       fi
+}
+
+user="$(id -un)"
+host="$(hostname)"
+
+runtest "${user}" 1 "Basic user test"
+runtest "${user}@${host}" 1 "Basic user@host test"
+runtest "nosuchuser@nosuchhost.blah" 0 "Basic non-matching test"
+
+myname="Random User <random@user.something.com>"
+
+echo "Local-Mailbox: ${myname}" >> ${MH}
+
+runtest "${myname}" 1 "Local-Mailbox test"
+runtest "${user}@${host}" 0 "Local-mailbox overriding user@host test"
+
+exit 0
diff --git a/test/format/test-myname b/test/format/test-myname
new file mode 100755 (executable)
index 0000000..4e1dfaa
--- /dev/null
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# Test that we can retrieve the local username via the passwd file,
+# the Signature profile entry, and via SIGNATURE environment variable.
+#
+
+if [ -z "${MH_TEST_COMMON}" ]; then
+       echo "MH_TEST_COMMON not set; try running via 'make check'"
+fi
+
+. ${MH_TEST_COMMON}
+
+setup_test
+
+unset SIGNATURE 
+
+runtest()
+{
+       testoutput=$(${MH_LIB_DIR}/ap -format "%(myname)" ignore)
+
+       if [ x"$1" != x"${testoutput}" ]; then
+               echo "For $2, expected $1 but got ${testoutput}"
+               exit 1
+       fi
+}
+
+runtest "$(${MH_OBJ_DIR}/test/getfullname)" "GECOS field test"
+
+echo "Signature: Some Random Name 1" >> ${MH}
+
+runtest "Some Random Name 1" "MH Profile Signature test"
+
+export SIGNATURE="Some Random Name 2"
+
+runtest "${SIGNATURE}" "SIGNATURE Environment test"
+
+exit 0
diff --git a/test/getfullname.c b/test/getfullname.c
new file mode 100644 (file)
index 0000000..3afa00f
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * getfullname.c - Extract a user's name out of the GECOS field in
+ *                the password file.
+ *
+ * This code is Copyright (c) 2012, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+
+int
+main(int argc, char *argv[])
+{
+       struct passwd *pwd;
+
+       pwd = getpwuid(getuid());
+
+       if (! pwd) {
+               fprintf(stderr, "Unable to retrieve user info for "
+                       "userid %d\n", getuid());
+               exit(1);
+       }
+
+       printf("%s\n", pwd->pw_gecos);
+
+       exit(0);
+}