diff options
author | Steve Harter <steveharter@users.noreply.github.com> | 2016-09-29 04:10:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-29 04:10:31 +0300 |
commit | a02d1949ac894a9f53ba20a3baea976ca839d207 (patch) | |
tree | 578faeddcba0b73ed475114528b11c7bf39f7766 /src/System.Security.Cryptography.Encoding/tests | |
parent | 372248aa7cdc34ee7b23c6fcba0fa819e0fd6144 (diff) |
Port System.Security.Cryptography Base64 transform classes (#12071)
Diffstat (limited to 'src/System.Security.Cryptography.Encoding/tests')
5 files changed, 250 insertions, 15 deletions
diff --git a/src/System.Security.Cryptography.Encoding/tests/Base64TransformsTests.cs b/src/System.Security.Cryptography.Encoding/tests/Base64TransformsTests.cs new file mode 100644 index 0000000000..c7ced87de5 --- /dev/null +++ b/src/System.Security.Cryptography.Encoding/tests/Base64TransformsTests.cs @@ -0,0 +1,204 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.IO; +using Test.Cryptography; +using Xunit; + +namespace System.Security.Cryptography.Encoding.Tests +{ + public class Base64TransformsTests + { + public static IEnumerable<object[]> TestData_Ascii() + { + // Test data taken from RFC 4648 Test Vectors + yield return new object[] { "", "" }; + yield return new object[] { "f", "Zg==" }; + yield return new object[] { "fo", "Zm8=" }; + yield return new object[] { "foo", "Zm9v" }; + yield return new object[] { "foob", "Zm9vYg==" }; + yield return new object[] { "fooba", "Zm9vYmE=" }; + yield return new object[] { "foobar", "Zm9vYmFy" }; + } + + public static IEnumerable<object[]> TestData_LongBlock_Ascii() + { + yield return new object[] { "fooba", "Zm9vYmE=" }; + yield return new object[] { "foobar", "Zm9vYmFy" }; + } + + public static IEnumerable<object[]> TestData_Ascii_NoPadding() + { + // Test data without padding + yield return new object[] { "Zg" }; + yield return new object[] { "Zm9vYg" }; + yield return new object[] { "Zm9vYmE" }; + } + + public static IEnumerable<object[]> TestData_Ascii_Whitespace() + { + yield return new object[] { "fo", "\tZ\tm8=\n" }; + yield return new object[] { "foo", " Z m 9 v" }; + } + + [Fact] + public void InvalidInput_ToBase64Transform() + { + byte[] data_5bytes = Text.Encoding.ASCII.GetBytes("aaaaa"); + + using (var transform = new ToBase64Transform()) + { + InvalidInput_Base64Transform(transform); + + // These exceptions only thrown in ToBase + Assert.Throws<ArgumentOutOfRangeException>("offsetOut", () => transform.TransformFinalBlock(data_5bytes, 0, 5)); + } + } + + [Fact] + public void InvalidInput_FromBase64Transform() + { + byte[] data_4bytes = Text.Encoding.ASCII.GetBytes("aaaa"); + + ICryptoTransform transform = new FromBase64Transform(); + InvalidInput_Base64Transform(transform); + + // These exceptions only thrown in FromBase + transform.Dispose(); + Assert.Throws<ObjectDisposedException>(() => transform.TransformBlock(data_4bytes, 0, 4, null, 0)); + Assert.Throws<ObjectDisposedException>(() => transform.TransformFinalBlock(Array.Empty<byte>(), 0, 0)); + } + + private void InvalidInput_Base64Transform(ICryptoTransform transform) + { + byte[] data_4bytes = Text.Encoding.ASCII.GetBytes("aaaa"); + + Assert.Throws<ArgumentNullException>("inputBuffer", () => transform.TransformBlock(null, 0, 0, null, 0)); + Assert.Throws<ArgumentOutOfRangeException>("inputOffset", () => transform.TransformBlock(Array.Empty<byte>(), -1, 0, null, 0)); + Assert.Throws<ArgumentNullException>("dst", () => transform.TransformBlock(data_4bytes, 0, 4, null, 0)); + Assert.Throws<ArgumentException>(null, () => transform.TransformBlock(Array.Empty<byte>(), 0, 1, null, 0)); + Assert.Throws<ArgumentException>(null, () => transform.TransformBlock(Array.Empty<byte>(), 1, 0, null, 0)); + + Assert.Throws<ArgumentNullException>("inputBuffer", () => transform.TransformFinalBlock(null, 0, 0)); + Assert.Throws<ArgumentOutOfRangeException>("inputOffset", () => transform.TransformFinalBlock(Array.Empty<byte>(), -1, 0)); + Assert.Throws<ArgumentOutOfRangeException>("inputOffset", () => transform.TransformFinalBlock(Array.Empty<byte>(), -1, 0)); + Assert.Throws<ArgumentException>(null, () => transform.TransformFinalBlock(Array.Empty<byte>(), 1, 0)); + } + + [Theory, MemberData(nameof(TestData_Ascii))] + public static void ValidateToBase64CryptoStream(string data, string encoding) + { + using (var transform = new ToBase64Transform()) + { + ValidateCryptoStream(encoding, data, transform); + } + } + + [Theory, MemberData(nameof(TestData_Ascii))] + public static void ValidateFromBase64CryptoStream(string data, string encoding) + { + using (var transform = new FromBase64Transform()) + { + ValidateCryptoStream(data, encoding, transform); + } + } + + private static void ValidateCryptoStream(string expected, string data, ICryptoTransform transform) + { + byte[] inputBytes = Text.Encoding.ASCII.GetBytes(data); + byte[] outputBytes = new byte[100]; + + using (var ms = new MemoryStream(inputBytes)) + using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Read)) + { + int bytesRead = cs.Read(outputBytes, 0, outputBytes.Length); + string outputString = Text.Encoding.ASCII.GetString(outputBytes, 0, bytesRead); + Assert.Equal(expected, outputString); + } + } + + [Theory, MemberData(nameof(TestData_LongBlock_Ascii))] + public static void ValidateToBase64TransformFinalBlock(string data, string expected) + { + using (var transform = new ToBase64Transform()) + { + byte[] inputBytes = Text.Encoding.ASCII.GetBytes(data); + Assert.True(inputBytes.Length > 4); + + // Test passing blocks > 4 characters to TransformFinalBlock (not supported) + Assert.Throws<ArgumentOutOfRangeException>("offsetOut", () => transform.TransformFinalBlock(inputBytes, 0, inputBytes.Length)); + } + } + + [Theory, MemberData(nameof(TestData_LongBlock_Ascii))] + public static void ValidateFromBase64TransformFinalBlock(string expected, string encoding) + { + using (var transform = new FromBase64Transform()) + { + byte[] inputBytes = Text.Encoding.ASCII.GetBytes(encoding); + Assert.True(inputBytes.Length > 4); + + // Test passing blocks > 4 characters to TransformFinalBlock (supported) + byte[] outputBytes = transform.TransformFinalBlock(inputBytes, 0, inputBytes.Length); + string outputString = Text.Encoding.ASCII.GetString(outputBytes, 0, outputBytes.Length); + Assert.Equal(expected, outputString); + } + } + + [Theory, MemberData(nameof(TestData_Ascii_NoPadding))] + public static void ValidateFromBase64_NoPadding(string data) + { + using (var transform = new FromBase64Transform()) + { + byte[] inputBytes = Text.Encoding.ASCII.GetBytes(data); + byte[] outputBytes = new byte[100]; + + using (var ms = new MemoryStream(inputBytes)) + using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Read)) + { + int bytesRead = cs.Read(outputBytes, 0, outputBytes.Length); + + // Missing padding bytes not supported (no exception, however) + Assert.NotEqual(inputBytes.Length, bytesRead); + } + } + } + + [Theory, MemberData(nameof(TestData_Ascii_Whitespace))] + public static void ValidateWhitespace(string expected, string data) + { + byte[] inputBytes = Text.Encoding.ASCII.GetBytes(data); + byte[] outputBytes = new byte[100]; + + // Verify default of FromBase64TransformMode.IgnoreWhiteSpaces + using (var base64Transform = new FromBase64Transform()) + using (var ms = new MemoryStream(inputBytes)) + using (var cs = new CryptoStream(ms, base64Transform, CryptoStreamMode.Read)) + { + int bytesRead = cs.Read(outputBytes, 0, outputBytes.Length); + string outputString = Text.Encoding.ASCII.GetString(outputBytes, 0, bytesRead); + Assert.Equal(expected, outputString); + } + + // Verify explicit FromBase64TransformMode.IgnoreWhiteSpaces + using (var base64Transform = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces)) + using (var ms = new MemoryStream(inputBytes)) + using (var cs = new CryptoStream(ms, base64Transform, CryptoStreamMode.Read)) + { + int bytesRead = cs.Read(outputBytes, 0, outputBytes.Length); + string outputString = Text.Encoding.ASCII.GetString(outputBytes, 0, bytesRead); + Assert.Equal(expected, outputString); + } + + // Verify FromBase64TransformMode.DoNotIgnoreWhiteSpaces + using (var base64Transform = new FromBase64Transform(FromBase64TransformMode.DoNotIgnoreWhiteSpaces)) + using (var ms = new MemoryStream(inputBytes)) + using (var cs = new CryptoStream(ms, base64Transform, CryptoStreamMode.Read)) + { + Assert.Throws<FormatException>(() => cs.Read(outputBytes, 0, outputBytes.Length)); + } + } + } +} diff --git a/src/System.Security.Cryptography.Encoding/tests/Oid.cs b/src/System.Security.Cryptography.Encoding/tests/Oid.cs index 0e89ea7223..bc2f000069 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Oid.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Oid.cs @@ -175,7 +175,7 @@ namespace System.Security.Cryptography.Encoding.Tests [Theory] [MemberData(nameof(ValidOidFriendlyNameHashAlgorithmPairs))] - [PlatformSpecific(PlatformID.Windows)] + [PlatformSpecific(Xunit.PlatformID.Windows)] public static void LookupOidByValue_Method_WrongGroup(string oidValue, string friendlyName) { // Oid group is implemented strictly - no fallback to OidGroup.All as with many other parts of Crypto. @@ -231,7 +231,7 @@ namespace System.Security.Cryptography.Encoding.Tests [Theory] [MemberData(nameof(ValidOidFriendlyNameHashAlgorithmPairs))] - [PlatformSpecific(PlatformID.Windows)] + [PlatformSpecific(Xunit.PlatformID.Windows)] public static void LookupOidByFriendlyName_Method_WrongGroup(string oidValue, string friendlyName) { // Oid group is implemented strictly - no fallback to OidGroup.All as with many other parts of Crypto. @@ -253,7 +253,7 @@ namespace System.Security.Cryptography.Encoding.Tests } [Fact] - [PlatformSpecific(PlatformID.AnyUnix)] + [PlatformSpecific(Xunit.PlatformID.AnyUnix)] public static void LookupOidByValue_Method_UnixOnly() { // This needs to be an OID not in the static lookup table. The purpose is to verify the @@ -267,7 +267,7 @@ namespace System.Security.Cryptography.Encoding.Tests } [Fact] - [PlatformSpecific(PlatformID.AnyUnix)] + [PlatformSpecific(Xunit.PlatformID.AnyUnix)] public static void LookupOidByFriendlyName_Method_UnixOnly() { // This needs to be a name not in the static lookup table. The purpose is to verify the diff --git a/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.builds b/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.builds index 478303b742..5f029c7d7d 100644 --- a/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.builds +++ b/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.builds @@ -6,7 +6,16 @@ <OSGroup>Unix</OSGroup> </Project> <Project Include="System.Security.Cryptography.Encoding.Tests.csproj"> - <TestTFMs>netcore50;netcoreapp1.0;net46</TestTFMs> + <TargetGroup>netstandard1.7</TargetGroup> + <TestTFMs>netcoreapp1.1</TestTFMs> + <OSGroup>Unix</OSGroup> + </Project> + <Project Include="System.Security.Cryptography.Encoding.Tests.csproj"> + <OSGroup>Windows_NT</OSGroup> + </Project> + <Project Include="System.Security.Cryptography.Encoding.Tests.csproj"> + <TargetGroup>netstandard1.7</TargetGroup> + <TestTFMs>netcoreapp1.1;net463</TestTFMs> <OSGroup>Windows_NT</OSGroup> </Project> </ItemGroup> diff --git a/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.csproj b/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.csproj index 76d302590c..03fe8707b9 100644 --- a/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.csproj +++ b/src/System.Security.Cryptography.Encoding/tests/System.Security.Cryptography.Encoding.Tests.csproj @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition="'$(Configuration)'==''">Windows_Debug</Configuration> @@ -12,7 +12,7 @@ <AssemblyName>System.Security.Cryptography.Encoding.Tests</AssemblyName> <RootNamespace>System.Security.Cryptography.Encoding.Tests</RootNamespace> <AllowUnsafeBlocks>true</AllowUnsafeBlocks> - <NugetTargetMoniker>.NETStandard,Version=v1.3</NugetTargetMoniker> + <NugetTargetMoniker Condition="'$(NugetTargetMoniker)'==''">.NETStandard,Version=v1.3</NugetTargetMoniker> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\pkg\System.Security.Cryptography.Encoding.pkgproj"> @@ -37,8 +37,11 @@ <Link>CommonTest\System\Security\Cryptography\ByteUtils.cs</Link> </Compile> </ItemGroup> + <ItemGroup Condition="'$(TargetGroup)'=='netstandard1.7'"> + <Compile Include="Base64TransformsTests.cs" /> + </ItemGroup> <ItemGroup> <None Include="project.json" /> </ItemGroup> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> -</Project> +</Project>
\ No newline at end of file diff --git a/src/System.Security.Cryptography.Encoding/tests/project.json b/src/System.Security.Cryptography.Encoding/tests/project.json index 547abd08cc..3e4389806a 100644 --- a/src/System.Security.Cryptography.Encoding/tests/project.json +++ b/src/System.Security.Cryptography.Encoding/tests/project.json @@ -9,17 +9,18 @@ "System.Runtime.Numerics": "4.3.0-beta-24522-03", "System.Security.Cryptography.Primitives": "4.3.0-beta-24522-03", "System.Text.RegularExpressions": "4.3.0-beta-24522-03", - "test-runtime": { - "target": "project", - "exclude": "compile" - }, + "System.Xml.XmlSerializer": "4.3.0-beta-24522-03", "Microsoft.xunit.netcore.extensions": "1.0.0-prerelease-00807-03", "Microsoft.DotNet.BuildTools.TestSuite": "1.0.0-prerelease-00807-03", "Microsoft.DotNet.xunit.performance": "1.0.0-alpha-build0040", - "System.Xml.XmlSerializer": "4.3.0-beta-24522-03" + "test-runtime": { + "target": "project", + "exclude": "compile" + } }, "frameworks": { - "netstandard1.3": {} + "netstandard1.3": {}, + "netstandard1.7": {} }, "supports": { "coreFx.Test.netcore50": {}, @@ -27,6 +28,24 @@ "coreFx.Test.net46": {}, "coreFx.Test.net461": {}, "coreFx.Test.net462": {}, - "coreFx.Test.net463": {} + "coreFx.Test.net463": {}, + "coreFx.Test.netcoreapp1.1-ns17": { + "netstandard1.7": [ + "win7-x86", + "win7-x64", + "win10-arm64", + "osx.10.10-x64", + "centos.7-x64", + "debian.8-x64", + "rhel.7-x64", + "ubuntu.14.04-x64", + "ubuntu.16.04-x64", + "ubuntu.16.10-x64", + "fedora.23-x64", + "linux-x64", + "opensuse.13.2-x64", + "opensuse.42.1-x64" + ] + } } } |