File: per_host_test.go

package info (click to toggle)
golang-golang-x-net 1%3A0.0%2Bgit20211209.491a49a%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,020 kB
  • sloc: asm: 18; makefile: 16
file content (76 lines) | stat: -rw-r--r-- 2,088 bytes parent folder | download | duplicates (11)
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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proxy

import (
	"context"
	"errors"
	"net"
	"reflect"
	"testing"
)

type recordingProxy struct {
	addrs []string
}

func (r *recordingProxy) Dial(network, addr string) (net.Conn, error) {
	r.addrs = append(r.addrs, addr)
	return nil, errors.New("recordingProxy")
}

func TestPerHost(t *testing.T) {
	expectedDef := []string{
		"example.com:123",
		"1.2.3.4:123",
		"[1001::]:123",
	}
	expectedBypass := []string{
		"localhost:123",
		"zone:123",
		"foo.zone:123",
		"127.0.0.1:123",
		"10.1.2.3:123",
		"[1000::]:123",
	}

	t.Run("Dial", func(t *testing.T) {
		var def, bypass recordingProxy
		perHost := NewPerHost(&def, &bypass)
		perHost.AddFromString("localhost,*.zone,127.0.0.1,10.0.0.1/8,1000::/16")
		for _, addr := range expectedDef {
			perHost.Dial("tcp", addr)
		}
		for _, addr := range expectedBypass {
			perHost.Dial("tcp", addr)
		}

		if !reflect.DeepEqual(expectedDef, def.addrs) {
			t.Errorf("Hosts which went to the default proxy didn't match. Got %v, want %v", def.addrs, expectedDef)
		}
		if !reflect.DeepEqual(expectedBypass, bypass.addrs) {
			t.Errorf("Hosts which went to the bypass proxy didn't match. Got %v, want %v", bypass.addrs, expectedBypass)
		}
	})

	t.Run("DialContext", func(t *testing.T) {
		var def, bypass recordingProxy
		perHost := NewPerHost(&def, &bypass)
		perHost.AddFromString("localhost,*.zone,127.0.0.1,10.0.0.1/8,1000::/16")
		for _, addr := range expectedDef {
			perHost.DialContext(context.Background(), "tcp", addr)
		}
		for _, addr := range expectedBypass {
			perHost.DialContext(context.Background(), "tcp", addr)
		}

		if !reflect.DeepEqual(expectedDef, def.addrs) {
			t.Errorf("Hosts which went to the default proxy didn't match. Got %v, want %v", def.addrs, expectedDef)
		}
		if !reflect.DeepEqual(expectedBypass, bypass.addrs) {
			t.Errorf("Hosts which went to the bypass proxy didn't match. Got %v, want %v", bypass.addrs, expectedBypass)
		}
	})
}