public void CannotSaveNull() { FileStatePersistenceService svc = new FileStatePersistenceService(); State state = null; svc.Save(state); }
public void InvalidCryptographySettingThrowsStatePersistenceException() { FileStatePersistenceService service = new FileStatePersistenceService(); NameValueCollection settings = new NameValueCollection(); settings["UseCryptography"] = "abc"; service.Configure(settings); }
public void StateContainsSavedStates() { State state = new State(stateID); FileStatePersistenceService svc = new FileStatePersistenceService(); Assert.IsFalse(svc.Contains(stateID), "The storage shouln't have the state persisted yet."); svc.Save(state); Assert.IsTrue(svc.Contains(stateID), "The storage should have the state persisted now."); }
public void CanLoadState() { CanSaveState(); FileStatePersistenceService svc = new FileStatePersistenceService(); State state = svc.Load(stateID); Assert.AreEqual(stateID, state.ID); Assert.AreEqual("somevalue", state["somekey"]); }
public void ThrowsIfStateCannotBeSaved() { FileStatePersistenceService svc = new FileStatePersistenceService(); State state = new State(stateID); using (StreamWriter sw = File.CreateText(filename)) { svc.Save(state); } }
public void CanRemoveState() { FileStatePersistenceService svc = new FileStatePersistenceService(); State state = new State(stateID); svc.Save(state); svc.Remove(stateID); Assert.IsFalse(File.Exists(filename)); }
public void CanSaveState() { State state = new State(stateID); state["somekey"] = "somevalue"; FileStatePersistenceService svc = new FileStatePersistenceService(); svc.Save(state); Assert.IsTrue(File.Exists(filename)); }
public void UsesCryptoToSave() { WorkItem host = container; ICryptographyService cryptoSvc = new DataProtectionCryptographyService(); host.Services.Add(typeof(ICryptographyService), cryptoSvc); FileStatePersistenceService perSvc = new FileStatePersistenceService(); host.Services.Add(typeof(IStatePersistenceService), perSvc); NameValueCollection settings = new NameValueCollection(); settings["UseCryptography"] = "True"; perSvc.Configure(settings); string id = Guid.NewGuid().ToString(); State testState = new State(id); testState["someValue"] = "value"; perSvc.Save(testState); byte[] stateData = null; string filename = string.Format(CultureInfo.InvariantCulture, "{0}.state", id); using (FileStream stream = File.OpenRead(filename)) { byte[] cipherData = new byte[stream.Length]; stream.Read(cipherData, 0, (int)stream.Length); stateData = ProtectedData.Unprotect(cipherData, null, DataProtectionScope.CurrentUser); } //BinaryFormatter fmt = new BinaryFormatter(); //State recovered = (State)fmt.Deserialize(ms); #pragma warning disable CS0618 // Type or member is obsolete System.Runtime.Serialization.DataContractSerializer fmt = new System.Runtime.Serialization.DataContractSerializer(typeof(State), new System.Collections.Generic.List <Type>() { typeof(System.Collections.CaseInsensitiveHashCodeProvider), typeof(System.Collections.CaseInsensitiveComparer), typeof(System.String[]), typeof(System.Object[]) }); #pragma warning restore CS0618 // Type or member is obsolete MemoryStream ms = new MemoryStream(stateData.Prune()); State recovered = (State)fmt.ReadObject(ms); Assert.AreEqual(id, recovered.ID, "The state id is different."); Assert.AreEqual("value", recovered["someValue"]); if (File.Exists(filename)) { File.Delete(filename); } }
public void CryptoFailsWhenCryptoNotAvailable() { WorkItem workItem = new TestableRootWorkItem(); workItem.Services.Remove(typeof(ICryptographyService)); FileStatePersistenceService perSvc = new FileStatePersistenceService(); workItem.Services.Add(typeof(IStatePersistenceService), perSvc); NameValueCollection settings = new NameValueCollection(); settings["UseCryptography"] = "True"; perSvc.Configure(settings); perSvc.Save(new State("junk")); }
public void CanConstructWithFolder() { string basePath = Path.GetTempPath(); string tempFile = Path.Combine(basePath, String.Format(CultureInfo.InvariantCulture, "{0}.state", stateID)); FileStatePersistenceService service = new FileStatePersistenceService(basePath); State st = new State(stateID); service.Save(st); Assert.AreEqual(basePath, service.BasePath); Assert.IsTrue(File.Exists(tempFile)); File.Delete(tempFile); }
public void UsesCryptoToSave() { WorkItem host = container; ICryptographyService cryptoSvc = new DataProtectionCryptographyService(); host.Services.Add(typeof(ICryptographyService), cryptoSvc); FileStatePersistenceService perSvc = new FileStatePersistenceService(); host.Services.Add(typeof(IStatePersistenceService), perSvc); NameValueCollection settings = new NameValueCollection(); settings["UseCryptography"] = "True"; perSvc.Configure(settings); string id = Guid.NewGuid().ToString(); State testState = new State(id); testState["someValue"] = "value"; perSvc.Save(testState); byte[] stateData = null; string filename = string.Format(CultureInfo.InvariantCulture, "{0}.state", id); using (FileStream stream = File.OpenRead(filename)) { byte[] cipherData = new byte[stream.Length]; stream.Read(cipherData, 0, (int)stream.Length); stateData = ProtectedData.Unprotect(cipherData, null, DataProtectionScope.CurrentUser); } BinaryFormatter fmt = new BinaryFormatter(); MemoryStream ms = new MemoryStream(stateData); State recovered = (State)fmt.Deserialize(ms); Assert.AreEqual(id, recovered.ID, "The state id is different."); Assert.AreEqual("value", recovered["someValue"]); if (File.Exists(filename)) { File.Delete(filename); } }
public void LoadingInexistentState() { FileStatePersistenceService svc = new FileStatePersistenceService(); State state = svc.Load("junk"); }