ServiceCredentialsTest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // ServiceCredentialsTest.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.Collections.ObjectModel;
  30. using System.IdentityModel.Claims;
  31. using System.IdentityModel.Selectors;
  32. using System.IdentityModel.Tokens;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Channels;
  36. using System.ServiceModel.Description;
  37. using System.ServiceModel.Dispatcher;
  38. using System.ServiceModel.Security;
  39. using NUnit.Framework;
  40. namespace MonoTests.System.ServiceModel.Description
  41. {
  42. [TestFixture]
  43. public class ServiceCredentialsTest
  44. {
  45. [Test]
  46. public void ClientCertificate ()
  47. {
  48. X509CertificateInitiatorServiceCredential cc =
  49. new ServiceCredentials ().ClientCertificate;
  50. Assert.IsNull (cc.Certificate, "#1");
  51. Assert.AreEqual (X509CertificateValidationMode.ChainTrust, cc.Authentication.CertificateValidationMode, "#2-1");
  52. Assert.IsNull (cc.Authentication.CustomCertificateValidator, "#2-2");
  53. Assert.IsTrue (cc.Authentication.IncludeWindowsGroups, "#2-3");
  54. Assert.IsFalse (cc.Authentication.MapClientCertificateToWindowsAccount, "#2-4");
  55. Assert.AreEqual (X509RevocationMode.Online, cc.Authentication.RevocationMode, "#2-5");
  56. Assert.AreEqual (StoreLocation.LocalMachine, cc.Authentication.TrustedStoreLocation, "#2-6");
  57. }
  58. [Test]
  59. public void IssuedTokenAuthentication ()
  60. {
  61. IssuedTokenServiceCredential ic =
  62. new ServiceCredentials ().IssuedTokenAuthentication;
  63. Assert.IsFalse (ic.AllowUntrustedRsaIssuers, "#1");
  64. Assert.AreEqual (X509CertificateValidationMode.ChainTrust, ic.CertificateValidationMode, "#2");
  65. Assert.IsNull (ic.CustomCertificateValidator, "#3");
  66. Assert.AreEqual (0, ic.KnownCertificates.Count, "#4");
  67. Assert.AreEqual (X509RevocationMode.Online, ic.RevocationMode, "#5");
  68. Assert.IsNull (ic.SamlSerializer, "#6");
  69. Assert.AreEqual (StoreLocation.LocalMachine, ic.TrustedStoreLocation, "#7");
  70. }
  71. [Test]
  72. public void Peer ()
  73. {
  74. PeerCredential p = new ServiceCredentials ().Peer;
  75. Assert.IsNull (p.Certificate, "#1");
  76. Assert.IsNull (p.MeshPassword, "#2");
  77. X509PeerCertificateAuthentication pa = p.MessageSenderAuthentication;
  78. Assert.AreEqual (X509CertificateValidationMode.PeerOrChainTrust, pa.CertificateValidationMode, "#3-1");
  79. Assert.IsNull (pa.CustomCertificateValidator, "#3-2");
  80. Assert.AreEqual (X509RevocationMode.Online, pa.RevocationMode, "#3-3");
  81. Assert.AreEqual (StoreLocation.CurrentUser, pa.TrustedStoreLocation, "#3-4");
  82. pa = p.PeerAuthentication;
  83. Assert.AreEqual (X509CertificateValidationMode.PeerOrChainTrust, pa.CertificateValidationMode, "#4-1");
  84. Assert.IsNull (pa.CustomCertificateValidator, "#4-2");
  85. Assert.AreEqual (X509RevocationMode.Online, pa.RevocationMode, "#4-3");
  86. Assert.AreEqual (StoreLocation.CurrentUser, pa.TrustedStoreLocation, "#4-4");
  87. }
  88. [Test]
  89. public void SecureConversationAuthentication ()
  90. {
  91. SecureConversationServiceCredential sc =
  92. new ServiceCredentials ().SecureConversationAuthentication;
  93. Assert.AreEqual (5, sc.SecurityContextClaimTypes.Count, "#1");
  94. Collection<Type> types = new Collection<Type> (new Type [] {
  95. typeof (SamlAuthorizationDecisionClaimResource),
  96. typeof (SamlAuthenticationClaimResource),
  97. typeof (SamlAccessDecision),
  98. typeof (SamlAuthorityBinding),
  99. typeof (SamlNameIdentifierClaimResource)});
  100. foreach (Type type in sc.SecurityContextClaimTypes)
  101. if (!types.Contains (type))
  102. Assert.Fail (type.ToString ());
  103. DataProtectionSecurityStateEncoder sse = sc.SecurityStateEncoder
  104. as DataProtectionSecurityStateEncoder;
  105. Assert.IsNotNull (sse, "#2-1");
  106. Assert.IsTrue (sse.UseCurrentUserProtectionScope, "#2-2");
  107. }
  108. [Test]
  109. public void ServiceCertificate ()
  110. {
  111. Assert.IsNull (new ServiceCredentials ().ServiceCertificate.Certificate, "#1");
  112. }
  113. [Test]
  114. public void UserNameAuthentication ()
  115. {
  116. UserNamePasswordServiceCredential un =
  117. new ServiceCredentials ().UserNameAuthentication;
  118. Assert.AreEqual (TimeSpan.FromMinutes (15), un.CachedLogonTokenLifetime, "#1");
  119. Assert.IsFalse (un.CacheLogonTokens, "#2");
  120. Assert.IsNull (un.CustomUserNamePasswordValidator, "#3");
  121. Assert.IsTrue (un.IncludeWindowsGroups, "#4");
  122. Assert.AreEqual (0x80, un.MaxCachedLogonTokens, "#5");
  123. Assert.IsNull (un.MembershipProvider, "#6");
  124. Assert.AreEqual (UserNamePasswordValidationMode.Windows, un.UserNamePasswordValidationMode, "#7");
  125. // TODO: WindowsAuthentication if we have infinite amount of free time.
  126. }
  127. }
  128. }