1
+ /* Copyright 2010-present MongoDB Inc.
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ using System ;
17
+ using FluentAssertions ;
18
+ using Xunit ;
19
+
20
+ namespace MongoDB . Driver . Core . Connections ;
21
+
22
+ public class Socks5ProxySettingsConnectionTest
23
+ {
24
+ [ Fact ]
25
+ public void Constructor_should_set_properties_correctly_with_host_only ( )
26
+ {
27
+ var settings = new Socks5ProxySettings ( "localhost" ) ;
28
+ settings . Host . Should ( ) . Be ( "localhost" ) ;
29
+ settings . Port . Should ( ) . Be ( 1080 ) ;
30
+ settings . Authentication . Should ( ) . Be ( Socks5AuthenticationSettings . None ) ;
31
+ }
32
+
33
+ [ Fact ]
34
+ public void Constructor_should_set_properties_correctly_with_host_and_port ( )
35
+ {
36
+ var settings = new Socks5ProxySettings ( "localhost" , 1234 ) ;
37
+ settings . Host . Should ( ) . Be ( "localhost" ) ;
38
+ settings . Port . Should ( ) . Be ( 1234 ) ;
39
+ settings . Authentication . Should ( ) . Be ( Socks5AuthenticationSettings . None ) ;
40
+ }
41
+
42
+ [ Fact ]
43
+ public void Constructor_should_set_properties_correctly_with_host_and_authentication ( )
44
+ {
45
+ var auth = Socks5AuthenticationSettings . UsernamePassword ( "user" , "pass" ) ;
46
+ var settings = new Socks5ProxySettings ( "localhost" , auth ) ;
47
+ settings . Host . Should ( ) . Be ( "localhost" ) ;
48
+ settings . Port . Should ( ) . Be ( 1080 ) ;
49
+ settings . Authentication . Should ( ) . Be ( auth ) ;
50
+ }
51
+
52
+ [ Fact ]
53
+ public void Constructor_should_set_properties_correctly_with_host_port_and_authentication ( )
54
+ {
55
+ var auth = Socks5AuthenticationSettings . UsernamePassword ( "user" , "pass" ) ;
56
+ var settings = new Socks5ProxySettings ( "localhost" , 1234 , auth ) ;
57
+ settings . Host . Should ( ) . Be ( "localhost" ) ;
58
+ settings . Port . Should ( ) . Be ( 1234 ) ;
59
+ settings . Authentication . Should ( ) . Be ( auth ) ;
60
+ }
61
+
62
+ [ Theory ]
63
+ [ InlineData ( null ) ]
64
+ [ InlineData ( "" ) ]
65
+ public void Constructor_should_throw_when_host_is_null_or_empty ( string host )
66
+ {
67
+ var ex = Record . Exception ( ( ) => new Socks5ProxySettings ( host ) ) ;
68
+ ex . Should ( ) . BeAssignableTo < ArgumentException > ( ) ;
69
+ }
70
+
71
+ [ Theory ]
72
+ [ InlineData ( 0 ) ]
73
+ [ InlineData ( 65536 ) ]
74
+ public void Constructor_should_throw_when_port_is_out_of_range ( int port )
75
+ {
76
+ var ex = Record . Exception ( ( ) => new Socks5ProxySettings ( "localhost" , port ) ) ;
77
+ ex . Should ( ) . BeOfType < ArgumentOutOfRangeException > ( ) ;
78
+ }
79
+
80
+ [ Fact ]
81
+ public void Constructor_should_throw_when_authentication_is_null ( )
82
+ {
83
+ var ex = Record . Exception ( ( ) => new Socks5ProxySettings ( "localhost" , 1080 , null ) ) ;
84
+ ex . Should ( ) . BeOfType < ArgumentNullException > ( ) ;
85
+ }
86
+
87
+ [ Fact ]
88
+ public void Equals_and_GetHashCode_should_work_for_Socks5ProxySettings ( )
89
+ {
90
+ var auth1 = Socks5AuthenticationSettings . UsernamePassword ( "user" , "pass" ) ;
91
+ var auth2 = Socks5AuthenticationSettings . UsernamePassword ( "user" , "pass" ) ;
92
+ var s1 = new Socks5ProxySettings ( "host" , 1234 , auth1 ) ;
93
+ var s2 = new Socks5ProxySettings ( "host" , 1234 , auth2 ) ;
94
+ s1 . Equals ( s2 ) . Should ( ) . BeTrue ( ) ;
95
+ s1 . GetHashCode ( ) . Should ( ) . Be ( s2 . GetHashCode ( ) ) ;
96
+ }
97
+
98
+ [ Fact ]
99
+ public void ToString_should_return_expected_string_for_no_auth ( )
100
+ {
101
+ var s = new Socks5ProxySettings ( "host" ) ;
102
+ s . ToString ( ) . Should ( ) . Contain ( "Host : host" ) . And . Contain ( "Port : 1080" ) . And . Contain ( "Authentication : None" ) ;
103
+ }
104
+
105
+ [ Fact ]
106
+ public void ToString_should_return_expected_string_for_username_password_auth ( )
107
+ {
108
+ var s = new Socks5ProxySettings ( "host" , 1234 , Socks5AuthenticationSettings . UsernamePassword ( "u" , "p" ) ) ;
109
+ s . ToString ( ) . Should ( ) . Contain ( "Host : host" )
110
+ . And . Contain ( "Port : 1234" )
111
+ . And . Contain ( "Username: u" )
112
+ . And . Contain ( "Password: p" ) ;
113
+ }
114
+
115
+ [ Fact ]
116
+ public void Create_should_return_expected_settings ( )
117
+ {
118
+ var s = Socks5ProxySettings . Create ( "host" , 1234 , "u" , "p" ) ;
119
+ s . Host . Should ( ) . Be ( "host" ) ;
120
+ s . Port . Should ( ) . Be ( 1234 ) ;
121
+ s . Authentication . Should ( ) . BeOfType < Socks5AuthenticationSettings . UsernamePasswordAuthenticationSettings > ( ) ;
122
+ var up = ( Socks5AuthenticationSettings . UsernamePasswordAuthenticationSettings ) s . Authentication ;
123
+ up . Username . Should ( ) . Be ( "u" ) ;
124
+ up . Password . Should ( ) . Be ( "p" ) ;
125
+ }
126
+
127
+ [ Fact ]
128
+ public void Create_should_return_no_auth_when_username_or_password_is_null ( )
129
+ {
130
+ var s1 = Socks5ProxySettings . Create ( "host" , 1234 , null , "p" ) ;
131
+ var s2 = Socks5ProxySettings . Create ( "host" , 1234 , "u" , null ) ;
132
+ s1 . Authentication . Should ( ) . Be ( Socks5AuthenticationSettings . None ) ;
133
+ s2 . Authentication . Should ( ) . Be ( Socks5AuthenticationSettings . None ) ;
134
+ }
135
+
136
+ // Socks5AuthenticationSettings tests
137
+ [ Fact ]
138
+ public void None_should_return_NoAuthenticationSettings_instance ( )
139
+ {
140
+ var none = Socks5AuthenticationSettings . None ;
141
+ none . Should ( ) . BeOfType < Socks5AuthenticationSettings . NoAuthenticationSettings > ( ) ;
142
+ }
143
+
144
+ [ Fact ]
145
+ public void UsernamePassword_should_return_UsernamePasswordAuthenticationSettings_instance_with_correct_values ( )
146
+ {
147
+ var up = Socks5AuthenticationSettings . UsernamePassword ( "user" , "pass" ) ;
148
+ up . Should ( ) . BeOfType < Socks5AuthenticationSettings . UsernamePasswordAuthenticationSettings > ( ) ;
149
+ var upcast = ( Socks5AuthenticationSettings . UsernamePasswordAuthenticationSettings ) up ;
150
+ upcast . Username . Should ( ) . Be ( "user" ) ;
151
+ upcast . Password . Should ( ) . Be ( "pass" ) ;
152
+ }
153
+
154
+ [ Theory ]
155
+ [ InlineData ( null , "pass" ) ]
156
+ [ InlineData ( "user" , null ) ]
157
+ [ InlineData ( "" , "pass" ) ]
158
+ [ InlineData ( "user" , "" ) ]
159
+ public void UsernamePassword_should_throw_when_username_or_password_is_null_or_empty ( string username , string password )
160
+ {
161
+ var ex = Record . Exception ( ( ) => Socks5AuthenticationSettings . UsernamePassword ( username , password ) ) ;
162
+ ex . Should ( ) . BeAssignableTo < ArgumentException > ( ) ;
163
+ }
164
+
165
+ [ Fact ]
166
+ public void NoAuthenticationSettings_Equals_should_return_true_for_any_Socks5AuthenticationSettings ( )
167
+ {
168
+ var none = Socks5AuthenticationSettings . None ;
169
+ none . Equals ( Socks5AuthenticationSettings . None ) . Should ( ) . BeTrue ( ) ;
170
+ none . Equals ( Socks5AuthenticationSettings . UsernamePassword ( "a" , "b" ) ) . Should ( ) . BeFalse ( ) ;
171
+ }
172
+
173
+ [ Fact ]
174
+ public void UsernamePasswordAuthenticationSettings_Equals_and_GetHashCode_should_work ( )
175
+ {
176
+ var a = Socks5AuthenticationSettings . UsernamePassword ( "u" , "p" ) ;
177
+ var b = Socks5AuthenticationSettings . UsernamePassword ( "u" , "p" ) ;
178
+ a . Equals ( b ) . Should ( ) . BeTrue ( ) ;
179
+ a . GetHashCode ( ) . Should ( ) . Be ( b . GetHashCode ( ) ) ;
180
+ var c = Socks5AuthenticationSettings . UsernamePassword ( "u" , "x" ) ;
181
+ a . Equals ( c ) . Should ( ) . BeFalse ( ) ;
182
+ }
183
+ }
0 commit comments