Esempio n. 1
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);
        }
Esempio n. 2
0
        public void RunSync_Success()
        {
            // ARRANGE
            byte[] expected = new byte[FactoryNode.Node.Size.Value];
            new Random().NextBytes(expected);
            IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock();
            Stream s = new MemoryStream();
            FileDownloadCallbackMock callback = new FileDownloadCallbackMock();
            FileDownload             f        = new FileDownload(c, "id1", FactoryNode.Node, s);

            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);
            DracoonWebClientExtension wc = Mock.Create <DracoonWebClientExtension>();

            Mock.Arrange(() => Mock.Create <DownloadProgressChangedEventArgs>().BytesReceived).IgnoreInstance().Returns(expected.Length);
            Mock.Arrange(() => c.Builder.ProvideChunkDownloadWebClient(Arg.AnyLong, Arg.AnyLong)).Returns(wc);
            Mock.Arrange(() => c.Executor.ExecuteWebClientDownload(Arg.IsAny <WebClient>(), Arg.IsAny <Uri>(),
                                                                   RequestType.GetDownloadChunk, Arg.IsAny <Thread>(), Arg.AnyInt)).DoInstead(() => Thread.Sleep(250))
            .Raises(() => wc.DownloadProgressChanged += null, null, Mock.Create <DownloadProgressChangedEventArgs>()).Returns(expected);

            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(callback);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
        }