示例#1
0
文件: SelfTest.cs 项目: Stoom/KeePass
		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
		}
示例#2
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");
            #endif
        }