| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- // Copyright (c) .NET Foundation. All rights reserved.
- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
- using System;
- using Microsoft.AspNetCore.Testing;
- using Xunit;
- namespace Microsoft.AspNetCore.DataProtection
- {
- public unsafe class SecretTests
- {
- [Fact]
- public void Ctor_ArraySegment_Default_Throws()
- {
- // Act & assert
- ExceptionAssert.ThrowsArgument(
- testCode: () => new Secret(default(ArraySegment<byte>)),
- paramName: "array",
- exceptionMessage: null);
- }
- [Fact]
- public void Ctor_ArraySegment_Success()
- {
- // Arrange
- var input = new ArraySegment<byte>(new byte[] { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60 }, 1, 3);
- // Act
- var secret = new Secret(input);
- input.Array[2] = 0xFF; // mutate original array - secret shouldn't be modified
- // Assert - length
- Assert.Equal(3, secret.Length);
- // Assert - managed buffer
- var outputSegment = new ArraySegment<byte>(new byte[7], 2, 3);
- secret.WriteSecretIntoBuffer(outputSegment);
- Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputSegment.AsStandaloneArray());
- // Assert - unmanaged buffer
- var outputBuffer = new byte[3];
- fixed (byte* pOutputBuffer = outputBuffer)
- {
- secret.WriteSecretIntoBuffer(pOutputBuffer, 3);
- }
- Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputBuffer);
- }
- [Fact]
- public void Ctor_Buffer_Success()
- {
- // Arrange
- var input = new byte[] { 0x20, 0x30, 0x40 };
- // Act
- var secret = new Secret(input);
- input[1] = 0xFF; // mutate original array - secret shouldn't be modified
- // Assert - length
- Assert.Equal(3, secret.Length);
- // Assert - managed buffer
- var outputSegment = new ArraySegment<byte>(new byte[7], 2, 3);
- secret.WriteSecretIntoBuffer(outputSegment);
- Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputSegment.AsStandaloneArray());
- // Assert - unmanaged buffer
- var outputBuffer = new byte[3];
- fixed (byte* pOutputBuffer = outputBuffer)
- {
- secret.WriteSecretIntoBuffer(pOutputBuffer, 3);
- }
- Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputBuffer);
- }
- [Fact]
- public void Ctor_Buffer_ZeroLength_Success()
- {
- // Act
- var secret = new Secret(new byte[0]);
- // Assert - none of these methods should throw
- Assert.Equal(0, secret.Length);
- secret.WriteSecretIntoBuffer(new ArraySegment<byte>(new byte[0]));
- byte dummy;
- secret.WriteSecretIntoBuffer(&dummy, 0);
- }
- [Fact]
- public void Ctor_Pointer_WithNullPointer_ThrowsArgumentNull()
- {
- // Act & assert
- ExceptionAssert.ThrowsArgumentNull(
- testCode: () => new Secret(null, 0),
- paramName: "secret");
- }
- [Fact]
- public void Ctor_Pointer_WithNegativeLength_ThrowsArgumentOutOfRange()
- {
- // Act & assert
- ExceptionAssert.ThrowsArgumentOutOfRange(
- testCode: () =>
- {
- byte dummy;
- new Secret(&dummy, -1);
- },
- paramName: "secretLength",
- exceptionMessage: Resources.Common_ValueMustBeNonNegative);
- }
- [Fact]
- public void Ctor_Pointer_ZeroLength_Success()
- {
- // Arrange
- byte input;
- // Act
- var secret = new Secret(&input, 0);
- // Assert - none of these methods should throw
- Assert.Equal(0, secret.Length);
- secret.WriteSecretIntoBuffer(new ArraySegment<byte>(new byte[0]));
- byte dummy;
- secret.WriteSecretIntoBuffer(&dummy, 0);
- }
- [Fact]
- public void Ctor_Pointer_Success()
- {
- // Arrange
- byte* input = stackalloc byte[3];
- input[0] = 0x20;
- input[1] = 0x30;
- input[2] = 0x40;
- // Act
- var secret = new Secret(input, 3);
- input[1] = 0xFF; // mutate original buffer - secret shouldn't be modified
- // Assert - length
- Assert.Equal(3, secret.Length);
- // Assert - managed buffer
- var outputSegment = new ArraySegment<byte>(new byte[7], 2, 3);
- secret.WriteSecretIntoBuffer(outputSegment);
- Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputSegment.AsStandaloneArray());
- // Assert - unmanaged buffer
- var outputBuffer = new byte[3];
- fixed (byte* pOutputBuffer = outputBuffer)
- {
- secret.WriteSecretIntoBuffer(pOutputBuffer, 3);
- }
- Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputBuffer);
- }
- [Fact]
- public void Random_ZeroLength_Success()
- {
- // Act
- var secret = Secret.Random(0);
- // Assert
- Assert.Equal(0, secret.Length);
- }
- [Fact]
- public void Random_LengthIsMultipleOf16_Success()
- {
- // Act
- var secret = Secret.Random(32);
- // Assert
- Assert.Equal(32, secret.Length);
- Guid* pGuids = stackalloc Guid[2];
- secret.WriteSecretIntoBuffer((byte*)pGuids, 32);
- Assert.NotEqual(Guid.Empty, pGuids[0]);
- Assert.NotEqual(Guid.Empty, pGuids[1]);
- Assert.NotEqual(pGuids[0], pGuids[1]);
- }
- [Fact]
- public void Random_LengthIsNotMultipleOf16_Success()
- {
- // Act
- var secret = Secret.Random(31);
- // Assert
- Assert.Equal(31, secret.Length);
- Guid* pGuids = stackalloc Guid[2];
- secret.WriteSecretIntoBuffer((byte*)pGuids, 31);
- Assert.NotEqual(Guid.Empty, pGuids[0]);
- Assert.NotEqual(Guid.Empty, pGuids[1]);
- Assert.NotEqual(pGuids[0], pGuids[1]);
- Assert.Equal(0, ((byte*)pGuids)[31]); // last byte shouldn't have been overwritten
- }
- [Fact]
- public void WriteSecretIntoBuffer_ArraySegment_IncorrectlySizedBuffer_Throws()
- {
- // Arrange
- var secret = Secret.Random(16);
- // Act & assert
- ExceptionAssert.ThrowsArgument(
- testCode: () => secret.WriteSecretIntoBuffer(new ArraySegment<byte>(new byte[100])),
- paramName: "buffer",
- exceptionMessage: Resources.FormatCommon_BufferIncorrectlySized(100, 16));
- }
- [Fact]
- public void WriteSecretIntoBuffer_ArraySegment_Disposed_Throws()
- {
- // Arrange
- var secret = Secret.Random(16);
- secret.Dispose();
- // Act & assert
- Assert.Throws<ObjectDisposedException>(
- testCode: () => secret.WriteSecretIntoBuffer(new ArraySegment<byte>(new byte[16])));
- }
- [Fact]
- public void WriteSecretIntoBuffer_Pointer_NullBuffer_Throws()
- {
- // Arrange
- var secret = Secret.Random(16);
- // Act & assert
- ExceptionAssert.ThrowsArgumentNull(
- testCode: () => secret.WriteSecretIntoBuffer(null, 100),
- paramName: "buffer");
- }
- [Fact]
- public void WriteSecretIntoBuffer_Pointer_IncorrectlySizedBuffer_Throws()
- {
- // Arrange
- var secret = Secret.Random(16);
- // Act & assert
- ExceptionAssert.ThrowsArgument(
- testCode: () =>
- {
- byte* pBuffer = stackalloc byte[100];
- secret.WriteSecretIntoBuffer(pBuffer, 100);
- },
- paramName: "bufferLength",
- exceptionMessage: Resources.FormatCommon_BufferIncorrectlySized(100, 16));
- }
- [Fact]
- public void WriteSecretIntoBuffer_Pointer_Disposed_Throws()
- {
- // Arrange
- var secret = Secret.Random(16);
- secret.Dispose();
- // Act & assert
- Assert.Throws<ObjectDisposedException>(
- testCode: () =>
- {
- byte* pBuffer = stackalloc byte[16];
- secret.WriteSecretIntoBuffer(pBuffer, 16);
- });
- }
- }
- }
|