Пример #1
0
        public void testReadString16()
        {
            // Test data with 1-byte UTF8 encoding.
            {
                char[] expect = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C', '\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064' };
                byte[] input  = { 0x00, 0x0E, 0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };

                readString16Helper(input, expect);
            }

            // Test data with 2-byte UT8 encoding.
            {
                char[] expect = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' };
                byte[] input  = { 0x00, 0x0A, 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC2, 0xA6 };
                readString16Helper(input, expect);
            }

            // Test data with 1-byte and 2-byte encoding with embedded NULL's.
            {
                char[] expect = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000', '\u00A6' };
                byte[] input  = { 0x00, 0x0D, 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC0, 0x80, 0xC2, 0xA6 };

                readString16Helper(input, expect);
            }

            // Test with bad UTF-8 encoding, missing 2nd byte of two byte value
            {
                byte[] input = { 0x00, 0x0D, 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xC2, 0xC3, 0x83, 0xC0, 0x80, 0xC2, 0xA6 };

                MemoryStream         stream = new MemoryStream(input);
                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);

                try
                {
                    reader.ReadString16();
                    Assert.Fail("Should Throw an IOException");
                }
                catch (NMSException)
                {
                }
            }

            // Test with three byte encode that's missing a last byte.
            {
                byte[] input = { 0x00, 0x02, 0xE8, 0xA8 };

                MemoryStream         stream = new MemoryStream(input);
                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);

                try
                {
                    reader.ReadString16();
                    Assert.Fail("Should Throw an IOException");
                }
                catch (NMSException)
                {
                }
            }
        }
Пример #2
0
        public void readString16Helper(byte[] input, char[] expect)
        {
            MemoryStream         stream = new MemoryStream(input);
            OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);

            char[] result = reader.ReadString16().ToCharArray();

            for (int i = 0; i < expect.Length; ++i)
            {
                Assert.AreEqual(expect[i], result[i]);
            }
        }
Пример #3
0
        protected void AssertMarshalBooleans(int count, GetBooleanValueDelegate valueDelegate)
        {
            BooleanStream bs = new BooleanStream();

            for (int i = 0; i < count; i++)
            {
                bs.WriteBoolean(valueDelegate(i, count));
            }
            MemoryStream buffer = new MemoryStream();
            BinaryWriter ds     = new OpenWireBinaryWriter(buffer);

            bs.Marshal(ds);
            ds.Write(endOfStreamMarker);

            // now lets read from the stream

            MemoryStream ins = new MemoryStream(buffer.ToArray());
            BinaryReader dis = new OpenWireBinaryReader(ins);

            bs = new BooleanStream();
            bs.Unmarshal(dis);

            for (int i = 0; i < count; i++)
            {
                bool expected = valueDelegate(i, count);

                try
                {
                    bool actual = bs.ReadBoolean();
                    Assert.AreEqual(expected, actual);
                }
                catch (Exception e)
                {
                    Assert.Fail(
                        "Failed to parse bool: " + i + " out of: " + count + " due to: " + e);
                }
            }
            int marker = dis.ReadInt32();

            Assert.AreEqual(
                endOfStreamMarker, marker, "did not match: " + endOfStreamMarker + " and " + marker);

            // lets try read and we should get an exception
            try
            {
                dis.ReadByte();
                Assert.Fail("Should have reached the end of the stream");
            }
            catch (IOException)
            {
            }
        }
        public void testWriteString32()
        {
            // Test data with 1-byte UTF8 encoding.
            {
                char[] input  = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C', '\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064' };
                byte[] expect = { 0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };

                writeString32TestHelper(input, expect);
            }

            // Test data with 2-byte UT8 encoding.
            {
                char[] input  = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' };
                byte[] expect = { 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC2, 0xA6 };

                writeString32TestHelper(input, expect);
            }

            // Test data with 1-byte and 2-byte encoding with embedded NULL's.
            {
                char[] input  = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000', '\u00A6' };
                byte[] expect = { 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC0, 0x80, 0xC2, 0xA6 };

                writeString32TestHelper(input, expect);
            }

            // test that a null strings writes a -1
            {
                MemoryStream         stream = new MemoryStream();
                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
                writer.WriteString32(null);

                stream.Seek(0, SeekOrigin.Begin);
                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
                Assert.AreEqual(-1, reader.ReadInt32());
            }
        }
        public void testWriteString16()
        {
            // Test data with 1-byte UTF8 encoding.
            {
                char[] input  = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C', '\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064' };
                byte[] expect = { 0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };

                writeString16TestHelper(input, expect);
            }

            // Test data with 2-byte UT8 encoding.
            {
                char[] input  = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' };
                byte[] expect = { 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC2, 0xA6 };

                writeString16TestHelper(input, expect);
            }

            // Test data with 1-byte and 2-byte encoding with embedded NULL's.
            {
                char[] input  = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000', '\u00A6' };
                byte[] expect = { 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC0, 0x80, 0xC2, 0xA6 };

                writeString16TestHelper(input, expect);
            }

            // test that a null string writes no output.
            {
                MemoryStream         stream = new MemoryStream();
                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
                writer.WriteString16(null);
                Assert.AreEqual(0, stream.Length);
            }

            // test that a null string writes no output.
            {
                MemoryStream         stream = new MemoryStream();
                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
                writer.WriteString16("");

                stream.Seek(0, SeekOrigin.Begin);
                OpenWireBinaryReader reader = new OpenWireBinaryReader(stream);
                Assert.AreEqual(0, reader.ReadInt16());
            }

            // String of length 65536 of Null Characters.
            {
                MemoryStream         stream = new MemoryStream();
                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
                String testStr = new String('a', 65536);
                try{
                    writer.Write(testStr);
                    Assert.Fail("Should throw an Exception");
                }
                catch (Exception)
                {
                }
            }

            // String of length 65535 of non Null Characters since Null encodes as UTF-8.
            {
                MemoryStream         stream = new MemoryStream();
                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
                String testStr = new String('a', 65535);
                try{
                    writer.Write(testStr);
                }
                catch (Exception)
                {
                    Assert.Fail("Should not throw an Exception");
                }
            }

            // Set one of the 65535 bytes to a value that will result in a 2 byte UTF8 encoded sequence.
            // This will cause the string of length 65535 to have a utf length of 65536.
            {
                MemoryStream         stream = new MemoryStream();
                OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream);
                String testStr = new String('a', 65535);
                char[] array   = testStr.ToCharArray();
                array[0] = '\u0000';
                testStr  = new String(array);

                try{
                    writer.Write(testStr);
                    Assert.Fail("Should throw an Exception");
                }
                catch (Exception)
                {
                }
            }
        }