예제 #1
0
        public void TestResizeBitArrayBigger()
        {
            PrivateType hh = new PrivateType(typeof(HammingHelper));

            BitArray bitArrayInput = new BitArray(10)
            {
                [0] = true,
                [2] = true,
                [8] = true,
                [9] = true,
            };

            BitArray bitArrayExpected = new BitArray(18)
            {
                [0] = true,
                [2] = true,
                [8] = true,
                [9] = true,
            };

            object[] args           = { bitArrayInput, 18, 0 };
            var      bitArrayOutput = (BitArray)hh.InvokeStatic("ResizeBitArray", args);

            Assert.AreEqual(HammingHelper.BitArrayToDigitString(bitArrayExpected), HammingHelper.BitArrayToDigitString(bitArrayOutput));
        }
예제 #2
0
        public void TestBitArrayToDigitString()
        {
            BitArray bitArray = new BitArray(5);

            Assert.AreEqual("00000", HammingHelper.BitArrayToDigitString(bitArray));

            bitArray[0] = true;
            bitArray[2] = true;
            Assert.AreEqual("10100", HammingHelper.BitArrayToDigitString(bitArray));

            bitArray.SetAll(true);
            Assert.AreEqual("11111", HammingHelper.BitArrayToDigitString(bitArray));
        }
