[PATCH consfigurator v3 6/8] add #'valid-hostname-p

Sean Whitton spwhitton at spwhitton.name
Sun Mar 20 17:39:39 GMT 2022


Acked-by: Sean Whitton <spwhitton at spwhitton.name>

On Sun 13 Mar 2022 at 11:40am -03, David Bremner wrote:

> Initial intended application is checking data source IDEN1. This could be done
> as a one-liner with a more complex regex, but that seems harder to debug.
>
> Signed-off-by: David Bremner <david at tethera.net>
> ---
>  src/package.lisp |  1 +
>  src/util.lisp    | 14 ++++++++++++++
>  tests/util.lisp  | 30 ++++++++++++++++++++++++++++++
>  3 files changed, 45 insertions(+)
>
> diff --git a/src/package.lisp b/src/package.lisp
> index eaa1aef..a0f857d 100644
> --- a/src/package.lisp
> +++ b/src/package.lisp
> @@ -123,6 +123,7 @@
>             #:lambda-ignoring-args
>             #:parse-cidr
>             #:random-alphanumeric
> +           #:valid-hostname-p
>  
>             #:*consfigurator-debug-level*
>             #:with-indented-inform
> diff --git a/src/util.lisp b/src/util.lisp
> index 1f74ca0..5c9193c 100644
> --- a/src/util.lisp
> +++ b/src/util.lisp
> @@ -724,3 +724,17 @@ Does not currently establish a PAM session."
>      (when-let ((output-stream (stream->output-stream stream)))
>        (when (open-stream-p output-stream)
>          (funcall function output-stream)))))
> +
> +
> +;;;; Hostnames
> +
> +(defun valid-hostname-p (string)
> +  "Test whether STRING looks like a valid hostname, as defined by RFCs 952 and
> +1123."
> +  (and
> +   (<= (length string) 253)
> +   (let ((parts (split-string string :separator ".")))
> +     (every (lambda (part)
> +              (and (<= (length part) 63)
> +                   (re:scan "^[a-zA-Z0-9][a-zA-Z0-9-]*$" part)))
> +            parts))))
> diff --git a/tests/util.lisp b/tests/util.lisp
> index bffda68..fd310a7 100644
> --- a/tests/util.lisp
> +++ b/tests/util.lisp
> @@ -17,3 +17,33 @@
>  (deftest version<.4 (version< "1.a.1" "1.1") t)
>  
>  (deftest version<.5 (version< "1..1" "1.1") t)
> +
> +;; without domain
> +(deftest valid-hostname-p.1 (valid-hostname-p "localhost") t)
> +
> +(deftest valid-hostname-p.2 (valid-hostname-p "host.example.com") t)
> +
> +;; case insensitive check
> +(deftest valid-hostname-p.3 (valid-hostname-p "host.Example.Com") t)
> +
> +;; total length too long
> +(deftest valid-hostname-p.4
> +    (valid-hostname-p (format nil "~127@{a.~}a" nil))
> +  nil)
> +
> +;; label too long
> +(deftest valid-hostname-p.5
> +    (valid-hostname-p (format nil "~64@{a~}a" nil))
> +  nil)
> +
> +;; valid use of `-'
> +(deftest valid-hostname-p.6 (valid-hostname-p "host-name.example.com") t)
> +
> +;; invalid use of `-'
> +(deftest valid-hostname-p.7 (valid-hostname-p "-hostname.example.com") nil)
> +
> +;; invalid character
> +(deftest valid-hostname-p.8 (valid-hostname-p "_hostname.example.com") nil)
> +
> +;; invalid character 2
> +(deftest valid-hostname-p.9 (valid-hostname-p "foo/bar") nil)

-- 
Sean Whitton



More information about the sgo-software-discuss mailing list