public void WebTransferCache_Download() { // Test the file download scenario. WebTransferCache cache; WebTransferFile transferFile; Guid id; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); cache.Start(); try { // Create a file transferFile = cache.GetCommittedFile(Guid.Empty, ".pdf"); id = transferFile.ID; Assert.AreNotEqual(Guid.Empty, transferFile.ID); Assert.IsFalse(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Write some data using (var stream = transferFile.GetStream()) { stream.WriteInt32(1001); } // Open an existing file transferFile = cache.GetCommittedFile(id, ".pdf"); Assert.AreEqual(id, transferFile.ID); Assert.IsFalse(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); using (var stream = transferFile.GetStream()) { Assert.AreEqual(1001, stream.ReadInt32()); } } finally { cache.Stop(); } }
public void WebTransferCache_UploadEncrypt() { // Test file upload with encryption. WebTransferCache cache; WebTransferFile transferFile; Guid id; byte[] block1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; byte[] block2 = new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; byte[] block; long pos; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); cache.Start(); try { // Create a file transferFile = cache.GetUploadFile(Guid.Empty, ".pdf"); id = transferFile.ID; Assert.AreNotEqual(Guid.Empty, transferFile.ID); Assert.IsTrue(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path + WebTransferCache.UploadExtension)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Append the first block pos = 0; transferFile.Append(pos, block1); // Append it again to simulate a duplicate transferFile.Append(pos, block1); pos += block1.Length; // Open an existing file transferFile = cache.GetUploadFile(id, ".pdf"); Assert.AreEqual(id, transferFile.ID); Assert.IsTrue(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path + WebTransferCache.UploadExtension)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Append the second block transferFile.Append(pos, block2); // Complete the upload and verify the file contents. transferFile.Commit(MD5Hasher.Compute(Helper.Concat(block1, block2))); Assert.AreEqual(id, transferFile.ID); Assert.IsFalse(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); using (var stream = transferFile.GetStream()) { block = stream.ReadBytes((int)stream.Length); CollectionAssert.AreEqual(Helper.Concat(block1, block2), block); } // Now encrypt the file and verify. var key = Crypto.GenerateSymmetricKey(CryptoAlgorithm.AES, 256); cache.EncryptFile(key, id, ".pdf"); using (var stream = transferFile.GetStream()) { block = stream.ReadBytes((int)stream.Length); CollectionAssert.AreNotEqual(Helper.Concat(block1, block2), block); // Shouldn't be equal any more due to the encryption. } // Decrypt and verify. using (var stream = transferFile.GetStream()) { using (var decryptor = new StreamDecryptor(key)) { var ms = new MemoryStream(); decryptor.Decrypt(stream, ms); CollectionAssert.AreEqual(Helper.Concat(block1, block2), ms.ToArray()); } } } finally { cache.Stop(); } }
public void WebTransferCache_UploadBadMD5() { // Verify that we can detect a corrupted upload. WebTransferCache cache; WebTransferFile transferFile; Guid id; byte[] block1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; byte[] block2 = new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; long pos; byte[] md5; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); cache.Start(); try { // Create a file transferFile = cache.GetUploadFile(Guid.Empty, ".pdf"); id = transferFile.ID; Assert.AreNotEqual(Guid.Empty, transferFile.ID); Assert.IsTrue(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path + WebTransferCache.UploadExtension)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Append the first block pos = 0; transferFile.Append(pos, block1); // Append it again to simulate a duplicate transferFile.Append(pos, block1); pos += block1.Length; // Open an existing file transferFile = cache.GetUploadFile(id, ".pdf"); Assert.AreEqual(id, transferFile.ID); Assert.IsTrue(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path + WebTransferCache.UploadExtension)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Append the second block transferFile.Append(pos, block2); // Complete the upload and verify the file contents. md5 = MD5Hasher.Compute(Helper.Concat(block1, block2)); md5[0] = (byte)~md5[0]; // Mess with the hash try { transferFile.Commit(md5); Assert.Fail("Expected an IOException detecting the MD5 signature mismatch."); } catch (IOException) { // Expected } } finally { cache.Stop(); } }
public void WebTransferCache_Upload() { // Test the file upload scenario. WebTransferCache cache; WebTransferFile transferFile; Guid id; byte[] block1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; byte[] block2 = new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; byte[] block; long pos; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); cache.Start(); try { // Create a file transferFile = cache.GetUploadFile(Guid.Empty, ".pdf"); id = transferFile.ID; Assert.AreNotEqual(Guid.Empty, transferFile.ID); Assert.IsTrue(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path + WebTransferCache.UploadExtension)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Append the first block pos = 0; transferFile.Append(pos, block1); // Append it again to simulate a duplicate transferFile.Append(pos, block1); pos += block1.Length; // Open an existing file transferFile = cache.GetUploadFile(id, ".pdf"); Assert.AreEqual(id, transferFile.ID); Assert.IsTrue(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path + WebTransferCache.UploadExtension)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Append the second block transferFile.Append(pos, block2); // Complete the upload and verify the file contents. transferFile.Commit(MD5Hasher.Compute(Helper.Concat(block1, block2))); Assert.AreEqual(id, transferFile.ID); Assert.IsFalse(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); using (var stream = transferFile.GetStream()) { block = stream.ReadBytes((int)stream.Length); CollectionAssert.AreEqual(Helper.Concat(block1, block2), block); } } finally { cache.Stop(); } }