74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package awg
|
|
|
|
import "testing"
|
|
|
|
func TestParseINI_AWG20(t *testing.T) {
|
|
raw := `[Interface]
|
|
PrivateKey = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
|
Address = 10.8.0.2/32
|
|
DNS = 1.1.1.1
|
|
Jc = 4
|
|
Jmin = 40
|
|
Jmax = 70
|
|
H1 = 1
|
|
H2 = 2
|
|
H3 = 3
|
|
H4 = 4
|
|
I1 = <r 128>
|
|
|
|
[Peer]
|
|
PublicKey = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE=
|
|
Endpoint = 203.0.113.10:51820
|
|
AllowedIPs = 0.0.0.0/0
|
|
PersistentKeepalive = 25
|
|
`
|
|
c, err := Parse(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.Jc != 4 || c.I1 != "<r 128>" || c.Endpoint != "203.0.113.10:51820" {
|
|
t.Fatalf("unexpected conf: %+v", c)
|
|
}
|
|
if !c.IsAWG20() {
|
|
t.Fatal("expected AWG2 markers")
|
|
}
|
|
ipc, err := c.ToIPC()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !containsAll(ipc, "jc=4", "i1=<r 128>", "endpoint=203.0.113.10:51820") {
|
|
t.Fatalf("bad ipc: %s", ipc)
|
|
}
|
|
}
|
|
|
|
func TestDetect(t *testing.T) {
|
|
if !Detect("[Interface]\nPrivateKey = x") {
|
|
t.Fatal("expected detect")
|
|
}
|
|
if !Detect("awg://1.2.3.4:51820/?private_key=a&public_key=b&address=10.0.0.2/32") {
|
|
t.Fatal("expected awg uri detect")
|
|
}
|
|
}
|
|
|
|
func containsAll(s string, parts ...string) bool {
|
|
for _, p := range parts {
|
|
if !stringsContains(s, p) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func stringsContains(s, sub string) bool {
|
|
return len(sub) == 0 || (len(s) >= len(sub) && indexOf(s, sub) >= 0)
|
|
}
|
|
|
|
func indexOf(s, sub string) int {
|
|
for i := 0; i+len(sub) <= len(s); i++ {
|
|
if s[i:i+len(sub)] == sub {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|