1 //
2 // ClaimTest.cs
3 //
4 // Author:
5 //	Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if !MOBILE
29 using System;
30 using System.IdentityModel.Claims;
31 using System.Net.Mail;
32 using System.Security.Principal;
33 using System.Security.Cryptography;
34 using System.Security.Cryptography.X509Certificates;
35 using NUnit.Framework;
36 
37 namespace MonoTests.System.IdentityModel.Claims
38 {
39 	[TestFixture]
40 	public class ClaimTest
41 	{
42 		[Test]
CreateClaims()43 		public void CreateClaims ()
44 		{
45 			Claim c;
46 
47 			// premises
48 			Assert.AreEqual ("http://schemas.xmlsoap.org/ws/2005/05/identity/right/identity", Rights.Identity, "#1");
49 			Assert.AreEqual ("http://schemas.xmlsoap.org/ws/2005/05/identity/right/possessproperty", Rights.PossessProperty, "#2");
50 
51 			c = Claim.CreateDnsClaim ("123.45.6.7");
52 			AssertClaim ("Dns", c, ClaimTypes.Dns, "123.45.6.7", Rights.PossessProperty);
53 
54 			Uri uri = new Uri ("http://www.mono-project.com");
55 			c = Claim.CreateUriClaim (uri);
56 			AssertClaim ("Uri", c, ClaimTypes.Uri, uri, Rights.PossessProperty);
57 
58 			MailAddress mail = new MailAddress ("rupert@ximian.com");
59 			c = Claim.CreateMailAddressClaim (mail);
60 			AssertClaim ("Mail", c, ClaimTypes.Email, mail, Rights.PossessProperty);
61 
62 			c = Claim.CreateNameClaim ("Rupert");
63 			AssertClaim ("Name", c, ClaimTypes.Name, "Rupert", Rights.PossessProperty);
64 
65 			c = Claim.CreateSpnClaim ("foo");
66 			AssertClaim ("Spn", c, ClaimTypes.Spn, "foo", Rights.PossessProperty);
67 
68 			c = Claim.CreateUpnClaim ("foo");
69 			AssertClaim ("Upn", c, ClaimTypes.Upn, "foo", Rights.PossessProperty);
70 
71 			//SecurityIdentifier sid = new SecurityIdentifier (blah);
72 			//c = Claim.CreateWindowsSidClaim (sid);
73 			//AssertClaim ("Sid", c, ClaimTypes.Sid, blah, Rights.PossessProperty);
74 
75 			byte [] hash = new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
76 			c = Claim.CreateHashClaim (hash);
77 			AssertClaim ("Hash", c, ClaimTypes.Hash, hash, Rights.PossessProperty);
78 
79 			RSA rsa = RSA.Create ();
80 			c = Claim.CreateRsaClaim (rsa);
81 			AssertClaim ("Rsa", c, ClaimTypes.Rsa, rsa, Rights.PossessProperty);
82 
83 			X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
84 			byte [] chash = cert.GetCertHash ();
85 			c = Claim.CreateThumbprintClaim (chash);
86 			AssertClaim ("Thumbprint", c, ClaimTypes.Thumbprint, chash, Rights.PossessProperty);
87 
88 			c = Claim.CreateX500DistinguishedNameClaim (cert.SubjectName);
89 			AssertClaim ("X500Name", c, ClaimTypes.X500DistinguishedName, cert.SubjectName, Rights.PossessProperty);
90 		}
91 
92 		[Test]
TestToString()93 		public void TestToString ()
94 		{
95 			Assert.AreEqual (
96 				String.Concat (Rights.PossessProperty, ": ", ClaimTypes.Name),
97 				Claim.CreateNameClaim ("mono").ToString (),
98 				"#1");
99 		}
100 
101 		[Test]
SystemClaim()102 		public void SystemClaim ()
103 		{
104 			Assert.AreEqual (
105 				String.Concat (Rights.Identity, ": ", ClaimTypes.System),
106 				Claim.System.ToString (),
107 				"#1");
108 			Assert.AreEqual ("System", Claim.System.Resource, "#2");
109 		}
110 
AssertClaim(string label, Claim c, string type, object resource, string right)111 		public static void AssertClaim (string label, Claim c, string type, object resource, string right)
112 		{
113 			Assert.AreEqual (type, c.ClaimType, label + ".ClaimType");
114 			if (resource != null)
115 				Assert.AreEqual (resource, c.Resource, label + ".Resource");
116 			Assert.AreEqual (right, c.Right, label + ".Right");
117 		}
118 	}
119 }
120 #endif
121