Example #1
0
        public void StringCtor()
        {
            string message             = "This is a test message.";
            RuntimeBinderException rbe = new RuntimeBinderException(message);

            Assert.Null(rbe.InnerException);
            Assert.Empty(rbe.Data);
            Assert.True((rbe.HResult & 0xFFFF0000) == 0x80130000); // Error from .NET
            Assert.Same(message, rbe.Message);
            rbe = new RuntimeBinderException(null);
            Assert.Equal(new RuntimeBinderException().Message, rbe.Message);
            BinaryFormatterHelpers.AssertRoundtrips(rbe);
        }
Example #2
0
        public void CustomClaimIdentity_SerializeDeserialize_Roundtrip()
        {
            var            id1 = new CustomClaimsIdentity("someAuthType", "someNameType", "someRoleType");
            ClaimsIdentity id2 = BinaryFormatterHelpers.Clone(id1);

            Assert.Equal(id1.Actor, id2.Actor);
            Assert.Equal(id1.AuthenticationType, id2.AuthenticationType);
            Assert.Equal(id1.BootstrapContext, id2.BootstrapContext);
            Assert.Equal(id1.IsAuthenticated, id2.IsAuthenticated);
            Assert.Equal(id1.Label, id2.Label);
            Assert.Equal(id1.Name, id2.Name);
            Assert.Equal(id1.NameClaimType, id2.NameClaimType);
            Assert.Equal(id1.RoleClaimType, id2.RoleClaimType);
        }
Example #3
0
        public static void Serialization_RefType()
        {
            var stream    = new MemoryStream();
            var formatter = new BinaryFormatter();

            formatter.Serialize(stream, new Lazy <string>(() => "42"));
            stream.Seek(0, SeekOrigin.Begin);

            var x        = BinaryFormatterHelpers.Clone(new object());
            var fortytwo = (Lazy <string>)formatter.Deserialize(stream);

            Assert.True(fortytwo.IsValueCreated);
            Assert.Equal(fortytwo.Value, "42");
        }
Example #4
0
        public static void ClosedStaticDelegateSerialization()
        {
            var t = new TestSerializableClass();

            Assert.Equal(1, t.x);
            Action d = t.IncrementX;

            d();
            Assert.Equal(2, t.x);

            d = BinaryFormatterHelpers.Clone(d);
            t = (TestSerializableClass)d.Target;
            Assert.Equal(2, t.x);
            d();
            Assert.Equal(3, t.x);
        }
Example #5
0
        public void Claim_SerializeDeserialize_Roundtrip()
        {
            var id = new ClaimsIdentity("someAuthType", "someNameType", "someRoleType");
            var c1 = new Claim("someType", "someValue", "someValueType", "anIssuer", "anOriginalIssuer", id);

            Assert.Same(id, c1.Subject);

            Claim c2 = BinaryFormatterHelpers.Clone(c1);

            Assert.Equal(c1.Type, c2.Type);
            Assert.Equal(c1.Value, c2.Value);
            Assert.Equal(c1.ValueType, c2.ValueType);
            Assert.Equal(c1.Issuer, c2.Issuer);
            Assert.Equal(c1.OriginalIssuer, c2.OriginalIssuer);
            Assert.Null(c2.Subject);
        }
Example #6
0
        public void IGenericSharedAPI_SerializeDeserialize(int count)
        {
            IEnumerable <T> expected = GenericIEnumerableFactory(count);
            IEnumerable <T> actual   = BinaryFormatterHelpers.Clone(expected);

            if (Order == EnumerableOrder.Sequential)
            {
                Assert.Equal(expected, actual);
            }
            else
            {
                var expectedSet = new HashSet <T>(expected);
                var actualSet   = new HashSet <T>(actual);
                Assert.Subset(expectedSet, actualSet);
                Assert.Subset(actualSet, expectedSet);
            }
        }
