Skip to content

Commit 6facf71

Browse files
committed
Various small fixes following PR's suggestions
1 parent e39fa0f commit 6facf71

File tree

6 files changed

+70
-34
lines changed

6 files changed

+70
-34
lines changed

src/MongoDB.Driver/Core/Connections/Socks5Helper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
using System.Threading;
2424
using System.Threading.Tasks;
2525
using MongoDB.Driver.Core.Misc;
26-
using MongoDB.Driver.GridFS;
26+
using MongoDB.Driver.Support;
2727

2828
namespace MongoDB.Driver.Core.Connections;
2929

@@ -321,7 +321,7 @@ private static void EnsureProtocolVersion(byte version)
321321

322322
private static void EnsureSocksSuccess(byte code, string operation)
323323
{
324-
if (code == 0x00)
324+
if (code == Socks5Success)
325325
{
326326
return; // success
327327
}

src/MongoDB.Driver/Core/Connections/Socks5ProxyStreamFactory.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace MongoDB.Driver.Core.Connections;
2424

25-
internal class Socks5ProxyStreamFactory: IStreamFactory
25+
internal sealed class Socks5ProxyStreamFactory : IStreamFactory
2626
{
2727
private readonly Socks5ProxyStreamSettings _settings;
2828
private readonly IStreamFactory _wrapped;
@@ -35,17 +35,37 @@ public Socks5ProxyStreamFactory(Socks5ProxyStreamSettings settings, IStreamFacto
3535

3636
public Stream CreateStream(EndPoint endPoint, CancellationToken cancellationToken)
3737
{
38-
var proxyEndpoint = new DnsEndPoint(_settings.Socks5ProxySettings.Host, _settings.Socks5ProxySettings.Port);
39-
var stream = _wrapped.CreateStream(proxyEndpoint, cancellationToken);
40-
Socks5Helper.PerformSocks5Handshake(stream, endPoint, _settings.Socks5ProxySettings.Authentication, cancellationToken);
41-
return stream;
38+
Stream stream = null;
39+
40+
try
41+
{
42+
var proxyEndpoint = new DnsEndPoint(_settings.Socks5ProxySettings.Host, _settings.Socks5ProxySettings.Port);
43+
stream = _wrapped.CreateStream(proxyEndpoint, cancellationToken);
44+
Socks5Helper.PerformSocks5Handshake(stream, endPoint, _settings.Socks5ProxySettings.Authentication, cancellationToken);
45+
return stream;
46+
}
47+
catch
48+
{
49+
stream?.Dispose();
50+
throw;
51+
}
4252
}
4353

4454
public async Task<Stream> CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
4555
{
46-
var proxyEndpoint = new DnsEndPoint(_settings.Socks5ProxySettings.Host, _settings.Socks5ProxySettings.Port);
47-
var stream = await _wrapped.CreateStreamAsync(proxyEndpoint, cancellationToken).ConfigureAwait(false);
48-
await Socks5Helper.PerformSocks5HandshakeAsync(stream, endPoint, _settings.Socks5ProxySettings.Authentication, cancellationToken).ConfigureAwait(false);
49-
return stream;
56+
Stream stream = null;
57+
58+
try
59+
{
60+
var proxyEndpoint = new DnsEndPoint(_settings.Socks5ProxySettings.Host, _settings.Socks5ProxySettings.Port);
61+
stream = await _wrapped.CreateStreamAsync(proxyEndpoint, cancellationToken).ConfigureAwait(false);
62+
await Socks5Helper.PerformSocks5HandshakeAsync(stream, endPoint, _settings.Socks5ProxySettings.Authentication, cancellationToken).ConfigureAwait(false);
63+
return stream;
64+
}
65+
catch
66+
{
67+
stream?.Dispose();
68+
throw;
69+
}
5070
}
5171
}

src/MongoDB.Driver/GridFS/GridFSBucket.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using MongoDB.Driver.Core.Misc;
2929
using MongoDB.Driver.Core.Operations;
3030
using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
31+
using MongoDB.Driver.Support;
3132

3233
namespace MongoDB.Driver.GridFS
3334
{

src/MongoDB.Driver/MongoClientSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ public override string ToString()
11881188
sb.AppendFormat("MaxConnectionLifeTime={0};", _maxConnectionLifeTime);
11891189
sb.AppendFormat("MaxConnectionPoolSize={0};", _maxConnectionPoolSize);
11901190
sb.AppendFormat("MinConnectionPoolSize={0};", _minConnectionPoolSize);
1191-
if (_socks5ProxySettings!= null)
1191+
if (_socks5ProxySettings != null)
11921192
{
11931193
sb.AppendFormat("ProxyHost={0};", _socks5ProxySettings);
11941194
}

src/MongoDB.Driver/GridFS/StreamExtensions.cs renamed to src/MongoDB.Driver/Support/StreamExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using System.Threading;
1818
using System.Threading.Tasks;
1919

20-
namespace MongoDB.Driver.GridFS
20+
namespace MongoDB.Driver.Support
2121
{
2222
internal static class StreamExtensions
2323
{

tests/MongoDB.Driver.Tests/Core/Connections/Socks5AuthenticationSettingsTests.cs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,45 @@ public void None_should_return_NoAuthenticationSettings_instance()
3636
public void UsernamePassword_should_return_UsernamePasswordAuthenticationSettings_instance_with_correct_values()
3737
{
3838
var up = Socks5AuthenticationSettings.UsernamePassword("user", "pass");
39-
var upcast = up.Should() .BeOfType<Socks5AuthenticationSettings.UsernamePasswordAuthenticationSettings>().Subject;
39+
var upcast = up.Should().BeOfType<Socks5AuthenticationSettings.UsernamePasswordAuthenticationSettings>().Subject;
4040
upcast.Username.Should().Be("user");
4141
upcast.Password.Should().Be("pass");
4242
}
4343

4444
[Theory]
45-
[InlineData(null, "pass")]
46-
[InlineData("user", null)]
47-
[InlineData("", "pass")]
48-
[InlineData("user", "")]
49-
public void UsernamePassword_should_throw_when_username_or_password_is_null_or_empty(string username, string password)
45+
[InlineData(null)]
46+
[InlineData("")]
47+
public void UsernamePassword_should_throw_when_username_is_null_or_empty(string username)
5048
{
51-
var ex = Record.Exception(() => Socks5AuthenticationSettings.UsernamePassword(username, password));
49+
var ex = Record.Exception(() => Socks5AuthenticationSettings.UsernamePassword(username, "pass"));
5250
ex.Should().BeAssignableTo<ArgumentException>();
51+
ex.Message.Should().Contain("username");
5352
}
5453

5554
[Theory]
56-
[InlineData(TooLong, "pass")]
57-
[InlineData("user", TooLong)]
58-
public void UsernamePassword_should_throw_when_username_or_password_are_too_long(string username, string password)
55+
[InlineData(null)]
56+
[InlineData("")]
57+
public void UsernamePassword_should_throw_when_password_is_null_or_empty(string password)
5958
{
60-
var ex = Record.Exception(() => Socks5AuthenticationSettings.UsernamePassword(username, password));
59+
var ex = Record.Exception(() => Socks5AuthenticationSettings.UsernamePassword("username", password));
6160
ex.Should().BeAssignableTo<ArgumentException>();
61+
ex.Message.Should().Contain("password");
62+
}
63+
64+
[Fact]
65+
public void UsernamePassword_should_throw_when_username_is_too_long()
66+
{
67+
var ex = Record.Exception(() => Socks5AuthenticationSettings.UsernamePassword(TooLong, "password"));
68+
ex.Should().BeAssignableTo<ArgumentException>();
69+
ex.Message.Should().Contain("username");
70+
}
71+
72+
[Fact]
73+
public void UsernamePassword_should_throw_when_password_is_too_long()
74+
{
75+
var ex = Record.Exception(() => Socks5AuthenticationSettings.UsernamePassword("username", TooLong));
76+
ex.Should().BeAssignableTo<ArgumentException>();
77+
ex.Message.Should().Contain("password");
6278
}
6379

6480
[Fact]
@@ -73,21 +89,20 @@ public void NoAuthenticationSettings_Equals_and_GetHashCode_should_work_correctl
7389
none.GetHashCode().Should().NotBe(up.GetHashCode());
7490
}
7591

76-
[Fact]
77-
public void UsernamePasswordAuthenticationSettings_Equals_and_GetHashCode_should_work_correctly()
92+
[Theory]
93+
[InlineData("u", "p", "u", "p", true)]
94+
[InlineData("u", "p", "u", "x", false)]
95+
[InlineData("u", "p", "x", "p", false)]
96+
public void UsernamePasswordAuthenticationSettings_Equals_and_GetHashCode_should_work_correctly(string u1, string p1, string u2, string p2, bool areEqual)
7897
{
79-
var up1 = Socks5AuthenticationSettings.UsernamePassword("u", "p");
98+
var up1 = Socks5AuthenticationSettings.UsernamePassword(u1, p1);
8099

81100
var none = Socks5AuthenticationSettings.None;
82101
up1.Equals(none).Should().BeFalse();
83102
up1.GetHashCode().Should().NotBe(none.GetHashCode());
84103

85-
var up2 = Socks5AuthenticationSettings.UsernamePassword("u", "p");
86-
up1.Equals(up2).Should().BeTrue();
87-
up1.GetHashCode().Should().Be(up2.GetHashCode());
88-
89-
var up3 = Socks5AuthenticationSettings.UsernamePassword("u", "x");
90-
up1.Equals(up3).Should().BeFalse();
91-
up1.GetHashCode().Should().NotBe(up3.GetHashCode());
104+
var up2 = Socks5AuthenticationSettings.UsernamePassword(u2, p2);
105+
up1.Equals(up2).Should().Be(areEqual);
106+
up1.GetHashCode().Equals(up2.GetHashCode()).Should().Be(areEqual);
92107
}
93108
}

0 commit comments

Comments
 (0)