Example #1
0
        public void TestSimpleDownload()
        {
            byte[]       src  = new byte[ushort.MaxValue];
            MemoryStream dest = new MemoryStream();

            new Random().NextBytes(src);

            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            server.MaxOutboundFileChunk = 1000;
            server.DownloadBytes       +=
                (o, e) =>
            {
                using (MemoryStream ms = new MemoryStream(e.ReadLength))
                {
                    ms.Write(src, (int)e.ReadOffset, e.ReadLength);
                    e.SetBytes(src.Length, ms.ToArray());
                }
            };

            SecureTransfer.Client client = new SecureTransfer.Client(ClientKey, ServerKey.PublicKey,
                                                                     (id, name, stream) => server.Receive(stream));

            client.Download("bla", dest);
            Assert.AreEqual(Hash.SHA256(src), Hash.SHA256(dest.ToArray()));
        }
Example #2
0
        public void TestDownloadOneChunk()
        {
            byte[]       src  = new byte[1024];
            MemoryStream dest = new MemoryStream();

            new Random().NextBytes(src);
            Hash expectHash = Hash.SHA256(src);

            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            server.MaxOutboundFileChunk = src.Length;
            server.DownloadBytes       +=
                (o, e) =>
            {
                if (e.ReadLength == 0)
                {
                    e.SetBytes(src.Length, null);
                }
                else
                {
                    e.SetBytes(src.Length, src);
                    src = null;
                }
            };

            SecureTransfer.Client client = new SecureTransfer.Client(ClientKey, ServerKey.PublicKey,
                                                                     (id, name, stream) => server.Receive(stream));

            client.Download("bla", dest);
            Assert.IsNull(src);
            Assert.AreEqual(expectHash, Hash.SHA256(dest.ToArray()));
        }
        public IContentResponse GetResponse(string method, string rawUrl, NameValueCollection headers, Stream inputStream)
        {
            if (StringComparer.Ordinal.Equals(rawUrl, PathSetPassword))
            {
                return(HandlePasswordSet(headers, inputStream));
            }

            if (!StringComparer.Ordinal.Equals(rawUrl, _publishUri))
            {
                throw new ApplicationException("Unexpected path " + rawUrl);
            }

            try
            {
                SecureTransfer.Server handler = new SecureTransfer.Server(
                    _content.KeyPair.ServerPrivateKey,
                    _content.KeyPair.ClientPublicKey,
                    new RegistryStorage(Path.Combine(Settings.RegistryPath, "Transfers")));

                handler.BeginTransfer    += BeginTransfer;
                handler.BytesReceived    += BytesReceived;
                handler.CompleteTransfer += CompleteTransfer;
                handler.ErrorRaised      += ErrorRaised;

                using (Stream response = handler.Receive(inputStream))
                {
                    return(new DynamicResponse("application/binary", IOStream.ReadAllBytes(response)));
                }
            }
            catch (Exception error)
            {
                Log.Error(error);
                return(HttpErrorHandler.Unauthorized.GetResponse(method, rawUrl, headers, inputStream));
            }
        }
Example #4
0
        public void TestSimpleUpload()
        {
            byte[] src  = new byte[ushort.MaxValue];
            byte[] work = null;
            byte[] dest = null;
            new Random().NextBytes(src);
            Hash hashSent = null;
            Guid transfer = Guid.Empty;

            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            server.MaxInboundFileChunk = 1000;
            server.BeginTransfer      += (o, e) => { work = new byte[e.TotalSize]; transfer = e.TransferId; };
            server.CompleteTransfer   += (o, e) => { dest = work; hashSent = e.ContentHash; };
            server.BytesReceived      +=
                (o, e) =>
            {
                Assert.AreEqual(transfer, e.TransferId);
                Assert.AreEqual(src.Length, e.TotalSize);
                Assert.AreEqual("bla", e.Location);
                Array.Copy(e.BytesReceived, 0, work, e.WriteOffset, e.BytesReceived.Length);
            };

            SecureTransfer.Client client = new SecureTransfer.Client(ClientKey, ServerKey.PublicKey,
                                                                     (id, name, stream) => server.Receive(stream));

            client.Upload("bla", src.Length, new MemoryStream(src, false));

            Assert.AreEqual(Hash.SHA256(src), hashSent);
            Assert.AreEqual(Hash.SHA256(src), Hash.SHA256(dest));
        }
Example #5
0
        public void TestAbort()
        {
            byte[] src = new byte[1024];
            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            SecureTransfer.Client client = new SecureTransfer.Client(ClientKey, ServerKey.PublicKey,
                                                                     (id, name, stream) => server.Receive(stream));

            client.Abort();
            Assert.IsFalse(client.Upload("bla", src.Length, new MemoryStream(src, false)));
        }
Example #6
0
        public void TestAbortByEvent()
        {
            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            SecureTransfer.Client client = new SecureTransfer.Client(ClientKey, ServerKey.PublicKey,
                                                                     (id, name, stream) => server.Receive(stream));

            client.ProgressChanged += (o, e) => { throw new OperationCanceledException(); };
            using (TempFile file = new TempFile())
            {
                file.WriteAllBytes(new byte[1024]);
                Assert.IsFalse(client.Upload("bla", file.TempPath));
            }
        }
Example #7
0
        public void TestRecieverError()
        {
            Exception error = null;

            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            server.ErrorRaised += (o, e) => error = e.GetException();
            try
            {
                server.Receive(new MemoryStream(new byte[1024]));
                Assert.Fail();
            }
            catch (InvalidDataException) { }
            Assert.IsNotNull(error);
        }
Example #8
0
        public void TestDownloadFails()
        {
            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            server.MaxOutboundFileChunk = 1000;
            server.DownloadBytes       += (o, e) => e.SetBytes(ushort.MaxValue, null);

            SecureTransfer.Client client = new SecureTransfer.Client(ClientKey, ServerKey.PublicKey,
                                                                     (id, name, stream) => server.Receive(stream));

            MemoryStream dest = new MemoryStream();

            try
            {
                client.Download("bla", dest);
                Assert.Fail();
            }
            catch (InvalidDataException) { }
        }
Example #9
0
        public void TestSendingError()
        {
            int count = 0;

            byte[] src = new byte[1024];
            SecureTransfer.Server server = new SecureTransfer.Server(ServerKey, ClientKey.PublicKey, new RegistryStorage());
            SecureTransfer.Client client = new SecureTransfer.Client(ClientKey, ServerKey.PublicKey,
                                                                     (id, name, stream) =>
            {
                if (++count < 3)
                {
                    return(server.Receive(stream));
                }
                //Bombs after negotiation
                throw new ApplicationException();
            }
                                                                     );

            client.Upload("bla", src.Length, new MemoryStream(src, false));
            Assert.Fail();
        }