This commit is contained in:
Offir Shvartz 2021-08-10 09:30:01 +03:00
Родитель a4ff700836
Коммит 4e51709ab0
4 изменённых файлов: 80 добавлений и 5 удалений

Просмотреть файл

@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="YamlDotNet" Version="6.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Просмотреть файл

@ -5,6 +5,7 @@ using System.Text;
using System.Text.RegularExpressions;
using Xunit;
using Xunit.Abstractions;
using YamlDotNet.Serialization;
namespace NonAsciiValidations.Tests
{
@ -41,13 +42,46 @@ namespace NonAsciiValidations.Tests
ValidateOnlyAscii(yamlFullPath, detectionsYamlFileName);
}
private void ValidateOnlyAscii(string yamlFilePath, string yamlFileName)
private bool ShouldSkipTemplateValidation(string templateId)
{
var yaml = File.ReadAllText(yamlFilePath);
var nonAsciiCharMatch = Regex.Match(yaml, @"[^\u0000-\u007F]+");
Assert.False(nonAsciiCharMatch.Success, $"${yamlFileName} includes the non ascii char:{nonAsciiCharMatch.Value} string index:{nonAsciiCharMatch.Index}");
return TemplatesToSkipValidationReader.WhiteListTemplates
.Where(template => template.id == templateId)
.Where(template => !string.IsNullOrWhiteSpace(template.validationFailReason))
.Where(template => !string.IsNullOrWhiteSpace(template.templateName))
.Any();
}
}
private void ValidateOnlyAscii(string yamlFilePath, string yamlFileName)
{
var yaml = File.ReadAllText(yamlFilePath);
var extracted = TryExtractTemplateId(yaml,out string id);
//we ignore specific templates
if (extracted && ShouldSkipTemplateValidation(id))
{
return;
}
var nonAsciiCharMatch = Regex.Match(yaml, @"[^\u0000-\u007F]+");
Assert.False(nonAsciiCharMatch.Success, $"${yamlFileName} includes the non ascii char:{nonAsciiCharMatch.Value} string index:{nonAsciiCharMatch.Index}");
}
private static bool TryExtractTemplateId(string yaml,out string tempateId)
{
tempateId = null;
try
{
var deserializer = new DeserializerBuilder().Build();
var res = deserializer.Deserialize<dynamic>(yaml);
tempateId = res["id"];
}
catch
{
return false;
}
return true;
}
}
}

Просмотреть файл

@ -0,0 +1,9 @@
[
{
"id": "a953f304-12e4-48ae-bedc-d58fb1b0c6a6",
"templateName": "Unicode Obfuscation in Command Line",
"validationFailReason": "include unicode by design"
}
]

Просмотреть файл

@ -0,0 +1,31 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
namespace NonAsciiValidations.Tests
{
public class SkipTemplate
{
public string id;
public string templateName;
public string validationFailReason;
}
public static class TemplatesToSkipValidationReader
{
private const string SKipJsonFileName = "SkipValidationsTemplates.json";
static TemplatesToSkipValidationReader()
{
var jsonFilePath = Path.Combine(FilesTestData.GetSkipTemplatesPath(), SKipJsonFileName);
using (StreamReader r = new StreamReader(jsonFilePath))
{
string json = r.ReadToEnd();
WhiteListTemplates = JsonConvert.DeserializeObject<IEnumerable<SkipTemplate>>(json);
}
}
public static IEnumerable<SkipTemplate> WhiteListTemplates { get; private set; }
}
}