Exemplo n.º 1
0
        public void Encrypted_RunSync_ProcessBytesIOError()
        {
            // ARRANGE
            Node node = FactoryNode.Node;

            node.Size = 1024;
            byte[] expected = new byte[node.Size.Value];
            new Random().NextBytes(expected);
            IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock();
            Stream s = new MemoryStream();
            FileDownloadCallbackMock callback = new FileDownloadCallbackMock();
            EncFileDownload          f        = new EncFileDownload(c, "id1", node, s, FactoryUser.UserPrivateKey);

            f.AddFileDownloadCallback(callback);
            Mock.Arrange(() => c.Builder.PostFileDownload(Arg.AnyLong)).Returns(FactoryRestSharp.PostFileDownloadMock(2354));
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiDownloadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostDownloadToken, 0))
            .Returns(FactoryNode.ApiDownloadToken);
            Mock.Arrange(() => c.NodesImpl.GetEncryptedFileKey(Arg.AnyLong)).Returns(FactoryFile.EncryptedFileKey);
            FileDecryptionCipher cipher = Mock.Create <FileDecryptionCipher>();

            Mock.Arrange(() => Crypto.Sdk.Crypto.DecryptFileKey(Arg.IsAny <EncryptedFileKey>(), Arg.IsAny <UserPrivateKey>(), Arg.AnyString))
            .Returns(FactoryFile.PlainFileKey);
            Mock.Arrange(() => Crypto.Sdk.Crypto.CreateFileDecryptionCipher(Arg.IsAny <PlainFileKey>())).Returns(cipher);
            Mock.NonPublic.Arrange <byte[]>(f, "DownloadChunk", ArgExpr.IsAny <Uri>(), ArgExpr.IsAny <long>(), ArgExpr.IsAny <long>()).Returns(expected);
            Mock.Arrange(() => cipher.ProcessBytes(Arg.IsAny <EncryptedDataContainer>())).Throws(new IOException("Error"));
            Mock.Arrange(() => cipher.DoFinal(Arg.IsAny <EncryptedDataContainer>())).Returns(new PlainDataContainer(new byte[0]));

            // ACT
            Assert.Throws <DracoonFileIOException>(() => f.RunSync());
            s.Close();
        }
Exemplo n.º 2
0
        public void Encrypted_RunSync_Success()
        {
            // ARRANGE
            Node node = FactoryNode.Node;

            node.Size = 1024;
            byte[] expected = new byte[node.Size.Value];
            new Random().NextBytes(expected);
            IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock();
            Stream s = new MemoryStream();
            FileDownloadCallbackMock callback = new FileDownloadCallbackMock();
            EncFileDownload          f        = new EncFileDownload(c, "id1", node, s, FactoryUser.UserPrivateKey);

            f.AddFileDownloadCallback(callback);
            Mock.Arrange(() => c.Builder.PostFileDownload(Arg.AnyLong)).Returns(FactoryRestSharp.PostFileDownloadMock(2354)).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiDownloadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostDownloadToken, 0))
            .Returns(FactoryNode.ApiDownloadToken).Occurs(1);
            Mock.Arrange(() => c.NodesImpl.GetEncryptedFileKey(Arg.AnyLong)).Returns(FactoryFile.EncryptedFileKey).Occurs(1);
            FileDecryptionCipher cipher = Mock.Create <FileDecryptionCipher>();

            Mock.Arrange(() => Crypto.Sdk.Crypto.DecryptFileKey(Arg.IsAny <EncryptedFileKey>(), Arg.IsAny <UserPrivateKey>(), Arg.AnyString))
            .Returns(FactoryFile.PlainFileKey).Occurs(1);
            Mock.Arrange(() => Crypto.Sdk.Crypto.CreateFileDecryptionCipher(Arg.IsAny <PlainFileKey>())).Returns(cipher).Occurs(1);
            Mock.NonPublic.Arrange <byte[]>(f, "DownloadChunk", ArgExpr.IsAny <Uri>(), ArgExpr.IsAny <long>(), ArgExpr.IsAny <long>()).Returns(expected);
            Mock.Arrange(() => cipher.ProcessBytes(Arg.IsAny <EncryptedDataContainer>())).Returns(new PlainDataContainer(expected)).OccursAtLeast(1);
            Mock.Arrange(() => cipher.DoFinal(Arg.IsAny <EncryptedDataContainer>())).Returns(new PlainDataContainer(new byte[0])).Occurs(1);

            Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1);
            Mock.Arrange(() => callback.OnRunning(Arg.AnyString, Arg.AnyLong, Arg.AnyLong)).OccursAtLeast(1);
            Mock.Arrange(() => callback.OnFinished(Arg.AnyString)).Occurs(1);

            // ACT
            f.RunSync();
            s.Position = 0;
            byte[] actual = new byte[expected.Length];
            s.Read(actual, 0, expected.Length);
            s.Close();

            // ASSERT
            Assert.Equal(expected, actual);
            Mock.Assert(() => Crypto.Sdk.Crypto.DecryptFileKey(Arg.IsAny <EncryptedFileKey>(), Arg.IsAny <UserPrivateKey>(), Arg.AnyString));
            Mock.Assert(() => Crypto.Sdk.Crypto.CreateFileDecryptionCipher(Arg.IsAny <PlainFileKey>()));
            Mock.Assert(callback);
            Mock.Assert(cipher);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
            Mock.Assert(c.NodesImpl);
        }