ClaimTest.cs 4.3 KB

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