Files
caddy/modules/caddyhttp/errors_randstring_test.go
T
5493791423 caddyhttp: fix randString sameCase dictionary to match its docs (#7972)
* caddyhttp: fix randString sameCase dictionary to match its docs

The doc comment says randString excludes confusing characters like
I, l, 1, 0, O. When sameCase is true, uppercase letters and the
characters l and o should be excluded. But the sameCase dictionary
still contained '0'. Drop it.

Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>

* Update modules/caddyhttp/errors.go

Signed-off-by: Matt Holt <mholt@users.noreply.github.com>

* elaborate the doc, and expand tests

Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>

* Update modules/caddyhttp/errors_randstring_test.go

Co-authored-by: Zen Dodd <mail@steadytao.com>
Signed-off-by: Mohammed Al Sahaf <mohammed@caffeinatedwonders.com>

---------

Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
Signed-off-by: Matt Holt <mholt@users.noreply.github.com>
Signed-off-by: Mohammed Al Sahaf <mohammed@caffeinatedwonders.com>
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Co-authored-by: Zen Dodd <mail@steadytao.com>
2026-09-25 16:01:50 +00:00

74 lines
2.9 KiB
Go

// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package caddyhttp
import (
"strings"
"testing"
)
// TestRandString_NotSameCaseExcludedChars is the regression test for the
// randString(sameCase=false) dictionary bug where excluded was still emitted
// despite being called out as a confusing character in the doc comment.
func TestRandString_NotSameCaseExcludedChars(t *testing.T) {
// A sample of 5000 characters makes a leaked excluded character extremely unlikely
// to go unnoticed: the dictionary is 55 characters, so the expected
// count for any single character would be ~90 if it were present.
s := randString(5000, false)
if strings.ContainsRune(s, 'l') {
t.Errorf("randString(n, sameCase=false) must not emit 'l'; got %q", s)
}
if strings.ContainsRune(s, 'I') {
t.Errorf("randString(n, sameCase=false) must not emit 'I'; got %q", s)
}
if strings.ContainsRune(s, 'O') {
t.Errorf("randString(n, sameCase=false) must not emit 'O'; got %q", s)
}
if strings.ContainsRune(s, 'S') {
t.Errorf("randString(n, sameCase=false) must not emit 'S'; got %q", s)
}
if strings.ContainsRune(s, '0') {
t.Errorf("randString(n, sameCase=false) must not emit '0'; got %q", s)
}
if strings.ContainsRune(s, '1') {
t.Errorf("randString(n, sameCase=false) must not emit '1'; got %q", s)
}
}
// TestRandString_SameCaseExcludedChars is the regression test for the
// randString(sameCase=true) dictionary bug where excluded was still emitted
// despite being called out as a confusing character in the doc comment.
func TestRandString_SameCaseExcludedChars(t *testing.T) {
// A sample of 5000 characters makes a leaked excluded character extremely unlikely
// to go unnoticed: the dictionary is 32 characters, so the expected
// count for any single character would be ~156 if it were present.
s := randString(5000, true)
if strings.ToLower(s) != s {
t.Errorf("randString(n, sameCase=true) must not emit uppercase letters; got %q", s)
}
if strings.ContainsRune(s, 'l') {
t.Errorf("randString(n, sameCase=true) must not emit 'l'; got %q", s)
}
if strings.ContainsRune(s, 'o') {
t.Errorf("randString(n, sameCase=true) must not emit 'o': got %q", s)
}
if strings.ContainsRune(s, '0') {
t.Errorf("randString(n, sameCase=true) must not emit '0'; got %q", s)
}
if strings.ContainsRune(s, '1') {
t.Errorf("randString(n, sameCase=true) must not emit '1'; got %q", s)
}
}