public void VerifyDerDecodeSequenceOfErrorCases() { // for reference, the valid SequenceOf DER block is: new byte[] { 0x30, 0x34, 0x1e, 0x26, 0x00, 0x56, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x69, 0x00, 0x64, 0x00, 0x69, 0x00, 0x74, 0x00, 0x79, 0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x64, 0x00, 0x55, 0x00, 0x6e, 0x00, 0x69, 0x00, 0x74, 0x00, 0x73, 0x1e, 0x0a, 0x00, 0x57, 0x00, 0x65, 0x00, 0x65, 0x00, 0x6b, 0x00, 0x73 } var testCases = new[] { new { test = new byte[] { 0x30, 0x35, 0x1e, 0x26, 0x00, 0x56, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x69, 0x00, 0x64, 0x00, 0x69, 0x00, 0x74, 0x00, 0x79, 0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x64, 0x00, 0x55, 0x00, 0x6e, 0x00, 0x69, 0x00, 0x74, 0x00, 0x73, 0x1e, 0x0a, 0x00, 0x57, 0x00, 0x65, 0x00, 0x65, 0x00, 0x6b, 0x00, 0x73 }, description = "SequenceOf length too large" }, new { test = new byte[] { 0x30, 0x33, 0x1e, 0x26, 0x00, 0x56, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x69, 0x00, 0x64, 0x00, 0x69, 0x00, 0x74, 0x00, 0x79, 0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x64, 0x00, 0x55, 0x00, 0x6e, 0x00, 0x69, 0x00, 0x74, 0x00, 0x73, 0x1e, 0x0a, 0x00, 0x57, 0x00, 0x65, 0x00, 0x65, 0x00, 0x6b, 0x00, 0x73 }, description = "SequenceOf length too small" }, new { test = new byte[] { 0x30 }, description = "incomplete header" }, new { test = new byte[] { 0x15, 0x34, 0x1e, 0x26, 0x00, 0x56, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x69, 0x00, 0x64, 0x00, 0x69, 0x00, 0x74, 0x00, 0x79, 0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x64, 0x00, 0x55, 0x00, 0x6e, 0x00, 0x69, 0x00, 0x74, 0x00, 0x73, 0x1e, 0x0a, 0x00, 0x57, 0x00, 0x65, 0x00, 0x65, 0x00, 0x6b, 0x00, 0x73 }, description = "wrong tag" }, new { test = (byte[])null, description = "null input" }, new { test = new byte[] { 0x30, 0x34, 0x1e, 0x26, 0x00, 0x56, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x69, 0x00, 0x64, 0x00, 0x69, 0x00, 0x74, 0x00, 0x79, 0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x64, 0x00, 0x55, 0x00, 0x6e, 0x00, 0x69, 0x00, 0x74, 0x00, 0x73, 0x1e, 0x0a, 0x00, 0x57, 0x00, 0x65, 0x00, 0x65, 0x00, 0x6b, 0x00 }, description = "entry has length too large" }, new { test = new byte[] { 0x30, 0x34, 0x1e, 0x26, 0x00, 0x56, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x69, 0x00, 0x64, 0x00, 0x69, 0x00, 0x74, 0x00, 0x79, 0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x64, 0x00, 0x55, 0x00, 0x6e, 0x00, 0x69, 0x00, 0x74, 0x00, 0x73, 0x1e, 0x0a, 0x00, 0x57, 0x00, 0x65, 0x00, 0x65, 0x00, 0x6b, 0x00, 0x73, 0x73 }, description = "entry has length too small" }, }; foreach (var testCase in testCases) { bool caught = false; try { DerUtils.DecodeSequenceOf(testCase.test); } catch (Exception) { caught = true; } Assert.IsTrue(caught, "excected an error, but none caught on test: " + testCase.description); } }
public void VerifyDerSequenceOfRoundTrip() { var testCases = new[] { new { entryData = new List <string>() { "data1" }, description = "single entry" }, new { entryData = new List <string>() { "data1", "data2", "data3" }, description = "multiple entries" }, new { entryData = new List <string>() { }, description = "no entries" }, }; foreach (var testCase in testCases) { // convert the input data from strings to der encoding, since that's the expected input format List <byte[]> derEntries = new List <byte[]>(); foreach (string entry in testCase.entryData) { derEntries.Add(DerUtils.EncodePrintableString(entry)); } List <byte[]> outputEntries = DerUtils.DecodeSequenceOf(DerUtils.EncodeSequenceOf(derEntries)); // verify the output matches the input Assert.AreEqual(testCase.entryData.Count, outputEntries.Count, "number of output entries should match the number of input entries for test case: " + testCase.description); for (int i = 0; i < outputEntries.Count; i++) { Assert.AreEqual(testCase.entryData[i], DerUtils.DecodeString(outputEntries[i]), "Output data should match input data for test case: " + testCase.description); } } }