/// <inheritdoc />
        /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/>
        /// <exception cref="ArgumentException">Throws if the argument is empty</exception>
        public async Task AppendAsync(ISafeBytes safeBytes)
        {
            ThrowIfDisposed();
            if (safeBytes.Length == 0)
            {
                throw new ArgumentException($"{nameof(safeBytes)} is empty.");
            }
            // If it's the known SafeBytes then it reveals nothing in the memory
            if (safeBytes is SafeBytes asSafeBytes)
            {
                var allBytes = await asSafeBytes._safeByteCollection.GetAllAsync()
                               .ConfigureAwait(false);

                await _safeByteCollection.AppendManyAsync(allBytes)
                .ConfigureAwait(false);

                foreach (var safeByte in allBytes)
                {
                    UpdateHashCode(safeByte);
                }
            }
            // If it's not, then reveals each byte in memory.
            else
            {
                var plainBytes = await safeBytes.RevealDecryptedBytesAsync()
                                 .ConfigureAwait(false);

                var stream = new SafeMemoryStream(plainBytes);
                await AppendManyAsync(stream).ConfigureAwait(false);
            }
        }
Beispiel #2
0
        public async Task GetHashCode_AppendedByCombinedMethods_ReturnsSame()
        {
            // Arrange
            var bytes = new byte[] { 5, 10, 15 };

            using var first = GetSut();

            await first.AppendAsync(bytes[0]);

            var safeBytes = GetSut();
            await safeBytes.AppendAsync(bytes[1]);

            await first.AppendAsync(safeBytes);

            var stream = new SafeMemoryStream(new[] { bytes[2] });
            await first.AppendManyAsync(stream);

            var second = GetSut();

            stream = new SafeMemoryStream(bytes);
            await second.AppendManyAsync(stream);

            // Act
            var hashFirst  = first.GetHashCode();
            var hashSecond = second.GetHashCode();

            // Assert
            Assert.AreEqual(hashFirst, hashSecond);
        }
Beispiel #3
0
        public async Task EqualsAsync_10KBSameSafeBytes_TakesLessThan200ms()
        {
            // Arrange
            const int expectedHigherLimit = 200;
            const int totalBytes          = 10000;
            await SafeOrbitCore.Current.StartEarlyAsync();

            var bytes = new byte[totalBytes];

            new Random().NextBytes(bytes);

            var sut    = GetSut();
            var stream = new SafeMemoryStream(bytes.CopyToNewArray());
            await sut.AppendManyAsync(stream);

            var other = new SafeBytes();

            stream = new SafeMemoryStream(bytes);
            await other.AppendManyAsync(stream);

            // Act
            var actualPerformance = await MeasureAsync(
                () => sut.EqualsAsync(other), 5);

            // Assert
            Assert.That(actualPerformance, Is.LessThanOrEqualTo(expectedHigherLimit));
        }
        /// <inheritdoc />
        /// <inheritdoc cref="AppendManyAsync(ISafeByte[])"/>
        public async Task AppendManyAsync(SafeMemoryStream stream)
        {
            var safeBytes = await _safeByteFactory.GetByBytesAsync(stream)
                            .ConfigureAwait(false);

            await AppendManyAsync(safeBytes as ISafeByte[] ?? safeBytes.ToArray())
            .ConfigureAwait(false);
        }
        public void Ctor_GivenBuffer_Clears()
        {
            // Arrange
            var buffer = new byte[] { 5, 10, 15, 20, 25, 30 };

            // Act
            _ = new SafeMemoryStream(buffer);

            // Assert
            Assert.That(buffer, Is.Empty.Or.All.EqualTo(0));
        }
Beispiel #6
0
        private async Task <ISafeBytes> TransformCharToSafeBytesAsync(char c, Encoding encoding)
        {
            var bytes  = _textService.GetBytes(c, encoding);
            var stream = new SafeMemoryStream();
            await stream.WriteAsync(bytes, 0, bytes.Length)
            .ConfigureAwait(false);

            var safeBytes = _safeBytesFactory.Create();
            await safeBytes.AppendManyAsync(stream).ConfigureAwait(false);

            return(safeBytes);
        }
