cli: make bool parsing case-sensitive
Completes the boolean fix: only literal "true"/"false" are accepted, with no case folding, so "True"/"TRUE" are rejected. Drop strings.ToLower. Keep TrimSpace so a config value with surrounding whitespace still parses; only the case folding was invented leniency.
This commit is contained in:
@@ -38,8 +38,11 @@ var boolWords = map[string]bool{
|
||||
}
|
||||
|
||||
// boolWord reports a value's truth and whether it is a recognised boolean word.
|
||||
// Whitespace is trimmed (aria2 strips config values too), but case is
|
||||
// significant: aria2 rejects "True"/"TRUE", so the match is against exactly
|
||||
// "true"/"false".
|
||||
func boolWord(s string) (val, ok bool) {
|
||||
val, ok = boolWords[strings.ToLower(strings.TrimSpace(s))]
|
||||
val, ok = boolWords[strings.TrimSpace(s)]
|
||||
return val, ok
|
||||
}
|
||||
|
||||
|
||||
@@ -139,14 +139,16 @@ func TestValidateFloatNonNegative(t *testing.T) {
|
||||
|
||||
func TestValidateBool(t *testing.T) {
|
||||
b := byLong["enable-dht"]
|
||||
// aria2 accepts only true/false; the invented yes/no/1/0/on/off spellings
|
||||
// were removed, so they must now fail validation.
|
||||
for _, ok := range []string{"true", "false"} {
|
||||
// aria2 accepts only literal true/false (case-sensitive) but trims
|
||||
// surrounding whitespace; match that exactly.
|
||||
for _, ok := range []string{"true", "false", " true ", "false\t"} {
|
||||
if err := validate(b, ok); err != nil {
|
||||
t.Errorf("validate bool %q: %v", ok, err)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"yes", "no", "1", "0", "on", "off", "flase"} {
|
||||
// The invented spellings and any case variant must now fail.
|
||||
for _, bad := range []string{"yes", "no", "1", "0", "on", "off", "flase",
|
||||
"True", "TRUE", "tRuE", "False", "FALSE"} {
|
||||
if err := validate(b, bad); err == nil {
|
||||
t.Errorf("validate bool %q: want error (only true/false accepted)", bad)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user