Skip to content

Commit

Permalink
Merge pull request #7 from Red4Sec/increase-coverage-json
Browse files Browse the repository at this point in the history
Increase coverage
  • Loading branch information
shargon authored Oct 16, 2019
2 parents aa0b2d5 + 3827ab4 commit cba6b9f
Show file tree
Hide file tree
Showing 23 changed files with 469 additions and 56 deletions.
78 changes: 78 additions & 0 deletions src/TuringMachine.Core/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using TuringMachine.Core.Helpers;

Expand All @@ -25,6 +26,83 @@ public static bool EqualWithNullCheck<T>(this IEquatable<T> a, IEquatable<T> b)
return a == null || a.Equals(b);
}

/// <summary>
/// Return true if are equals, with null check
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="a">A</param>
/// <param name="b">B</param>
/// <returns>True if are equals</returns>
public static bool SequenceEqualWithNullCheck<T>(this IList<T> a, IList<T> b)
{
if ((a == null) != (b == null))
{
return false;
}

return a == null || a.SequenceEqual(b);
}

/// <summary>
/// Return true if are equals, with null check
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="a">A</param>
/// <param name="b">B</param>
/// <returns>True if are equals</returns>
public static bool ChunkSequenceEqualWithNullCheck(this IList<byte[]> chunks1, IList<byte[]> chunks2)
{
if ((chunks1 == null) != (chunks2 == null)) return false;
if (chunks1 == null) return true;

if (chunks1.Count != chunks2.Count)
{
return false;
}

for (int x = chunks1.Count - 1; x >= 0; x--)
{
if (!chunks1[x].SequenceEqual(chunks2[x]))
{
return false;
}
}

return true;
}

/// <summary>
/// Return true if are equals, with null check
/// </summary>
/// <param name="a">A</param>
/// <param name="b">B</param>
/// <returns>True if are equals</returns>
public static bool SequenceEqualWithNullCheck(this byte[] a, byte[] b)
{
if ((a == null) != (b == null))
{
return false;
}

return a == null || a.SequenceEqual(b);
}

/// <summary>
/// Return true if are equals, with null check
/// </summary>
/// <param name="a">A</param>
/// <param name="b">B</param>
/// <returns>True if are equals</returns>
public static bool EqualWithNullCheck(this IPEndPoint a, IPEndPoint b)
{
if ((a == null) != (b == null))
{
return false;
}

return a == null || a.Equals(b);
}

/// <summary>
/// GetHashCode checking if is null
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public bool Equals(MixCaseFilter obj)

return obj.Weight == Weight
&& obj.MixType == MixType
&& obj.FilterPercent.Equals(FilterPercent);
&& obj.FilterPercent.EqualWithNullCheck(FilterPercent);
}

/// <summary>
Expand Down
13 changes: 11 additions & 2 deletions src/TuringMachine.Core/Fuzzers/Mutational/MutationConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using TuringMachine.Core.Extensions;
using TuringMachine.Core.Fuzzers.Patch;
using TuringMachine.Core.Interfaces;
Expand Down Expand Up @@ -79,12 +78,22 @@ public override bool Equals(object obj)
return Equals(o);
}

public override bool Equals(FuzzingConfigBase obj)
{
if (!(obj is MutationConfig o))
{
return false;
}

return Equals(o);
}

public bool Equals(MutationConfig obj)
{
if (obj == null) return false;

return base.Equals(obj)
&& obj.Mutations.SequenceEqual(obj.Mutations);
&& obj.Mutations.SequenceEqualWithNullCheck(obj.Mutations);
}

/// <summary>
Expand Down
23 changes: 1 addition & 22 deletions src/TuringMachine.Core/Fuzzers/Mutational/MutationalChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,7 @@ public bool Equals(MutationalChunk obj)
if (obj == null) return false;

return obj.Type == Type
&& SequenceEqual(obj.Allowed, Allowed);
}

private bool SequenceEqual(IList<byte[]> chunks1, IList<byte[]> chunks2)
{
if ((chunks1 == null) != (chunks2 == null)) return false;
if (chunks1 == null) return true;

if (chunks1.Count != chunks2.Count)
{
return false;
}

for (int x = chunks1.Count - 1; x >= 0; x--)
{
if (!chunks1[x].SequenceEqual(chunks2[x]))
{
return false;
}
}

return true;
&& obj.Allowed.ChunkSequenceEqualWithNullCheck(Allowed);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public bool Equals(MutationalEntry obj)
&& obj.ValidOffset.EqualWithNullCheck(ValidOffset)
&& obj.FuzzPercent.EqualWithNullCheck(FuzzPercent)
&& obj.MaxChanges.EqualWithNullCheck(MaxChanges)
&& obj.Changes.EqualWithNullCheck(obj.Changes);
&& obj.Changes.EqualWithNullCheck(Changes);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/TuringMachine.Core/Fuzzers/Patch/PatchChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public bool Equals(PatchChange obj)
if (obj == null) return false;

return obj.Description == Description
&& obj.Append.SequenceEqual(Append)
&& obj.Offset == Offset
&& obj.Remove == Remove;
&& obj.Remove == Remove
&& obj.Append.SequenceEqualWithNullCheck(Append);
}

