public void SerializeManualFileTest()
        {
            var a = new ManualFuzzingInput(new byte[] { 0x01 });

            // Deserialize

            var file = Path.GetTempFileName();

            File.WriteAllBytes(file, a.Data);
            var b = SerializationHelper.DeserializeFromFile <FuzzingInputBase>(file).FirstOrDefault();

            File.Delete(file);

            a.Id          = b.Id;
            a.Description = b.Description;

            Assert.AreEqual(a, b);
        }
Exemple #2
0
        public void ManualFuzzingInput()
        {
            var value = new ManualFuzzingInput(new byte[] { 0x00 })
            {
                Description = "Test",
                Id          = Guid.NewGuid()
            };

            var ret = value.GetStream();

            CollectionAssert.AreEqual(new byte[] { 0x00 }, ret);

            // Regular constructor

            value = new ManualFuzzingInput(new byte[0]);
            ret   = value.GetStream();

            Assert.AreEqual(0, ret.Length);

            // Serialize

            var json  = SerializationHelper.SerializeToJson(value, true);
            var copy  = SerializationHelper.DeserializeFromJson <ManualFuzzingInput>(json);
            var copy2 = SerializationHelper.DeserializeFromJson <FuzzingInputBase>(json);

            Assert.IsTrue(copy.Equals(copy2));

            // Equals

            Assert.IsTrue(value.Equals(copy));
            Assert.IsTrue(value.Equals((object)copy));
            Assert.IsFalse(value.Equals(new object()));
            Assert.IsFalse(value.Equals((FuzzingInputBase) new FileFuzzingInput()));
            Assert.AreEqual(value.GetHashCode(), copy.GetHashCode());

            value.Id = Guid.NewGuid();
            Assert.AreNotEqual(value.GetHashCode(), copy.GetHashCode());
        }
        public void MutationalEntryPeerStreamTest()
        {
            var config = new MutationConfig()
            {
                Description = "Test"
            };
            var entry = new MutationalEntry()
            {
                FuzzPercent     = new FromToValue <double>(100),
                ValidOffset     = new FromToValue <long>(0, long.MaxValue),
                MaxChanges      = new FromToValue <ushort>(50),
                FuzzPercentType = EFuzzingPercentType.PeerStream
            };

            // Config

            config.Mutations.Add(entry);
            entry.Changes.Add(new MutationalChange()
            {
                Weight           = 1,
                Append           = new MutationalFromTo(0x01),
                RemoveLength     = new FromToValue <ushort>(),
                AppendIterations = new FromToValue <ushort>(1)
            });

            // 100% / 50 changes

            var input = new ManualFuzzingInput(new byte[200]);

            using (var copy = new MemoryStream())
                using (var stream = new FuzzingStream(config, input.GetStream()))
                {
                    stream.CopyTo(copy, 200);
                    Assert.AreEqual(50, copy.ToArray().Count(u => u == 0x01));
                }

            // 0%

            entry.FuzzPercent = new FromToValue <double>(0);

            input = new ManualFuzzingInput(new byte[200]);
            using (var copy = new MemoryStream())
                using (var stream = new FuzzingStream(config, input.GetStream()))
                {
                    stream.CopyTo(copy, 200);
                    Assert.AreEqual(200, copy.ToArray().Count(u => u == 0x00));
                }

            // Only offset 5

            entry.FuzzPercent = new FromToValue <double>(100);
            entry.ValidOffset = new FromToValue <long>(5);
            entry.MaxChanges  = new FromToValue <ushort>(1);

            input = new ManualFuzzingInput(new byte[200]);
            using (var stream = new FuzzingStream(config, input.GetStream()))
            {
                for (long x = 0; x < 200; x++)
                {
                    var next = entry.Get(stream, x, 0);

                    if (x == 5)
                    {
                        Assert.IsNotNull(next);
                    }
                    else
                    {
                        Assert.IsNull(next);
                    }
                }
            }

            // Max changes 2

            entry.ValidOffset = new FromToValue <long>(0, long.MaxValue);
            entry.MaxChanges  = new FromToValue <ushort>(2);

            input = new ManualFuzzingInput(new byte[200]);
            using (var stream = new FuzzingStream(config, input.GetStream()))
            {
                stream.CopyTo(new MemoryStream(), 200);

                Assert.AreEqual(2, stream.Log.Length);
            }
        }
        public void MutationalEntryPeerByteTest()
        {
            var config = new MutationConfig()
            {
                Description = "Test"
            };
            var entry = new MutationalEntry()
            {
                FuzzPercent     = new FromToValue <double>(100),
                ValidOffset     = new FromToValue <long>(0, long.MaxValue),
                MaxChanges      = new FromToValue <ushort>(ushort.MaxValue),
                FuzzPercentType = EFuzzingPercentType.PeerByte
            };

            // Config

            config.Mutations.Add(entry);
            entry.Changes.Add(new MutationalChange()
            {
                Weight           = 5,
                Description      = "Add A",
                Append           = new MutationalFromTo((byte)'A'),
                RemoveLength     = new FromToValue <ushort>(1),
                AppendIterations = new FromToValue <ushort>(1)
            });
            entry.Changes.Add(new MutationalChange()
            {
                // Remmove
                Weight           = 1,
                Description      = "Remove",
                RemoveLength     = new FromToValue <ushort>(1),
                AppendIterations = new FromToValue <ushort>(1)
            });

            // 100%

            var input = new ManualFuzzingInput(new byte[200]);

            using (var stream = new FuzzingStream(config, input.GetStream()))
            {
                for (long x = 0; x < 200; x++)
                {
                    Assert.IsNotNull(entry.Get(stream, x, 0));
                }
            }

            // 0%

            entry.FuzzPercent = new FromToValue <double>(0);

            input = new ManualFuzzingInput(new byte[200]);
            using (var stream = new FuzzingStream(config, input.GetStream()))
            {
                for (long x = 0; x < 200; x++)
                {
                    Assert.IsNull(entry.Get(stream, x, 0));
                }
            }

            // Argument excepcion

            entry.FuzzPercentType = (EFuzzingPercentType)197;
            Assert.Throws <ArgumentException>(() => entry.Get(null, 0, 0));

            // Only offset 5

            entry.FuzzPercentType = EFuzzingPercentType.PeerByte;
            entry.FuzzPercent     = new FromToValue <double>(100);
            entry.ValidOffset     = new FromToValue <long>(5);

            input = new ManualFuzzingInput(new byte[100]);
            using (var stream = new FuzzingStream(config, input.GetStream()))
            {
                for (long x = 0; x < 100; x++)
                {
                    var next = entry.Get(stream, x, 0);

                    if (x == 5)
                    {
                        Assert.IsNotNull(next);
                    }
                    else
                    {
                        Assert.IsNull(next);
                    }
                }
            }

            // Max changes 2

            entry.Changes.RemoveAt(1);
            entry.ValidOffset = new FromToValue <long>(0, long.MaxValue);
            entry.MaxChanges  = new FromToValue <ushort>(2);
            input             = new ManualFuzzingInput(new byte[100]);

            using (var stream = new FuzzingStream(config, input.GetStream()))
            {
                stream.CopyTo(new MemoryStream(), 16);

                Assert.AreEqual(2, stream.Log.Length);
                Assert.AreEqual(0, stream.Log[0].Offset);
                Assert.AreEqual(1, stream.Log[1].Offset);
            }
        }