Esempio n. 1
0
        public void CannotSaveNull()
        {
            FileStatePersistenceService svc = new FileStatePersistenceService();
            State state = null;

            svc.Save(state);
        }
Esempio n. 2
0
        public void InvalidCryptographySettingThrowsStatePersistenceException()
        {
            FileStatePersistenceService service  = new FileStatePersistenceService();
            NameValueCollection         settings = new NameValueCollection();

            settings["UseCryptography"] = "abc";

            service.Configure(settings);
        }
Esempio n. 3
0
        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.");
        }
Esempio n. 4
0
        public void CanLoadState()
        {
            CanSaveState();
            FileStatePersistenceService svc = new FileStatePersistenceService();
            State state = svc.Load(stateID);

            Assert.AreEqual(stateID, state.ID);
            Assert.AreEqual("somevalue", state["somekey"]);
        }
Esempio n. 5
0
        public void ThrowsIfStateCannotBeSaved()
        {
            FileStatePersistenceService svc = new FileStatePersistenceService();
            State state = new State(stateID);

            using (StreamWriter sw = File.CreateText(filename))
            {
                svc.Save(state);
            }
        }
Esempio n. 6
0
        public void CanRemoveState()
        {
            FileStatePersistenceService svc = new FileStatePersistenceService();
            State state = new State(stateID);

            svc.Save(state);

            svc.Remove(stateID);
            Assert.IsFalse(File.Exists(filename));
        }
Esempio n. 7
0
        public void CanSaveState()
        {
            State state = new State(stateID);

            state["somekey"] = "somevalue";
            FileStatePersistenceService svc = new FileStatePersistenceService();

            svc.Save(state);
            Assert.IsTrue(File.Exists(filename));
        }
Esempio n. 8
0
        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);
            }
        }
Esempio n. 9
0
        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"));
        }
Esempio n. 10
0
        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);
            }
        }
Esempio n. 12
0
 public void LoadingInexistentState()
 {
     FileStatePersistenceService svc = new FileStatePersistenceService();
     State state = svc.Load("junk");
 }