golang.zx2c4.com/wireguard/windows@v0.5.4-0.20230123132234-dcc0eb72a04b/conf/parser_test.go (about) 1 /* SPDX-License-Identifier: MIT 2 * 3 * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved. 4 */ 5 6 package conf 7 8 import ( 9 "net/netip" 10 "reflect" 11 "runtime" 12 "testing" 13 ) 14 15 const testInput = ` 16 [Interface] 17 Address = 10.192.122.1/24 18 Address = 10.10.0.1/16 19 PrivateKey = yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= 20 ListenPort = 51820 #comments don't matter 21 22 [Peer] 23 PublicKey = xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg= 24 Endpoint = 192.95.5.67:1234 25 AllowedIPs = 10.192.122.3/32, 10.192.124.1/24 26 27 [Peer] 28 PublicKey = TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0= 29 Endpoint = [2607:5300:60:6b0::c05f:543]:2468 30 AllowedIPs = 10.192.122.4/32, 192.168.0.0/16 31 PersistentKeepalive = 100 32 33 [Peer] 34 PublicKey = gN65BkIKy1eCE9pP1wdc8ROUtkHLF2PfAqYdyYBz6EA= 35 PresharedKey = TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0= 36 Endpoint = test.wireguard.com:18981 37 AllowedIPs = 10.10.10.230/32` 38 39 func noError(t *testing.T, err error) bool { 40 if err == nil { 41 return true 42 } 43 _, fn, line, _ := runtime.Caller(1) 44 t.Errorf("Error at %s:%d: %#v", fn, line, err) 45 return false 46 } 47 48 func equal(t *testing.T, expected, actual any) bool { 49 if reflect.DeepEqual(expected, actual) { 50 return true 51 } 52 _, fn, line, _ := runtime.Caller(1) 53 t.Errorf("Failed equals at %s:%d\nactual %#v\nexpected %#v", fn, line, actual, expected) 54 return false 55 } 56 57 func lenTest(t *testing.T, actualO any, expected int) bool { 58 actual := reflect.ValueOf(actualO).Len() 59 if reflect.DeepEqual(expected, actual) { 60 return true 61 } 62 _, fn, line, _ := runtime.Caller(1) 63 t.Errorf("Wrong length at %s:%d\nactual %#v\nexpected %#v", fn, line, actual, expected) 64 return false 65 } 66 67 func contains(t *testing.T, list, element any) bool { 68 listValue := reflect.ValueOf(list) 69 for i := 0; i < listValue.Len(); i++ { 70 if reflect.DeepEqual(listValue.Index(i).Interface(), element) { 71 return true 72 } 73 } 74 _, fn, line, _ := runtime.Caller(1) 75 t.Errorf("Error %s:%d\nelement not found: %#v", fn, line, element) 76 return false 77 } 78 79 func TestFromWgQuick(t *testing.T) { 80 conf, err := FromWgQuick(testInput, "test") 81 if noError(t, err) { 82 lenTest(t, conf.Interface.Addresses, 2) 83 contains(t, conf.Interface.Addresses, netip.PrefixFrom(netip.AddrFrom4([4]byte{0, 10, 0, 1}), 16)) 84 contains(t, conf.Interface.Addresses, netip.PrefixFrom(netip.AddrFrom4([4]byte{10, 192, 122, 1}), 24)) 85 equal(t, "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=", conf.Interface.PrivateKey.String()) 86 equal(t, uint16(51820), conf.Interface.ListenPort) 87 88 lenTest(t, conf.Peers, 3) 89 lenTest(t, conf.Peers[0].AllowedIPs, 2) 90 equal(t, Endpoint{Host: "192.95.5.67", Port: 1234}, conf.Peers[0].Endpoint) 91 equal(t, "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=", conf.Peers[0].PublicKey.String()) 92 93 lenTest(t, conf.Peers[1].AllowedIPs, 2) 94 equal(t, Endpoint{Host: "2607:5300:60:6b0::c05f:543", Port: 2468}, conf.Peers[1].Endpoint) 95 equal(t, "TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0=", conf.Peers[1].PublicKey.String()) 96 equal(t, uint16(100), conf.Peers[1].PersistentKeepalive) 97 98 lenTest(t, conf.Peers[2].AllowedIPs, 1) 99 equal(t, Endpoint{Host: "test.wireguard.com", Port: 18981}, conf.Peers[2].Endpoint) 100 equal(t, "gN65BkIKy1eCE9pP1wdc8ROUtkHLF2PfAqYdyYBz6EA=", conf.Peers[2].PublicKey.String()) 101 equal(t, "TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0=", conf.Peers[2].PresharedKey.String()) 102 } 103 } 104 105 func TestParseEndpoint(t *testing.T) { 106 _, err := parseEndpoint("[192.168.42.0:]:51880") 107 if err == nil { 108 t.Error("Error was expected") 109 } 110 e, err := parseEndpoint("192.168.42.0:51880") 111 if noError(t, err) { 112 equal(t, "192.168.42.0", e.Host) 113 equal(t, uint16(51880), e.Port) 114 } 115 e, err = parseEndpoint("test.wireguard.com:18981") 116 if noError(t, err) { 117 equal(t, "test.wireguard.com", e.Host) 118 equal(t, uint16(18981), e.Port) 119 } 120 e, err = parseEndpoint("[2607:5300:60:6b0::c05f:543]:2468") 121 if noError(t, err) { 122 equal(t, "2607:5300:60:6b0::c05f:543", e.Host) 123 equal(t, uint16(2468), e.Port) 124 } 125 _, err = parseEndpoint("[::::::invalid:18981") 126 if err == nil { 127 t.Error("Error was expected") 128 } 129 }