package wireguard
import (
"bytes"
"io"
"reflect"
"testing"
"github.com/h44z/wg-portal/internal/persistence"
"github.com/stretchr/testify/assert"
)
func TestNewTemplateHandler(t *testing.T) {
got, err := newTemplateHandler()
assert.NoError(t, err)
assert.NotNil(t, got)
}
func TestTemplateHandler_GetInterfaceConfig(t *testing.T) {
type args struct {
cfg *persistence.InterfaceConfig
peers []*persistence.PeerConfig
}
tests := []struct {
name string
args args
want io.Reader
wantErr bool
}{
{
name: "All Empty",
args: args{
cfg: &persistence.InterfaceConfig{Identifier: "test0"},
},
want: bytes.NewBuffer([]byte(`# AUTOGENERATED FILE - DO NOT EDIT
# This file uses wg-quick format. See https://man7.org/linux/man-pages/man8/wg-quick.8.html#CONFIGURATION
# -WGP- WIREGUARD PORTAL CONFIGURATION FILE, version unknown
# Lines starting with the -WGP- tag are used by the WireGuard Portal configuration parser.
[Interface]
# -WGP- Interface: test0 | Updated: 0001-01-01 00:00:00 +0000 UTC | Created: 0001-01-01 00:00:00 +0000 UTC
# -WGP- Display name:
# -WGP- Interface mode:
# -WGP- PublicKey =
# Core settings
PrivateKey =
Address =
# Misc. settings (optional)
# Interface hooks (optional)
#
# Peers
#
`)),
wantErr: false,
},
}
c, _ := newTemplateHandler()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := c.GetInterfaceConfig(tt.args.cfg, tt.args.peers)
if (err != nil) != tt.wantErr {
t.Errorf("GetInterfaceConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetInterfaceConfig() got = %v, want %v", got, tt.want)
}
})
}
}
func TestTemplateHandler_GetPeerConfig(t *testing.T) {
type args struct {
peer *persistence.PeerConfig
}
tests := []struct {
name string
args args
want io.Reader
wantErr bool
}{
{
name: "All empty",
args: args{
peer: &persistence.PeerConfig{Identifier: "peer0", Interface: &persistence.PeerInterfaceConfig{}},
},
want: bytes.NewBuffer([]byte(`# AUTOGENERATED FILE - DO NOT EDIT
# This file uses wg-quick format. See https://man7.org/linux/man-pages/man8/wg-quick.8.html#CONFIGURATION
# -WGP- WIREGUARD PORTAL CONFIGURATION FILE, version unknown
# Lines starting with the -WGP- tag are used by the WireGuard Portal configuration parser.
[Interface]
# -WGP- Peer: peer0 | Updated: 0001-01-01 00:00:00 +0000 UTC | Created: 0001-01-01 00:00:00 +0000 UTC
# -WGP- Display name:
# -WGP- PublicKey:
# -WGP- Peer type: server
# Core settings
PrivateKey =
Address =
# Misc. settings (optional)
# Interface hooks (optional)
[Peer]
PublicKey =
Endpoint = `)),
wantErr: false,
},
}
c, _ := newTemplateHandler()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := c.GetPeerConfig(tt.args.peer)
if (err != nil) != tt.wantErr {
t.Errorf("GetPeerConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetPeerConfig() got = %v, want %v", got, tt.want)
}
})
}
}