public void SecureFile_File_Validate() { string originalName = Path.GetTempFileName(); string encryptName = Path.GetTempFileName(); string privateKey = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024); string publicKey = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey); EnhancedStream original = null; EnhancedStream encrypted = null; SecureFile secure = null; byte b; try { original = new EnhancedFileStream(originalName, FileMode.Create, FileAccess.ReadWrite); for (int i = 0; i < 100; i++) { original.WriteByte((byte)i); } original.Close(); original = null; secure = new SecureFile(originalName, SecureFileMode.Encrypt, publicKey); secure.EncryptTo(encryptName, CryptoAlgorithm.AES, 256); secure.Close(); secure = null; Assert.IsTrue(SecureFile.Validate(encryptName, privateKey)); encrypted = new EnhancedFileStream(encryptName, FileMode.Open, FileAccess.ReadWrite); encrypted.Position = encrypted.Length - 1; b = (byte)encrypted.ReadByte(); encrypted.Position = encrypted.Length - 1; encrypted.WriteByte((byte)(~b)); encrypted.Close(); Assert.IsFalse(SecureFile.Validate(encryptName, privateKey)); } finally { if (original != null) { original.Close(); } if (encrypted != null) { encrypted.Close(); } System.IO.File.Delete(originalName); System.IO.File.Delete(encryptName); } }
public void MsgQueueFileStore_Corrupt_Messages() { MsgQueueFileStore store = null; int count; QueuedMsgInfo msgInfo; QueuedMsg msg; object persistID; Guid[] ids; Dictionary <Guid, bool> corruptIDs; List <string> corruptFiles; // Create a store with a bunch of messages then close // it and corrupt some of the message files. Then attempt // to open the files and verify that the corrupted files // are detected. ClearFolders(); try { store = new MsgQueueFileStore(root); store.Open(); ids = new Guid[MessageCount]; for (int i = 0; i < MessageCount; i++) { msg = new QueuedMsg(); msg.TargetEP = "logical://test/" + i.ToString(); msg.Body = i; msgInfo = new QueuedMsgInfo(null, msg); store.Add(msgInfo, msg); ids[i] = msg.ID; } Assert.AreEqual(MessageCount, store.Count); count = 0; foreach (QueuedMsgInfo i in store) { count++; } Assert.AreEqual(MessageCount, count); for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } // Corrupt 1/2 of the message files in various ways. corruptIDs = new Dictionary <Guid, bool>(); corruptFiles = new List <string>(); for (int i = 0; i < MessageCount / 2; i++) { corruptIDs.Add(ids[i], true); corruptFiles.Add(store.GetMessagePath(ids[i], false)); } int cbTruncate = 0; for (int i = 0; i < corruptFiles.Count; i++) { string file = corruptFiles[i]; using (EnhancedFileStream fs = new EnhancedFileStream(file, FileMode.Open, FileAccess.ReadWrite)) { if ((i & 1) == 0) { // Truncate the file by at least one byte int cb; cb = Math.Min((int)fs.Length - 1, cbTruncate++); fs.SetLength(cb); } else { // Mess with a byte at random position in the file. int pos = Helper.Rand((int)fs.Length); byte b; fs.Position = pos; b = (byte)fs.ReadByte(); fs.Position = pos; fs.WriteByte((byte)(~b)); } } } // Load all of the message files and verify that the corrupt files // are detected. for (int i = 0; i < ids.Length; i++) { Guid id = ids[i]; if (corruptIDs.ContainsKey(id)) { try { persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); Assert.Fail("Exception expected"); } catch { // Expecting an exception } } else { persistID = store.GetPersistID(id); Assert.IsNotNull(persistID); msgInfo = store.GetInfo(persistID); Assert.IsNotNull(msgInfo); Assert.AreEqual((MsgEP)("logical://test/" + i.ToString()), msgInfo.TargetEP); msg = store.Get(persistID); msg.DeserializedBody(); Assert.AreEqual(i, (int)msg.Body); } } } finally { if (store != null) { store.Close(); } } }
public void SecureFile_File_BadHash() { string originalName = Path.GetTempFileName(); string encryptName = Path.GetTempFileName(); string decryptName = Path.GetTempFileName(); string privateKey = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024); string publicKey = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey); EnhancedStream original = null; EnhancedStream encrypted = null; SecureFile secure = null; byte b; try { original = new EnhancedFileStream(originalName, FileMode.Create, FileAccess.ReadWrite); for (int i = 0; i < 100; i++) { original.WriteByte((byte)i); } original.Close(); original = null; secure = new SecureFile(originalName, SecureFileMode.Encrypt, publicKey); secure.EncryptTo(encryptName, CryptoAlgorithm.AES, 256); secure.Close(); secure = null; // Munge the last byte of the hash digest and then confirm // that the bad hash is detected. encrypted = new EnhancedFileStream(encryptName, FileMode.Open, FileAccess.ReadWrite); encrypted.Position = encrypted.Length - 1; b = (byte)encrypted.ReadByte(); encrypted.Position = encrypted.Length - 1; encrypted.WriteByte((byte)(~b)); encrypted.Close(); encrypted = null; ExtendedAssert.Throws <CryptographicException>( () => { secure = new SecureFile(encryptName, SecureFileMode.Decrypt, privateKey); secure.DecryptTo(decryptName); }); } finally { if (original != null) { original.Close(); } if (encrypted != null) { encrypted.Close(); } try { System.IO.File.Delete(originalName); } catch { } try { System.IO.File.Delete(encryptName); } catch { } try { System.IO.File.Delete(decryptName); } catch { } } }