Example #7
0
        public void SerializeDeserialize_Roundtrips(ReadOnlyDictionary <string, string> d)
        {
            ReadOnlyDictionary <string, string> clone = BinaryFormatterHelpers.Clone(d);

            Assert.NotSame(d, clone);
            Assert.Equal(d, clone);

            ReadOnlyDictionary <string, string> .KeyCollection keys      = d.Keys;
            ReadOnlyDictionary <string, string> .KeyCollection keysClone = BinaryFormatterHelpers.Clone(keys);
            Assert.NotSame(keys, keysClone);
            Assert.Equal(keys, keysClone);

            ReadOnlyDictionary <string, string> .ValueCollection values      = d.Values;
            ReadOnlyDictionary <string, string> .ValueCollection valuesClone = BinaryFormatterHelpers.Clone(values);
            Assert.NotSame(values, valuesClone);
            Assert.Equal(values, valuesClone);
        }
Example #8
0
        static void ValidateSerializeDeserialize(Encoding e)
        {
            // Make sure the Encoding roundtrips
            Assert.Equal(e, BinaryFormatterHelpers.Clone(e));

            // Get an encoder and decoder from the encoding, and clone them
            Encoder origEncoder   = e.GetEncoder();
            Decoder origDecoder   = e.GetDecoder();
            Encoder clonedEncoder = BinaryFormatterHelpers.Clone(origEncoder);
            Decoder clonedDecoder = BinaryFormatterHelpers.Clone(origDecoder);

            // Encode and decode some text with each pairing
            const string InputText = "abcdefghijklmnopqrstuvwxyz";

            char[] inputTextChars = InputText.ToCharArray();
            var    pairs          = new[]
            {
                Tuple.Create(origEncoder, origDecoder),
                Tuple.Create(origEncoder, clonedDecoder),
                Tuple.Create(clonedEncoder, origDecoder),
                Tuple.Create(clonedEncoder, clonedDecoder),
            };
            var results = new List <char[]>();

            foreach (Tuple <Encoder, Decoder> pair in pairs)
            {
                byte[] encodedBytes = new byte[pair.Item1.GetByteCount(inputTextChars, 0, inputTextChars.Length, true)];
                Assert.Equal(encodedBytes.Length, pair.Item1.GetBytes(inputTextChars, 0, inputTextChars.Length, encodedBytes, 0, true));
                char[] decodedChars = new char[pair.Item2.GetCharCount(encodedBytes, 0, encodedBytes.Length)];
                Assert.Equal(decodedChars.Length, pair.Item2.GetChars(encodedBytes, 0, encodedBytes.Length, decodedChars, 0));
                results.Add(decodedChars);
            }

            // Validate that all of the pairings produced the same results
            foreach (char[] a in results)
            {
                foreach (char[] b in results)
                {
                    Assert.Equal(a, b);
                }
            }
        }
Example #9
0
    public static void CloneAndProperties(bool cloneViaSerialization)
    {
        SafeAccessTokenHandle token = WindowsIdentity.GetCurrent().AccessToken;
        bool gotRef = false;

        try
        {
            token.DangerousAddRef(ref gotRef);
            IntPtr          logonToken = token.DangerousGetHandle();
            WindowsIdentity winId      = new WindowsIdentity(logonToken);

            WindowsIdentity cloneWinId = cloneViaSerialization ?
                                         BinaryFormatterHelpers.Clone(winId) :
                                         winId.Clone() as WindowsIdentity;
            Assert.NotNull(cloneWinId);

            Assert.Equal(winId.IsSystem, cloneWinId.IsSystem);
            Assert.Equal(winId.IsGuest, cloneWinId.IsGuest);
            Assert.Equal(winId.ImpersonationLevel, cloneWinId.ImpersonationLevel);

            Assert.Equal(winId.Name, cloneWinId.Name);
            Assert.Equal(winId.Owner, cloneWinId.Owner);

            IdentityReferenceCollection irc1 = winId.Groups;
            IdentityReferenceCollection irc2 = cloneWinId.Groups;
            Assert.Equal(irc1.Count, irc2.Count);

            CheckDispose(winId);
            CheckDispose(cloneWinId);
        }
        finally
        {
            if (gotRef)
            {
                token.DangerousRelease();
            }
        }
    }
        public void IGenericSharedAPI_SerializeDeserialize(int count)
        {
            IEnumerable <T> expected = GenericIEnumerableFactory(count);

            // Not all IEnumerables are intended to be Serializable
            if (!expected.GetType().IsSerializable)
            {
                return;
            }

            IEnumerable <T> actual = BinaryFormatterHelpers.Clone(expected);

            if (Order == EnumerableOrder.Sequential)
            {
                Assert.Equal(expected, actual);
            }
            else
            {
                var expectedSet = new HashSet <T>(expected);
                var actualSet   = new HashSet <T>(actual);
                Assert.Subset(expectedSet, actualSet);
                Assert.Subset(actualSet, expectedSet);
            }
        }
