File: network_util_test.go

package info (click to toggle)
incus 6.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,428 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (82 lines) | stat: -rw-r--r-- 4,429 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package endpoints

import (
	"net"
	"testing"

	"github.com/stretchr/testify/assert"
)

func Test_networkServerErrorLogWriter_shouldDiscard(t *testing.T) {
	tests := []struct {
		name    string
		proxies []net.IP
		log     []byte
		want    string
	}{
		{
			name:    "ipv4 trusted proxy (write)",
			proxies: []net.IP{net.ParseIP("10.24.0.32")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from 10.24.0.32:55672: write tcp 10.24.0.22:8443->10.24.0.32:55672: write: connection reset by peer\n"),
			want:    "",
		},
		{
			name:    "ipv4 non-trusted proxy (write)",
			proxies: []net.IP{net.ParseIP("10.24.0.33")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from 10.24.0.32:55672: write tcp 10.24.0.22:8443->10.24.0.32:55672: write: connection reset by peer\n"),
			want:    "http: TLS handshake error from 10.24.0.32:55672: write tcp 10.24.0.22:8443->10.24.0.32:55672: write: connection reset by peer",
		},
		{
			name:    "ipv6 trusted proxy (write)",
			proxies: []net.IP{net.ParseIP("2602:fd23:8:1003:1266:6aff:fefa:7670")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from [2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: write tcp [2602:fd23:8:101::100]:8443->[2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: write: connection reset by peer\n"),
			want:    "",
		},
		{
			name:    "ipv6 non-trusted proxy (write)",
			proxies: []net.IP{net.ParseIP("2602:fd23:8:1003:1266:6aff:fefa:7671")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from [2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: write tcp [2602:fd23:8:101::100]:8443->[2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: write: connection reset by peer\n"),
			want:    "http: TLS handshake error from [2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: write tcp [2602:fd23:8:101::100]:8443->[2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: write: connection reset by peer",
		},
		{
			name:    "ipv4 trusted proxy (read)",
			proxies: []net.IP{net.ParseIP("10.24.0.32")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from 10.24.0.32:55672: read tcp 10.24.0.22:8443->10.24.0.32:55672: read: connection reset by peer\n"),
			want:    "",
		},
		{
			name:    "ipv4 non-trusted proxy (read)",
			proxies: []net.IP{net.ParseIP("10.24.0.33")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from 10.24.0.32:55672: read tcp 10.24.0.22:8443->10.24.0.32:55672: read: connection reset by peer\n"),
			want:    "http: TLS handshake error from 10.24.0.32:55672: read tcp 10.24.0.22:8443->10.24.0.32:55672: read: connection reset by peer",
		},
		{
			name:    "ipv6 trusted proxy (read)",
			proxies: []net.IP{net.ParseIP("2602:fd23:8:1003:1266:6aff:fefa:7670")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from [2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: read tcp [2602:fd23:8:101::100]:8443->[2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: read: connection reset by peer\n"),
			want:    "",
		},
		{
			name:    "ipv6 non-trusted proxy (read)",
			proxies: []net.IP{net.ParseIP("2602:fd23:8:1003:1266:6aff:fefa:7671")},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: TLS handshake error from [2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: read tcp [2602:fd23:8:101::100]:8443->[2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: read: connection reset by peer\n"),
			want:    "http: TLS handshake error from [2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: read tcp [2602:fd23:8:101::100]:8443->[2602:fd23:8:1003:1266:6aff:fefa:7670]:55672: read: connection reset by peer",
		},

		{
			name:    "unrelated",
			proxies: []net.IP{},
			log:     []byte("Sep 17 04:58:30 abydos incus.daemon[21884]: 2021/09/17 04:58:30 http: response.WriteHeader on hijacked connection from yourfunction (yourfile.go:80)\n"),
			want:    "http: response.WriteHeader on hijacked connection from yourfunction (yourfile.go:80)",
		},
	}

	for i, tt := range tests {
		t.Logf("Case %d: %s", i, tt.name)
		d := networkServerErrorLogWriter{
			proxies: tt.proxies,
		}

		assert.Equal(t, tt.want, d.stripLog(tt.log))
	}
}