public void Extract_The_Correct_String_Value()
        {
            // arrange
            var extractor = new StringExtractor();
            var builder   = new ClrObjectBuilder();

            builder
            .WithType("System.String")
            .WithAddress(0x1234)
            .WithSize(100)
            .WithSimpleValue("Duke the corgi");
            var runtimeBuilder = new ClrRuntimeBuilder();
            var heapBuilder    = new ClrHeapBuilder();

            heapBuilder.WithGetGeneration(2);
            runtimeBuilder.WithHeap(heapBuilder.Build());
            var runtime = runtimeBuilder.Build();
            var built   = builder.Build();

            // act
            var result = (StringDumpObject)extractor.Extract(built, runtime);

            // assert
            result.Address.Should().Be(0x1234);
            result.Size.Should().Be(100);
            result.Gen.Should().Be(2);
            result.FullTypeName.Should().Be("System.String");
            result.Value.Should().Be("Duke the corgi");
        }
Example #2
0
        public void CorruptedNoEnd()
        {
            var instance = new StringExtractor("12", "345");

            var text = "qwert12asdzxc";

            var result = instance.Extract(text);

            Assert.AreEqual(0, result.Length);
        }
Example #3
0
        public void Scenario1()
        {
            var instance = new StringExtractor("12", "345");

            var text = "qwert12asd345zxc";

            var result = instance.Extract(text);

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual("asd", result[0]);
        }
Example #4
0
        public void RealCase()
        {
            var instance = new StringExtractor("@R(\"", "\"");

            var text = "< a rel = \"nofollow\" href = \"@Url.Action(\"history\", \"Resource\", new { id = Model.Id, title = Model.Title.NormalizeFormat(true), area = string.Empty })\" id = \"history\" > @R(\"Show history\") </ a > ";

            var result = instance.Extract(text);

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual("Show history", result[0]);
        }
Example #5
0
        private void ExtractStringList()
        {
            const string _File   = "file";
            const string _Line   = "line";
            const string _Col    = "col";
            const string _Type   = "type";
            const string _String = "string";
            Dictionary <string, string> defaultColumns = new Dictionary <string, string>
            {
                [_File]   = "File",
                [_Line]   = "Line",
                [_Col]    = "Col",
                [_Type]   = "Type",
                [_String] = "String",
            };

            string schema_name    = cmd.GetValue("schema-name") ?? SchemaName.dbo;
            string table_name     = cmd.GetValue("table-name");
            bool   allDirectories = cmd.Has("subdirectory");

            string[] file_names = cmd.InputFiles(allDirectories);
            string[] excludes   = cmd.Excludes;

            IDictionary <string, string> column_names = cmd.GetDictionary("column-names", defaultColumns);

            if (file_names == null)
            {
                cerr.WriteLine($"file name or directory is not defined, use option /in:file_name");
                return;
            }

            if (file_names.Length == 0)
            {
                cerr.WriteLine($"file doesn't exist: \"{file_names}\"");
                return;
            }

            if (tname == null)
            {
                if (table_name == null)
                {
                    cerr.WriteLine($"/table-name is not defined");
                    return;
                }

                if (dname == null)
                {
                    cerr.WriteLine($"required to select a database");
                    return;
                }

                tname = new TableName(dname, schema_name, table_name);
                if (!tname.Exists())
                {
                    cerr.WriteLine($"table-name doesn't exist: {tname}");
                    return;
                }
            }

            DataTable dt = new TableReader(tname)
            {
                CaseSensitive = true,
            }.Table;

            StringDumper dumper = new StringDumper(tname)
            {
                Line     = column_names[_Line],
                Column   = column_names[_Col],
                Type     = column_names[_Type],
                FileName = column_names[_File],
                Value    = column_names[_String],
            };

            dumper.Initialize();
            StringExtractor extractor = new StringExtractor(dumper);

            if (!ValidateColumn <int>(dt, dumper.Line, "column-name", required: true))
            {
                return;
            }
            if (!ValidateColumn <int>(dt, dumper.Column, "column-name", required: true))
            {
                return;
            }
            if (!ValidateColumn <string>(dt, dumper.FileName, "column-name", required: false))
            {
                return;
            }
            if (!ValidateColumn <string>(dt, dumper.Type, "column-name", required: false))
            {
                return;
            }
            if (!ValidateColumn <string>(dt, dumper.Value, "column-name", required: false))
            {
                return;
            }


            foreach (string file in file_names)
            {
                if (file.IsMatch(excludes))
                {
                    Console.WriteLine($"skip: {file}");
                    continue;
                }

                if (file.EndsWith("AssemblyInfo.cs"))
                {
                    continue;
                }

                int count = extractor.Extract(file);
                if (count > 0)
                {
                    cout.WriteLine($"{count} of strings were extracted in file: \"{file}\"");
                }
                else
                {
                    cout.WriteLine($"no string found in file: \"{file}\"");
                }
            }

            bool commit = cmd.Has("submit-changes");

            if (!commit)
            {
                return;
            }

            cout.WriteLine($"starting to save changes into table \"{tname}\"");
            try
            {
                TableWriter tableWriter = new TableWriter(tname);
                tableWriter.Save(dumper.Table);
                cout.WriteLine($"completed to save on table \"{tname}\" from \"{cmd.InputPath()}\"");
            }
            catch (Exception ex)
            {
                cerr.WriteLine($"failed to save in \"{tname}\" , {ex.AllMessages()}");
            }
        }