Skip to content

Instantly share code, notes, and snippets.

@jstedfast
Created June 4, 2019 02:53
Show Gist options
  • Save jstedfast/b0a6bc46202e7a52b602f9f30586e971 to your computer and use it in GitHub Desktop.
Save jstedfast/b0a6bc46202e7a52b602f9f30586e971 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Linq;
using System.Text;
using YamlDotNet.Core;
using YamlDotNet.RepresentationModel;
namespace ArcSigningTestGenerator
{
class Program
{
public static void Main (string[] args)
{
using (var reader = File.OpenText ("arc-draft-sign-tests.yml")) {
var yaml = new YamlStream ();
yaml.Load (reader);
Console.WriteLine ("using System;");
Console.WriteLine ("using System.IO;");
Console.WriteLine ("using System.Text;");
Console.WriteLine ("using System.Collections.Generic;");
Console.WriteLine ();
Console.WriteLine ("using NUnit.Framework;");
Console.WriteLine ();
Console.WriteLine ("using Org.BouncyCastle.Crypto;");
Console.WriteLine ("using Org.BouncyCastle.OpenSsl;");
Console.WriteLine ();
Console.WriteLine ("using MimeKit.Cryptography;");
Console.WriteLine ();
Console.WriteLine ("namespace UnitTests.Cryptography {");
Console.WriteLine ("\t[TestFixture]");
Console.WriteLine ("\tpublic class ArcSignerTests");
Console.WriteLine ("\t{");
Console.WriteLine ("\t\tstatic void AssertHeadersEqual (string description, HeaderId id, string expected, string actual)");
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\tvar expectedTags = DkimVerifierBase.ParseParamaterTags (id, expected);");
Console.WriteLine ("\t\t\tvar actualTags = DkimVerifierBase.ParseParameterTags (id, actual);");
Console.WriteLine ();
Console.WriteLine ("\t\t}");
Console.WriteLine ();
Console.WriteLine ("\t\tstatic void Sign (string description, string input, DkimPublicKeyLocator locator, string srvid, string domain, string selector, string privateKey, long t, string[] hdrs, string aar, string ams, string seal)");
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\tArcSigner signer;");
Console.WriteLine ();
Console.WriteLine ("\t\t\tusing (var stream = new MemoryStream (Encoding.ASCII.GetBytes (privateKey), false))");
Console.WriteLine ("\t\t\t\tsigner = new DummyArcSigner (stream, domain, selector, srvid, locator);");
Console.WriteLine ();
Console.WriteLine ("\t\t\tusing (var stream = new MemoryStream (Encoding.UTF8.GetBytes (input), false)) {");
Console.WriteLine ("\t\t\t\tvar message = MimeMessage.Load (stream);");
Console.WriteLine ("\t\t\t\tHeader header;");
Console.WriteLine ("\t\t\t\tint index;");
Console.WriteLine ();
Console.WriteLine ("\t\t\t\tsigner.Sign (message, hdrs);");
Console.WriteLine ();
Console.WriteLine ("\t\t\t\tindex = message.Headers.IndexOf (HeaderId.ArcAuthenticationResults);");
Console.WriteLine ("\t\t\t\tAssert.AreEqual (2, index, \"IndexOf AAR\");");
Console.WriteLine ("\t\t\t\theader = message.Headers[index];");
Console.WriteLine ("\t\t\t\tAssert.AreEqual (aar, header.Value, \"AAR\");");
Console.WriteLine ();
Console.WriteLine ("\t\t\t\tindex = message.Headers.IndexOf (HeaderId.ArcMessageSignature);");
Console.WriteLine ("\t\t\t\tAssert.AreEqual (1, index, \"IndexOf AMS\");");
Console.WriteLine ("\t\t\t\theader = message.Headers[index];");
Console.WriteLine ("\t\t\t\tAssertHeadersEqual (description, HeaderId.ArcMessageSignature, ams, header.Value);");
Console.WriteLine ();
Console.WriteLine ("\t\t\t\tindex = message.Headers.IndexOf (HeaderId.ArcSeal);");
Console.WriteLine ("\t\t\t\tAssert.AreEqual (0, index, \"IndexOf AS\");");
Console.WriteLine ("\t\t\t\theader = message.Headers[index];");
Console.WriteLine ("\t\t\t\tAssertHeadersEqual (description, HeaderId.ArcSeal, seal, header.Value);");
Console.WriteLine ("\t\t\t}");
Console.WriteLine ("\t\t}");
Console.WriteLine ();
var descriptionKey = new YamlScalarNode ("description");
var sigHeadersKey = new YamlScalarNode ("sig-headers");
var recordsKey = new YamlScalarNode ("txt-records");
var privateKey = new YamlScalarNode ("privatekey");
var messageKey = new YamlScalarNode ("message");
var domainKey = new YamlScalarNode ("domain");
var selectorKey = new YamlScalarNode ("sel");
var srvidKey = new YamlScalarNode ("srv-id");
var testsKey = new YamlScalarNode ("tests");
var aarKey = new YamlScalarNode ("AAR");
var amsKey = new YamlScalarNode ("AMS");
var asKey = new YamlScalarNode ("AS");
var tKey = new YamlScalarNode ("t");
foreach (var document in yaml.Documents) {
var mapping = (YamlMappingNode) document.RootNode;
var section = ((YamlScalarNode) mapping.Children[descriptionKey]).Value;
var selector = ((YamlScalarNode) mapping.Children[selectorKey]).Value;
var domain = ((YamlScalarNode) mapping.Children[domainKey]).Value;
var keyblock = ((YamlScalarNode) mapping.Children[privateKey]).Value;
var records = (YamlMappingNode) mapping.Children[recordsKey];
var tests = (YamlMappingNode) mapping.Children[testsKey];
Console.WriteLine ("\t\t#region {0}", section);
Console.WriteLine ();
foreach (var node in tests.Children) {
var name = ((YamlScalarNode) node.Key).Value;
var test = (YamlMappingNode) node.Value;
var description = ((YamlScalarNode) test.Children[descriptionKey]).Value;
var message = ((YamlScalarNode) test.Children[messageKey]).Value;
var hdrs = ((YamlScalarNode) test.Children[sigHeadersKey]).Value;
var srvid = ((YamlScalarNode) test.Children[srvidKey]).Value;
var seal = ((YamlScalarNode) test.Children[asKey]).Value;
var ams = ((YamlScalarNode) test.Children[amsKey]).Value;
var aar = ((YamlScalarNode) test.Children[aarKey]).Value;
var t = ((YamlScalarNode) test.Children[tKey]).Value;
Console.WriteLine ("\t\t[Test]");
Console.WriteLine ("\t\tpublic void {0} ()", name);
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\tconst string input = @\"{0}\";", message.Replace ("\"", "\"\""));
Console.WriteLine ("\t\t\tconst string keyblock = @\"{0}\";", keyblock);
Console.WriteLine ("\t\t\tconst string seal = \"{0}\";", seal.Replace (Environment.NewLine, " ").TrimEnd ());
Console.WriteLine ("\t\t\tconst string ams = \"{0}\";", ams.Replace (Environment.NewLine, " ").TrimEnd ());
Console.WriteLine ("\t\t\tconst string aar = \"{0}\";", aar.Replace (Environment.NewLine, " ").TrimEnd ());
Console.WriteLine ("\t\t\tvar hdrs = new string[] {{ \"{0}\" }};", hdrs.Replace (":", "\", \""));
Console.WriteLine ("\t\t\tvar locator = new DkimPublicKeyLocator ();");
Console.WriteLine ();
foreach (var record in records.Children) {
var key = ((YamlScalarNode) record.Key).Value;
var value = ((YamlScalarNode) record.Value).Value.Replace (Environment.NewLine, "");
Console.WriteLine ($"\t\t\tlocator.Add (\"{key}\", \"{value}\");");
}
Console.WriteLine ();
Console.WriteLine ($"\t\t\tSign (\"{description}\", input, locator, \"{srvid}\", \"{domain}\", \"{selector}\", keyblock, {t}, hdrs, aar, ams, seal);");
Console.WriteLine ("\t\t}");
Console.WriteLine ();
}
Console.WriteLine ("\t\t#endregion");
}
Console.WriteLine ("\t}");
Console.WriteLine ("}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment