Exemple #1
0
        private static string MinimiseJsonAndRunInit(string json_string, InputArgs inInputArgs, bool isErrOk, bool showInfo)
        {
            string myApp = inInputArgs.CmdFileName;

            JArray jsonJArrayObj = JArray.Parse(json_string);

            if (!BinaryFormatterDeserializeABFJson(json_string, showInfo))
            {
                isErrOk = true;
            }

            if (KillMyProcess(myApp))
            {
                // rules:
                // remove a Data object
                // nullify a Data object
                // replace a string with null
                // replace a non empty string with an empty string ('')
                // replace a non empty string greater than one character with one character ('x')
                // replace space in string if it contains a space
                // replace a full class or assembly string to only keep class - then class and assembly
                // replace an integer with 0 to N - when int is M and N < M && N < 20 (we need a limit)

                StringBuilder sbSuccessResult = new StringBuilder();

                List <String> valueExclusionList = new List <string> {
                    inInputArgs.CmdFullString, inInputArgs.CmdFileName, inInputArgs.CmdArguments, inInputArgs.CmdFromFile
                };
                List <String> typeExclusionList = new List <string> {
                    "SerializationHeaderRecord", "MessageEnd"
                };
                List <String> nameExclusionList = new List <string> {
                    "$type", "objectId"
                };

                // remove a Data object
                sbSuccessResult.Append(DataObjectRemovalTester(ref jsonJArrayObj, myApp, isErrOk, showInfo));
                if (showInfo)
                {
                    Console.WriteLine(sbSuccessResult);
                }
                sbSuccessResult.Clear();

                // nullify a Data object
                sbSuccessResult.Append(DataObjectNullifyTester(ref jsonJArrayObj, myApp, isErrOk, valueExclusionList, showInfo));
                if (showInfo)
                {
                    Console.WriteLine(sbSuccessResult);
                }
                sbSuccessResult.Clear();

                // replace a string with null
                // replace a non empty string with an empty string ('')
                // replace a non empty string greater than one character with one character ('x')
                // replace space in string if it contains a space
                // replace a full class or assembly string to only keep class - then class and assembly
                // replace an integer with 0 to N - when int is M and N < M && N < 20 (we need a limit)

                JArray origJsonJArrayObj = new JArray(jsonJArrayObj.ToList().ToArray());
                bool   ruleComplete      = false;
                while (!ruleComplete)
                {
                    int internalCounter = 0;

                    foreach (JObject item in jsonJArrayObj)
                    {
                        internalCounter++;
                        JToken dataItem = item["Data"];
                        foreach (JProperty subDataItem in dataItem)
                        {
                            string     subDataItemName  = subDataItem.Name;
                            JToken     subDataItemValue = subDataItem.Value;
                            JTokenType subDataItemType  = subDataItem.Value.Type;

                            if (subDataItemName.Equals("$type"))
                            {
                                if (subDataItemValue.ToString().Equals("MessageEnd"))
                                {
                                    ruleComplete = true;
                                    break;
                                }

                                if (typeExclusionList.Contains(subDataItemValue.ToString()))
                                {
                                    break;
                                }
                            }

                            string subDataItemValueStringValue = "";

                            if (item["Data"]["value"] != null)
                            {
                                subDataItemValueStringValue = (string)item["Data"]["value"];
                            }

                            if (nameExclusionList.Contains(subDataItemName) || valueExclusionList.Contains(subDataItemValueStringValue))
                            {
                                continue;
                            }

                            switch (subDataItemType)
                            {
                            case JTokenType.String:
                            case JTokenType.Integer:
                                sbSuccessResult.Append(RulesRunner(ref jsonJArrayObj, subDataItem, myApp, isErrOk, showInfo));
                                break;

                            case JTokenType.Null:
                                // do nothing!
                                break;

                            case JTokenType.Array:
                                // we will have string, int, and null again (never another Array in this case) -> we care about int and string

                                // check if the whole array can be replaced with null
                                // note: as we have added "typeInformationB" ourselves to BF, we don't want to remove it
                                if (subDataItemName != "typeInformationB")
                                {
                                    sbSuccessResult.Append(RulesRunner(ref jsonJArrayObj, subDataItem, -1, myApp, isErrOk, showInfo));
                                }

                                if (subDataItem.Value != null)
                                {
                                    // this is when it could not be null by the previous rule

                                    // first we need to remove items within the array to see which one can be safely removed


                                    int externalArrayCounter = 0;
                                    while (externalArrayCounter < subDataItem.Value.ToList().Count)
                                    {
                                        var origArrayList = subDataItem.Value.ToList();
                                        var newArrayList  = subDataItem.Value.ToList();

                                        if (!valueExclusionList.Contains(newArrayList[externalArrayCounter].ToString()))
                                        {
                                            newArrayList.RemoveAt(externalArrayCounter);
                                            subDataItem.Value = new JArray(newArrayList);

                                            if (CheckIfSuccess(jsonJArrayObj.ToString(), myApp, isErrOk, showInfo))
                                            {
                                                // it is a success so we can remove it!
                                                externalArrayCounter--;
                                                sbSuccessResult.AppendLine("Successful in removing:" + newArrayList + " - item: " + externalArrayCounter);
                                            }
                                            else
                                            {
                                                //undo
                                                subDataItem.Value = new JArray(origArrayList);
                                            }
                                        }
                                        externalArrayCounter++;
                                    }

                                    int counter = 0;
                                    foreach (JToken subArrayItem in subDataItem.Value.ToList())
                                    {
                                        JToken     arrayItemValue = subArrayItem;
                                        JTokenType arrayItemType  = subArrayItem.Type;

                                        if (valueExclusionList.Contains(arrayItemValue.ToString()))
                                        {
                                            continue;
                                        }

                                        switch (arrayItemType)
                                        {
                                        case JTokenType.String:
                                        case JTokenType.Integer:
                                            sbSuccessResult.Append(RulesRunner(ref jsonJArrayObj, subDataItem, counter, myApp, isErrOk, showInfo));
                                            break;

                                        default:
                                            break;
                                        }
                                        counter++;
                                    }
                                }

                                break;

                            default:
                                RulesRunner(ref jsonJArrayObj, subDataItem, myApp, isErrOk, showInfo);
                                break;
                            }
                        }

                        if (ruleComplete)
                        {
                            break;
                        }
                    }
                }


                //*
                if (showInfo)
                {
                    Console.WriteLine(sbSuccessResult);
                }
                //*/

                /*
                 * var resultString = jsonJArrayObj.ToString();
                 * Console.WriteLine(resultString);
                 */
            }
            else
            {
                throw new Exception("Invalid test case!");
            }

            return(jsonJArrayObj.ToString());
        }
Exemple #2
0
 public static byte[] MinimiseBFAndRun(byte[] binaryFormatted, InputArgs inInputArgs, bool isErrOk, bool showInfo)
 {
     return(MinimiseBFAndRun(new MemoryStream(binaryFormatted), inInputArgs, isErrOk, showInfo).ToArray());
 }