Exemplo n.º 1
0
        public void IStructuralEquatablePropTest()
        {
            var sb  = TestArray;
            var e   = sb as IStructuralEquatable;
            var iec = EqualityComparer <byte> .Default;

            Assert.That(e.Equals(sb, iec), Is.True);
            var h1 = e.GetHashCode(iec);

            Assert.That(() => e.GetHashCode(null), Throws.ArgumentNullException);
            Assert.That(() => e.Equals(null, null), Throws.ArgumentNullException);
            Assert.That(e.Equals(null, iec), Is.False);
            Assert.That(() => e.Equals(1, iec), Throws.TypeOf <ArgumentOutOfRangeException>());
            var sb2 = new SafeByteArray(3);
            var h2  = ((IStructuralEquatable)sb2).GetHashCode(iec);

            Assert.That(() => e.Equals(sb2, iec), Throws.TypeOf <ArgumentOutOfRangeException>());
            Assert.That(e.Equals(bytes, iec), Is.True);
            Assert.That(e.Equals(new List <byte>(bytes), iec), Is.True);
            var sb3 = new SafeByteArray(bytes.Reverse().ToArray());

            Assert.That(e.Equals(sb3, iec), Is.False);
            var h3 = ((IStructuralEquatable)sb3).GetHashCode(iec);

            var sb4 = new SafeByteArray(bytes);
            var h4  = ((IStructuralEquatable)sb4).GetHashCode(iec);

            Assert.That(h1 != h2);
            Assert.That(h1 != h3);
            Assert.That(h1 == h4);
        }
Exemplo n.º 2
0
        public void AllocateAndInitializeSidTest()
        {
            var b = AllocateAndInitializeSid(KnownSIDAuthority.SECURITY_WORLD_SID_AUTHORITY, 1, KnownSIDRelativeID.SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, out var pSid);

            Assert.That(b);
            var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            var esid     = new byte[everyone.BinaryLength];

            everyone.GetBinaryForm(esid, 0);
            var peSid = new SafeByteArray(esid);

            Assert.That(EqualSid(pSid, (IntPtr)peSid));
            ConvertStringSidToSid("S-1-2-0", out PSID lsid);
            Assert.That(EqualSid(pSid, (IntPtr)lsid), Is.False);
            string s = null;
            var    p = new PSID(pSid);

            Assert.That(IsValidSid(p), Is.True);
            Assert.That(() => s = ConvertSidToStringSid(p), Throws.Nothing);
            Assert.That(s, Is.EqualTo("S-1-1-0"));
            var saptr = GetSidSubAuthority(p, 0);

            Assert.That(Marshal.ReadInt32(saptr), Is.EqualTo(0));
            var len = GetLengthSid(pSid);
            var p2  = new PSID(len);

            b = CopySid(len, (IntPtr)p2, pSid);
            Assert.That(EqualSid(p2, p));
            Assert.That(b);
        }
Exemplo n.º 3
0
        public void SafeByteArrayTest1()
        {
            var a = new SafeByteArray(73);

            Assert.That(a.Count == 73);
            var r = new Random();

            Assert.That(a[r.Next(0, 72)], Is.Zero);
            Assert.That(a[r.Next(0, 72)], Is.Zero);
            Assert.That(a[r.Next(0, 72)], Is.Zero);
        }
Exemplo n.º 4
0
        public void SafeByteArrayTest()
        {
            var a = TestArray;

            Assert.That(a.Count == bytes.Length);
            Assert.That(a[4] == 4);
            Assert.That(a.IsReadOnly, Is.False);

            byte[] nullBytes = null;
            a = new SafeByteArray(nullBytes);
            Assert.That(a.Count == 0);
        }
Exemplo n.º 5
0
        public void SafeByteArrayTest2()
        {
            var t = TestArray;
            var a = new SafeByteArray(t);

            t = null;
            Assert.That(a.Count == bytes.Length);
            Assert.That(a[4] == 4);

            Assert.That(() => new SafeByteArray(t), Throws.ArgumentNullException);
            a.Dispose();
            Assert.That(() => new SafeByteArray(a), Throws.ArgumentException);

            Assert.That(() => a[0], Throws.TypeOf <IndexOutOfRangeException>());
            Assert.That(() => a[0] = 1, Throws.TypeOf <IndexOutOfRangeException>());
        }
Exemplo n.º 6
0
        public void IStructuralComparablePropTest()
        {
            var c = TestArray as IStructuralComparable;

            Assert.That(() => c.CompareTo(null, null), Throws.ArgumentNullException);
            Assert.That(c.CompareTo(null, Comparer <byte> .Default), Is.EqualTo(1));
            Assert.That(() => c.CompareTo(1, Comparer <byte> .Default), Throws.TypeOf <ArgumentOutOfRangeException>());
            var sb2 = new SafeByteArray(3);

            Assert.That(() => c.CompareTo(sb2, Comparer <byte> .Default), Throws.TypeOf <ArgumentOutOfRangeException>());
            Assert.That(c.CompareTo(bytes, Comparer <byte> .Default), Is.EqualTo(0));
            var sb3 = bytes.Reverse().ToArray();

            Assert.That(c.CompareTo(sb3, Comparer <byte> .Default), Is.LessThan(0));
            Assert.That(c.CompareTo(new List <byte>(bytes), Comparer <byte> .Default), Is.EqualTo(0));
        }
Exemplo n.º 7
0
        public void PSIDTest()
        {
            var sb  = new SafeByteArray(WindowsIdentity.GetCurrent().User.GetBytes());
            var sid = new PSID(sb.DangerousGetHandle(), false);

            Assert.That(!sid.IsInvalid);
            Assert.That(sid.IsValidSid);
            Assert.That(sid.ToString(), Does.StartWith("S-1-5"));

            var sidc = PSID.CreateFromPtr(sb.DangerousGetHandle());

            Assert.That((IntPtr)sidc, Is.Not.EqualTo(sb.DangerousGetHandle()));
            Assert.That(sidc.IsValidSid);

            var sid2 = new PSID(sid);

            Assert.That(!sid2.IsInvalid);
            Assert.That(sid2.ToString(), Is.EqualTo(sid.ToString()));

            var sid3 = new PSID("S-1-1-0");
            var id2  = new SecurityIdentifier((IntPtr)sid3);

            Assert.That(id2.IsWellKnown(WellKnownSidType.WorldSid));

            var sid4 = new PSID(100);

            Assert.That(!sid4.IsClosed);
            Assert.That(!sid4.IsValidSid);
            Assert.That(sid4.Size, Is.EqualTo(100));
            sid4.Dispose();
            Assert.That(sid4.IsClosed);
            Assert.That(sid4.Size, Is.EqualTo(0));

            var sid5 = new PSID();

            Assert.That(!sid5.IsClosed);
            Assert.That(sid5.IsInvalid);
            Assert.That(!sid5.IsValidSid);
            Assert.That(sid5.Size, Is.EqualTo(0));

            Assert.That(sid.Equals(sidc));
            Assert.That(sidc.Equals(sb.DangerousGetHandle()));
            Assert.That(sid.Equals("X"), Is.False);
            Assert.That(sid.Equals(sid3), Is.False);
        }