ClaimTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // ClaimTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. using System;
  29. using System.IdentityModel.Claims;
  30. using System.Net.Mail;
  31. using System.Security.Principal;
  32. using System.Security.Cryptography;
  33. using System.Security.Cryptography.X509Certificates;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.IdentityModel.Claims
  36. {
  37. [TestFixture]
  38. public class ClaimTest
  39. {
  40. [Test]
  41. public void CreateClaims ()
  42. {
  43. Claim c;
  44. // premises
  45. Assert.AreEqual ("http://schemas.xmlsoap.org/ws/2005/05/identity/right/identity", Rights.Identity, "#1");
  46. Assert.AreEqual ("http://schemas.xmlsoap.org/ws/2005/05/identity/right/possessproperty", Rights.PossessProperty, "#2");
  47. c = Claim.CreateDnsClaim ("123.45.6.7");
  48. AssertClaim ("Dns", c, ClaimTypes.Dns, "123.45.6.7", Rights.PossessProperty);
  49. Uri uri = new Uri ("http://www.mono-project.com");
  50. c = Claim.CreateUriClaim (uri);
  51. AssertClaim ("Uri", c, ClaimTypes.Uri, uri, Rights.PossessProperty);
  52. MailAddress mail = new MailAddress ("[email protected]");
  53. c = Claim.CreateMailAddressClaim (mail);
  54. AssertClaim ("Mail", c, ClaimTypes.Email, mail, Rights.PossessProperty);
  55. c = Claim.CreateNameClaim ("Rupert");
  56. AssertClaim ("Name", c, ClaimTypes.Name, "Rupert", Rights.PossessProperty);
  57. c = Claim.CreateSpnClaim ("foo");
  58. AssertClaim ("Spn", c, ClaimTypes.Spn, "foo", Rights.PossessProperty);
  59. c = Claim.CreateUpnClaim ("foo");
  60. AssertClaim ("Upn", c, ClaimTypes.Upn, "foo", Rights.PossessProperty);
  61. //SecurityIdentifier sid = new SecurityIdentifier (blah);
  62. //c = Claim.CreateWindowsSidClaim (sid);
  63. //AssertClaim ("Sid", c, ClaimTypes.Sid, blah, Rights.PossessProperty);
  64. byte [] hash = new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
  65. c = Claim.CreateHashClaim (hash);
  66. AssertClaim ("Hash", c, ClaimTypes.Hash, hash, Rights.PossessProperty);
  67. RSA rsa = RSA.Create ();
  68. c = Claim.CreateRsaClaim (rsa);
  69. AssertClaim ("Rsa", c, ClaimTypes.Rsa, rsa, Rights.PossessProperty);
  70. X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  71. byte [] chash = cert.GetCertHash ();
  72. c = Claim.CreateThumbprintClaim (chash);
  73. AssertClaim ("Thumbprint", c, ClaimTypes.Thumbprint, chash, Rights.PossessProperty);
  74. c = Claim.CreateX500DistinguishedNameClaim (cert.SubjectName);
  75. AssertClaim ("X500Name", c, ClaimTypes.X500DistinguishedName, cert.SubjectName, Rights.PossessProperty);
  76. }
  77. [Test]
  78. public void TestToString ()
  79. {
  80. Assert.AreEqual (
  81. String.Concat (Rights.PossessProperty, ": ", ClaimTypes.Name),
  82. Claim.CreateNameClaim ("mono").ToString (),
  83. "#1");
  84. }
  85. [Test]
  86. public void SystemClaim ()
  87. {
  88. Assert.AreEqual (
  89. String.Concat (Rights.Identity, ": ", ClaimTypes.System),
  90. Claim.System.ToString (),
  91. "#1");
  92. Assert.AreEqual ("System", Claim.System.Resource, "#2");
  93. }
  94. public static void AssertClaim (string label, Claim c, string type, object resource, string right)
  95. {
  96. Assert.AreEqual (type, c.ClaimType, label + ".ClaimType");
  97. if (resource != null)
  98. Assert.AreEqual (resource, c.Resource, label + ".Resource");
  99. Assert.AreEqual (right, c.Right, label + ".Right");
  100. }
  101. }
  102. }