Example #1
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 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)
                {
                }
            }
        }