diff options
-rw-r--r-- | src/package.lisp | 1 | ||||
-rw-r--r-- | src/util.lisp | 11 | ||||
-rw-r--r-- | tests/util.lisp | 30 |
3 files changed, 42 insertions, 0 deletions
diff --git a/src/package.lisp b/src/package.lisp index 6197c12..4d27970 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -122,6 +122,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 63ca21a..d1658d3 100644 --- a/src/util.lisp +++ b/src/util.lisp @@ -399,6 +399,17 @@ expansion as a starting point for your own DEFPACKAGE form for your consfig." (with-standard-io-syntax (write object :stream fifo) (terpri fifo) (finish-output fifo))) +(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)))) + ;;;; Progress & debug printing 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) |