public void TestAreBinaryObjectsProtected()
        {
            var pbData = _enc.GetBytes("Test Test Test Test");
            var pb     = new ProtectedBinary(true, pbData);

            Assert.That(pb.IsProtected, Is.True);

            var pbDec = pb.ReadData();

            Assert.That(MemUtil.ArraysEqual(pbData, pbDec), Is.True);
            Assert.That(pb.IsProtected, Is.True);

            var pbData2 = _enc.GetBytes("Test Test Test Test");
            var pbData3 = _enc.GetBytes("Test Test Test Test Test");
            var pb2     = new ProtectedBinary(true, pbData2);
            var pb3     = new ProtectedBinary(true, pbData3);

            Assert.That(pb.Equals(pb2), Is.True);
            Assert.That(pb.Equals(pb3), Is.False);
            Assert.That(pb2.Equals(pb3), Is.False);

            Assert.That(pb.GetHashCode(), Is.EqualTo(pb2.GetHashCode()));
            Assert.That(pb.Equals((object)pb2), Is.True);
            Assert.That(pb.Equals((object)pb3), Is.False);
            Assert.That(pb2.Equals((object)pb3), Is.False);
        }
Пример #2
0
        public bool EqualsDictionary(ProtectedBinaryDictionary dict)
        {
            if (dict == null)
            {
                Debug.Assert(false); return(false);
            }

            if (m_vBinaries.Count != dict.m_vBinaries.Count)
            {
                return(false);
            }

            foreach (KeyValuePair <string, ProtectedBinary> kvp in m_vBinaries)
            {
                ProtectedBinary pb = dict.Get(kvp.Key);
                if (pb == null)
                {
                    return(false);
                }
                if (!pb.Equals(kvp.Value))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        private string BinPoolFind(ProtectedBinary pb)
        {
            if (pb == null)
            {
                Debug.Assert(false); return(null);
            }

            foreach (KeyValuePair <string, ProtectedBinary> kvp in m_dictBinPool)
            {
                if (pb.Equals(kvp.Value))
                {
                    return(kvp.Key);
                }
            }

            return(null);
        }
Пример #4
0
        private static void TestProtectedObjects()
        {
#if DEBUG
            byte[]          pbData = Encoding.ASCII.GetBytes("Test Test Test Test");
            ProtectedBinary pb     = new ProtectedBinary(true, pbData);
            if (!pb.IsProtected)
            {
                throw new SecurityException("ProtectedBinary-1");
            }

            byte[] pbDec = pb.ReadData();
            if (!MemUtil.ArraysEqual(pbData, pbDec))
            {
                throw new SecurityException("ProtectedBinary-2");
            }
            if (!pb.IsProtected)
            {
                throw new SecurityException("ProtectedBinary-3");
            }

            byte[]          pbData2 = Encoding.ASCII.GetBytes("Test Test Test Test");
            byte[]          pbData3 = Encoding.ASCII.GetBytes("Test Test Test Test Test");
            ProtectedBinary pb2     = new ProtectedBinary(true, pbData2);
            ProtectedBinary pb3     = new ProtectedBinary(true, pbData3);
            if (!pb.Equals(pb2))
            {
                throw new SecurityException("ProtectedBinary-4");
            }
            if (pb.Equals(pb3))
            {
                throw new SecurityException("ProtectedBinary-5");
            }
            if (pb2.Equals(pb3))
            {
                throw new SecurityException("ProtectedBinary-6");
            }

            if (pb.GetHashCode() != pb2.GetHashCode())
            {
                throw new SecurityException("ProtectedBinary-7");
            }
            if (!((object)pb).Equals((object)pb2))
            {
                throw new SecurityException("ProtectedBinary-8");
            }
            if (((object)pb).Equals((object)pb3))
            {
                throw new SecurityException("ProtectedBinary-9");
            }
            if (((object)pb2).Equals((object)pb3))
            {
                throw new SecurityException("ProtectedBinary-10");
            }

            ProtectedString ps = new ProtectedString();
            if (ps.Length != 0)
            {
                throw new SecurityException("ProtectedString-1");
            }
            if (!ps.IsEmpty)
            {
                throw new SecurityException("ProtectedString-2");
            }
            if (ps.ReadString().Length != 0)
            {
                throw new SecurityException("ProtectedString-3");
            }

            ps = new ProtectedString(true, "Test");
            ProtectedString ps2 = new ProtectedString(true,
                                                      StrUtil.Utf8.GetBytes("Test"));
            if (ps.IsEmpty)
            {
                throw new SecurityException("ProtectedString-4");
            }
            pbData  = ps.ReadUtf8();
            pbData2 = ps2.ReadUtf8();
            if (!MemUtil.ArraysEqual(pbData, pbData2))
            {
                throw new SecurityException("ProtectedString-5");
            }
            if (pbData.Length != 4)
            {
                throw new SecurityException("ProtectedString-6");
            }
            if (ps.ReadString() != ps2.ReadString())
            {
                throw new SecurityException("ProtectedString-7");
            }
            pbData  = ps.ReadUtf8();
            pbData2 = ps2.ReadUtf8();
            if (!MemUtil.ArraysEqual(pbData, pbData2))
            {
                throw new SecurityException("ProtectedString-8");
            }
            if (!ps.IsProtected)
            {
                throw new SecurityException("ProtectedString-9");
            }
            if (!ps2.IsProtected)
            {
                throw new SecurityException("ProtectedString-10");
            }
#endif
        }
Пример #5
0
        private static void TestProtectedObjects()
        {
#if DEBUG
            Encoding enc = StrUtil.Utf8;

            byte[]          pbData = enc.GetBytes("Test Test Test Test");
            ProtectedBinary pb     = new ProtectedBinary(true, pbData);
            if (!pb.IsProtected)
            {
                throw new SecurityException("ProtectedBinary-1");
            }

            byte[] pbDec = pb.ReadData();
            if (!MemUtil.ArraysEqual(pbData, pbDec))
            {
                throw new SecurityException("ProtectedBinary-2");
            }
            if (!pb.IsProtected)
            {
                throw new SecurityException("ProtectedBinary-3");
            }

            byte[]          pbData2 = enc.GetBytes("Test Test Test Test");
            byte[]          pbData3 = enc.GetBytes("Test Test Test Test Test");
            ProtectedBinary pb2     = new ProtectedBinary(true, pbData2);
            ProtectedBinary pb3     = new ProtectedBinary(true, pbData3);
            if (!pb.Equals(pb2))
            {
                throw new SecurityException("ProtectedBinary-4");
            }
            if (pb.Equals(pb3))
            {
                throw new SecurityException("ProtectedBinary-5");
            }
            if (pb2.Equals(pb3))
            {
                throw new SecurityException("ProtectedBinary-6");
            }

            if (pb.GetHashCode() != pb2.GetHashCode())
            {
                throw new SecurityException("ProtectedBinary-7");
            }
            if (!((object)pb).Equals((object)pb2))
            {
                throw new SecurityException("ProtectedBinary-8");
            }
            if (((object)pb).Equals((object)pb3))
            {
                throw new SecurityException("ProtectedBinary-9");
            }
            if (((object)pb2).Equals((object)pb3))
            {
                throw new SecurityException("ProtectedBinary-10");
            }

            ProtectedString ps = new ProtectedString();
            if (ps.Length != 0)
            {
                throw new SecurityException("ProtectedString-1");
            }
            if (!ps.IsEmpty)
            {
                throw new SecurityException("ProtectedString-2");
            }
            if (ps.ReadString().Length != 0)
            {
                throw new SecurityException("ProtectedString-3");
            }

            ps = new ProtectedString(true, "Test");
            ProtectedString ps2 = new ProtectedString(true, enc.GetBytes("Test"));
            if (ps.IsEmpty)
            {
                throw new SecurityException("ProtectedString-4");
            }
            pbData  = ps.ReadUtf8();
            pbData2 = ps2.ReadUtf8();
            if (!MemUtil.ArraysEqual(pbData, pbData2))
            {
                throw new SecurityException("ProtectedString-5");
            }
            if (pbData.Length != 4)
            {
                throw new SecurityException("ProtectedString-6");
            }
            if (ps.ReadString() != ps2.ReadString())
            {
                throw new SecurityException("ProtectedString-7");
            }
            pbData  = ps.ReadUtf8();
            pbData2 = ps2.ReadUtf8();
            if (!MemUtil.ArraysEqual(pbData, pbData2))
            {
                throw new SecurityException("ProtectedString-8");
            }
            if (!ps.IsProtected)
            {
                throw new SecurityException("ProtectedString-9");
            }
            if (!ps2.IsProtected)
            {
                throw new SecurityException("ProtectedString-10");
            }

            Random r   = new Random();
            string str = string.Empty;
            ps = new ProtectedString();
            for (int i = 0; i < 100; ++i)
            {
                bool bProt = ((r.Next() % 4) != 0);
                ps = ps.WithProtection(bProt);

                int  x  = r.Next(str.Length + 1);
                int  c  = r.Next(20);
                char ch = (char)r.Next(1, 256);

                string strIns = new string(ch, c);
                str = str.Insert(x, strIns);
                ps  = ps.Insert(x, strIns);

                if (ps.IsProtected != bProt)
                {
                    throw new SecurityException("ProtectedString-11");
                }
                if (ps.ReadString() != str)
                {
                    throw new SecurityException("ProtectedString-12");
                }

                ps = ps.WithProtection(bProt);

                x = r.Next(str.Length);
                c = r.Next(str.Length - x + 1);

                str = str.Remove(x, c);
                ps  = ps.Remove(x, c);

                if (ps.IsProtected != bProt)
                {
                    throw new SecurityException("ProtectedString-13");
                }
                if (ps.ReadString() != str)
                {
                    throw new SecurityException("ProtectedString-14");
                }
            }
#endif
        }