Exemplo n.º 1
0
        public void Decoder_LongPacketWithMultipleDelimiters_ParsedSuccessfully()
        {
            var input = new byte[512];

            input[0] = 128;
            Array.Fill <byte>(input, 0x04, 1, 127);
            input[128] = 255;
            Array.Fill <byte>(input, 0x42, 129, 254);
            input[383] = 128;
            Array.Fill <byte>(input, 0xAB, 384, 127);
            input[511] = 0;

            var decoder = new CobsDecoder();

            byte[] output = null;
            foreach (var b in input)
            {
                output = decoder.NextByte(b);
            }

            Assert.NotNull(output);
            Assert.Equal(127 + 254 + 127 + 1, output.Length);

            Assert.Equal(1, output.Count(a => a == 0x00));
            Assert.Equal(127, output.Count(a => a == 0x04));
            Assert.Equal(254, output.Count(a => a == 0x42));
            Assert.Equal(127, output.Count(a => a == 0xAB));
        }
Exemplo n.º 2
0
        public void Decode_TooLongInput_Fails()
        {
            var fullChunkLength        = CobsEncoder.MAX_CHUNK_LENGTH - 1;
            var fullChunksCount        = CobsEncoder.MAX_PACKET_SIZE / fullChunkLength;
            var totalBytesInFullChunks = fullChunksCount * fullChunkLength;
            var bytesInLastChunk       = CobsEncoder.MAX_PACKET_SIZE - totalBytesInFullChunks;

            fullChunksCount++;

            var input = GenerateInput(
                Enumerable
                .Repeat((byte)fullChunkLength, fullChunksCount)
                .Append((byte)bytesInLastChunk)
                .ToArray(),
                0x04
                );

            var decoder = new CobsDecoder();

            foreach (var b in input)
            {
                byte[] output = decoder.NextByte(b);
                Assert.Null(output);
            }
        }
Exemplo n.º 3
0
        public void Decode_WithMaximumLengthInput_Works()
        {
            var fullChunkLength        = CobsEncoder.MAX_CHUNK_LENGTH - 1;
            var fullChunksCount        = CobsEncoder.MAX_PACKET_SIZE / fullChunkLength;
            var totalBytesInFullChunks = fullChunksCount * fullChunkLength;
            var bytesInLastChunk       = CobsEncoder.MAX_PACKET_SIZE - totalBytesInFullChunks;

            var input = GenerateInput(
                Enumerable
                .Repeat((byte)fullChunkLength, fullChunksCount)
                .Append((byte)bytesInLastChunk)
                .ToArray(),
                0x04
                );

            var decoder = new CobsDecoder();

            byte[] output = null;
            foreach (var b in input)
            {
                output = decoder.NextByte(b);
            }

            Assert.NotNull(output);
            Assert.Equal(CobsEncoder.MAX_PACKET_SIZE, output.Length);
            Assert.Equal(CobsEncoder.MAX_PACKET_SIZE, output.Count(a => a == 0x04));
        }
Exemplo n.º 4
0
        public void Decoder_CompletePacket_ParsedSuccessfully()
        {
            var input   = new byte[] { 0x01, 0x01, 0x00 };
            var decoder = new CobsDecoder();

            byte[] output = null;
            foreach (var b in input)
            {
                output = decoder.NextByte(b);
            }

            Assert.NotNull(output);
            Assert.Equal(new byte[] { 0x00 }, output);
        }
Exemplo n.º 5
0
        public void Decoder_CompletePacketWithMultipleDelimiters_ParsedSuccessfully()
        {
            var input   = new byte[] { 0x03, 0x01, 0x02, 0x04, 0x03, 0x04, 0x05, 0x02, 0x06, 0x00 };
            var decoder = new CobsDecoder();

            byte[] output = null;
            foreach (var b in input)
            {
                output = decoder.NextByte(b);
            }

            Assert.NotNull(output);
            Assert.Equal(new byte[] { 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x00, 0x06 }, output);
        }
Exemplo n.º 6
0
        public void Decode_WithMixedChunkLength_Works(byte[] segmentsLength)
        {
            var input = GenerateInput(segmentsLength, 0x42);

            var decoder = new CobsDecoder();

            byte[] output = null;
            foreach (var b in input)
            {
                output = decoder.NextByte(b);
            }

            var expectedValuesCount            = segmentsLength.Select(b => (int)b).Sum();
            var expectedEncodedDelimitersCount = segmentsLength.Count(b => b == (0xFF - 1));
            var expectedLength = expectedValuesCount + expectedEncodedDelimitersCount;

            Assert.NotNull(output);
            Assert.Equal(expectedLength, output.Length);
            Assert.Equal(expectedValuesCount, output.Count(a => a == 0x42));
        }
Exemplo n.º 7
0
        public void Decoder_DelimiterInInput_ResetsParser()
        {
            var input = new byte[32];

            Array.Fill <byte>(input, 0x04);
            input[0]  = 0x1F;
            input[31] = 0x00;
            input[12] = 0x00;
            input[13] = 0x12;

            var decoder = new CobsDecoder();

            byte[] output = null;
            foreach (var b in input)
            {
                output = decoder.NextByte(b);
            }

            Assert.NotNull(output);
            Assert.Equal(17, output.Length);
            Assert.Equal(17, output.Count(a => a == 0x04));
        }