mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 11:46:09 +01:00
68 lines
2.5 KiB
Go
68 lines
2.5 KiB
Go
package configfile
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/h44z/wg-portal/internal/config"
|
|
"github.com/h44z/wg-portal/internal/domain"
|
|
)
|
|
|
|
type amneziaConfigRepo struct {
|
|
iface domain.Interface
|
|
peer domain.Peer
|
|
}
|
|
|
|
func (r *amneziaConfigRepo) GetInterface(context.Context, domain.InterfaceIdentifier) (*domain.Interface, error) {
|
|
return &r.iface, nil
|
|
}
|
|
func (r *amneziaConfigRepo) GetPeer(context.Context, domain.PeerIdentifier) (*domain.Peer, error) {
|
|
peer := r.peer
|
|
return &peer, nil
|
|
}
|
|
func (r *amneziaConfigRepo) GetInterfaceAndPeers(context.Context, domain.InterfaceIdentifier) (*domain.Interface, []domain.Peer, error) {
|
|
return &r.iface, []domain.Peer{r.peer}, nil
|
|
}
|
|
|
|
func TestAmneziaWGConfigExports(t *testing.T) {
|
|
tpl, err := newTemplateHandler()
|
|
require.NoError(t, err)
|
|
repo := &amneziaConfigRepo{
|
|
iface: domain.Interface{Identifier: "awg0", Backend: config.AmneziaWGBackendName, PrivateKey: "private",
|
|
AmneziaWG: domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4", "I1": "<b 0x1234>"}},
|
|
peer: domain.Peer{Identifier: "peer", InterfaceIdentifier: "awg0", Interface: domain.PeerInterfaceConfig{PrivateKey: "private"}},
|
|
}
|
|
manager := Manager{wg: repo, tplHandler: tpl}
|
|
ctx := domain.SetUserInfo(t.Context(), domain.SystemAdminContextUserInfo())
|
|
read := func(reader io.Reader, err error) string {
|
|
t.Helper()
|
|
require.NoError(t, err)
|
|
data, err := io.ReadAll(reader)
|
|
require.NoError(t, err)
|
|
return string(data)
|
|
}
|
|
const options = "PrivateKey = private\nH1 = 100-200\nI1 = <b 0x1234>\nJc = 4\n"
|
|
server := read(manager.GetInterfaceConfig(ctx, "awg0"))
|
|
require.Contains(t, server, options)
|
|
require.Contains(t, server, "\nAddress = ")
|
|
for _, style := range []string{domain.ConfigStyleWgQuick, domain.ConfigStyleRaw} {
|
|
client := read(manager.GetPeerConfig(ctx, "peer", style))
|
|
beforePeers, _, found := strings.Cut(client, "[Peer]")
|
|
require.True(t, found)
|
|
require.Contains(t, beforePeers, options)
|
|
}
|
|
firstQR := read(manager.GetPeerConfigQrCode(ctx, "peer", domain.ConfigStyleWgQuick))
|
|
require.NotEmpty(t, firstQR)
|
|
repo.iface.AmneziaWG["H1"] = "300-400"
|
|
require.Contains(t, read(manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick)), "H1 = 300-400")
|
|
require.NotEqual(t, firstQR, read(manager.GetPeerConfigQrCode(ctx, "peer", domain.ConfigStyleWgQuick)))
|
|
repo.iface.AmneziaWG = nil
|
|
client := read(manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick))
|
|
require.Contains(t, client, "PrivateKey = private\nAddress =")
|
|
require.NotContains(t, client, "H1 =")
|
|
}
|