mirror of
https://github.com/profullstack/agentbbs.git
synced 2026-08-13 14:27:27 +00:00
fix(config): reject non-positive integer settings (#102)
This commit is contained in:
parent
1849a25f10
commit
20cdb6432d
2 changed files with 18 additions and 2 deletions
|
|
@ -99,10 +99,10 @@ func env(k, def string) string {
|
|||
return def
|
||||
}
|
||||
|
||||
// envInt reads an integer environment variable, falling back to def.
|
||||
// envInt reads a positive integer environment variable, falling back to def.
|
||||
func envInt(k string, def int) int {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil {
|
||||
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
||||
return n
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,22 @@ package main
|
|||
|
||||
import "testing"
|
||||
|
||||
func TestEnvIntRequiresPositiveValue(t *testing.T) {
|
||||
const key = "AGENTBBS_TEST_POSITIVE_INT"
|
||||
|
||||
for _, value := range []string{"", "invalid", "0", "-1"} {
|
||||
t.Setenv(key, value)
|
||||
if got := envInt(key, 15); got != 15 {
|
||||
t.Errorf("envInt(%q, 15) with %q = %d, want 15", key, value, got)
|
||||
}
|
||||
}
|
||||
|
||||
t.Setenv(key, "30")
|
||||
if got := envInt(key, 15); got != 30 {
|
||||
t.Errorf("envInt(%q, 15) = %d, want 30", key, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidIRCServerPortRange(t *testing.T) {
|
||||
for _, server := range []string{
|
||||
"irc.example.com",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue