3rdparties/software/cl+ssl-20200427-git/test/verify-hostname.lisp (3800 bytes)
1 ;;;; -*- Mode: LISP; Syntax: COMMON-LISP; indent-tabs-mode: nil; coding: utf-8; show-trailing-whitespace: t -*- 2 3 (in-package :cl+ssl.test) 4 5 (def-suite :cl+ssl.verify-hostname :in :cl+ssl 6 :description "Hostname verification tests") 7 8 (in-suite :cl+ssl.verify-hostname) 9 10 (test veriy-hostname-success 11 ;; presented identifier, reference identifier, validation and parsing result 12 (let ((tests '(("www.example.com" "WWW.eXamPle.CoM" (nil)) ;; case insensitive match 13 ("www.example.com." "www.example.com" (nil)) ;; ignore trailing dots (prevenet *.com. matches) 14 ("www.example.com" "www.example.com." (nil)) 15 ("*.example.com" "www.example.com" (t "" ".example.com" t)) 16 ("b*z.example.com" "buzz.example.com" (t "b" "z.example.com" nil)) 17 ("*baz.example.com" "foobaz.example.com" (t "" "baz.example.com" nil)) 18 ("baz*.example.com" "baz1.example.com" (t "baz" ".example.com" nil))))) 19 (loop for (i r v) in tests do 20 (is (equalp (multiple-value-list (cl+ssl::validate-and-parse-wildcard-identifier i r)) v)) 21 (is (cl+ssl::try-match-hostname i r))))) 22 23 (test verify-hostname-fail 24 (let ((tests '(("*.com" "eXamPle.CoM") 25 (".com." "example.com.") 26 ("*.www.example.com" "www.example.com.") 27 ("foo.*.example.com" "foo.bar.example.com.") 28 ("xn--*.example.com" "xn-foobar.example.com") 29 ("*fooxn--bar.example.com" "bazfooxn--bar.example.com") 30 ("*.akamaized.net" "tv.eurosport.com") 31 ("a*c.example.com" "abcd.example.com") 32 ("*baz.example.com" "foobuzz.example.com")))) 33 (loop for (i r) in tests do 34 (is-false (cl+ssl::try-match-hostname i r))))) 35 36 (defun full-cert-path (name) 37 (merge-pathnames (concatenate 'string 38 "test/certs/" 39 name) 40 (asdf:component-pathname (asdf:find-system :cl+ssl.test)))) 41 42 (defun load-cert(name) 43 (let ((full-path (full-cert-path name))) 44 (unless (probe-file full-path) 45 (error "Unable to find certificate ~a~%Full path: ~a" name full-path)) 46 (cl+ssl:decode-certificate-from-file full-path))) 47 48 (defmacro with-cert ((name var) &body body) 49 `(let* ((,var (load-cert ,name))) 50 (when (cffi:null-pointer-p ,var) 51 (error "Unable to load certificate: ~a" ,name)) 52 (unwind-protect 53 (progn ,@body) 54 (cl+ssl::x509-free ,var)))) 55 56 (test verify-google-cert 57 (with-cert ("google.der" cert) 58 (is-true (cl+ssl:verify-hostname cert 59 "qwe.fr.doubleclick.net")))) 60 61 (test verify-google-cert-dns-wildcard 62 (with-cert ("google_wildcard.der" cert) 63 (is-true (cl+ssl:verify-hostname cert 64 "www.google.co.uk")))) 65 66 (test verify-google-cert-without-dns 67 (with-cert ("google_nodns.der" cert) 68 (is-true (cl+ssl:verify-hostname cert 69 "www.google.co.uk")))) 70 71 (test verify-google-cert-printable-string 72 (with-cert ("google_printable.der" cert) 73 (is-true (cl+ssl:verify-hostname cert 74 "www.google.co.uk")))) 75 76 (test verify-google-cert-teletex-string 77 (with-cert ("google_teletex.der" cert) 78 (is-true (cl+ssl:verify-hostname cert 79 "www.google.co.uk")))) 80 81 (test verify-google-cert-bmp-string 82 (with-cert ("google_bmp.der" cert) 83 (is-true (cl+ssl:verify-hostname cert 84 "google.co.uk")))) 85 86 (test verify-google-cert-universal-string 87 (with-cert ("google_universal.der" cert) 88 (is-true (cl+ssl:verify-hostname cert 89 "google.co.uk"))))