aboutsummaryrefslogtreecommitdiff
# GNU Guix --- Functional package management for GNU
# Copyright © 2020, 2022, 2024 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/>.

#
# Test the 'guix git authenticate' command-line utility.
#

# Skip if we're not in a Git checkout.
[ -d "$abs_top_srcdir/.git" ] || exit 77

# Skip if there's no 'keyring' branch.
guile -c '(use-modules (git))
  (member "refs/heads/keyring" (branch-list (repository-open ".")))' || \
    exit 77

# Keep in sync with '%default-channels' in (guix channels)!
intro_commit="9edb3f66fd807b096b48283debdcddccfea34bad"
intro_signer="BBB0 2DDF 2CEA F6A8 0D1D  E643 A2A0 6DF2 A33A 54FA"

cache_key="test-$$"

# This must fail because the end commit is not a descendant of $intro_commit.
guix git authenticate "$intro_commit" "$intro_signer"	\
     --cache-key="$cache_key" --stats			\
     --end=9549f0283a78fe36f2d4ff2a04ef8ad6b0c02604 && false

# The v1.2.0 commit is a descendant of $intro_commit and it satisfies the
# authorization invariant.
v1_2_0_commit="a099685659b4bfa6b3218f84953cbb7ff9e88063"
guix git authenticate "$intro_commit" "$intro_signer"	\
     --cache-key="$cache_key" --stats			\
     --end="$v1_2_0_commit"

# Check a commit that came soon after v1.2.0.  No need to repeat $intro_commit
# and $intro_signer because it should have been recorded in '.git/config'.
after_v1_2_0="be4d9527b55b6829e33a6e0727496af25927a786"
guix git authenticate				\
     --cache-key="$cache_key" --stats		\
     --end="$v1_2_0_commit"

rm "$XDG_CACHE_HOME/guix/authentication/$cache_key"

# Commit and signer of the 'v1.0.0' tag.
v1_0_0_commit="6298c3ffd9654d3231a6f25390b056483e8f407c"
v1_0_0_signer="3CE4 6455 8A84 FDC6 9DB4  0CFB 090B 1199 3D9A EBB5" # civodul
v1_0_1_commit="d68de958b60426798ed62797ff7c96c327a672ac"

# This should succeed because v1.0.0 is an ancestor of $intro_commit.
guix git authenticate "$intro_commit" "$intro_signer"	\
     --cache-key="$cache_key" --stats			\
     --end="$v1_0_0_commit"

# This should fail because these commits lack '.guix-authorizations'.
guix git authenticate "$v1_0_0_commit" "$v1_0_0_signer" \
       --cache-key="$cache_key" --end="$v1_0_1_commit" && false

# This should work thanks to '--historical-authorizations'.
guix git authenticate "$v1_0_0_commit" "$v1_0_0_signer" 	\
     --cache-key="$cache_key" --end="$v1_0_1_commit" --stats	\
     --historical-authorizations="$abs_top_srcdir/etc/historical-authorizations"
