Ejemplo n.º 1
0
        public override ISafeString Provide()
        {
            var fake       = new Mock <ISafeString>();
            var chars      = new List <char>();
            var isDisposed = false;

            fake.Setup(x => x.Append(It.IsAny <char>())).Callback <char>(x => chars.Add(x));
            fake.Setup(x => x.Append(It.IsAny <string>())).Callback <string>(x => chars.AddRange(x));
            fake.Setup(x => x.Append(It.IsAny <ISafeBytes>(), It.IsAny <Encoding>())).Callback <ISafeBytes, Encoding>(
                (c, e) => chars.Add((char)c.ToByteArray().FirstOrDefault())); //this  might broke new tests
            fake.Setup(x => x.ToSafeBytes()).Returns(() =>
            {
                var safeBytes = Stubs.Get <ISafeBytes>();
                foreach (char c in chars)
                {
                    safeBytes.Append((byte)c);
                }
                return(safeBytes);
            });
            fake.Setup(x => x.GetAsSafeBytes(It.IsAny <int>())).Returns((int i) =>
            {
                var c         = chars.ElementAt(i);
                var safeBytes = Stubs.Get <ISafeBytes>();
                safeBytes.Append((byte)c);
                return(safeBytes);
            });
            fake.Setup(x => x.GetAsChar(It.IsAny <int>())).Returns((int i) => chars.ElementAt(i));
            fake.Setup(x => x.Length).Returns(() => chars.Count());
            fake.Setup(x => x.IsEmpty).Returns(() => !chars.Any());
            fake.Setup(x => x.DeepClone()).Returns(() => fake.Object);
            fake.Setup(x => x.Equals(It.IsAny <string>())).Returns((string text) => chars.ToArray().SequenceEqual(text.ToCharArray()));
            fake.Setup(x => x.IsDisposed).Returns(() => isDisposed);
            fake.Setup(x => x.Dispose()).Callback(() => isDisposed = true);
            return(fake.Object);
        }
Ejemplo n.º 2
0
        public override IPadderFactory Provide()
        {
            var fake = new Mock <IPadderFactory>();

            fake.Setup(f => f.GetPadder(PaddingMode.PKCS7))
            .Returns(Stubs.Get <IPkcs7Padder>());
            return(fake.Object);
        }
Ejemplo n.º 3
0
        public override ISafeByteFactory Provide()
        {
            var fake = new Mock <ISafeByteFactory>();

            fake.Setup(x => x.GetByByteAsync(It.IsAny <byte>()))
            .ReturnsAsync((byte @byte) =>
            {
                var safeByte = Stubs.Get <ISafeByte>();
                TaskContext.RunSync(() => safeByte.SetAsync(@byte));
                return(safeByte);
            });
            fake.Setup(x => x.GetByBytesAsync(It.IsAny <SafeMemoryStream>()))
            .ReturnsAsync((SafeMemoryStream stream) =>
            {
                var bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);

                var result = new Collection <ISafeByte>();
                foreach (var @byte in bytes)
                {
                    var safeByte = Stubs.Get <ISafeByte>();
                    TaskContext.RunSync(() => safeByte.SetAsync(@byte));
                    result.Add(safeByte);
                }
                return(result);
            });
            fake.Setup(x => x.GetByIdAsync(It.IsAny <int>()))
            .ReturnsAsync(
                (int id) =>
            {
                var safeByte = Stubs.Get <ISafeByte>();
                TaskContext.RunSync(() => safeByte.SetAsync((byte)id));
                return(safeByte);
            });
            fake.Setup(x => x.GetByIdsAsync(It.IsAny <IEnumerable <int> >()))
            .ReturnsAsync((IEnumerable <int> byteIds) =>
            {
                var result = new Collection <ISafeByte>();
                foreach (var byteId in byteIds)
                {
                    var safeByte = Stubs.Get <ISafeByte>();
                    TaskContext.RunSync(() => safeByte.SetAsync((byte)byteId));
                    result.Add(safeByte);
                }
                return(result.ToArray());
            });
            return(fake.Object);
        }
Ejemplo n.º 4
0
        public override ISafeString Provide()
        {
            var fake       = new Mock <ISafeString>();
            var chars      = new List <char>();
            var isDisposed = false;

            fake.Setup(x => x.AppendAsync(It.IsAny <char>()))
            .Callback <char>(x => chars.Add(x));
            fake.Setup(x => x.AppendAsync(It.IsAny <string>()))
            .Callback <string>(x => chars.AddRange(x));
            fake.Setup(x => x.AppendAsync(It.IsAny <ISafeBytes>(), It.IsAny <Encoding>()))
            .Callback <ISafeBytes, Encoding>(
                (bytes, e) => chars.AddRange(System.Text.Encoding.Unicode.GetString(TaskContext.RunSync(bytes.RevealDecryptedBytesAsync))));
            fake.Setup(x => x.RevealDecryptedBytesAsync())
            .ReturnsAsync(() => System.Text.Encoding.Unicode.GetBytes(new string(chars.ToArray())));
            fake.Setup(x => x.GetAsSafeBytes(It.IsAny <int>()))
            .Returns((int i) =>
            {
                var c         = chars.ElementAt(i);
                var safeBytes = Stubs.Get <ISafeBytes>();
                var bytes     = System.Text.Encoding.Unicode.GetBytes($"{c}");
                foreach (var @byte in bytes)
                {
                    TaskContext.RunSync(() => safeBytes.AppendAsync(@byte));
                }
                return(safeBytes);
            });
            fake.Setup(x => x.RevealDecryptedCharAsync(It.IsAny <int>()))
            .ReturnsAsync((int i) => chars.ElementAt(i));
            fake.Setup(x => x.Length)
            .Returns(() => chars.Count);
            fake.Setup(x => x.IsEmpty)
            .Returns(() => !chars.Any());
            fake.Setup(x => x.DeepCloneAsync())
            .ReturnsAsync(() => fake.Object);
            fake.Setup(x => x.EqualsAsync(It.IsAny <string>()))
            .ReturnsAsync((string text) => chars.AsEnumerable().SequenceEqual(text.ToCharArray()));
            fake.Setup(x => x.IsDisposed)
            .Returns(() => isDisposed);
            fake.Setup(x => x.Dispose())
            .Callback(() => isDisposed = true);
            return(fake.Object);
        }
Ejemplo n.º 5
0
        public override ISafeByteFactory Provide()
        {
            var fake = new Mock <ISafeByteFactory>();

            fake.Setup(x => x.GetByByte(It.IsAny <byte>())).Returns(
                (byte b) =>
            {
                var safeByte = Stubs.Get <ISafeByte>();
                safeByte.Set(b);
                return(safeByte);
            });
            fake.Setup(x => x.GetById(It.IsAny <int>())).Returns(
                (int id) =>
            {
                var safeByte = Stubs.Get <ISafeByte>();
                safeByte.Set((byte)id);
                return(safeByte);
            });
            return(fake.Object);
        }