Example #11
0
 public void RegexMatchTimeoutExceptionSerialization(RegexMatchTimeoutException ex)
 {
     BinaryFormatterHelpers.AssertRoundtrips(ex, e => e.Input, e => e.Pattern, e => e.MatchTimeout);
 }
 public void SerializeDeserialize_Roundtrip(WebHeaderCollection c)
 {
     Assert.Equal(c, BinaryFormatterHelpers.Clone(c));
 }
Example #13
0
 public void Color_Roundtrip(Color c)
 {
     Assert.Equal(c, BinaryFormatterHelpers.Clone(c));
 }
Example #14
0
        public void UnitySerializationHolderWithAssemblySingleton()
        {
            const string           UnitySerializationHolderAssemblyBase64String = "AAEAAAD/////AQAAAAAAAAAEAQAAAB9TeXN0ZW0uVW5pdHlTZXJpYWxpemF0aW9uSG9sZGVyAwAAAAREYXRhCVVuaXR5VHlwZQxBc3NlbWJseU5hbWUBAAEIBgIAAABLbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BgAAAAkCAAAACw==";
            SerializationException se = AssertExtensions.Throws <SerializationException>(() =>
                                                                                         BinaryFormatterHelpers.FromBase64String(UnitySerializationHolderAssemblyBase64String));

            Assert.IsAssignableFrom <ArgumentException>(se.InnerException);
        }
        public void SerializationRoundTrip()
        {
            var ex = new InvalidOleVariantTypeException("E_BAD_PIZZA");

            BinaryFormatterHelpers.AssertRoundtrips(ex);
        }
Example #16
0
        public void SerializationRoundTrip()
        {
            var ex = new MarshalDirectiveException("E_BAD_PIZZA");

            BinaryFormatterHelpers.AssertRoundtrips(ex);
        }
Example #17
0
 public void SerializeModule()
 {
     Assert.Equal(TestModule, BinaryFormatterHelpers.Clone(TestModule));
 }
Example #18
0
        public void SerializationRoundTrip()
        {
            var ex = new COMException("E_BAD_PIZZA", -1337);

            BinaryFormatterHelpers.AssertRoundtrips(ex);
        }
Example #19
0
 public static void Deserialize_NetCore_ThrowsPlatformNotSupportedException()
 {
     BinaryFormatterHelpers.AssertExceptionDeserializationFails <Win32Exception>();
 }
Example #20
0
 public static void SerializeDeserialize_Roundtrip_EqualObjects(object obj)
 {
     Assert.Equal(obj, BinaryFormatterHelpers.Clone(obj));
 }
Example #21
0
 public void AssertExceptionDeserializationFails()
 {
     BinaryFormatterHelpers.AssertExceptionDeserializationFails <RuntimeBinderException>();
 }
Example #22
0
        public void SerializationRoundTrip()
        {
            var ex = new SafeArrayRankMismatchException("E_BAD_PIZZA");

            BinaryFormatterHelpers.AssertRoundtrips(ex);
        }
Example #23
0
 public void ClaimPrincipal_SerializeDeserialize_Roundtrip()
 {
     Assert.NotNull(BinaryFormatterHelpers.Clone(new ClaimsPrincipal()));
 }
Example #24
0
 public static void SerializeDeserialize(Win32Exception exception)
 {
     BinaryFormatterHelpers.AssertRoundtrips(exception, e => e.NativeErrorCode, e => e.ErrorCode);
 }
Example #25
0
 public void AssertExceptionDeserializationFails()
 {
     BinaryFormatterHelpers.AssertExceptionDeserializationFails <ValidationException>();
 }
 public static void ExceptionRoundtrips()
 {
     BinaryFormatterHelpers.AssertRoundtrips(new InternalBufferOverflowException());
 }