From b1b95e821435225fdfa82270f79a11d92466de01 Mon Sep 17 00:00:00 2001 From: Brian Cunnie Date: Sat, 12 Jul 2025 05:50:40 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20Don't=20accidentally=20produce?= =?UTF-8?q?=20hex-notation=20hostname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have a test helper which produces a random 8-character string consisting of mixed-case alphabetic characters; however, there's a small chance (6/26^8 == 0.0008%) that it would produce a valid 8-character hexadecimal string, which could be mistakenly recognized as an IP address, which _might_ break a test. Out of an abundance of caution, I guard against producing an 8-byte string that might be accidentally recognized as 8-byte hexadecimal notation by making sure the first & last bytes are NOT hex characters. --- testhelper/helpers.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/testhelper/helpers.go b/testhelper/helpers.go index 23a6f1a..e6dc20b 100644 --- a/testhelper/helpers.go +++ b/testhelper/helpers.go @@ -28,11 +28,15 @@ func RandomIPv6Address() net.IP { } // Random8ByteString() returns an 8-char mixed-case string consisting solely of the letters a-z. +// the first & last characters are non-hexadecimal to avoid confusion with hexadecimal notation func Random8ByteString() string { var randomString []byte - for i := 0; i < 8; i++ { + // 71 == ascii 'G', +32 (103) == ascii 'g' + randomString = append(randomString, byte(71+32*rand.Intn(2)+rand.Intn(20))) + for range 6 { // 65 == ascii 'A', +32 (96) == ascii 'a', there are 26 letters in the alphabet. Mix upper case, too. randomString = append(randomString, byte(65+32*rand.Intn(2)+rand.Intn(26))) } + randomString = append(randomString, byte(71+32*rand.Intn(2)+rand.Intn(20))) return string(randomString) }