예제 #3
0
        public void TestEncryptDecryptManager()
        {
            // #1 case 10000010 (8 bits)
            BitArray bitArray = new BitArray(8)
            {
                [0] = true,
                [6] = true
            };

            BitArray bitArrayEncrypt = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.CORRECT).Item1;
            BitArray bitArrayDecrypt = HammingHelper.DecryptManager(bitArrayEncrypt, HammingHelper.Mode.CORRECT).Item1;

            Assert.AreEqual(HammingHelper.BitArrayToDigitString(bitArray), HammingHelper.BitArrayToDigitString(bitArrayDecrypt));


            // # Random case
            bitArray = new BitArray(32)
            {
                [0]  = false,
                [6]  = true,
                [8]  = true,
                [18] = true,
                [20] = true,
                [29] = true,
            };

            Tuple <BitArray, HammingHelper.Status> tupleEncrypt = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.CORRECT);
            Tuple <BitArray, HammingHelper.Status> tupleDecrypt = HammingHelper.DecryptManager(tupleEncrypt.Item1, HammingHelper.Mode.CORRECT);

            Assert.AreEqual(HammingHelper.Status.OK, tupleEncrypt.Item2);
            Assert.AreEqual(HammingHelper.Status.OK, tupleDecrypt.Item2);
            Assert.AreEqual(HammingHelper.BitArrayToDigitString(bitArray), HammingHelper.BitArrayToDigitString(tupleDecrypt.Item1));

            // # Random case with 1 error (CORRECTED)
            bitArray = new BitArray(8)
            {
                [0] = true,
                [3] = true,
                [5] = true,
            };

            tupleEncrypt          = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.CORRECT);
            tupleEncrypt.Item1[9] = !tupleEncrypt.Item1[9]; // Inject error here
            tupleDecrypt          = HammingHelper.DecryptManager(tupleEncrypt.Item1, HammingHelper.Mode.CORRECT);

            Assert.AreEqual(HammingHelper.Status.OK, tupleEncrypt.Item2);
            Assert.AreEqual(HammingHelper.Status.CORRECTED, tupleDecrypt.Item2);
            Assert.AreEqual(HammingHelper.BitArrayToDigitString(bitArray), HammingHelper.BitArrayToDigitString(tupleDecrypt.Item1));


            // # Random case with 2 error (DETECTED)
            bitArray = new BitArray(8)
            {
                [0] = true,
                [3] = true,
                [5] = true,
            };

            tupleEncrypt          = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.DETECT);
            tupleEncrypt.Item1[1] = !tupleEncrypt.Item1[1]; // Inject error here
            tupleEncrypt.Item1[9] = !tupleEncrypt.Item1[9]; // Inject error here
            tupleDecrypt          = HammingHelper.DecryptManager(tupleEncrypt.Item1, HammingHelper.Mode.DETECT);

            Assert.AreEqual(HammingHelper.Status.OK, tupleEncrypt.Item2);
            Assert.AreEqual(HammingHelper.Status.DETECTED, tupleDecrypt.Item2);

            // # Random case with 3 error (1 CORRECTED and 2 DETECTED)
            bitArray = new BitArray(16)
            {
                [0]  = true,
                [1]  = true,
                [5]  = true,
                [7]  = true,
                [13] = true,
            };

            tupleEncrypt           = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.CORRECT);
            tupleEncrypt.Item1[1]  = !tupleEncrypt.Item1[1];  // Inject error here (first 13 bits)
            tupleEncrypt.Item1[20] = !tupleEncrypt.Item1[20]; // Inject error here (second 13 bits)
            tupleEncrypt.Item1[25] = !tupleEncrypt.Item1[25]; // Inject error here (second 13 bits)
            tupleDecrypt           = HammingHelper.DecryptManager(tupleEncrypt.Item1, HammingHelper.Mode.CORRECT);

            Assert.AreEqual(HammingHelper.Status.OK, tupleEncrypt.Item2);
            Assert.AreEqual(HammingHelper.Status.DETECTED, tupleDecrypt.Item2);


            // # Random case with 3 error (NOT SUPPORTED)
            bitArray = new BitArray(8)
            {
                [0] = true,
                [1] = true,
                [5] = true,
            };

            tupleEncrypt          = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.CORRECT);
            tupleEncrypt.Item1[1] = !tupleEncrypt.Item1[1]; // Inject error here
            tupleEncrypt.Item1[5] = !tupleEncrypt.Item1[5]; // Inject error here
            tupleEncrypt.Item1[9] = !tupleEncrypt.Item1[9]; // Inject error here
            tupleDecrypt          = HammingHelper.DecryptManager(tupleEncrypt.Item1, HammingHelper.Mode.CORRECT);

            Assert.AreEqual(HammingHelper.Status.OK, tupleEncrypt.Item2);
            Assert.AreEqual(HammingHelper.Status.DETECTED, tupleDecrypt.Item2);


            // # Random case with 1 (Detect only 1 bit)
            bitArray = new BitArray(8)
            {
                [0] = true,
                [1] = true,
                [5] = true,
            };

            tupleEncrypt          = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.DETECT);
            tupleEncrypt.Item1[1] = !tupleEncrypt.Item1[1]; // Inject error here
            tupleDecrypt          = HammingHelper.DecryptManager(tupleEncrypt.Item1, HammingHelper.Mode.DETECT);

            Assert.AreEqual(HammingHelper.Status.OK, tupleEncrypt.Item2);
            Assert.AreEqual(HammingHelper.Status.DETECTED, tupleDecrypt.Item2);


            // # Brute force cases
            for (int num = 0; num < 256; ++num)
            {
                for (int i = 0; i < 13; ++i)
                {
                    for (int j = 0; j < 13; ++j)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        byte[] bytes = BitConverter.GetBytes(num);
                        bitArray = new BitArray(new[] { bytes[0] });

                        tupleEncrypt          = HammingHelper.EncryptManager(bitArray, HammingHelper.Mode.CORRECT);
                        tupleEncrypt.Item1[j] = !tupleEncrypt.Item1[j]; // Inject error here
                        tupleEncrypt.Item1[i] = !tupleEncrypt.Item1[i]; // Inject error here
                        tupleDecrypt          = HammingHelper.DecryptManager(tupleEncrypt.Item1, HammingHelper.Mode.CORRECT);

                        Assert.AreEqual(HammingHelper.Status.OK, tupleEncrypt.Item2);
                        Assert.AreEqual(HammingHelper.Status.DETECTED, tupleDecrypt.Item2);
                    }
                }
            }
        }