Esempio n. 1
0
        public void CanRoundTripData()
        {
            // Arrange
            AntiForgeryDataSerializer serializer = new AntiForgeryDataSerializer
            {
                Decoder = value => Convert.FromBase64String(value),
                Encoder = bytes => Convert.ToBase64String(bytes),
            };
            AntiForgeryData input = new AntiForgeryData
            {
                Salt         = "The Salt",
                Username     = "******",
                Value        = "The Value",
                CreationDate = DateTime.Now,
            };

            // Act
            AntiForgeryData output = serializer.Deserialize(serializer.Serialize(input));

            // Assert
            Assert.NotNull(output);
            Assert.Equal(input.Salt, output.Salt);
            Assert.Equal(input.Username, output.Username);
            Assert.Equal(input.Value, output.Value);
            Assert.Equal(input.CreationDate, output.CreationDate);
        }
        public void SerializeThrowsIfTokenIsNull()
        {
            // Arrange
            AntiForgeryDataSerializer serializer = new AntiForgeryDataSerializer();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                serializer.Serialize(null);
            }, "token");
        }
Esempio n. 3
0
        public void SerializeReturnsSerializedString()
        {
            // Arrange
            AntiForgeryData token = new AntiForgeryData()
            {
                CreationDate = new DateTime(2001, 1, 1),
                Salt         = "the salt",
                Username     = "******",
                Value        = "the value"
            };

            Mock <IStateFormatter> mockFormatter = new Mock <IStateFormatter>();

            mockFormatter
            .Expect(f => f.Serialize(It.IsAny <object>()))
            .Returns(
                delegate(object state) {
                object[] t = state as object[];
                Assert.IsNotNull(t);
                Assert.AreEqual("the salt", t[0]);
                Assert.AreEqual("the value", t[1]);
                Assert.AreEqual(new DateTime(2001, 1, 1), t[2]);
                Assert.AreEqual("someuser", t[3]);
                return("serialized value");
            }
                );

            AntiForgeryDataSerializer serializer = new AntiForgeryDataSerializer()
            {
                Formatter = mockFormatter.Object
            };

            // Act
            string serializedValue = serializer.Serialize(token);

            // Assert
            Assert.AreEqual("serialized value", serializedValue);
        }
Esempio n. 4
0
        public void GuardClauses()
        {
            // Arrange
            AntiForgeryDataSerializer serializer = new AntiForgeryDataSerializer();

            // Act & assert
            Assert.ThrowsArgumentNull(
                () => serializer.Serialize(null),
                "token"
                );
            Assert.ThrowsArgumentNullOrEmptyString(
                () => serializer.Deserialize(null),
                "serializedToken"
                );
            Assert.ThrowsArgumentNullOrEmptyString(
                () => serializer.Deserialize(String.Empty),
                "serializedToken"
                );
            Assert.Throws <HttpAntiForgeryException>(
                () => serializer.Deserialize("Corrupted Base-64 Value"),
                "A required anti-forgery token was not supplied or was invalid."
                );
        }