From 7d5b0016ef19df489ada1dd5399e7e38431227f4 Mon Sep 17 00:00:00 2001 From: xeptore <29199390+xeptore@users.noreply.github.com> Date: Fri, 24 Mar 2023 10:41:40 +0330 Subject: [PATCH] add invalid wg dump file test case Signed-off-by: xeptore <29199390+xeptore@users.noreply.github.com> --- pkg/dump/parse.go | 12 ++++++----- pkg/dump/parse_test.go | 33 ++++++++++++++++++++++++++---- pkg/dump/testdata/dump-invalid.txt | 17 +++++++++++++++ pkg/dump/testdata/dump.txt | 3 ++- 4 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 pkg/dump/testdata/dump-invalid.txt diff --git a/pkg/dump/parse.go b/pkg/dump/parse.go index fe3d9a3..6df707c 100644 --- a/pkg/dump/parse.go +++ b/pkg/dump/parse.go @@ -67,10 +67,6 @@ func Parse(raw []byte) ([]Peer, error) { var allowedIPs []net.IPNet raw = splits[3] rawAllowedIPs := strings.Split(raw, ",") - if len(rawAllowedIPs) < 1 { - return nil, fmt.Errorf("expected at least one allowed ip (%s): %v", raw, err) - } - for _, v := range rawAllowedIPs { _, ipnet, err := net.ParseCIDR(v) if nil != err { @@ -85,7 +81,10 @@ func Parse(raw []byte) ([]Peer, error) { if raw != "0" { i, err := strconv.ParseInt(raw, 10, 64) if nil != err { - return nil, fmt.Errorf("invalid latest handshake unit timestamp (%s): %v", raw, err) + return nil, fmt.Errorf("invalid latest handshake unix timestamp (%s): %v", raw, err) + } + if i < 0 { + return nil, fmt.Errorf("unexpected negative latest handshake unix timestamp value (%s)", raw) } latestHandshakeTime = time.Unix(i, 0) } @@ -112,6 +111,9 @@ func Parse(raw []byte) ([]Peer, error) { if nil != err { return 0, fmt.Errorf("invalid persistent keepalive interval (%s): %v", raw, err) } + if i < 0 { + return 0, fmt.Errorf("unexpected negative persistent keepalive value (%s)", raw) + } return time.Duration(i), nil }() diff --git a/pkg/dump/parse_test.go b/pkg/dump/parse_test.go index 84751b4..08e8c0c 100644 --- a/pkg/dump/parse_test.go +++ b/pkg/dump/parse_test.go @@ -1,6 +1,8 @@ package dump_test import ( + "bufio" + "bytes" _ "embed" "net" "net/netip" @@ -13,8 +15,12 @@ import ( "github.com/xeptore/wireuse/pkg/dump" ) -//go:embed testdata/dump.txt -var raw []byte +var ( + //go:embed testdata/dump.txt + rawValidDump []byte + //go:embed testdata/dump-invalid.txt + rawInvalidDump []byte +) func mustParseKey(t *testing.T, s string) wgtypes.Key { key, err := wgtypes.ParseKey(s) @@ -45,8 +51,8 @@ func mustParseUnixTime(t *testing.T, u int64) time.Time { return time.Unix(u, 0) } -func TestParse(t *testing.T) { - peers, err := dump.Parse(raw) +func TestParseValidDumpFile(t *testing.T) { + peers, err := dump.Parse(rawValidDump) require.Nil(t, err) require.NotNil(t, peers) require.Equal(t, []dump.Peer{ @@ -740,5 +746,24 @@ func TestParse(t *testing.T) { TransmitBytes: 349602820, PersistentKeepaliveInterval: 0, }, + { + PublicKey: mustParseKey(t, "RFrjTKnZWr/CmI/XC+zgPeGJzbDGQhA9eOBb+ge90wA="), + PresharedKey: mustParseKey(t, "uox3nW4W81djWoJEN/lgdrvjqtLnl36lhkJZcbSkwOM="), + Endpoint: mustParseUDPAddr(t, "5.232.204.179:10611"), + AllowedIPs: mustParseAllowedIPs(t, "10.0.0.113/32", "fdd0:438e:19ba:5069::71/128"), + LastHandshakeTime: mustParseUnixTime(t, 1672307401), + ReceiveBytes: 10695096, + TransmitBytes: 349602820, + PersistentKeepaliveInterval: 10, + }, }, peers) } + +func TestParseInvalidDumpFile(t *testing.T) { + scanner := bufio.NewScanner(bytes.NewBuffer(rawInvalidDump)) + for scanner.Scan() { + peers, err := dump.Parse(scanner.Bytes()) + require.Nil(t, peers) + require.NotNil(t, err) + } +} diff --git a/pkg/dump/testdata/dump-invalid.txt b/pkg/dump/testdata/dump-invalid.txt new file mode 100644 index 0000000..484fb76 --- /dev/null +++ b/pkg/dump/testdata/dump-invalid.txt @@ -0,0 +1,17 @@ +yLk+kQwd2Zm4I6f6Aeg/OGlT3GLtHsNmY+kUXK2sfD0= QMXzIPq+o5zNvecAIQZn61rB9SmolhW0fgj7Zwylzvg= (none) 10.0.0.19/32,fdd0:438e:19ba:5069::13/128 0 0 0 offs +XuyQmI/KXTBvwdpfjUzULj4PfBW15mrMPXm8pS1OnhQ= kT0tuNL/rhPoWjWiQhSCHFJuaSKLxlqg2aAuxwwpz4I= (none) 10.0.0.20/32,fdd0:438e:19ba:5069::14/128 -8 0 0 off +UDrZG/thxkzPbow3Lr1gGHYeKbNlQF8Znob9hujRYjo= fkKqj8RmLjHbo8T63OtLn6JM8MnCIp+LZLpvxiIl2HU= 83.123abc.9.201:42944 10.0.0.21/32,fdd0:438e:19ba:5069::15/128 1679600629 7138444 103247936 off +qwQBlkUITgLsddSi9kbKM14HCkvqmVYi1VT/8n9u6kY= MFToe6QumdelJu3whvgSDniVNM+jmuTNMXP9FY07F8g= - 10.0.0.29/32,fdd0:438e:19ba:5069::1d/128 0 0 0 off +5jNQhl3WZJBOI4+M5e4Pk7+9SCMSg6GyG+Dkc+wpCVA= l7itiN0Pr0fQMD7Ae8EMpDHmQ0rL-4KyTNuL/5J9+O6o= (none) 10.0.0.30/32,fdd0:438e:19ba:5069::1e/128 0 0 0 off +OafCe4tLL8rShnOiQod35G+NdDPQh-wIN4JKRoyKbNkI= mJBHO390BWD3GxHMIdWFey70N7c6x5EDrVuzcROh/Ao= 5.125.49.118:32738 10.0.0.41/32,fdd0:438e:19ba:5069::29/128 1679608693 69519144 475482968 off +R86vOf/VdN9m0Ey4AD80D4i5UdTefMsKJw/+BuOSeS4= +V9iBlvEB7FCZcUMcY40c1FazbleACJY/qButRjkUUg= 95.38.119.159:524919 10.0.0.42/32,fdd0:438e:19ba:5069::2a/128 1679597842 8385684 188015208 off +FaibxGztHHy3pm0kiiBfI6GMM6UpdiD6Kinekh0ARDk= 8R5QEYDbJw/VYsNw62LX9p+RGtn3NKeVyDeCol1zzT8= 188.229.11.180:62680 1679610586 91786228 109298580 off +gOZgWvD4pVpGNx8ZBJOrEFbUvdOmIqyJ5xaWkb0QkwY= 4fpiAvyjMmUPGDsi5IV7EPWmq9ANOCsuBU81s1KKvwg= 5.123.27.254:10852 10.0.0.444/32,fdd0:438e:19ba:5069::2c/128 1679622245 38969328 500779248 off +nRhGvfiDIdUts1yJn4Bt2QVURBK73ks++SrqdseUZjA= DK+KyyjvtAHARlSkvH6ogNYeCb//GSVfp/SaYGCkTvc= 83.122.114.201:53992 10.0.0.45/32,fdd0:438e:19ba:g069::2d/128 1679614805 49627932 1698468224 off +cM6cjxjXEfbV/MmdIaqnI3iG6NX0eTGrxi2xdr8+XlA= yHHspi9idt69QY3EawOQrEVdi2kMQ4CdW4jadVHDWHo= 5.126.154.201:32527 10.0.0.46/32,fdd0:438e:19ba:5069::2e/129 1679622324 41659912 387348224 off +gsqgOUyV3wl2QjGemvO73O1MD9PB/umq6lH18Px/1RU= +NjgCZQ3224mpbDZUgIj24mzX6NcovotLuDOqjTHLw8= 5.125.223.142:36784 10.0.0.47/32,fdd0:438e:19ba:5069::2f/128 1679611750q 60529560 1421720032 off +mYDlBMbMZKEjcX8nTjYWmOGQlguyLomQVm+q9yc9eAM= MXSRqHNEMM48WGXqyUmCC2YxsueGZvyxIW8UnwFibt0= (none) 10.0.0.49/32,fdd0:438e:19ba:5069::31/128 0 - 0 off +P7wDzaPcNHAfR2MVuQV//qD+YfXHR3Ad/7uLm61xUXA= VuYrRbWESPD7kFpXdSs6BJ7fMppBM4IBxOURx/lYv9Q= (none) 10.0.0.50/32,fdd0:438e:19ba:5069::32/128 0 0 x off +rauZLam+acOP+/mC0C/vAjIweVoWZNQ0jK5iBmpAT0w= dQQA+bYhLgtXwnPug9rQVok1R+ud9RRAqKgo04K/ljU= 5.119.75.31:47338 10.0.0.51/32,fdd0:438e:19ba:5069::33/128 1679622318 49183124 946856032 -9 +xwlGtnl3nmV+uR+nN8No7mMQiv1OTTOKMdgrwaDBMFk= NKeDh2sJkweEbC7ZspA5fPBL+9ahk/eqqKFcl0IemBg= 37.129.244.184:43698 10.0.0.52/32,fdd0:438e:19ba:5069::34/128 1679613152 57819876 687229524 +xwlGtnl3nmV+uR+nN8No7mMQiv1OTTOKMdgrwaDBMFk= NKeDh2sJkweEbC7ZspA5fPBL+9ahk/eqqKFcl0IemBg= 37.129.244.184:43698 10.0.0.52/32,fdd0:438e:19ba:5069::34/128 1679613152 57819876 687229524 off 123 \ No newline at end of file diff --git a/pkg/dump/testdata/dump.txt b/pkg/dump/testdata/dump.txt index ae2c7d8..6ad7ef2 100644 --- a/pkg/dump/testdata/dump.txt +++ b/pkg/dump/testdata/dump.txt @@ -66,4 +66,5 @@ NehSfEfBgEIMmMWKJc6dNe+Kk+eowsokeUTJlioPOWM= h/76X+Cuwo123DQETPWEDqVTZwiZXHM0zCS 1wgfj6gR/fhrfW909Ejnu9xPS+gCMDhvqCQFzH8tP0Y= Eqekgl1wqY4Gkf8VaApuXVw0354BWbCelcpZv9WzYHc= (none) 10.0.0.109/32,fdd0:438e:19ba:5069::6d/128 0 0 0 off WtNgBST4LflxP2IR/oaNkULvQ5909BhZaizrtODXFQs= zoR7N5mGIR1/A9PNC08DK2eOFHCBdG+SapPNyXyNGIo= (none) 10.0.0.110/32,fdd0:438e:19ba:5069::6e/128 0 0 0 off kPuHjkT58MsNP55oQFJszXhpuN6ukG++WUDsR4DJQjo= G4Z57Ihjk3Et+O68iFj8I3CT2SgbjMDnpa+/lFuH6gU= (none) 10.0.0.111/32,fdd0:438e:19ba:5069::6f/128 0 0 0 off -4ejeXAAOX6UrBnaAivqQ11JSanLzXxBpVd+A1Aozl3A= 77EuIYC1+S1i4LP5j8TibwL8didMy+mI+5bOfqKC+sA= 5.232.204.179:10610 10.0.0.112/32,fdd0:438e:19ba:5069::70/128 1679607401 20695096 349602820 off \ No newline at end of file +4ejeXAAOX6UrBnaAivqQ11JSanLzXxBpVd+A1Aozl3A= 77EuIYC1+S1i4LP5j8TibwL8didMy+mI+5bOfqKC+sA= 5.232.204.179:10610 10.0.0.112/32,fdd0:438e:19ba:5069::70/128 1679607401 20695096 349602820 off +RFrjTKnZWr/CmI/XC+zgPeGJzbDGQhA9eOBb+ge90wA= uox3nW4W81djWoJEN/lgdrvjqtLnl36lhkJZcbSkwOM= 5.232.204.179:10611 10.0.0.113/32,fdd0:438e:19ba:5069::71/128 1672307401 10695096 349602820 10 \ No newline at end of file