Пример #1
0
        public void CanWrite()
        {
            var sample1 = Enumerable.Range(0, 4).Select(x => (byte)x).ToArray();
            var sample2 = Enumerable.Range(4, 4).Select(x => (byte)x).ToArray();
            var sample3 = Enumerable.Range(8, 4).Select(x => (byte)x).ToArray();

            var dfuMock = new Mock <IDfu>();

            var dfuPrefixSerializerMock = new Mock <IDfuPrefixSerializer>();

            dfuPrefixSerializerMock.Setup(
                x => x.Write(
                    It.IsAny <Stream>(),
                    It.IsAny <IDfuPrefix>(),
                    It.IsAny <IDfuImages>()))
            .Callback <Stream, IDfuPrefix, IDfuImages>((s, p, i) => s.Write(sample1, 0, sample1.Length));

            var dfuImagesSerializerMock = new Mock <IDfuImagesSerializer>();

            dfuImagesSerializerMock.Setup(
                x => x.Write(
                    It.IsAny <Stream>(),
                    It.IsAny <IDfuImages>()))
            .Callback <Stream, IDfuImages>((s, i) => s.Write(sample2, 0, sample2.Length));

            var dfuSuffixSerializerMock = new Mock <IDfuSuffixSerializer>();

            dfuSuffixSerializerMock.Setup(
                x => x.Write(
                    It.IsAny <Stream>(), It.IsAny <IDfuSuffix>()))
            .Callback <Stream, IDfuSuffix>((s, su) => s.Write(sample3, 0, sample3.Length));

            var sut = new DfuSerializer(
                () => dfuPrefixSerializerMock.Object,
                () => dfuImagesSerializerMock.Object,
                () => dfuSuffixSerializerMock.Object);

            var tempStream = new MemoryStream();

            sut.Write(tempStream, dfuMock.Object);

            var actual   = tempStream.ToArray();
            var expected = Enumerable.Range(0, 12).Select(x => (byte)x).ToArray();

            Assert.That(actual, Is.EqualTo(expected));
        }
Пример #2
0
        public void CanWriteFullMocks()
        {
            var sampleData = new byte[] { 1, 2, 3, 4 };

            var dfuPrefixMock = new Mock <IDfuPrefix>();

            dfuPrefixMock.SetupGet(x => x.Signature).Returns("DfuSe");
            dfuPrefixMock.SetupGet(x => x.Version).Returns(1);

            var dfuSuffixMock = new Mock <IDfuSuffix>();

            dfuSuffixMock.SetupGet(x => x.Device).Returns(0x5678);
            dfuSuffixMock.SetupGet(x => x.Product).Returns(0x1234);
            dfuSuffixMock.SetupGet(x => x.Vendor).Returns(0x0483);
            dfuSuffixMock.SetupGet(x => x.Dfu).Returns(0x011a);
            dfuSuffixMock.SetupGet(x => x.Length).Returns(16);
            dfuSuffixMock.SetupGet(x => x.DfuSignature).Returns("UFD");

            var targetPrefixMock = new Mock <ITargetPrefix>();

            targetPrefixMock.SetupGet(x => x.TargetId).Returns(2);
            targetPrefixMock.SetupGet(x => x.Signature).Returns("Target");
            targetPrefixMock.SetupGet(x => x.IsTargetNamed).Returns(true);
            targetPrefixMock.SetupGet(x => x.TargetName).Returns(new string('Q', 256));

            var imageElementMock = new Mock <IImageElement>();

            imageElementMock.SetupGet(x => x.ElementAddress).Returns(0x08000000);
            imageElementMock.SetupGet(x => x.Data).Returns(sampleData);
            imageElementMock.SetupGet(x => x.ElementSize).Returns(Convert.ToUInt32(sampleData.Length));

            var dfuImageMock = new Mock <IDfuImage>();

            dfuImageMock.SetupGet(x => x.Prefix).Returns(targetPrefixMock.Object);
            dfuImageMock.SetupGet(x => x.ImageElements)
            .Returns(new EditableList <IImageElement> {
                imageElementMock.Object
            });

            var dfuImagesMock = new Mock <IDfuImages>();

            dfuImagesMock.SetupGet(x => x.Images).Returns(new EditableList <IDfuImage> {
                dfuImageMock.Object
            });


            var dfuMock = new Mock <IDfu>();

            dfuMock.SetupGet(x => x.Prefix).Returns(dfuPrefixMock.Object);
            dfuMock.SetupGet(x => x.Images).Returns(dfuImagesMock.Object);
            dfuMock.SetupGet(x => x.Suffix).Returns(dfuSuffixMock.Object);

            // Array generated by DFU File Manager v3.0.6
            var expected = new byte[] {
                0x44, 0x66, 0x75, 0x53, 0x65,
                0x01,
                0x29, 0x01, 0x00, 0x00,
                0x01,

                0x54, 0x61, 0x72, 0x67, 0x65, 0x74,
                0x02,
                0x01, 0x00, 0x00, 0x00,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
                0x0C, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00,

                0x00, 0x00, 0x00, 0x08,
                0x04, 0x00, 0x00, 0x00,
                0x01, 0x02, 0x03, 0x04,

                0x78, 0x56,
                0x34, 0x12,
                0x83, 0x04,
                0x1A, 0x01,
                0x55, 0x46, 0x44,
                0x10,
                0xB7, 0x02, 0xE0, 0x8D
            };

            var sut = new DfuSerializer(
                () => new DfuPrefixSerializer(
                    () => new DfuImagesSerializer(
                        () => new DfuImageSerializer(
                            () => new TargetPrefixSerializer(),
                            () => new ImageElementSerializer()))),
                () => new DfuImagesSerializer(
                    () => new DfuImageSerializer(
                        () => new TargetPrefixSerializer(),
                        () => new ImageElementSerializer())),
                () => new DfuSuffixSerializer());

            var tempStream = new MemoryStream();

            sut.Write(tempStream, dfuMock.Object);

            var actual = tempStream.ToArray();

            Assert.That(actual, Is.EqualTo(expected));
        }