@@ -2,6 +2,8 @@ package fastdialer
22
33import (
44 "context"
5+ "errors"
6+ "sync"
57 "testing"
68)
79
@@ -57,3 +59,216 @@ func testDialer(t *testing.T, options Options) {
5759 t .Error ("no A results found" )
5860 }
5961}
62+
63+ func TestDialerPortPolicy (t * testing.T ) {
64+ t .Run ("DenyPortBlocks" , func (t * testing.T ) {
65+ options := DefaultOptions
66+ options .DenyPortList = []int {80 }
67+
68+ fd , err := NewDialer (options )
69+ if err != nil {
70+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
71+ }
72+ defer fd .Close ()
73+
74+ ctx := context .Background ()
75+ conn , err := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
76+ if conn != nil {
77+ _ = conn .Close ()
78+ }
79+ if err != NoAddressAllowedError {
80+ t .Fatalf ("expected NoAddressAllowedError for denied port, got: %v" , err )
81+ }
82+ })
83+
84+ t .Run ("DenyPortAllowsOther" , func (t * testing.T ) {
85+ options := DefaultOptions
86+ options .DenyPortList = []int {8081 }
87+
88+ fd , err := NewDialer (options )
89+ if err != nil {
90+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
91+ }
92+ defer fd .Close ()
93+
94+ ctx := context .Background ()
95+ conn , err := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
96+ if err != nil || conn == nil {
97+ t .Fatalf ("expected connection to succeed on non-denied port, got: %v" , err )
98+ }
99+ _ = conn .Close ()
100+ })
101+
102+ t .Run ("AllowPortPermits" , func (t * testing.T ) {
103+ options := DefaultOptions
104+ options .AllowPortList = []int {80 , 443 }
105+
106+ fd , err := NewDialer (options )
107+ if err != nil {
108+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
109+ }
110+ defer fd .Close ()
111+
112+ ctx := context .Background ()
113+ conn , err := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
114+ if err != nil || conn == nil {
115+ t .Fatalf ("expected connection to succeed on allowed port, got: %v" , err )
116+ }
117+ _ = conn .Close ()
118+ })
119+
120+ t .Run ("AllowPortBlocksOther" , func (t * testing.T ) {
121+ options := DefaultOptions
122+ options .AllowPortList = []int {443 }
123+
124+ fd , err := NewDialer (options )
125+ if err != nil {
126+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
127+ }
128+ defer fd .Close ()
129+
130+ ctx := context .Background ()
131+ conn , err := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
132+ if conn != nil {
133+ _ = conn .Close ()
134+ }
135+ if err != NoAddressAllowedError {
136+ t .Fatalf ("expected NoAddressAllowedError for non-allowed port, got: %v" , err )
137+ }
138+ })
139+
140+ t .Run ("NoPortPolicyUnchanged" , func (t * testing.T ) {
141+ options := DefaultOptions
142+
143+ fd , err := NewDialer (options )
144+ if err != nil {
145+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
146+ }
147+ defer fd .Close ()
148+
149+ ctx := context .Background ()
150+ conn , err := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
151+ if err != nil || conn == nil {
152+ t .Fatalf ("expected connection to succeed without port policy, got: %v" , err )
153+ }
154+ _ = conn .Close ()
155+ })
156+
157+ t .Run ("DenyPortTriggersOnInvalidTarget" , func (t * testing.T ) {
158+ options := DefaultOptions
159+ options .DenyPortList = []int {80 }
160+
161+ var invalidCalled bool
162+ var mu sync.Mutex
163+ options .OnInvalidTarget = func (hostname , ip , port string ) {
164+ mu .Lock ()
165+ invalidCalled = true
166+ mu .Unlock ()
167+ }
168+
169+ fd , err := NewDialer (options )
170+ if err != nil {
171+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
172+ }
173+ defer fd .Close ()
174+
175+ ctx := context .Background ()
176+ conn , _ := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
177+ if conn != nil {
178+ _ = conn .Close ()
179+ }
180+
181+ mu .Lock ()
182+ called := invalidCalled
183+ mu .Unlock ()
184+ if ! called {
185+ t .Error ("OnInvalidTarget was not called for denied port" )
186+ }
187+ })
188+ }
189+
190+ func TestDialerTargetValidation (t * testing.T ) {
191+ t .Run ("ValidTarget" , func (t * testing.T ) {
192+ options := DefaultOptions
193+
194+ var validateCalled bool
195+ options .OnValidateTarget = func (hostname , ip , port string ) error {
196+ validateCalled = true
197+ if hostname != "www.projectdiscovery.io" {
198+ return errors .New ("invalid hostname" )
199+ }
200+ return nil
201+ }
202+
203+ var invalidCalled bool
204+ options .OnInvalidTarget = func (hostname , ip , port string ) {
205+ invalidCalled = true
206+ }
207+
208+ fd , err := NewDialer (options )
209+ if err != nil {
210+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
211+ }
212+ defer fd .Close ()
213+
214+ ctx := context .Background ()
215+ conn , err := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
216+ if err != nil || conn == nil {
217+ t .Fatalf ("couldn't connect to target: %s" , err )
218+ }
219+ defer func () {
220+ _ = conn .Close ()
221+ }()
222+
223+ if ! validateCalled {
224+ t .Error ("OnValidateTarget was not called" )
225+ }
226+ if invalidCalled {
227+ t .Error ("OnInvalidTarget was called for a valid target" )
228+ }
229+ })
230+
231+ t .Run ("InvalidTarget" , func (t * testing.T ) {
232+ options := DefaultOptions
233+
234+ var validateCalled bool
235+ options .OnValidateTarget = func (hostname , ip , port string ) error {
236+ validateCalled = true
237+ return errors .New ("target rejected" )
238+ }
239+
240+ var invalidCalled bool
241+ var mu sync.Mutex
242+ options .OnInvalidTarget = func (hostname , ip , port string ) {
243+ mu .Lock ()
244+ invalidCalled = true
245+ mu .Unlock ()
246+ }
247+
248+ fd , err := NewDialer (options )
249+ if err != nil {
250+ t .Fatalf ("couldn't create fastdialer instance: %s" , err )
251+ }
252+ defer fd .Close ()
253+
254+ ctx := context .Background ()
255+ conn , err := fd .Dial (ctx , "tcp" , "www.projectdiscovery.io:80" )
256+ if err != NoAddressAllowedError {
257+ if conn != nil {
258+ _ = conn .Close ()
259+ }
260+ t .Fatalf ("expected NoAddressAllowedError, got: %v" , err )
261+ }
262+
263+ if ! validateCalled {
264+ t .Error ("OnValidateTarget was not called" )
265+ }
266+
267+ mu .Lock ()
268+ called := invalidCalled
269+ mu .Unlock ()
270+ if ! called {
271+ t .Error ("OnInvalidTarget was not called for an invalid target" )
272+ }
273+ })
274+ }
0 commit comments