tests: Harmonize temporary and socket directory handling.
authorJustus Winter <justus@g10code.com>
Mon, 6 Mar 2017 16:16:41 +0000 (17:16 +0100)
committerJustus Winter <justus@g10code.com>
Mon, 6 Mar 2017 17:10:10 +0000 (18:10 +0100)
* tests/gpgscm/tests.scm (mkdtemp): Do not magically obey the
environment variable 'TMP', make sure to always return an absolute
path.
* tests/gpgme/Makefile.am (TMP): Drop variable.
(TESTS_ENVIRONMENT): Drop 'TMP'.
* tests/gpgme/gpgme-defs.scm (create-gpgmehome): Start the agent.  Do
not create private key store, the agent does that for us.
* tests/gpgsm/Makefile.am (TMP): Drop variable.
(TESTS_ENVIRONMENT): Drop 'TMP'.
* tests/gpgme/gpgme-defs.scm (create-gpgsmhome): Start the agent.  Do
not create private key store, the agent does that for us.
* tests/migrations/Makefile.am (TMP): Drop variable.
(TESTS_ENVIRONMENT): Drop 'TMP'.
* tests/migrations/common.scm (gpgconf): New variable.
(run-test): Create and remove socket directory.
* tests/migrations/extended-pkf.scm (src-tarball): Remove variable.
(setup): Remove function.
(trigger-migration): Likewise.
Use 'run-test' to execute the test.
* tests/migrations/from-classic.scm (src-tarball): Remove variable.
(setup): Remove function.
Use 'run-test' to execute the tests.
* tests/openpgp/Makefile.am (TMP): Drop variable.
(TESTS_ENVIRONMENT): Drop 'TMP'.
* tests/openpgp/README: Do not mention 'TMP'.
* tests/openpgp/defs.scm (with-home-directory): New macro.
(create-legacy-gpghome): Do not create private key store, the agent
does that for us.
(start-agent): Make sure to terminate the right agent with 'atexit'.
--

Previously, the test suite relied upon creating home directories in
'/tmp'.  This has been problematic in some build environments,
although POSIX mandates that '/tmp' must be available.

We now rely on 'gpgconf --create-socketdir' to create a suitable
socket directory for us.  This allows us to get rid of some cruft.  It
also aligns the environment the tests are run in closer with the
environment that we intend that GnuPG runs in.

Signed-off-by: Justus Winter <justus@g10code.com>
13 files changed:
tests/gpgme/Makefile.am
tests/gpgme/gpgme-defs.scm
tests/gpgscm/tests.scm
tests/gpgsm/Makefile.am
tests/gpgsm/gpgsm-defs.scm
tests/migrations/Makefile.am
tests/migrations/common.scm
tests/migrations/extended-pkf.scm
tests/migrations/from-classic.scm
tests/openpgp/Makefile.am
tests/openpgp/README
tests/openpgp/defs.scm
tests/openpgp/setup.scm

index d7fd87c..0d0edc0 100644 (file)
@@ -28,12 +28,9 @@ include $(top_srcdir)/am/cmacros.am
 
 AM_CFLAGS =
 
-TMP ?= /tmp
-
 TESTS_ENVIRONMENT = LC_ALL=C \
        EXEEXT=$(EXEEXT) \
        PATH=../gpgscm:$(PATH) \
-       TMP=$(TMP) \
        srcdir=$(abs_srcdir) \
        objdir=$(abs_top_builddir) \
        GPGSCM_PATH=$(abs_top_srcdir)/tests/gpgscm:$(abs_top_srcdir)/tests/openpgp:$(abs_top_srcdir)/tests/gpgme
