Newer
Older
wg-portal / internal / wireguard / template_test.go
@Christoph Haas Christoph Haas on 9 Oct 2021 2 KB wg manager
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{},
			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:  | 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
		iface persistence.InterfaceConfig
	}
	tests := []struct {
		name    string
		args    args
		want    io.Reader
		wantErr bool
	}{
		{
			name: "All empty",
			args: args{},
			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:  | 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)
			}
		})
	}
}