public void BasicTest() { string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" + " <DataModel name=\"TheDataModel\">"+ " <Blob name=\"TheBlob\">" + " <Analyzer class=\"Binary\"> " + " <Param name=\"AnalyzeStrings\" value=\"false\"/> " + " </Analyzer> " + " </Blob>" + " </DataModel>"+ "</Peach>"; PitParser parser = new PitParser(); Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml))); Random rnd = new Random(123); BitStream bs = new BitStream(); BitWriter data = new BitWriter(bs); data.LittleEndian(); for (int cnt = 0; cnt < 100; cnt++) { data.WriteInt32(rnd.NextInt32()); } data.WriteString("Hello World"); for (int cnt = 0; cnt < 100; cnt++) { data.WriteInt32(rnd.NextInt32()); } data.WriteString("Peach Fuzzer"); for (int cnt = 0; cnt < 100; cnt++) { data.WriteInt32(rnd.NextInt32()); } bs.SeekBits(0, SeekOrigin.Begin); DataCracker cracker = new DataCracker(); cracker.CrackData(dom.dataModels[0], bs); bs.Seek(0, SeekOrigin.Begin); Assert.IsTrue(dom.dataModels["TheDataModel"][0] is Block); Assert.AreEqual("TheBlob", dom.dataModels["TheDataModel"][0].name); Assert.AreEqual(bs.ToArray(), dom.dataModels["TheDataModel"].Value.ToArray()); var block = dom.dataModels["TheDataModel"][0] as Block; Assert.IsTrue(block[5] is Dom.String); Assert.AreEqual("Hello WorldYY&", (string)block[5].InternalValue); Assert.IsTrue(block[11] is Dom.String); Assert.AreEqual("Peach Fuzzer|", (string)block[11].InternalValue); }
public void ReadWriteNumbersOddOffset() { BitStream bs = new BitStream(); BitWriter w = new BitWriter(bs); BitReader r = new BitReader(bs); w.LittleEndian(); r.LittleEndian(); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); Assert.AreEqual(3, bs.LengthBits); Assert.AreEqual(3, bs.PositionBits); //Max w.WriteSByte(sbyte.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(0, r.ReadBit()); Assert.AreEqual(1, r.ReadBit()); Assert.AreEqual(1, r.ReadBit()); Assert.AreEqual(1, r.ReadBit()); Assert.AreEqual(1, r.ReadBit()); Assert.AreEqual(1, r.ReadBit()); Assert.AreEqual(1, r.ReadBit()); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MaxValue, r.ReadSByte()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt16(short.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MaxValue, r.ReadInt16()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt32(67305985); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(67305985, r.ReadInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt32(Int32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MaxValue, r.ReadInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt64(Int64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MaxValue, r.ReadInt64()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteByte(byte.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(byte.MaxValue, r.ReadByte()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteUInt16(ushort.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(ushort.MaxValue, r.ReadUInt16()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteUInt32(UInt32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt32.MaxValue, r.ReadUInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteUInt64(UInt64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt64.MaxValue, r.ReadUInt64()); //Min bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteSByte(sbyte.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MinValue, r.ReadSByte()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt16(short.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MinValue, r.ReadInt16()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt32(Int32.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MinValue, r.ReadInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt64(Int64.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MinValue, r.ReadInt64()); // BIG ENDIAN ////////////////////////////////////////// bs.SetLength(0); r.BigEndian(); w.BigEndian(); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); //Max w.WriteSByte(sbyte.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MaxValue, r.ReadSByte()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt16(short.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MaxValue, r.ReadInt16()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt32(67305985); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(67305985, r.ReadInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt32(Int32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MaxValue, r.ReadInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt64(Int64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MaxValue, r.ReadInt64()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteByte(byte.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(byte.MaxValue, r.ReadByte()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteUInt16(ushort.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(ushort.MaxValue, r.ReadUInt16()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteUInt32(UInt32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt32.MaxValue, r.ReadUInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteUInt64(UInt64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt64.MaxValue, r.ReadUInt64()); //Min bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteSByte(sbyte.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MinValue, r.ReadSByte()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt16(short.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MinValue, r.ReadInt16()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt32(Int32.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MinValue, r.ReadInt32()); bs.SetLength(0); w.WriteBit(1); w.WriteBit(1); w.WriteBit(1); w.WriteInt64(Int64.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MinValue, r.ReadInt64()); }