o-serialization" ;; When serialization is disabled, the serializer is set to #f, so ;; attempting to use it fails with a 'wrong-type-arg' error. (not (false-if-exception (let ((config (serializable-configuration))) (serialize-configuration config serializable-configuration-fields))))) (define (custom-prefix-serialize-integer field-name name) name) (define-configuration configuration-with-prefix (port (integer 10) "The port number.") (prefix custom-prefix-)) (test-assert "serialize-configuration with prefix" (gexp? (let ((config (configuration-with-prefix))) (serialize-configuration config configuration-with-prefix-fields)))) ;;; ;;; define-configuration macro, extra-args literals ;;; (define (eval-gexp x) "Get serialized config as string." (eval (gexp->approximate-sexp x) (current-module))) (define (port? value) (or (string? value) (number? value))) (define (sanitize-port value) (cond ((number? value) value) ((string? value) (string->number value)) (else (raise (formatted-message (G_ "Bad value: ~a") value))))) (test-group "Basic sanitizer literal tests" (define serialize-port serialize-number) (define-configuration config-with-sanitizer (port (port 80) "Lorem Ipsum." (sanitizer sanitize-port))) (test-equal "default value, sanitizer" 80 (config-with-sanitizer-port (config-with-sanitizer))) (test-equal "string value, sanitized to number" 56 (config-with-sanitizer-port (config-with-sanitizer (port "56")))) (define (custom-serialize-port field-name value) (number->string value)) (define-configuration config-serializer (port (port 80) "Lorem Ipsum." (serializer custom-serialize-port))) (test-equal "default value, serializer literal" "80" (eval-gexp (serialize-configuration (config-serializer) config-serializer-fields)))) (test-group "empty-serializer as literal/procedure tests" (define-configuration config-with-literal (port (port 80) "Lorem Ipsum." empty-serializer)) (define-configuration config-with-proc (port (port 80) "Lorem Ipsum." (serializer empty-serializer))) (test-equal "empty-serializer as literal" "" (eval-gexp (serialize-configuration (config-with-literal) config-with-literal-fields))) (test-equal "empty-serializer as procedure" "" (eval-gexp (serialize-configuration (config-with-proc) config-with-proc-fields)))) (test-group "permutation tests" (define-configuration config-san+empty-ser (port (port 80) "Lorem Ipsum." (sanitizer sanitize-port) empty-serializer)) (define-configuration config-san+ser (port (port 80) "Lorem Ipsum." (sanitizer sanitize-port) (serializer (lambda _ "foo")))) (test-equal "default value, sanitizer, permutation" 80 (config-san+empty-ser-port (config-san+empty-ser))) (test-equal "default value, serializer, permutation" "foo" (eval-gexp (serialize-configuration (config-san+ser) config-san+ser-fields))) (test-equal "string value sanitized to number, permutation" 56 (config-san+ser-port (config-san+ser (port "56")))) ;; Ordering tests. (define-configuration config-ser+san (port (port 80) "Lorem Ipsum." (sanitizer sanitize-port) (serializer (lambda _ "foo")))) (define-configuration config-empty-ser+san (port (port 80) "Lorem Ipsum." empty-serializer (sanitizer sanitize-port))) (test-equal "default value, sanitizer, permutation 2" 56 (config-empty-ser+san-port (config-empty-ser+san (port "56")))) (test-equal "default value, serializer, permutation 2" "foo" (eval-gexp (serialize-configuration (config-ser+san) config-ser+san-fields)))) (test-group "duplicated/conflicting entries" (test-error "duplicate sanitizer" #t (macroexpand '(define-configuration dupe-san (foo (list '()) "Lorem Ipsum." (sanitizer (lambda () #t)) (sanitizer (lambda () #t)))))) (test-error "duplicate serializer" #t (macroexpand '(define-configuration dupe-ser (foo (list '()) "Lorem Ipsum." (serializer (lambda _ "")) (serializer (lambda _ "")))))) (test-error "conflicting use of serializer + empty-serializer" #t (macroexpand '(define-configuration ser+empty-ser (foo (list '()) "Lorem Ipsum." (serializer (lambda _ "lorem")) empty-serializer))))) (test-group "Mix of deprecated and new syntax" (test-error "Mix of bare serializer and new syntax" #t (macroexpand '(define-configuration mixed (foo (list '()) "Lorem Ipsum." (sanitizer (lambda () #t)) (lambda _ "lorem"))))) (test-error "Mix of bare serializer and new syntax, permutation)" #t (macroexpand '(define-configuration mixed (foo (list '()) "Lorem Ipsum." (lambda _ "lorem") (sanitizer (lambda () #t))))))) ;;; ;;; define-maybe macro. ;;; (define-maybe number) (define-configuration config-with-maybe-number (port (maybe-number 80) "") (count maybe-number "")) (test-equal "maybe value serialization" "port=80" (serialize-maybe-number "port" 80)) (define (config-with-maybe-number->string x) (eval (gexp->approximate-sexp (serialize-configuration x config-with-maybe-number-fields)) (current-module))) (test-equal "maybe value serialization of the instance" "port=42count=43" (config-with-maybe-number->string (config-with-maybe-number (port 42) (count 43)))) (test-equal "maybe value serialization of the instance, unspecified" "port=42" (config-with-maybe-number->string (config-with-maybe-number (port 42)))) (define (serialize-symbol name value) (format #f "~a=~a~%" name value)) (define-maybe symbol) (define-configuration config-with-maybe-symbol (protocol maybe-symbol "")) (test-equal "symbol maybe value serialization, unspecified" "" (eval-gexp (serialize-configuration (config-with-maybe-symbol) config-with-maybe-symbol-fields))) (define-maybe/no-serialization string) (define-configuration config-with-maybe-string/no-serialization (name (maybe-string) "The name of the item.") (no-serialization)) (test-assert "maybe value without serialization no procedure bound" (not (defined? 'serialize-maybe-string))) (test-assert "maybe type, no default" (eq? %unset-value (config-with-maybe-string/no-serialization-name (config-with-maybe-string/no-serialization)))) (test-assert "maybe type, with default" (equal? "foo" (config-with-maybe-string/no-serialization-name (config-with-maybe-string/no-serialization (name "foo")))))