コード例 #1
0
        public void UsesCryptoToSave()
        {
            WorkItem container = new TestableRootWorkItem();
            DataProtectionCryptographyService cryptoSvc = new DataProtectionCryptographyService();
            MemoryStreamPersistence           perSvc    = new MemoryStreamPersistence();

            container.Services.Add(typeof(ICryptographyService), cryptoSvc);
            container.Services.Add(typeof(IStatePersistenceService), perSvc);
            NameValueCollection settings = new NameValueCollection();

            settings["UseCryptography"] = "True";
            perSvc.Configure(settings);

            Guid  id        = Guid.NewGuid();
            State testState = new State(id.ToString());

            testState["someValue"] = "value";
            perSvc.Save(testState);

            byte[] stateData = null;
            perSvc.Stream.Position = 0;
            Stream stream = perSvc.Stream;

            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.ToString(), recovered.ID, "The state id is different.");
            Assert.AreEqual("value", recovered["someValue"]);
        }
コード例 #2
0
        public void DoesNotDisposeStreamIfNotToldTo()
        {
            MemoryStreamPersistence svc = new MemoryStreamPersistence();
            State state = new State(stateID);

            svc.Save(state);

            Assert.IsTrue(svc.Stream.CanSeek);
        }
コード例 #3
0
        public void CryptoFailsWhenCryptoNotAvailable()
        {
            MemoryStreamPersistence svc      = new MemoryStreamPersistence();
            NameValueCollection     settings = new NameValueCollection();

            settings["UseCryptography"] = "True";
            svc.Configure(settings);
            svc.Save(new State(stateID));
        }
コード例 #4
0
        public void CanSaveAndLoadState()
        {
            State state = new State(stateID);

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

            svc.Save(state);

            State state2 = svc.Load(stateID);

            Assert.AreEqual(state["somekey"], state2["somekey"]);
        }
コード例 #5
0
        public void UsesCryptoToSave()
        {
            WorkItem container = new TestableRootWorkItem();
            DataProtectionCryptographyService cryptoSvc = new DataProtectionCryptographyService();
            MemoryStreamPersistence           perSvc    = new MemoryStreamPersistence();

            container.Services.Add(typeof(ICryptographyService), cryptoSvc);
            container.Services.Add(typeof(IStatePersistenceService), perSvc);
            NameValueCollection settings = new NameValueCollection();

            settings["UseCryptography"] = "True";
            perSvc.Configure(settings);

            Guid  id        = Guid.NewGuid();
            State testState = new State(id.ToString());

            testState["someValue"] = "value";
            perSvc.Save(testState);

            byte[] stateData = null;
            perSvc.Stream.Position = 0;
            Stream stream = perSvc.Stream;

            byte[] cipherData = new byte[stream.Length];
            stream.Read(cipherData, 0, (int)stream.Length);
            stateData = ProtectedData.Unprotect(cipherData, null, DataProtectionScope.CurrentUser);
            //stateData = cryptoSvc.DecryptSymmetric(cipherData);
            //string result = System.Text.Encoding.UTF8.GetString(stateData);
            //BinaryFormatter fmt = new BinaryFormatter();
            //State recovered = (State)fmt.Deserialize(ms);
            MemoryStream ms = new MemoryStream(stateData.Prune());

#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
            //var obj = fmt.ReadObject(System.Xml.XmlReader.Create(ms));
            State recovered = (State)fmt.ReadObject(System.Xml.XmlReader.Create(ms));
            Assert.AreEqual(id.ToString(), recovered.ID, "The state id is different.");
            Assert.AreEqual("value", recovered["someValue"]);
        }
コード例 #6
0
        public void CannotSaveNull()
        {
            MemoryStreamPersistence svc = new MemoryStreamPersistence();

            svc.Save(null);
        }