From 0acec608abe08d41c7b420a30a3ccb4e03615e4a Mon Sep 17 00:00:00 2001 From: Bruce Bowyer-Smyth Date: Sun, 11 Sep 2016 07:49:19 +1000 Subject: [PATCH 1/2] Adds a leaveOpen ctor to CryptoStream so that the internal stream is not disposed --- ...System.Security.Cryptography.Primitives.cs | 1 + .../Security/Cryptography/CryptoStream.cs | 14 +++++++-- .../tests/CryptoStream.cs | 29 +++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs index b2c086fa363f..80fbdc76e2d5 100644 --- a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs +++ b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs @@ -44,6 +44,7 @@ public CryptographicUnexpectedOperationException(string format, string insert) { public partial class CryptoStream : System.IO.Stream, System.IDisposable { public CryptoStream(System.IO.Stream stream, System.Security.Cryptography.ICryptoTransform transform, System.Security.Cryptography.CryptoStreamMode mode) { } + public CryptoStream(System.IO.Stream stream, System.Security.Cryptography.ICryptoTransform transform, System.Security.Cryptography.CryptoStreamMode mode, bool leaveOpen) { } public override bool CanRead { get { throw null; } } public override bool CanSeek { get { throw null; } } public override bool CanWrite { get { throw null; } } diff --git a/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs b/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs index db81acde2f9f..d51ec1e9bdca 100644 --- a/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs +++ b/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs @@ -25,15 +25,22 @@ public class CryptoStream : Stream, IDisposable private bool _canWrite; private bool _finalBlockTransformed; private SemaphoreSlim _lazyAsyncActiveSemaphore; - + private readonly bool _leaveOpen; + // Constructors public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode) + : this(stream, transform, mode, false) + { + } + + public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode, bool leaveOpen) { _stream = stream; _transformMode = mode; _transform = transform; + _leaveOpen = leaveOpen; switch (_transformMode) { case CryptoStreamMode.Read: @@ -523,7 +530,10 @@ protected override void Dispose(bool disposing) { FlushFinalBlock(); } - _stream.Dispose(); + if (!_leaveOpen) + { + _stream.Dispose(); + } } } finally diff --git a/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs b/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs index e99e7891dd21..424586076ac6 100644 --- a/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs +++ b/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs @@ -162,10 +162,35 @@ public static void NestedCryptoStreams() public static void MultipleDispose() { ICryptoTransform encryptor = new IdentityTransform(1, 1, true); + + using (MemoryStream output = new MemoryStream()) + { + using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write)) + { + encryptStream.Dispose(); + } + + Assert.Equal(false, output.CanRead); + } + + using (MemoryStream output = new MemoryStream()) + { + using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write, leaveOpen: false)) + { + encryptStream.Dispose(); + } + + Assert.Equal(false, output.CanRead); + } + using (MemoryStream output = new MemoryStream()) - using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write)) { - encryptStream.Dispose(); + using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write, leaveOpen: true)) + { + encryptStream.Dispose(); + } + + Assert.Equal(true, output.CanRead); } } From 3e33ffde6932f78d1bc7089ea975acf92d81a319 Mon Sep 17 00:00:00 2001 From: Jeremy Barton Date: Mon, 17 Oct 2016 08:55:19 -0700 Subject: [PATCH 2/2] Contract work for CryptoStream leaveOpen ctor --- .../System.Security.Cryptography.Primitives.pkgproj | 4 ++-- .../System.Security.Cryptography.Primitives.builds | 11 +++++++++++ .../ref/System.Security.Cryptography.Primitives.cs | 2 ++ .../System.Security.Cryptography.Primitives.csproj | 5 +++-- .../ref/project.json | 7 ++----- .../tests/CryptoStream.cs | 2 ++ ...stem.Security.Cryptography.Primitives.Tests.builds | 4 ++++ ...stem.Security.Cryptography.Primitives.Tests.csproj | 1 + 8 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.builds diff --git a/src/System.Security.Cryptography.Primitives/pkg/System.Security.Cryptography.Primitives.pkgproj b/src/System.Security.Cryptography.Primitives/pkg/System.Security.Cryptography.Primitives.pkgproj index 1b208f27f49b..11ddde4e5e3b 100644 --- a/src/System.Security.Cryptography.Primitives/pkg/System.Security.Cryptography.Primitives.pkgproj +++ b/src/System.Security.Cryptography.Primitives/pkg/System.Security.Cryptography.Primitives.pkgproj @@ -2,7 +2,7 @@ - + net463;netcoreapp1.1;$(AllXamarinFrameworks) @@ -16,4 +16,4 @@ - \ No newline at end of file + diff --git a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.builds b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.builds new file mode 100644 index 000000000000..ccf56d6c0615 --- /dev/null +++ b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.builds @@ -0,0 +1,11 @@ + + + + + + + netcoreapp1.1 + + + + diff --git a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs index 80fbdc76e2d5..3d16b625a224 100644 --- a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs +++ b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs @@ -44,7 +44,9 @@ public CryptographicUnexpectedOperationException(string format, string insert) { public partial class CryptoStream : System.IO.Stream, System.IDisposable { public CryptoStream(System.IO.Stream stream, System.Security.Cryptography.ICryptoTransform transform, System.Security.Cryptography.CryptoStreamMode mode) { } +#if netcoreapp11 public CryptoStream(System.IO.Stream stream, System.Security.Cryptography.ICryptoTransform transform, System.Security.Cryptography.CryptoStreamMode mode, bool leaveOpen) { } +#endif public override bool CanRead { get { throw null; } } public override bool CanSeek { get { throw null; } } public override bool CanWrite { get { throw null; } } diff --git a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.csproj b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.csproj index 8d3c6e0a3775..ce2107915c61 100644 --- a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.csproj +++ b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.csproj @@ -3,7 +3,8 @@ Library - .NETStandard,Version=v1.7 + .NETStandard,Version=v1.7 + $(DefineConstants);netcoreapp11 @@ -12,4 +13,4 @@ - \ No newline at end of file + diff --git a/src/System.Security.Cryptography.Primitives/ref/project.json b/src/System.Security.Cryptography.Primitives/ref/project.json index 0fb177a70a05..d4cc523c4762 100644 --- a/src/System.Security.Cryptography.Primitives/ref/project.json +++ b/src/System.Security.Cryptography.Primitives/ref/project.json @@ -5,10 +5,7 @@ "System.Threading.Tasks": "4.4.0-beta-24615-03" }, "frameworks": { - "netstandard1.7": { - "imports": [ - "dotnet5.8" - ] - } + "netstandard1.7": {}, + "netcoreapp1.1": {} } } diff --git a/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs b/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs index 424586076ac6..1dcdb3aeebbb 100644 --- a/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs +++ b/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs @@ -173,6 +173,7 @@ public static void MultipleDispose() Assert.Equal(false, output.CanRead); } +#if netcoreapp11 using (MemoryStream output = new MemoryStream()) { using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write, leaveOpen: false)) @@ -192,6 +193,7 @@ public static void MultipleDispose() Assert.Equal(true, output.CanRead); } +#endif } private const string LoremText = diff --git a/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.builds b/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.builds index c14835b4f50c..8d8aae0cba92 100644 --- a/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.builds +++ b/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.builds @@ -8,6 +8,10 @@ Windows_NT netcoreapp1.0 + + netcoreapp1.1 + netcoreapp1.1 + diff --git a/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj b/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj index 7b51198943ef..e725167b5224 100644 --- a/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj +++ b/src/System.Security.Cryptography.Primitives/tests/System.Security.Cryptography.Primitives.Tests.csproj @@ -10,6 +10,7 @@ System.Security.Cryptography.Primitives.Tests System.Security.Cryptography.Primitives.Tests .NETStandard,Version=v1.7 + $(DefineConstants);netcoreapp11