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
|
package net
import (
"testing"
"time"
)
// remoteAddrConn is a minimal Conn that also reports a peer address, so the
// Session.RemoteHost port-stripping contract can be tested without a socket.
type remoteAddrConn struct{ addr string }
func (c remoteAddrConn) ReadMessage() (string, error) { return "", nil }
func (c remoteAddrConn) Write(b []byte) (int, error) { return len(b), nil }
func (c remoteAddrConn) Close() error { return nil }
func (c remoteAddrConn) SetWriteDeadline(time.Time) error { return nil }
func (c remoteAddrConn) SetReadDeadline(time.Time) error { return nil }
func (c remoteAddrConn) SetEcho(bool) error { return nil }
func (c remoteAddrConn) RemoteAddr() string { return c.addr }
// TestSessionRemoteHostStripsPort: login throttling keys on the client host,
// so RemoteHost must drop the ephemeral source port (RemoteAddr keeps it).
// Keying on the raw address would let a reconnect reset the limiter.
func TestSessionRemoteHostStripsPort(t *testing.T) {
for _, tc := range []struct{ in, want string }{
{"203.0.113.7:54321", "203.0.113.7"},
{"[2001:db8::1]:443", "2001:db8::1"},
{"203.0.113.7", "203.0.113.7"},
{"", ""},
} {
sess := &Session{Conn: remoteAddrConn{addr: tc.in}}
if got := sess.RemoteHost(); got != tc.want {
t.Fatalf("RemoteHost(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
|