fix: tests

This commit is contained in:
David Arnold 2020-06-10 01:40:13 -05:00
parent 5e1200ef9b
commit 47d6fbf381
No known key found for this signature in database
GPG Key ID: 6D6A936E69C59D08
3 changed files with 68 additions and 51 deletions

View File

@ -2,4 +2,4 @@ linters:
enable-all: true enable-all: true
output: output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: code-climate format: tab

106
setup.go
View File

@ -1,6 +1,7 @@
package ldap package ldap
import ( import (
"fmt"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -96,105 +97,118 @@ func ParseStanza(c *caddy.Controller) (*Ldap, error) {
ldap.Upstream = upstream.New() ldap.Upstream = upstream.New()
for c.NextBlock() { for c.NextBlock() {
fmt.Printf("111 %#v\n", c.Val())
switch c.Val() { switch c.Val() {
// RFC 4516 URL // RFC 4516 URL
case "ldap_url": case "ldap_url":
c.NextArg() if !c.NextArg() {
ldap.ldapURL = c.Val()
continue
case "paging_limit":
c.NextArg()
pagingLimit, err := strconv.ParseUint(c.Val(), 10, 0)
if err != nil {
return nil, c.ArgErr() return nil, c.ArgErr()
} }
ldap.ldapURL = c.Val()
case "paging_limit":
if !c.NextArg() {
return nil, c.ArgErr()
}
pagingLimit, err := strconv.ParseUint(c.Val(), 10, 0)
if err != nil {
return nil, c.Errf("paging_limit: %w", err)
}
ldap.pagingLimit = uint32(pagingLimit) ldap.pagingLimit = uint32(pagingLimit)
continue
case "base_dn": case "base_dn":
c.NextArg() // ou=ae-dir if !c.NextArg() {
ldap.searchRequest.BaseDN = c.Val() return nil, c.ArgErr()
continue }
ldap.searchRequest.BaseDN = c.Val() // ou=ae-dir
case "filter": case "filter":
c.NextArg() // (objectClass=aeNwDevice) if !c.NextArg() {
ldap.searchRequest.Filter = c.Val() return nil, c.ArgErr()
continue }
ldap.searchRequest.Filter = c.Val() // (objectClass=aeNwDevice)
case "attributes": case "attributes":
c.Next()
for c.NextBlock() { for c.NextBlock() {
switch c.Val() { switch c.Val() {
case "fqdn": case "fqdn":
c.NextArg() // aeFqdn if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.searchRequest.Attributes = append(ldap.searchRequest.Attributes, c.Val()) ldap.searchRequest.Attributes = append(ldap.searchRequest.Attributes, c.Val())
ldap.fqdnAttr = c.Val() ldap.fqdnAttr = c.Val() // aeFqdn
continue
case "ip4": case "ip4":
c.NextArg() // ipHostNumber if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.searchRequest.Attributes = append(ldap.searchRequest.Attributes, c.Val()) ldap.searchRequest.Attributes = append(ldap.searchRequest.Attributes, c.Val())
ldap.ip4Attr = c.Val() ldap.ip4Attr = c.Val() // ipHostNumber
continue
default: default:
return nil, c.Errf("unknown attributes property '%s'", c.Val()) return nil, c.Errf("unknown attributes property '%s'", c.Val())
} }
} }
continue continue
case "username": case "username":
c.NextArg() if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.username = c.Val() ldap.username = c.Val()
continue
case "password": case "password":
c.NextArg() if !c.NextArg() {
return nil, c.ArgErr()
}
ldap.password = c.Val() ldap.password = c.Val()
continue
case "sasl": case "sasl":
c.NextArg()
ldap.sasl = true ldap.sasl = true
continue
case "ttl": case "ttl":
c.NextArg() if !c.NextArg() {
return nil, c.ArgErr()
}
ttl, err := time.ParseDuration(c.Val()) ttl, err := time.ParseDuration(c.Val())
if err != nil { if err != nil {
return nil, c.ArgErr() return nil, c.Errf("ttl: %w", err)
} }
ldap.ttl = ttl ldap.ttl = ttl
continue
case "sync_interval": case "sync_interval":
c.NextArg() if !c.NextArg() {
syncInterval, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.ArgErr() return nil, c.ArgErr()
} }
syncInterval, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Errf("sync_interval: %w", err)
}
ldap.syncInterval = syncInterval ldap.syncInterval = syncInterval
continue
case "fallthrough": case "fallthrough":
ldap.Fall.SetZonesFromArgs(c.RemainingArgs()) ldap.Fall.SetZonesFromArgs(c.RemainingArgs())
continue
default: default:
return nil, c.Errf("unknown property '%s'", c.Val()) return nil, c.Errf("unknown property '%s'", c.Val())
} }
} }
// validate non-default ldap values ... // validate non-default ldap values ...
if ldap.ldapURL == "" || &ldap.ldapURL == nil { if ldap.ldapURL == "" {
return nil, c.ArgErr() return nil, c.Err("ldap_url cannot be empty")
} }
if ldap.searchRequest.BaseDN == "" { if ldap.searchRequest.BaseDN == "" {
return nil, c.ArgErr() return nil, c.Err("base_dn cannot be empty")
} }
if ldap.searchRequest.Filter == "" { if ldap.searchRequest.Filter == "" {
return nil, c.ArgErr() return nil, c.Err("filter cannot be empty")
} }
if len(ldap.searchRequest.Attributes) != 2 { if ldap.fqdnAttr == "" {
return nil, c.ArgErr() return nil, c.Err("fqdn attribute cannot be empty")
}
if ldap.ip4Attr == "" {
return nil, c.Err("ip4 attribute cannot be empty")
} }
// if only one of password and username set // if only one of password and username set
if (&ldap.username == nil) != (&ldap.password == nil) { if (ldap.username == "") != (ldap.password == "") {
return nil, c.ArgErr() return nil, c.Err("if not using sasl, both, username and password must be set")
} }
// if both username/password and sasl are set // if both username/password and sasl are set
if &ldap.username != nil && &ldap.sasl != nil { if ldap.username != "" && ldap.sasl == true {
return nil, c.ArgErr() fmt.Printf("666 %#v\t%#v", ldap.username, ldap.sasl)
return nil, c.Err("cannot use sasl and username based authentication at the same time")
} }
// if neither username/password nor sasl are set // if neither username/password nor sasl are set
if &ldap.username == nil && &ldap.sasl == nil { if ldap.username == "" && ldap.sasl == false {
return nil, c.ArgErr() return nil, c.Err("authenticate either via username/pwassword or sasl")
} }
return ldap, nil return ldap, nil

View File

@ -13,20 +13,23 @@ func TestSetup(t *testing.T) {
body string body string
expectedError bool expectedError bool
}{ }{
{`ldap`, false}, {`ldap`, true},
{`ldap :`, true}, {`ldap :`, true},
{`ldap { {`ldap {
ldap_url ldap://example.com ldap_url ldap://example.com
base_dn ou=ae-dir base_dn ou=ae-dir
filter (objectClass=aeNwDevice) filter (objectClass=aeNwDevice)
attributes aeFqdn ipHostNumber
sasl sasl
attributes {
fqdn aeFqdn
ip4 ipHostNumber
}
}`, false}, }`, false},
} }
for i, test := range tests { for i, test := range tests {
c := caddy.NewTestController("dns", test.body) c := caddy.NewTestController("dns", test.body)
if _, err := ldapParse(c); (err == nil) == test.expectedError { if _, err := ParseStanza(c); (err == nil) == test.expectedError {
t.Fatalf("Unexpected errors: %v in test: %d\n\t%s", err, i, test.body) t.Fatalf("Unexpected errors in test %d: %v\n%s", i, err, test.body)
} }
} }
} }