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));
            }
        }
        public void TestLogCorruption()
        {
            using (TempFile tmp = new TempFile())
            {
                using (var log = new TransactionLog<int, string>(Options(tmp)))
                {
                    var token = log.BeginTransaction();
                    log.AddValue(ref token, 1, Guid.NewGuid().ToString());
                    log.CommitTransaction(ref token);
                    var test = new Dictionary<int, string>();
                    long offset = 0;
                    log.ReplayLog(test, ref offset);
                    Assert.AreEqual(1, test.Count);
                }
                byte[] bytes = tmp.ReadAllBytes();

                var TestVariants =
                    new Converter<KeyValuePair<int, byte[]>, byte[]>[]
                        {
                            kv => { kv.Value[kv.Key] ^= 0xff; return kv.Value; },
                            kv => { kv.Value[kv.Key] = 0xff; return kv.Value; },
                            kv => { byte[] b = kv.Value; Array.Resize(ref b, kv.Key); return b; },
                        };

                for (int corruptionIx = 0; corruptionIx < bytes.Length; corruptionIx++)
                {
                    foreach (var testcase in TestVariants)
                    {
                        byte[] corrupt = testcase(new KeyValuePair<int, byte[]>(corruptionIx, (byte[]) bytes.Clone()));
                        tmp.WriteAllBytes(corrupt);

                        using (var log = new TransactionLog<int, string>(Options(tmp)))
                        {
                            var test = new Dictionary<int, string>();
                            log.ReplayLog(test);
                            Assert.AreEqual(0, test.Count);
                        }
                        Assert.IsFalse(File.Exists(tmp.TempPath));
                    }
                }
            }
        }
        public void TestCompressFile()
        {
            byte[] testData = new byte[ushort.MaxValue];
            new Random().NextBytes(testData);
            Array.Clear(testData, 0, testData.Length / 2);
            Hash compressedHash;
            Hash hash = Hash.SHA512(testData);

            using (TempFile f = new TempFile())
            {
                f.WriteAllBytes(testData);
                Assert.AreEqual((long)testData.Length, f.Length);

                IOStream.Compress(f.TempPath);
                Assert.IsTrue(f.Length < testData.Length);
                compressedHash = Hash.SHA512(f.Read());
                Assert.IsFalse(compressedHash == hash);

                IOStream.Decompress(f.TempPath);
                Assert.AreEqual((long)testData.Length, f.Length);
                Assert.IsTrue(hash == Hash.SHA512(f.Read()));
            }

            MemoryStream ms = new MemoryStream(testData), gz = new MemoryStream();
            IOStream.Compress(ms, gz);

            Assert.IsTrue(compressedHash == Hash.SHA512(gz.ToArray()));
            ms = new MemoryStream();
            gz.Position = 0;
            IOStream.Decompress(gz, ms);

            Assert.IsTrue(hash == Hash.SHA512(ms.ToArray()));
        }
		public void TestReadWrite()
		{
			string test = "Hello World!\u1255";
			TempFile file = new TempFile();
			File.WriteAllBytes(file.TempPath, Encoding.UTF8.GetBytes(test));

			Assert.AreEqual(Encoding.UTF8.GetBytes(test), file.ReadAllBytes());
			Assert.AreEqual(test, file.ReadAllText());

			file.Delete();
			Assert.IsFalse(File.Exists(file.TempPath));
			Assert.IsFalse(file.Exists);
			file.WriteAllBytes(Encoding.UTF8.GetBytes(test));
			Assert.AreEqual(test, file.ReadAllText());

			file.Delete();
			Assert.IsFalse(File.Exists(file.TempPath));
			Assert.IsFalse(file.Exists);
			file.WriteAllText(test);
			Assert.AreEqual(test, file.ReadAllText());
		}