Skip to content

Commit 84e3246

Browse files
committed
test: add TestDial test
Signed-off-by: Dwi Siswanto <[email protected]>
1 parent bc2fc9c commit 84e3246

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

fastdialer/dialer_private_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package fastdialer
22

33
import (
4+
"context"
5+
"net"
46
"sync/atomic"
57
"testing"
68
"time"
9+
10+
ztls "github.com/zmap/zcrypto/tls"
711
)
812

913
type mockCloser struct {
@@ -85,3 +89,84 @@ func TestCloseAfterTimeout_ExternalCancelPreemptsTimeout(t *testing.T) {
8589
t.Fatalf("expected zero close calls at end of test, got %d", got)
8690
}
8791
}
92+
93+
func TestDial(t *testing.T) {
94+
t.Run("ZTLSWithConfig", func(t *testing.T) {
95+
listener, err := net.Listen("tcp", "127.0.0.1:0")
96+
if err != nil {
97+
t.Fatalf("Failed to start listener: %v", err)
98+
}
99+
defer listener.Close()
100+
101+
serverAddr := listener.Addr().String()
102+
103+
go func() {
104+
for {
105+
conn, err := listener.Accept()
106+
if err != nil {
107+
return
108+
}
109+
110+
// hold conn w/o completing handshake
111+
time.Sleep(5 * time.Second)
112+
conn.Close()
113+
}
114+
}()
115+
116+
options := DefaultOptions
117+
options.DialerTimeout = 100 * time.Millisecond
118+
119+
dialer, err := NewDialer(options)
120+
if err != nil {
121+
t.Fatalf("Failed to create dialer: %v", err)
122+
}
123+
defer dialer.Close()
124+
125+
ztlsConfig := &ztls.Config{
126+
InsecureSkipVerify: true,
127+
ServerName: "localhost",
128+
}
129+
130+
_, err = dialer.DialZTLSWithConfig(context.Background(), "tcp", serverAddr, ztlsConfig)
131+
if err == nil {
132+
t.Fatal("Expected an error due to timeout, got nil")
133+
}
134+
})
135+
136+
t.Run("TLSWithConfigFallback", func(t *testing.T) {
137+
listener, err := net.Listen("tcp", "127.0.0.1:0")
138+
if err != nil {
139+
t.Fatalf("Failed to start listener: %v", err)
140+
}
141+
defer listener.Close()
142+
143+
serverAddr := listener.Addr().String()
144+
145+
go func() {
146+
for {
147+
conn, err := listener.Accept()
148+
if err != nil {
149+
return
150+
}
151+
time.Sleep(5 * time.Second)
152+
conn.Close()
153+
}
154+
}()
155+
156+
options := DefaultOptions
157+
options.DialerTimeout = 100 * time.Millisecond
158+
options.DisableZtlsFallback = false
159+
160+
dialer, err := NewDialer(options)
161+
if err != nil {
162+
t.Fatalf("Failed to create dialer: %v", err)
163+
}
164+
defer dialer.Close()
165+
166+
_, err = dialer.DialTLSWithConfig(context.Background(), "tcp", serverAddr, DefaultTLSConfig)
167+
168+
if err == nil {
169+
t.Fatal("Expected an error due to timeout, got nil")
170+
}
171+
})
172+
}

0 commit comments

Comments
 (0)