public void LinesDiff() { string text1 = "LineLine1.1\r\n" + "eniLLine1.3\r\n" + "ABC1.4"; string text2 = "LineLine2.1\r\n" + "XYZ2.4\r\n" + "eniLLine2.3"; LinesDiffer differ = new LinesDiffer(); DiffResult result = differ.DoDiff(text1, text2); IDiffObjectsCollection firstDiffs = result.DifferencesForFirst; IDiffObjectsCollection secondDiffs = result.DifferencesForSecond; firstDiffs.SortByPosition(); secondDiffs.SortByPosition(); Assert.IsTrue(firstDiffs[0].ValueEquals(new LetterDiffObject(0, 0, "1"))); Assert.IsTrue(secondDiffs[0].ValueEquals(new LetterDiffObject(0, 0, "2"))); Assert.IsTrue(firstDiffs[1].ValueEquals(new LetterDiffObject(0, 0, "1"))); Assert.IsTrue(secondDiffs[1].ValueEquals(new LineDiffObject(0, 0, "XYZ2.4"))); Assert.IsTrue(firstDiffs[2].ValueEquals(new LineDiffObject(0, 0, "ABC1.4"))); Assert.IsTrue(secondDiffs[2].ValueEquals(new LetterDiffObject(0, 0, "2"))); }
/// <summary> /// Diffs the response bodies /// </summary> /// <param name="result"></param> /// <param name="firstResponse"></param> /// <param name="secondResponse"></param> /// <param name="firstRespBasePos"></param> /// <param name="secondRespBasePos"></param> /// <returns></returns> private RequestsDifferResult DiffResponseBody(RequestsDifferResult result, string firstResponse, string secondResponse, int firstRespBasePos, int secondRespBasePos) { int firstResponseBodyStart = FindResponseBodyStart(firstResponse); int secondResponseBodyStart = FindResponseBodyStart(secondResponse); string firstResponseBody = firstResponse.Substring(firstResponseBodyStart); string secondResponseBody = secondResponse.Substring(secondResponseBodyStart); LinesDiffer linesDiffer = new LinesDiffer(); //this differ first compares the lines then the words then the letters linesDiffer.Properties.Sorted = false; linesDiffer.AddTask(firstResponseBody, firstRespBasePos + firstResponseBodyStart); linesDiffer.AddTask(secondResponseBody, secondRespBasePos + secondResponseBodyStart); result.BodyDiffSimilarity = linesDiffer.DoDiff(); //calculate aproximate similarity as generated by appscan algorithm result.BodyAproximateSimilarity = ASESimilarityAlgorithm.CalculateSimilarity(firstResponseBody, secondResponseBody); result = AppendDiffs(result, linesDiffer); return(result); }
/// <summary> /// Executes a diff operation /// </summary> /// <param name="firstRequestId"></param> /// <param name="secondRequestId"></param> /// <param name="source"></param> /// <returns></returns> public RequestsDifferResult Diff(int firstRequestId, int secondRequestId, ITrafficDataAccessor source) { RequestsDifferResult result = null; if (source.ContainsId(firstRequestId) && source.ContainsId(secondRequestId)) { //first retrieve the requests byte [] bytes1 = source.LoadRequestData(firstRequestId); byte [] bytes2 = source.LoadRequestData(secondRequestId); string firstRequest = String.Empty; string secondRequest = String.Empty; if (bytes1 != null) { firstRequest = GetString(bytes1); } if (bytes2 != null) { secondRequest = GetString(bytes2); } result = new RequestsDifferResult(); try //try to diff the request components { //diff the request line result = DiffFirstLine(result, firstRequest, secondRequest, 0, 0); //diff the request headers result = DiffHeaders(result, firstRequest, secondRequest, 0, 0); //next compare POST data if available result = DiffPostData(result, firstRequest, secondRequest); } catch (DiffException) //if the request are malformed use a regular letters differ { LettersDiffer lettersDiffer = new LettersDiffer(); DiffResult lettersResult = lettersDiffer.DoDiff(firstRequest, secondRequest); result.DiffsForFirst = lettersResult.DifferencesForFirst; result.DiffsForSecond = lettersResult.DifferencesForSecond; } //done with the requests calculate the base position for the response result.FirstText = firstRequest + "\n\n"; result.SecondText = secondRequest + "\n\n"; int firstRespBasePos = result.FirstText.Length; int secondRespBasePos = result.SecondText.Length; //load the responses bytes1 = source.LoadResponseData(firstRequestId); bytes2 = source.LoadResponseData(secondRequestId); string firstResponse = String.Empty; string secondResponse = String.Empty; if (bytes1 != null) { firstResponse = GetString(bytes1); } if (bytes2 != null) { secondResponse = GetString(bytes2); } try { //diff the status line result = DiffFirstLine(result, firstResponse, secondResponse, firstRespBasePos, secondRespBasePos); //diff the request headers result = DiffHeaders(result, firstResponse, secondResponse, firstRespBasePos, secondRespBasePos); //diff the response body result = DiffResponseBody(result, firstResponse, secondResponse, firstRespBasePos, secondRespBasePos); } catch (DiffException) { //if the responses are missing the headers use a regular lines differ LinesDiffer linesDiffer = new LinesDiffer(); linesDiffer.AddTask(firstResponse, firstRespBasePos); linesDiffer.AddTask(secondResponse, secondRespBasePos); linesDiffer.DoDiff(); result.DiffsForFirst = linesDiffer.GetResultingDifferences(0); result.DiffsForSecond = linesDiffer.GetResultingDifferences(1); } //append the responses to the result text result.FirstText += firstResponse; result.SecondText += secondResponse; //sort and merge the resulting differences result.DiffsForFirst.MergeAll(); result.DiffsForSecond.MergeAll(); } return(result); }