/// <summary>
Expand Down
25 changes: 19 additions & 6 deletions src/TuringMachine.Core/Fuzzers/Patch/PatchConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using TuringMachine.Core.Extensions;
using TuringMachine.Core.Interfaces;

Expand All @@ -13,7 +12,7 @@ public class PatchConfig : FuzzingConfigBase, IEquatable<PatchConfig>
/// Mutations
/// </summary>
[Category("1 - Collection")]
public List<PatchChange> Changes { get; set; }
public IList<PatchChange> Changes { get; set; }

/// <summary>
/// Constructor
Expand All @@ -28,11 +27,15 @@ public PatchConfig() : base("Patch")
/// Constructor
/// </summary>
/// <param name="description">Description</param>
/// <param name="changes">Changes</param>
public PatchConfig(string description, params PatchChange[] changes) : this()
/// <param name="entries">Changes</param>
public PatchConfig(string description, params PatchChange[] entries) : this()
{
Description = description;
Changes.AddRange(changes);

foreach (var entry in entries)
{
Changes.Add(entry);
}
}

/// <summary>
Expand Down Expand Up @@ -66,12 +69,22 @@ public override bool Equals(object obj)
return Equals(o);
}

public override bool Equals(FuzzingConfigBase obj)
{
if (!(obj is PatchConfig o))
{
return false;
}

return Equals(o);
}

public bool Equals(PatchConfig obj)
{
if (obj == null) return false;

return base.Equals(obj)
&& obj.Changes.SequenceEqual(obj.Changes);
&& obj.Changes.SequenceEqualWithNullCheck(obj.Changes);
}

/// <summary>
Expand Down
23 changes: 20 additions & 3 deletions src/TuringMachine.Core/Inputs/ExecutionFuzzingInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public bool Equals(ExecutionFuzzingInput obj)
{
if (obj == null) return false;

return Equals((FuzzingInputBase)obj)
&& FileName.Equals(obj.FileName)
&& Arguments.Equals(obj.Arguments);
return base.Equals(obj)
&& obj.ExitTimeOut.EqualWithNullCheck(ExitTimeOut)
&& obj.FileName.EqualWithNullCheck(FileName)
&& obj.Arguments.EqualWithNullCheck(Arguments);
}

/// <summary>
Expand All @@ -92,6 +93,21 @@ public override bool Equals(object obj)
return false;
}

/// <summary>
/// Equals
/// </summary>
/// <param name="obj">Object</param>
/// <returns>Return true if are equals</returns>
public override bool Equals(FuzzingInputBase obj)
{
if (obj is ExecutionFuzzingInput o)
{
return Equals(o);
}

return false;
}

/// <summary>
/// GetHashCode
/// </summary>
Expand All @@ -102,6 +118,7 @@ public override int GetHashCode()
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + FileName.GetHashCodeWithNullCheck();
hashCode = hashCode * -1521134295 + Arguments.GetHashCodeWithNullCheck();
hashCode = hashCode * -1521134295 + ExitTimeOut.GetHashCodeWithNullCheck();
return hashCode;
}
}
Expand Down
21 changes: 18 additions & 3 deletions src/TuringMachine.Core/Inputs/FileFuzzingInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public bool Equals(FileFuzzingInput obj)
{
if (obj == null) return false;

return Equals((FuzzingInputBase)obj)
&& FileName == obj.FileName
&& UseCache == obj.UseCache;
return base.Equals(obj)
&& obj.FileName.EqualWithNullCheck(FileName)
&& obj.UseCache.EqualWithNullCheck(UseCache);
}

/// <summary>
Expand All @@ -84,6 +84,21 @@ public override bool Equals(object obj)
return false;
}

/// <summary>
/// Equals
/// </summary>
/// <param name="obj">Object</param>
/// <returns>Return true if are equals</returns>
public override bool Equals(FuzzingInputBase obj)
{
if (obj is FileFuzzingInput o)
{
return Equals(o);
}

return false;
}

/// <summary>
/// GetHashCode
/// </summary>
Expand Down
19 changes: 17 additions & 2 deletions src/TuringMachine.Core/Inputs/ManualFuzzingInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public bool Equals(ManualFuzzingInput obj)
{
if (obj == null) return false;

return Equals((FuzzingInputBase)obj)
&& ((Data == null && obj.Data == null) || Data.SequenceEqual(obj.Data));
return base.Equals(obj)
&& obj.Data.SequenceEqualWithNullCheck(Data);
}

/// <summary>
Expand All @@ -60,6 +60,21 @@ public override bool Equals(object obj)
return false;
}

/// <summary>
/// Equals
/// </summary>
/// <param name="obj">Object</param>
/// <returns>Return true if are equals</returns>
public override bool Equals(FuzzingInputBase obj)
{
if (obj is ManualFuzzingInput o)
{
return Equals(o);
}

return false;
}

/// <summary>
/// GetHashCode
/// </summary>
Expand Down
Loading

0 comments on commit cba6b9f

Please sign in to comment.