458 lines
9.3 KiB
Go
458 lines
9.3 KiB
Go
package main
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func TestPrepareConfig(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
users []User
|
|
rrconfigs []RRConfig
|
|
expSuccess bool
|
|
expUsers map[string]*User
|
|
expRrconfigs map[string]*RRConfig
|
|
}{
|
|
{
|
|
"empty config",
|
|
nil,
|
|
nil,
|
|
false,
|
|
map[string]*User{},
|
|
map[string]*RRConfig{},
|
|
},
|
|
{
|
|
"simple config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
true,
|
|
map[string]*User{
|
|
"user": &User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
records: map[string]bool{
|
|
"record.example.org": true,
|
|
},
|
|
},
|
|
},
|
|
map[string]*RRConfig{
|
|
"record.example.org": &RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"more complex config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
"record2.example.org",
|
|
},
|
|
},
|
|
User{
|
|
Username: "user2",
|
|
Password: "pass2",
|
|
Records: []string{
|
|
"record.example.com",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
RRConfig{
|
|
Recordname: "record2.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-other-secret-key",
|
|
},
|
|
RRConfig{
|
|
Recordname: "record.example.com",
|
|
Zonename: "zone.example.com",
|
|
Nameserver: "ns1.example.com",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "one-more-secret-key",
|
|
},
|
|
},
|
|
true,
|
|
map[string]*User{
|
|
"user": &User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
"record2.example.org",
|
|
},
|
|
records: map[string]bool{
|
|
"record.example.org": true,
|
|
"record2.example.org": true,
|
|
},
|
|
},
|
|
"user2": &User{
|
|
Username: "user2",
|
|
Password: "pass2",
|
|
Records: []string{
|
|
"record.example.com",
|
|
},
|
|
records: map[string]bool{
|
|
"record.example.com": true,
|
|
},
|
|
},
|
|
},
|
|
map[string]*RRConfig{
|
|
"record.example.org": &RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
"record2.example.org": &RRConfig{
|
|
Recordname: "record2.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-other-secret-key",
|
|
},
|
|
"record.example.com": &RRConfig{
|
|
Recordname: "record.example.com",
|
|
Zonename: "zone.example.com",
|
|
Nameserver: "ns1.example.com",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "one-more-secret-key",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"no users config",
|
|
nil,
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"no records config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
},
|
|
nil,
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"duplicate user",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
User{
|
|
Username: "user",
|
|
Password: "pass2",
|
|
Records: []string{
|
|
"record2.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"duplicate record",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"missing user config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
RRConfig{
|
|
Recordname: "record2.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"missing record/one user config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
"record2.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"missing record/multiple users config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
User{
|
|
Username: "user2",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record2.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"record reuse config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
User{
|
|
Username: "user2",
|
|
Password: "pass2",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
{
|
|
"user without records config",
|
|
[]User{
|
|
User{
|
|
Username: "user",
|
|
Password: "pass",
|
|
Records: []string{},
|
|
},
|
|
User{
|
|
Username: "user2",
|
|
Password: "pass2",
|
|
Records: []string{
|
|
"record.example.org",
|
|
},
|
|
},
|
|
},
|
|
[]RRConfig{
|
|
RRConfig{
|
|
Recordname: "record.example.org",
|
|
Zonename: "zone.example.org",
|
|
Nameserver: "ns1.example.org",
|
|
Tsigalgo: "hmac-sha256",
|
|
Tsigid: "tsig-id",
|
|
Tsigkey: "some-secret-key",
|
|
},
|
|
},
|
|
false,
|
|
nil,
|
|
nil,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
cfg := Config{
|
|
ServerPort: 1234,
|
|
Users: tc.users,
|
|
RRConfigs: tc.rrconfigs,
|
|
}
|
|
res, err := prepareConfig(&cfg)
|
|
|
|
// check for error
|
|
if tc.expSuccess && err != nil {
|
|
// prepareConfig failed even though it should not have
|
|
t.Errorf("%s: err == nil: %v, expected: %v (err was: %v)", tc.name,
|
|
err == nil, tc.expSuccess, err)
|
|
} else if !tc.expSuccess && err == nil {
|
|
// prepareConfig succeeded even though it should have failed
|
|
t.Errorf("%s: err == nil: %v, expected: %v", tc.name,
|
|
err == nil, tc.expSuccess)
|
|
}
|
|
|
|
// check whether result is valid on success
|
|
if err == nil && res == nil {
|
|
t.Errorf("%s: res is nil even though it should not be", tc.name)
|
|
}
|
|
|
|
// try to compare res with expected values only if res is not nil
|
|
if res != nil {
|
|
if !reflect.DeepEqual(tc.expUsers, res.users) {
|
|
t.Errorf("%s: cfg.users: %s", tc.name,
|
|
cmp.Diff(tc.expUsers, res.users, cmp.AllowUnexported(User{})))
|
|
}
|
|
if !reflect.DeepEqual(tc.expRrconfigs, res.rrconfigs) {
|
|
t.Errorf("%s: cfg.rrconfigs: %s", tc.name,
|
|
cmp.Diff(tc.expRrconfigs, res.rrconfigs))
|
|
}
|
|
}
|
|
}
|
|
}
|