SspiSecurityTokenParametersTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // SspiSecurityTokenParametersTest.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.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.Net;
  32. using System.Net.Security;
  33. using System.IdentityModel.Selectors;
  34. using System.IdentityModel.Tokens;
  35. using System.ServiceModel;
  36. using System.ServiceModel.Channels;
  37. using System.ServiceModel.Description;
  38. using System.ServiceModel.Security;
  39. using System.ServiceModel.Security.Tokens;
  40. using NUnit.Framework;
  41. using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
  42. namespace MonoTests.System.ServiceModel.Security.Tokens
  43. {
  44. [TestFixture]
  45. public class SspiSecurityTokenParametersTest
  46. {
  47. class MyParameters : SspiSecurityTokenParameters
  48. {
  49. public bool HasAsymmetricKeyEx {
  50. get { return HasAsymmetricKey; }
  51. }
  52. public bool SupportsClientAuthenticationEx {
  53. get { return SupportsClientAuthentication; }
  54. }
  55. public bool SupportsClientWindowsIdentityEx {
  56. get { return SupportsClientWindowsIdentity; }
  57. }
  58. public bool SupportsServerAuthenticationEx {
  59. get { return SupportsServerAuthentication; }
  60. }
  61. public SecurityKeyIdentifierClause CallCreateKeyIdentifierClause (
  62. SecurityToken token, SecurityTokenReferenceStyle referenceStyle)
  63. {
  64. return CreateKeyIdentifierClause (token, referenceStyle);
  65. }
  66. public void InitRequirement (SecurityTokenRequirement requirement)
  67. {
  68. InitializeSecurityTokenRequirement (requirement);
  69. }
  70. }
  71. [Test]
  72. public void DefaultValues ()
  73. {
  74. MyParameters tp = new MyParameters ();
  75. Assert.AreEqual (SecurityTokenInclusionMode.AlwaysToRecipient, tp.InclusionMode, "#1");
  76. Assert.AreEqual (SecurityTokenReferenceStyle.Internal, tp.ReferenceStyle, "#2");
  77. Assert.AreEqual (true, tp.RequireDerivedKeys, "#3");
  78. Assert.AreEqual (false, tp.HasAsymmetricKeyEx, "#4");
  79. Assert.AreEqual (true, tp.SupportsClientAuthenticationEx, "#5");
  80. Assert.AreEqual (true, tp.SupportsClientWindowsIdentityEx, "#6");
  81. Assert.AreEqual (true, tp.SupportsServerAuthenticationEx, "#7");
  82. Assert.AreEqual (false, tp.RequireCancellation, "#2-1");
  83. }
  84. [Test]
  85. public void InitializeSecurityTokenParameters ()
  86. {
  87. MyParameters tp = new MyParameters ();
  88. InitiatorServiceModelSecurityTokenRequirement r =
  89. new InitiatorServiceModelSecurityTokenRequirement ();
  90. tp.InitRequirement (r);
  91. Assert.AreEqual (ServiceModelSecurityTokenTypes.Spnego, r.TokenType, "#1");
  92. Assert.AreEqual (false, r.Properties [ReqType.SupportSecurityContextCancellationProperty], "#2");
  93. }
  94. [Test]
  95. [ExpectedException (typeof (ArgumentException))]
  96. public void CreateProviderNoTargetAddress ()
  97. {
  98. MyParameters tp = new MyParameters ();
  99. InitiatorServiceModelSecurityTokenRequirement r =
  100. new InitiatorServiceModelSecurityTokenRequirement ();
  101. tp.InitRequirement (r);
  102. ClientCredentials cred = new ClientCredentials ();
  103. ClientCredentialsSecurityTokenManager manager =
  104. new ClientCredentialsSecurityTokenManager (cred);
  105. manager.CreateSecurityTokenProvider (r);
  106. }
  107. [Test]
  108. [ExpectedException (typeof (ArgumentException))]
  109. public void CreateProviderNoSecurityBindingElement ()
  110. {
  111. MyParameters tp = new MyParameters ();
  112. InitiatorServiceModelSecurityTokenRequirement r =
  113. new InitiatorServiceModelSecurityTokenRequirement ();
  114. tp.InitRequirement (r);
  115. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  116. ClientCredentials cred = new ClientCredentials ();
  117. ClientCredentialsSecurityTokenManager manager =
  118. new ClientCredentialsSecurityTokenManager (cred);
  119. manager.CreateSecurityTokenProvider (r);
  120. }
  121. [Test]
  122. [ExpectedException (typeof (ArgumentException))]
  123. public void CreateProviderNoIssuerBindingContext ()
  124. {
  125. MyParameters tp = new MyParameters ();
  126. InitiatorServiceModelSecurityTokenRequirement r =
  127. new InitiatorServiceModelSecurityTokenRequirement ();
  128. tp.InitRequirement (r);
  129. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  130. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  131. ClientCredentials cred = new ClientCredentials ();
  132. ClientCredentialsSecurityTokenManager manager =
  133. new ClientCredentialsSecurityTokenManager (cred);
  134. //SecurityTokenProvider p =
  135. manager.CreateSecurityTokenProvider (r);
  136. }
  137. [Test]
  138. [ExpectedException (typeof (ArgumentException))]
  139. public void CreateProviderNoMessageSecurityVersion ()
  140. {
  141. MyParameters tp = new MyParameters ();
  142. InitiatorServiceModelSecurityTokenRequirement r =
  143. new InitiatorServiceModelSecurityTokenRequirement ();
  144. tp.InitRequirement (r);
  145. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  146. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  147. r.Properties [ReqType.IssuerBindingContextProperty] =
  148. new BindingContext (new CustomBinding (),
  149. new BindingParameterCollection ());
  150. ClientCredentials cred = new ClientCredentials ();
  151. ClientCredentialsSecurityTokenManager manager =
  152. new ClientCredentialsSecurityTokenManager (cred);
  153. //SecurityTokenProvider p =
  154. manager.CreateSecurityTokenProvider (r);
  155. }
  156. [Test]
  157. public void CreateProvider ()
  158. {
  159. MyParameters tp = new MyParameters ();
  160. InitiatorServiceModelSecurityTokenRequirement r =
  161. new InitiatorServiceModelSecurityTokenRequirement ();
  162. tp.InitRequirement (r);
  163. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  164. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  165. r.Properties [ReqType.IssuerBindingContextProperty] =
  166. new BindingContext (new CustomBinding (),
  167. new BindingParameterCollection ());
  168. r.MessageSecurityVersion = MessageSecurityVersion.Default.SecurityTokenVersion;
  169. ClientCredentials cred = new ClientCredentials ();
  170. ClientCredentialsSecurityTokenManager manager =
  171. new ClientCredentialsSecurityTokenManager (cred);
  172. //SecurityTokenProvider p =
  173. manager.CreateSecurityTokenProvider (r);
  174. }
  175. void CreateProviderOpenGetToken (bool getToken)
  176. {
  177. MyParameters tp = new MyParameters ();
  178. InitiatorServiceModelSecurityTokenRequirement r =
  179. new InitiatorServiceModelSecurityTokenRequirement ();
  180. tp.InitRequirement (r);
  181. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  182. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  183. r.Properties [ReqType.IssuerBindingContextProperty] =
  184. new BindingContext (new CustomBinding (new HttpTransportBindingElement ()),
  185. new BindingParameterCollection ());
  186. r.MessageSecurityVersion = MessageSecurityVersion.Default.SecurityTokenVersion;
  187. // This is required at GetToken().
  188. r.SecurityAlgorithmSuite = SecurityAlgorithmSuite.Default;
  189. ClientCredentials cred = new ClientCredentials ();
  190. ClientCredentialsSecurityTokenManager manager =
  191. new ClientCredentialsSecurityTokenManager (cred);
  192. // TLS negotiation token provider is created.
  193. SecurityTokenProvider p =
  194. manager.CreateSecurityTokenProvider (r);
  195. ((ICommunicationObject) p).Open ();
  196. if (!getToken)
  197. return;
  198. p.GetToken (TimeSpan.FromSeconds (5));
  199. }
  200. [Test]
  201. public void CreateProviderOpen ()
  202. {
  203. CreateProviderOpenGetToken (false);
  204. }
  205. [Test]
  206. [Ignore ("This ends up to fail to connect. Anyways it's too implementation dependent.")]
  207. public void CreateProviderGetToken ()
  208. {
  209. CreateProviderOpenGetToken (true);
  210. }
  211. [Test]
  212. public void CreateAuthenticator ()
  213. {
  214. MyParameters tp = new MyParameters ();
  215. InitiatorServiceModelSecurityTokenRequirement r =
  216. new InitiatorServiceModelSecurityTokenRequirement ();
  217. tp.InitRequirement (r);
  218. ClientCredentials cred = new ClientCredentials ();
  219. ClientCredentialsSecurityTokenManager manager =
  220. new ClientCredentialsSecurityTokenManager (cred);
  221. SecurityTokenResolver resolver;
  222. // SecurityTokenAuthenticator authenticator =
  223. manager.CreateSecurityTokenAuthenticator (r, out resolver);
  224. }
  225. }
  226. }