;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix 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.
;;;
;;; GNU Guix 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 GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (test-debug-link)
  #:use-module (guix elf)
  #:use-module (guix build utils)
  #:use-module (guix build debug-link)
  #:use-module (guix gexp)
  #:use-module (guix store)
  #:use-module (guix tests)
  #:use-module (guix monads)
  #:use-module (guix derivations)
  #:use-module (gnu packages bootstrap)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-64)
  #:use-module (rnrs io ports)
  #:use-module (ice-9 match))

(define %guile-executable
  (match (false-if-exception (readlink "/proc/self/exe"))
    ((? string? program)
     (and (file-exists? program) (elf-file? program)
          program))
    (_
     #f)))

(define read-elf
  (compose parse-elf get-bytevector-all))


(test-begin "debug-link")

(unless %guile-executable (test-skip 1))
(test-assert "elf-debuglink"
  (let ((elf (call-with-input-file %guile-executable read-elf)))
    (match (call-with-values (lambda () (elf-debuglink elf)) list)
      ((#f #f)                                    ;no '.gnu_debuglink' section
       (pk 'no-debuglink #t))
      (((? string? file) (? integer? crc))
       (string-suffix? ".debug" file)))))

;; Since we need %BOOTSTRAP-GCC and co., we have to skip the following tests
;; when networking is unreachable because we'd fail to download it.
(unless (network-reachable?) (test-skip 1))
(test-assertm "elf-debuglink"
  ;; Check whether we can compute the CRC just like objcopy, and whether we
  ;; can retrieve it.
  (let* ((code (plain-file "test.c" "int main () { return 42; }"))
         (exp  (with-imported-modules '((guix build utils)
                                        (guix build debug-link)
                                        (guix elf))
                 #~(begin
                     (use-modules (guix build utils)
                                  (guix build debug-link)
                                  (guix elf)
                                  (rnrs io ports))

                     (define read-elf
                       (compose parse-elf get-bytevector-all))

                     (setenv "PATH" (string-join '(#$%bootstrap-gcc
                                                   #$%bootstrap-binutils)
                                                 "/bin:" 'suffix))
                     (invoke "gcc" "-O0" "-g" #$code "-o" "exe")
                     (copy-file "exe" "exe.debug")
                     (invoke "strip" "--only-keep-debug" "exe.debug")
                     (invoke "strip" "--strip-debug" "exe")
                     (invoke "objcopy" "--add-gnu-debuglink=exe.debug"
                             "exe")
                     (call-with-values (lambda ()
                                         (elf-debuglink
                                          (call-with-input-file "exe"
                                            read-elf)))
                       (lambda (file crc)
                         (call-with-output-file #$output
                           (lambda (port)
                             (let ((expected (call-with-input-file "exe.debug"
                                               debuglink-crc32)))
                               (write (list file (= crc expected))
                                      port))))))))))
    (mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
                         (x   (built-derivations (list drv))))
      (call-with-input-file (derivation->output-path drv)
        (lambda (port)
          (return (match (read port)
                    (("exe.debug" #t) #t)
                    (x                (pk 'fail x #f)))))))))

(unless (network-reachable?) (test-skip 1))
(test-assertm "set-debuglink-crc"
  ;; Check whether 'set-debuglink-crc' successfully updates the CRC.
  (let* ((code  (plain-file "test.c" "int main () { return 42; }"))
         (debug (plain-file "exe.debug" "a"))
         (exp   (with-imported-modules '((guix build utils)
                                         (guix build debug-link)
                                         (guix elf))
                  #~(begin
                      (use-modules (guix build utils)
                                   (guix build debug-link)
                                   (guix elf)
                                   (rnrs io ports))

                      (define read-elf
                        (compose parse-elf get-bytevector-all))

                      (setenv "PATH" (string-join '(#$%bootstrap-gcc
                                                    #$%bootstrap-binutils)
                                                  "/bin:" 'suffix))
                      (invoke "gcc" "-O0" "-g" #$code "-o" "exe")
                      (copy-file "exe" "exe.debug")
                      (invoke "strip" "--only-keep-debug" "exe.debug")
                      (invoke "strip" "--strip-debug" "exe")
                      (invoke "objcopy" "--add-gnu-debuglink=exe.debug"
                              "exe")
                      (set-debuglink-crc "exe" #$debug)
                      (call-with-values (lambda ()
                                          (elf-debuglink
                                           (call-with-input-file "exe"
                                             read-elf)))
                        (lambda (file crc)
                          (call-with-output-file #$output
                            (lambda (port)
                              (write (list file crc) port)))))))))
    (mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
                         (x   (built-derivations (list drv))))
      (call-with-input-file (derivation->output-path drv)
        (lambda (port)
          (return (match (read port)
                    (("exe.debug" crc)
                     (= crc (debuglink-crc32 (open-input-string "a"))))
                    (x
                     (pk 'fail x #f)))))))))

(test-end "debug-link")
put-port (open-file "/dev/console" "w0")) (chmod "/export" #o777) (with-output-to-file "/export/hello" (lambda () (display "hello world"))) (chmod "/export/hello" #o777)) marionette) (test-assert "nscd PID file is created" (marionette-eval '(begin (use-modules (gnu services herd)) (start-service 'nscd)) marionette)) (test-assert "nscd is listening on its socket" (wait-for-unix-socket "/var/run/nscd/socket" marionette)) (test-assert "network is up" (marionette-eval '(begin (use-modules (gnu services herd)) (start-service 'networking)) marionette)) ;; Wait for the NFS services to be up and running. (test-assert "nfs services are running" (and (marionette-eval '(begin (use-modules (gnu services herd)) (start-service 'nfs)) marionette) (wait-for-file "/var/run/rpc.statd.pid" marionette))) (test-assert "nfs share is advertised" (marionette-eval '(zero? (system* (string-append #$nfs-utils "/sbin/showmount") "-e" "nfs-server")) marionette)) (test-assert "nfs share mounted" (marionette-eval '(begin (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4") "nfs-server:/" "/remote" "-v")) (file-exists? "/remote/hello"))) marionette)) (test-end)))) (gexp->derivation "nfs-server-test" test)) (define %test-nfs-server (system-test (name "nfs-server") (description "Test that an NFS server can be started and exported directories can be mounted.") (value (run-nfs-server-test)))) (define (run-nfs-full-test) "Run a test of an OS mounting its root file system via NFS." (define nfs-root-server-os (let ((os (simple-operating-system))) (marionette-operating-system (operating-system (inherit os) (services (cons* (service static-networking-service-type (list (static-networking (addresses (list (network-address (device "ens5") (value "10.0.2.15/24"))))))) (simple-service 'export activation-service-type #~(begin (mkdir-p "/export") (chmod "/export" #o777))) (service nfs-service-type (nfs-configuration (nfsd-port 2049) (nfs-versions '("4.2")) (exports '(("/export" "*(rw,insecure,no_subtree_check,\ crossmnt,fsid=root,no_root_squash,insecure,async)"))))) (modify-services (operating-system-user-services os) (syslog-service-type config => (syslog-configuration (inherit config) (config-file (plain-file "syslog.conf" "*.* /dev/console\n")))))))) #:requirements '(nscd) #:imported-modules '((gnu services herd) (guix combinators))))) (define nfs-root-client-os (marionette-operating-system (simple-operating-system (service static-networking-service-type (list (static-networking (addresses (list (network-address (device "ens5") (value "10.0.2.16/24"))))))) (service nfs-service-type (nfs-configuration (nfsd-port 2049) (nfs-versions '("4.2")))) (simple-service 'export activation-service-type #~(begin (mkdir-p "/export") (chmod "/export" #o777)))) #:requirements '(nscd) #:imported-modules '((gnu services herd) (guix combinators)))) (define test (with-imported-modules '((gnu build marionette)) #~(begin (use-modules (gnu build marionette) (srfi srfi-64)) (test-runner-current (system-test-runner #$output)) (test-begin "start-nfs-boot-test") ;;; Start up NFS server host. (mkdir "/tmp/server") (define server-marionette (make-marionette (cons* #$(virtual-machine (operating-system nfs-root-server-os) (volatile? #f)) '("-device" "e1000,netdev=n1,mac=52:54:00:12:34:56" "-netdev" "socket,id=n1,listen=:1234")) #:socket-directory "/tmp/server")) ;;; Wait for the NFS services to be up and running. (test-assert "nfs services are running" (wait-for-file "/var/run/rpc.statd.pid" server-marionette)) (test-assert "NFS port is ready" (wait-for-tcp-port 2049 server-marionette)) ;;; Start up NFS client host. (mkdir "/tmp/client") (define client-marionette (make-marionette (cons* #$(virtual-machine (operating-system nfs-root-client-os) (volatile? #f)) '("-device" "e1000,netdev=n2,mac=52:54:00:12:34:57" "-netdev" "socket,id=n2,connect=127.0.0.1:1234")) #:socket-directory "/tmp/client")) (test-assert "NFS port is ready" (wait-for-tcp-port 2049 client-marionette)) (marionette-eval '(begin (use-modules (rnrs io ports)) (current-output-port (open-file "/dev/console" "w0")) (and (system* (string-append #$nfs-utils "/sbin/mount.nfs") "10.0.2.15:/export" "/export" "-v") (let ((content (call-with-input-file "/proc/mounts" get-string-all))) (call-with-output-file "/export/mounts" (lambda (port) (display content port)))))) client-marionette) ;;; Check whether NFS client host communicated with NFS server host. (test-assert "nfs client deposited file" (wait-for-file "/export/mounts" server-marionette)) (marionette-eval '(begin (current-output-port (open-file "/dev/console" "w0")) (call-with-input-file "/export/mounts" display)) server-marionette) (test-end)))) (gexp->derivation "nfs-full-test" test)) (define %test-nfs-full (system-test (name "nfs-full") (description "Test that an NFS server can be started and the exported directory can be used by another machine.") (value (run-nfs-full-test))))