119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const validWG = `[Interface]
|
|
PrivateKey = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
|
Address = 10.8.0.2/32
|
|
DNS = 1.1.1.1
|
|
[Peer]
|
|
PublicKey = AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=
|
|
AllowedIPs = 0.0.0.0/0
|
|
Endpoint = [2001:db8::1]:51820
|
|
`
|
|
|
|
func TestConfigAndRewrite(t *testing.T) {
|
|
w, e := parseWG(validWG)
|
|
if e != nil || w.Host != "2001:db8::1" || w.Port != 51820 {
|
|
t.Fatalf("%+v %v", w, e)
|
|
}
|
|
p := Profile{Name: "home", WireGuard: validWG, Tunnel: Tunnel{Enabled: true, Port: 443}}
|
|
if e = validateProfile(p); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
s := renderWG(p, "127.0.0.1:1234")
|
|
if strings.Contains(s, "DNS =") || !strings.Contains(s, "Endpoint = 127.0.0.1:1234") {
|
|
t.Fatal(s)
|
|
}
|
|
if !strings.Contains(p.WireGuard, "[2001:db8::1]") {
|
|
t.Fatal("original modified")
|
|
}
|
|
}
|
|
func TestRejectHooksAndBadConfig(t *testing.T) {
|
|
for _, s := range []string{"", strings.Replace(validWG, "PrivateKey = ", "PrivateKey = BAD", 1), validWG + "PostUp = rm -rf /\n", strings.Replace(validWG, "51820", "0", 1)} {
|
|
if _, e := parseWG(s); e == nil {
|
|
t.Fatalf("accepted %q", s)
|
|
}
|
|
}
|
|
}
|
|
func TestSettingsRoundTrip(t *testing.T) {
|
|
d := t.TempDir()
|
|
s := Settings{Profiles: []Profile{{ID: "home", Name: "дом", WireGuard: validWG}}, Last: "home"}
|
|
if e := saveSettings(d, s); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
s, e := loadSettings(d)
|
|
if e != nil || len(s.Profiles) != 1 || s.Last != "home" {
|
|
t.Fatalf("%+v %v", s, e)
|
|
}
|
|
if _, e = s.find("1"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.find("дом"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.find("2"); e == nil {
|
|
t.Fatal("unknown config accepted")
|
|
}
|
|
st, _ := os.Stat(filepath.Join(d, "settings.json"))
|
|
if st.Mode().Perm() != 0600 {
|
|
t.Fatal(st.Mode())
|
|
}
|
|
}
|
|
func TestLossRollingHundred(t *testing.T) {
|
|
var w lossWindow
|
|
if got := w.add(false); got != 100 {
|
|
t.Fatal(got)
|
|
}
|
|
for i := 0; i < 99; i++ {
|
|
w.add(true)
|
|
}
|
|
if got := w.add(true); got != 0 {
|
|
t.Fatal(got)
|
|
}
|
|
for i := 0; i < 25; i++ {
|
|
w.add(false)
|
|
}
|
|
if got := w.add(true); got != 25 {
|
|
t.Fatal(got)
|
|
}
|
|
for i := 0; i < 100; i++ {
|
|
w.add(true)
|
|
}
|
|
if w.lost != 0 || w.count != 100 {
|
|
t.Fatalf("%+v", w)
|
|
}
|
|
}
|
|
func TestEditorArguments(t *testing.T) {
|
|
a, e := editorArgs(`"/some path/editor" --wait`)
|
|
if e != nil || len(a) != 2 || a[0] != "/some path/editor" {
|
|
t.Fatalf("%q %v", a, e)
|
|
}
|
|
if _, e = editorArgs(`"unfinished`); e == nil {
|
|
t.Fatal("unclosed quote accepted")
|
|
}
|
|
}
|
|
func TestProfileDuplicateAndDeleteLast(t *testing.T) {
|
|
d := t.TempDir()
|
|
a := App{dir: d, settings: Settings{Profiles: []Profile{}}}
|
|
p := Profile{Name: "home", WireGuard: validWG}
|
|
if e := a.save(p); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := a.save(p); e == nil {
|
|
t.Fatal("duplicate accepted")
|
|
}
|
|
id := a.settings.Profiles[0].ID
|
|
a.settings.Last = id
|
|
if e := a.remove(id); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if a.settings.Last != "" || len(a.settings.Profiles) != 0 {
|
|
t.Fatal(a.settings)
|
|
}
|
|
}
|