Beispiel #7
0
        public async Task ToByteArrayAsync_MultipleBytesAddedAtOnce_ReturnsExpected()
        {
            // Arrange
            byte[] expected = { 5, 10, 15 };
            using var sut = GetSut();
            var stream = new SafeMemoryStream(expected.CopyToNewArray());

            // Act
            await sut.AppendManyAsync(stream);

            var actual = await sut.RevealDecryptedBytesAsync();

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
        public void Ctor_GivenBuffer_Writes()
        {
            // Arrange
            var expected = new byte[] { 5, 10, 15, 20, 25, 30 };
            var input    = expected.CopyToNewArray();

            // Act
            var sut = new SafeMemoryStream(input);

            // Assert
            var actual = new byte[expected.Length];

            sut.Read(actual, 0, expected.Length);
            CollectionAssert.AreEqual(expected, actual);
        }
Beispiel #9
0
        public async Task AppendManyAsync_SomeBytesAdded_CanRetrieve()
        {
            // Arrange
            using var sut = GetSut();
            var expected = new byte[] { 5, 10, 15 };

            using var stream = new SafeMemoryStream(expected.CopyToNewArray());

            // Act
            await sut.AppendManyAsync(stream);

            var actual = await sut.RevealDecryptedBytesAsync();

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Beispiel #10
0
        public async Task EqualsAsync_SafeBytesDifferentSameBytes_ReturnsFalse()
        {
            // Arrange
            using var sut = GetSut();
            var stream = new SafeMemoryStream(new byte[] { 1, 2, 3 });
            await sut.AppendManyAsync(stream);

            using var other = GetSut();
            stream          = new SafeMemoryStream(new byte[] { 4, 5 });
            await other.AppendManyAsync(stream);

            // Act
            var actual = await sut.EqualsAsync(other);

            // Assert
            Assert.IsFalse(actual);
        }
Beispiel #11
0
        public async Task EqualsAsync_SafeBytesHoldingSameBytes_ReturnsTrue()
        {
            // Arrange
            using var sut = GetSut();
            var bytes  = new byte[] { 5, 10, 15 };
            var stream = new SafeMemoryStream(bytes.CopyToNewArray());
            await sut.AppendManyAsync(stream);

            using var other = GetSut();
            stream          = new SafeMemoryStream(bytes.CopyToNewArray());
            await other.AppendManyAsync(stream);

            // Act
            var actual = await sut.EqualsAsync(other);

            // Assert
            Assert.IsTrue(actual);
        }
Beispiel #12
0
        public async Task AppendManyAsync_SingleMegaByteStream_TakesLessThan5000ms()
        {
            // Arrange
            await SafeOrbitCore.Current.StartEarlyAsync();

            const int expectedHigherLimit = 5000;
            var       sut   = GetSut();
            var       bytes = new byte[1000000];

            new Random().NextBytes(bytes);
            var stream = new SafeMemoryStream(bytes);

            // Act
            var actualPerformance = await MeasureAsync(() =>
                                                       sut.AppendManyAsync(stream));

            // Assert
            Assert.That(actualPerformance, Is.LessThanOrEqualTo(expectedHigherLimit));
        }
Beispiel #13
0
        public async Task GetHashCode_AppendedByByteAndMany_ReturnsSame()
        {
            // Arrange
            using var first = GetSut();
            await first.AppendAsync(1);

            await first.AppendAsync(2);

            var second = GetSut();
            var stream = new SafeMemoryStream(new byte[] { 1, 2 });
            await second.AppendManyAsync(stream);

            // Act
            var hashFirst  = first.GetHashCode();
            var hashSecond = second.GetHashCode();

            // Assert
            Assert.AreEqual(hashFirst, hashSecond);
        }
Beispiel #14
0
        private async Task <ISafeBytes> ConvertEncodingAsync(IReadOnlySafeBytes character, Encoding sourceEncoding, Encoding destinationEncoding)
        {
            var buffer = await character.RevealDecryptedBytesAsync().ConfigureAwait(false);

            try
            {
                buffer = _textService.Convert(sourceEncoding, destinationEncoding, buffer);
                var safeBytes = _safeBytesFactory.Create();
                var stream    = new SafeMemoryStream(buffer);
                await safeBytes.AppendManyAsync(stream)
                .ConfigureAwait(false);

                return(safeBytes);
            }
            finally
            {
                Array.Clear(buffer, 0, buffer.Length);
            }
        }
Beispiel #15
0
        /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/>
        public async Task <bool> EqualsAsync(IReadOnlySafeBytes other)
        {
            ThrowIfDisposed();
            if (other == null || other.Length == 0)
            {
                return(Length == 0);
            }
            if (this.GetHashCode() != other.GetHashCode())
            {
                return(false);
            }

            var otherBytes = GetOtherBytesAsync();
            var ownBytes   = _safeByteCollection.GetAllAsync();
            await Task.WhenAll(otherBytes, ownBytes)
            .ConfigureAwait(false);

            return(AreEqual(otherBytes.Result, ownBytes.Result));

            async Task <ISafeByte[]> GetOtherBytesAsync()
            {
                if (other is SafeBytes safeBytes)
                {
                    // If it's the known SafeBytes then it reveals nothing in the memory
                    var bytes = await safeBytes._safeByteCollection.GetAllAsync()
                                .ConfigureAwait(false);

                    return(bytes);
                }
                else
                {
                    // If it's not, then reveals each byte in memory.
                    var bytes = await other.RevealDecryptedBytesAsync().ConfigureAwait(false);

                    var stream = new SafeMemoryStream(bytes);
                    var safe   = await _safeByteFactory.GetByBytesAsync(stream)
                                 .ConfigureAwait(false);

                    return(safe.ToArray());
                }
            }
        }
Beispiel #16
0
        public async Task ToByteArrayAsync_1MBBytes_TakesLessThan_2000ms()
        {
            // Arrange
            const int expectedHigherLimit = 2000;
            const int totalBytes          = 1000000;
            await SafeOrbitCore.Current.StartEarlyAsync();

            var sut   = GetSut();
            var bytes = new byte[totalBytes];

            new Random().NextBytes(bytes);
            var stream = new SafeMemoryStream(bytes);
            await sut.AppendManyAsync(stream);

            // Act
            var actualPerformance = await MeasureAsync(
                () => sut.RevealDecryptedBytesAsync(), 5);

            // Assert
            Assert.That(actualPerformance, Is.LessThanOrEqualTo(expectedHigherLimit));
        }
        public async Task AppendManyAsync_MultipleBytes_AppendsToInternalCollection()
        {
            // Arrange
            const byte firstByte = 55, secondByte = 77;
            var        expected = Stubs.Get <ISafeBytes>();
            var        stream   = new SafeMemoryStream(new [] { firstByte, secondByte });
            await expected.AppendManyAsync(stream);

            var collection = Stubs.Get <ISafeByteCollection>();

            using var sut = GetSut(collection: collection);

            // Act
            await sut.AppendAsync(expected);

            // Assert
            var actual = await collection.GetAllAsync();

            Assert.AreEqual(2, actual.Length);
            Assert.AreEqual(firstByte, actual.ElementAt(0).RevealDecryptedByteAsync().Result);
            Assert.AreEqual(secondByte, actual.ElementAt(1).RevealDecryptedByteAsync().Result);
        }
Beispiel #18
0
        /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/>
        public async Task <bool> EqualsAsync(byte[] other)
        {
            ThrowIfDisposed();
            if (other == null || !other.Any())
            {
                return(Length == 0);
            }

            var otherBytes = GetOtherBytes();
            var ownBytes   = _safeByteCollection.GetAllAsync();
            await Task.WhenAll(otherBytes, ownBytes)
            .ConfigureAwait(false);

            return(AreEqual(otherBytes.Result, ownBytes.Result));

            async Task <ISafeByte[]> GetOtherBytes()
            {
                var stream = new SafeMemoryStream(other.CopyToNewArray());
                var bytes  = await _safeByteFactory.GetByBytesAsync(stream).ConfigureAwait(false);

                return(bytes.ToArray());
            }
        }