summary refs log tree commit diff
path: root/crypto/test/src/asn1/test/AdmissionSyntaxUnitTest.cs
blob: 59464d212c3b85403eaa76484c5a0811bb4a768d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;

using NUnit.Framework;

using Org.BouncyCastle.Asn1.IsisMtt.X509;
using Org.BouncyCastle.Asn1.X500;
using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Tests
{
	[TestFixture]
	public class AdmissionSyntaxUnitTest
		: Asn1UnitTest
	{
		public override string Name
		{
			get { return "AdmissionSyntax"; }
		}

		public override void PerformTest()
		{
			GeneralName name = new GeneralName(new X509Name("CN=hello world"));
			Asn1Sequence admissions = new DerSequence(
				new Admissions(name,
				new NamingAuthority(new DerObjectIdentifier("1.2.3"), "url", new DirectoryString("fred")),
				new ProfessionInfo[0]));
			AdmissionSyntax syntax = new AdmissionSyntax(name, admissions);

			checkConstruction(syntax, name, admissions);

			syntax = AdmissionSyntax.GetInstance(null);

			if (syntax != null)
			{
				Fail("null GetInstance() failed.");
			}

			try
			{
				AdmissionSyntax.GetInstance(new Object());

				Fail("GetInstance() failed to detect bad object.");
			}
			catch (ArgumentException)
			{
				// expected
			}
		}

		private void checkConstruction(
			AdmissionSyntax	syntax,
			GeneralName		authority,
			Asn1Sequence	admissions)
		{
			checkValues(syntax, authority, admissions);

			syntax = AdmissionSyntax.GetInstance(syntax);

			checkValues(syntax, authority, admissions);

			Asn1InputStream aIn = new Asn1InputStream(syntax.ToAsn1Object().GetEncoded());

			Asn1Sequence info = (Asn1Sequence) aIn.ReadObject();

			syntax = AdmissionSyntax.GetInstance(info);

			checkValues(syntax, authority, admissions);
		}

		private void checkValues(
			AdmissionSyntax syntax,
			GeneralName     authority,
			Asn1Sequence    admissions)
		{
			checkMandatoryField("admissionAuthority", authority, syntax.AdmissionAuthority);

			Admissions[] adm = syntax.GetContentsOfAdmissions();

			if (adm.Length != 1 || !adm[0].Equals(admissions[0]))
			{
				Fail("admissions check failed");
			}
		}

		public static void Main(
			string[] args)
		{
			RunTest(new AdmissionSyntaxUnitTest());
		}

		[Test]
		public void TestFunction()
		{
			string resultText = Perform().ToString();

			Assert.AreEqual(Name + ": Okay", resultText);
		}
	}
}