index c102c93..7a7166c 100644 (file)
@@ -54,7 +54,8 @@
   (create-file
    "gpg-agent.conf"
    (string-append "pinentry-program " (tool 'pinentry)))
-  (mkdir "private-keys-v1.d" "-rwx")
+
+  (start-agent)
 
   (log "Storing private keys")
   (for-each
index e5858d9..b3da919 100644 (file)
 ;; Make a temporary directory.  If arguments are given, they are
 ;; joined using path-join, and must end in a component ending in
 ;; "XXXXXX".  If no arguments are given, a suitable location and
-;; generic name is used.
+;; generic name is used.  Returns an absolute path.
 (define (mkdtemp . components)
-  (_mkdtemp (if (null? components)
-               (path-join (getenv "TMP")
-                          (string-append "gpgscm-" (get-isotime) "-"
-                                         (basename-suffix *scriptname* ".scm")
-                                         "-XXXXXX"))
-               (apply path-join components))))
+  (canonical-path (_mkdtemp (if (null? components)
+                               (string-append "gpgscm-" (get-isotime) "-"
+                                              (basename-suffix *scriptname* ".scm")
+                                              "-XXXXXX")
+                               (apply path-join components)))))
 
 (define-macro (with-temporary-working-directory . expressions)
   (let ((tmp-sym (gensym)))
index aad328b..28db501 100644 (file)
@@ -28,12 +28,9 @@ include $(top_srcdir)/am/cmacros.am
 
 AM_CFLAGS =
 
-TMP ?= /tmp
-
 TESTS_ENVIRONMENT = LC_ALL=C \
        EXEEXT=$(EXEEXT) \
        PATH=../gpgscm:$(PATH) \
-       TMP=$(TMP) \
        srcdir=$(abs_srcdir) \
        objdir=$(abs_top_builddir) \
        GPGSCM_PATH=$(abs_top_srcdir)/tests/gpgscm:$(abs_top_srcdir)/tests/openpgp:$(abs_top_srcdir)/tests/gpgsm
index aa5af3d..5f9be7f 100644 (file)
@@ -73,6 +73,7 @@
               "faked-system-time 1008241200")
   (create-file "gpg-agent.conf"
               (string-append "pinentry-program " (tool 'pinentry)))
+  (start-agent)
   (create-file
    "trustlist.txt"
    "32100C27173EF6E9C4E9A25D3D69F86D37A4F939"
@@ -80,7 +81,6 @@
    "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E S")
 
   (log "Storing private keys")
-  (mkdir "private-keys-v1.d" "-rwx")
   (for-each
    (lambda (name)
      (file-copy (in-srcdir name)
index d0cd9ee..0895aff 100644 (file)
@@ -28,12 +28,9 @@ include $(top_srcdir)/am/cmacros.am
 
 AM_CFLAGS =
 
-TMP ?= /tmp
-
 TESTS_ENVIRONMENT = GPG_AGENT_INFO= LC_ALL=C \
        EXEEXT=$(EXEEXT) \
        PATH=../gpgscm:$(PATH) \
-       TMP=$(TMP) \
        srcdir=$(abs_srcdir) \
        objdir=$(abs_top_builddir) \
        GPGSCM_PATH=$(abs_top_srcdir)/tests/gpgscm:$(abs_top_srcdir)/tests/migrations
index 30ac62b..fa8f129 100644 (file)
@@ -26,6 +26,7 @@
   (string-append executable (getenv "EXEEXT")))
 
 ;; We may not use a relative name for gpg-agent.
+(define gpgconf (path-join (getenv "objdir") "tools" (qualify "gpgconf")))
 (define GPG-AGENT (path-join (getenv "objdir") "agent" (qualify "gpg-agent")))
 (define GPG `(,(path-join (getenv "objdir") "g10" (qualify "gpg"))
              --no-permission-warning --no-greeting
@@ -51,4 +52,9 @@
    (info message)
    (untar-armored src-tarball)
    (setenv "GNUPGHOME" (getcwd) #t)
-   (test (getcwd))))
+
+   (catch (log "Warning: Creating socket directory failed:" (car *error*))
+         (call-popen `(,gpgconf --create-socketdir) ""))
+   (test (getcwd))
+   (catch (log "Warning: Removing socket directory failed.")
+         (call-popen `(,gpgconf --remove-socketdir) ""))))
index bf2c49e..1317cd4 100755 (executable)
 (catch (skip "gpgtar not built")
        (call-check `(,GPGTAR --help)))
 
-(define src-tarball (in-srcdir "extended-pkf.tar.asc"))
-
-(define (setup)
-  (untar-armored src-tarball)
-  (setenv "GNUPGHOME" (getcwd) #t))
-
-(define (trigger-migration)
-  (call-check `(,@GPG --list-secret-keys)))
-
 (define (assert-keys-usable)
   (for-each
    (lambda (keyid)
            (call-check `(,@GPG --list-secret-keys ,keyid))))
    '("C40FDECF" "ECABF51D")))
 
-(info "Testing the extended private key format ...")
-(with-temporary-working-directory
- (setup)
- (assert-keys-usable))
+(run-test
+ "Testing the extended private key format ..."
+ (in-srcdir "extended-pkf.tar.asc")
+ (lambda (gpghome)
+   (assert-keys-usable)))
 
 ;; XXX try changing a key, and check that the format is not changed.
index d540470..ace458e 100755 (executable)
 (catch (skip "gpgtar not built")
        (call-check `(,GPGTAR --help)))
 
-(define src-tarball (in-srcdir "from-classic.tar.asc"))
-
-(define (setup)
-  (untar-armored src-tarball)
-  (setenv "GNUPGHOME" (getcwd) #t))
-
 (define (trigger-migration)
   (call-check `(,@GPG --list-secret-keys)))
 
            (call-check `(,@GPG --list-secret-keys ,keyid))))
    '("D74C5F22" "C40FDECF" "ECABF51D")))
 
-(info "Testing a clean migration ...")
-(with-temporary-working-directory
- (setup)
- (trigger-migration)
- (assert-migrated))
-
-(info "Testing a migration with existing private-keys-v1.d ...")
-(with-temporary-working-directory
- (setup)
- (mkdir "private-keys-v1.d" "-rwx")
- (trigger-migration)
- (assert-migrated))
-
-(info "Testing a migration with existing but weird private-keys-v1.d ...")
-(with-temporary-working-directory
- (setup)
- (mkdir "private-keys-v1.d" "")
- (trigger-migration)
- (assert-migrated))
+(run-test
+ "Testing a clean migration ..."
+ (in-srcdir "from-classic.tar.asc")
+ (lambda (gpghome)
+   (trigger-migration)
+   (assert-migrated)))
+
+(run-test
+ "Testing a migration with existing private-keys-v1.d ..."
+ (in-srcdir "from-classic.tar.asc")
+ (lambda (gpghome)
+   (mkdir "private-keys-v1.d" "-rwx")
+   (trigger-migration)
+   (assert-migrated)))
+
+(run-test
+ "Testing a migration with existing but weird private-keys-v1.d ..."
+ (in-srcdir "from-classic.tar.asc")
+ (lambda (gpghome)
+   (mkdir "private-keys-v1.d" "")
+   (trigger-migration)
+   (assert-migrated)))
 
 ;; XXX Check a case where the migration fails.
index afac58f..518af20 100644 (file)
@@ -33,12 +33,9 @@ noinst_PROGRAMS = fake-pinentry
 
 fake_pinentry_SOURCES = fake-pinentry.c
 
-TMP ?= /tmp
-
 TESTS_ENVIRONMENT = LC_ALL=C \
        EXEEXT=$(EXEEXT) \
        PATH=../gpgscm:$(PATH) \
-       TMP=$(TMP) \
        srcdir=$(abs_srcdir) \
        objdir=$(abs_top_builddir) \
        GPGSCM_PATH=$(abs_top_srcdir)/tests/gpgscm:$(abs_top_srcdir)/tests/openpgp
index eba77b1..b9d5607 100644 (file)
@@ -30,7 +30,7 @@ This is a bit tricky because one needs to manually set some
 environment variables.  We should make that easier.  See discussion
 below.  From your build directory, do:
 
-  obj $ TMP=/tmp srcdir=<path to>/tests/openpgp \
+  obj $ srcdir=<path to>/tests/openpgp \
         GPGSCM_PATH=<path to>/tests/gpgscm:<path to>/tests/openpgp \
         $(pwd)/tests/gpgscm/gpgscm [gpgscm args] \
         run-tests.scm [test suite runner args]
index a06a570..568ffab 100644 (file)
 ;; GnuPG helper.
 ;;
 
+;; Evaluate a sequence of expressions with the given home directory.
+(define-macro (with-home-directory gnupghome . expressions)
+  (let ((original-home-directory (gensym)))
+    `(let ((,original-home-directory (getenv "GNUPGHOME")))
+       (dynamic-wind
+          (lambda () (setenv "GNUPGHOME" ,gnupghome #t))
+          (lambda () ,@expressions)
+          (lambda () (setenv "GNUPGHOME" ,original-home-directory #t))))))
+
 ;; Evaluate a sequence of expressions with an ephemeral home
 ;; directory.
 (define-macro (with-ephemeral-home-directory . expressions)
 
 (define (create-legacy-gpghome)
   (create-sample-files)
-  (mkdir "private-keys-v1.d" "-rwx")
 
   (log "Storing private keys")
   (for-each
 ;; Create the socket dir and start the agent.
 (define (start-agent)
   (log "Starting gpg-agent...")
-  (atexit stop-agent)
+  (let ((gnupghome (getenv "GNUPGHOME")))
+    (atexit (lambda ()
+             (with-home-directory gnupghome
+                                  (stop-agent)))))
   (catch (log "Warning: Creating socket directory failed:" (car *error*))
         (call-popen `(,(tool 'gpgconf) --create-socketdir) ""))
   (call-check `(,(tool 'gpg-connect-agent) --verbose
index d13799d..bf1876e 100755 (executable)
@@ -25,6 +25,7 @@
 (with-ephemeral-home-directory
  (chdir (getenv "GNUPGHOME"))
  (create-gpghome)
+ (start-agent)
  (create-legacy-gpghome)
  (stop-agent)
  (call-check `(,(tool 'gpgtar) --create --output ,(cadr *args*) ".")))