Ejemplo n.º 1
0
        public void Encode_Range()
        {
            Action TestCase(int testNumber, byte[] bytes, int offset, int length, bool toUpper, string expected, Type expectedExceptionType = null) => () => {
                new TestCaseRunner($"No.{testNumber}")
                .Run(() => Base16.Encode(bytes, offset, length, toUpper))
                .Verify(expected, expectedExceptionType);
            };

            var rndBytes = Rand.Bytes();

            new[] {
                TestCase(0, null, 0, 0, false, null, typeof(ArgumentNullException)),
                TestCase(1, Bytes(), 0, 0, false, ""),
                TestCase(2, Bytes(0x0f), 0, 1, false, "0f"),
                TestCase(3, Bytes(0x0f, 0xf0), 0, 2, false, "0ff0"),
                TestCase(4, Bytes(0x0f, 0xf0), 0, 2, true, "0FF0"),
                TestCase(10, Bytes(0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef), 0, 8, false, "0123456789abcdef"),
                TestCase(11, Bytes(0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef), 0, 8, true, "0123456789ABCDEF"),
                TestCase(20, Bytes(0x0f, 0xf0), -1, 0, true, null, typeof(ArgumentOutOfRangeException)),
                TestCase(21, Bytes(0x0f, 0xf0), 0, -1, true, null, typeof(ArgumentOutOfRangeException)),
                TestCase(22, Bytes(0x0f, 0xf0), 2, 0, true, ""),
                TestCase(23, Bytes(0x0f, 0xf0), 1, 1, true, "F0"),
                TestCase(24, Bytes(0x0f, 0xf0), 1, 2, true, null, typeof(ArgumentOutOfRangeException)),
                TestCase(50, rndBytes, 0, rndBytes.Length, false, BitConverter.ToString(rndBytes).Replace("-", "").ToLowerInvariant()),
                TestCase(51, rndBytes, 0, rndBytes.Length, true, BitConverter.ToString(rndBytes).Replace("-", "").ToUpperInvariant()),
            }.Run();
        }
        public void Ctor()
        {
            Action TestCase(int testNumber, byte[] hash, Type expectedException) => () => {
                var version = (byte)Rand.Int();

                new TestCaseRunner($"No.{testNumber}")
                .Run(() => new UrlSafeSignature(version, hash))
                .Verify((actual, desc) => {
                    actual.Version.Is(version, desc);
                }, expectedException);
            };

            new[] {
                TestCase(0, null, typeof(ArgumentNullException)),
                TestCase(1, new byte[0], null),
                TestCase(2, Rand.Bytes(), null),
            }.Run();
        }
Ejemplo n.º 3
0
        public void Ctor()
        {
            Action TestCase(int testNumber, byte[] eKey, byte[] iKey, Type expectedExceptionType = null) => () => {
                new TestCaseRunner($"No.{testNumber}")
                .Run(() => new ABCryptoKeys(eKey, iKey))
                .Verify((actual, desc) => {
                    actual.EncryptionKey.Is(eKey, desc);
                    actual.IntegrityKey.Is(iKey, desc);
                }, expectedExceptionType);
            };

            new[] {
                TestCase(0, null, Rand.Bytes(minLength: 1), typeof(ArgumentNullException)),
                TestCase(1, new byte[0], Rand.Bytes(minLength: 1), typeof(ArgumentException)),
                TestCase(2, Rand.Bytes(minLength: 1), null, typeof(ArgumentNullException)),
                TestCase(3, Rand.Bytes(minLength: 1), new byte[0], typeof(ArgumentException)),
                TestCase(10, Rand.Bytes(minLength: 1), Rand.Bytes(minLength: 1)),
            }.Run();
        }
        public void EncryptPrice_AutoIV()
        {
            Action TestCase(int testNumber, ABCrypto crypto, decimal price, Type expectedExceptionType = null) => () => {
                new TestCaseRunner($"No.{testNumber}")
                .Run(() => {
                    var cipher = ABPriceCrypto.EncryptPrice(crypto, price);
                    Console.WriteLine(cipher);
                    return(success: crypto.TryDecryptPrice(cipher, out var actualPrice), price: actualPrice);
                })
                .Verify((actual, desc) => {
                    actual.success.Is(true, desc);
                    actual.price.Is(price, desc);
                }, expectedExceptionType);
            };

            var rndCrypto = new ABCrypto(new StubABCryptoKeys(
                                             eKey: Rand.Bytes()
                                             , iKey: Rand.Bytes()
                                             ));

            new[] {
Ejemplo n.º 5
0
        public void Encode()
        {
            Action TestCase(TestNumber testNumber, byte[] bytes, bool toUpper, string expected) => () => {
                TestAA
                .Act(() => Base16.Encode(bytes, toUpper))
                .Assert(expected, message: testNumber);
            };

            var rndBytes = Rand.Bytes();

            new[] {
                TestCase(1, Bytes(), toUpper: false, expected: ""),
                TestCase(2, Bytes(0x0f), toUpper: false, expected: "0f"),
                TestCase(3, Bytes(0x0f, 0xf0), toUpper: false, expected: "0ff0"),
                TestCase(4, Bytes(0x0f, 0xf0), toUpper: true, expected: "0FF0"),
                TestCase(10, Bytes(0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef), toUpper: false, expected: "0123456789abcdef"),
                TestCase(11, Bytes(0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef), toUpper: true, expected: "0123456789ABCDEF"),
                TestCase(50, rndBytes, toUpper: false, expected: BitConverter.ToString(rndBytes).Replace("-", "").ToLowerInvariant()),
                TestCase(51, rndBytes, toUpper: true, expected: BitConverter.ToString(rndBytes).Replace("-", "").ToUpperInvariant()),
            }.Run();
        }