Skip to content

Commit

Permalink
Restore some code only used for test automation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenegriffin committed Dec 6, 2023
1 parent dd1e236 commit 3989c96
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 3 deletions.
111 changes: 111 additions & 0 deletions MAPIInspector/Source/MAPIInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Be.Windows.Forms;
using Fiddler;
using global::MAPIInspector.Parsers;
using static MapiInspector.MAPIParser;

/// <summary>
/// MAPIInspector Class
/// </summary>
public abstract class MAPIInspector : Inspector2
{
/// <summary>
/// The JsonResult is used to save the Json string which converted by parse result
/// </summary>
public static string JsonResult = string.Empty;

/// <summary>
/// The JsonFile is used to set a file name to save JsonResult
/// </summary>
public static string JsonFile = "Json.txt";

/// <summary>
/// The JsonFile is used to set a file name to save error messages when automation test
/// </summary>
public static string ErrorFile = "Error.txt";

/// <summary>
/// Gets or sets the Tree View control where displayed the MAPI message.
/// </summary>
Expand Down Expand Up @@ -383,5 +401,98 @@ private void UpdateView()
return;
}
}

/// <summary>
/// Parse the sessions from capture file using the MAPI Inspector
/// Used in test automation
/// </summary>
/// <param name="sessionsFromCore">The sessions which from FiddlerCore to parse</param>
/// <param name="pathName">The path for save result file</param>
/// <param name="autoCaseName">The test case name to parse</param>
/// <param name="allRops">All ROPs contained in list</param>
/// <returns>Parse result, true means success</returns>
public bool ParseCaptureFile(Fiddler.Session[] sessionsFromCore, string pathName, string autoCaseName, out List<string> allRops)
{
var errorStringList = new List<string>();
StringBuilder stringBuilder = new StringBuilder();
AllSessions = sessionsFromCore;
DecodingContext decodingContext = new DecodingContext();
ResetPartialParameters();
ResetPartialContextInformation();
ResetHandleInformation();
for (int i = 0; i < AllSessions.Length; i++)
{
var session = AllSessions[i];
Session val = AllSessions[i];
if (AllSessions[i]["VirtualID"] != null)
{
MAPIParser.ParsingSession = val;
}
if (AllSessions.Length > 0 && AllSessions[AllSessions.Length - 1]["Number"] == null)
{
SetIndexForContextRelatedMethods();
}
if (IsMapihttpWithoutUI())
{
try
{
MAPIParser.IsLooperCall = false;
ResetPartialParameters();
BaseHeaders = val.RequestHeaders;
byte[] bytes;
object obj = ParseHTTPPayload(BaseHeaders, val, val.requestBodyBytes, TrafficDirection.In, out bytes);
JsonResult += Utilities.ConvertCSharpToJson(i, isRequest: true, obj);
if (val["X-ResponseCode"] == "0")
{
object obj2 = ParseHTTPPayload(BaseHeaders, val, val.responseBodyBytes, TrafficDirection.Out, out bytes);
JsonResult += Utilities.ConvertCSharpToJson(i, isRequest: false, obj2);
}
}
catch (Exception ex)
{
errorStringList.Add(string.Format("{0}. Error: Frame#{1} Error Message:{2}", errorStringList.Count + 1, val["VirtualID"], ex.Message));
}
finally
{
DecodingContext.Notify_handlePropertyTags = new Dictionary<uint, Dictionary<int, Tuple<string, string, string, PropertyTag[], string>>>();
DecodingContext.RowRops_handlePropertyTags = new Dictionary<uint, Dictionary<int, Tuple<string, string, string, PropertyTag[]>>>();
TargetHandle = new Stack<Dictionary<ushort, Dictionary<int, uint>>>();
ContextInformationCollection = new List<ContextInformation>();
MAPIParser.IsLooperCall = true;
}
}
if (i % 10 == 0 && JsonResult.Length != 0)
{
string path = pathName + Path.DirectorySeparatorChar.ToString() + autoCaseName + "-" + JsonFile;
if (!File.Exists(path))
{
using (StreamWriter streamWriter = File.CreateText(path))
{
streamWriter.WriteLine(JsonResult);
}
}
else
{
using (StreamWriter streamWriter2 = File.AppendText(path))
{
streamWriter2.WriteLine(JsonResult);
}
}
JsonResult = string.Empty;
}
}
allRops = AllRopsList;
foreach (string errorString in errorStringList)
{
stringBuilder.AppendLine(errorString);
}
if (stringBuilder.Length != 0)
{
string path2 = pathName + Path.DirectorySeparatorChar.ToString() + autoCaseName + "-" + ErrorFile;
File.WriteAllText(path2, stringBuilder.ToString());
return false;
}
return true;
}
}
}
74 changes: 71 additions & 3 deletions MAPIInspector/Source/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,78 @@ public static byte[] GetPaylodFromChunkedBody(byte[] responseBodyFromFiddler)
}

/// <summary>
/// Converts a simple (non-flag) enum to string. If the value is not present in the underlying enum, converts to a hex string.
/// The SealTheObject Class is used to sealing the parse result.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public class SealTheObject
{
private string title;

private string message;

private object obj;

public string Title
{
get
{
return title;
}
set
{
title = value;
}
}

public string Message
{
get
{
return message;
}
set
{
message = value;
}
}

public object Obj
{
get
{
return obj;
}
set
{
obj = value;
}
}

public SealTheObject(int id, bool isRequest, object obj)
{
title = "Frame" + id + (isRequest ? "Request" : "Response");
message = obj.GetType().Name;
Obj = obj;
}
}
/// <summary>
/// Method to seal parse result to Json string
/// Used in test automation
/// </summary>
/// <param name="id">The id of Fiddler session</param>
/// <param name="isRequest">Bool value indicates the session is a HttpRequest message or HttpResponse message</param>
/// <param name="obj">The object of parse result</param>
/// <returns>Json string converted by parse result</returns>
public static string ConvertCSharpToJson(int id, bool isRequest, object obj)
{
SealTheObject sealTheObject = new SealTheObject(id, isRequest, obj);
return JsonConvert.SerializeObject((object)sealTheObject);
}

/// <summary>
/// Converts a simple (non-flag) enum to string. If the value is not present in the underlying enum, converts to a hex string.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string EnumToString(object obj)
{
if (Enum.IsDefined(obj.GetType(), obj))
Expand Down

0 comments on commit 3989c96

Please sign in to comment.