This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
connectivity_test.go
133 lines (110 loc) · 3.54 KB
/
connectivity_test.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package shadowsocks
import (
"errors"
"net"
"reflect"
"testing"
"time"
onet "github.com/Jigsaw-Code/outline-ss-server/net"
shadowsocks "github.com/Jigsaw-Code/outline-ss-server/client"
)
func TestCheckUDPConnectivityWithDNS_Success(t *testing.T) {
client := &fakeSSClient{}
err := CheckUDPConnectivityWithDNS(client, shadowsocks.NewAddr("", ""))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
func TestCheckUDPConnectivityWithDNS_Fail(t *testing.T) {
client := &fakeSSClient{failUDP: true}
err := CheckUDPConnectivityWithDNS(client, shadowsocks.NewAddr("", ""))
if err == nil {
t.Fail()
}
}
func TestCheckTCPConnectivityWithHTTP_Success(t *testing.T) {
client := &fakeSSClient{}
err := CheckTCPConnectivityWithHTTP(client, "")
if err != nil {
t.Fail()
}
}
func TestCheckTCPConnectivityWithHTTP_FailReachability(t *testing.T) {
client := &fakeSSClient{failReachability: true}
err := CheckTCPConnectivityWithHTTP(client, "")
if err == nil {
t.Fail()
}
if _, ok := err.(*ReachabilityError); !ok {
t.Fatalf("Expected reachability error, got: %v", reflect.TypeOf(err))
}
}
func TestCheckTCPConnectivityWithHTTP_FailAuthentication(t *testing.T) {
client := &fakeSSClient{failAuthentication: true}
err := CheckTCPConnectivityWithHTTP(client, "")
if err == nil {
t.Fail()
}
if _, ok := err.(*AuthenticationError); !ok {
t.Fatalf("Expected authentication error, got: %v", reflect.TypeOf(err))
}
}
// Fake shadowsocks.Client that can be configured to return failing UDP and TCP connections.
type fakeSSClient struct {
failReachability bool
failAuthentication bool
failUDP bool
}
func (c *fakeSSClient) DialTCP(laddr *net.TCPAddr, raddr string) (onet.DuplexConn, error) {
if c.failReachability {
return nil, &net.OpError{}
}
return &fakeDuplexConn{failRead: c.failAuthentication}, nil
}
func (c *fakeSSClient) ListenUDP(laddr *net.UDPAddr) (net.PacketConn, error) {
conn, err := net.ListenPacket("udp", "")
if err != nil {
return nil, err
}
// The UDP check should fail if any of the failure conditions are true since it is a superset of the others.
failRead := c.failAuthentication || c.failUDP || c.failReachability
return &fakePacketConn{PacketConn: conn, failRead: failRead}, nil
}
// Fake PacketConn that fails `ReadFrom` calls when `failRead` is true.
type fakePacketConn struct {
net.PacketConn
addr net.Addr
failRead bool
}
func (c *fakePacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
c.addr = addr
return len(b), nil // Write always succeeds
}
func (c *fakePacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
if c.failRead {
return 0, c.addr, errors.New("Fake read error")
}
return len(b), c.addr, nil
}
// Fake DuplexConn that fails `Read` calls when `failRead` is true.
type fakeDuplexConn struct {
onet.DuplexConn
failRead bool
}
func (c *fakeDuplexConn) Read(b []byte) (int, error) {
if c.failRead {
return 0, errors.New("Fake read error")
}
return len(b), nil
}
func (c *fakeDuplexConn) Write(b []byte) (int, error) {
return len(b), nil // Write always succeeds
}
func (c *fakeDuplexConn) Close() error { return nil }
func (c *fakeDuplexConn) LocalAddr() net.Addr { return nil }
func (c *fakeDuplexConn) RemoteAddr() net.Addr { return nil }
func (c *fakeDuplexConn) SetDeadline(t time.Time) error { return nil }
func (c *fakeDuplexConn) SetReadDeadline(t time.Time) error { return nil }
func (c *fakeDuplexConn) SetWriteDeadline(t time.Time) error { return nil }
func (c *fakeDuplexConn) CloseRead() error { return nil }
func (c *fakeDuplexConn) CloseWrite() error { return nil }