2
0

SslSecurityTokenParametersTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // SslSecurityTokenParametersTest.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 System.Xml;
  41. using NUnit.Framework;
  42. using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
  43. namespace MonoTests.System.ServiceModel.Security.Tokens
  44. {
  45. [TestFixture]
  46. public class SslSecurityTokenParametersTest
  47. {
  48. class MyParameters : SslSecurityTokenParameters
  49. {
  50. public bool HasAsymmetricKeyEx {
  51. get { return HasAsymmetricKey; }
  52. }
  53. public bool SupportsClientAuthenticationEx {
  54. get { return SupportsClientAuthentication; }
  55. }
  56. public bool SupportsClientWindowsIdentityEx {
  57. get { return SupportsClientWindowsIdentity; }
  58. }
  59. public bool SupportsServerAuthenticationEx {
  60. get { return SupportsServerAuthentication; }
  61. }
  62. public SecurityKeyIdentifierClause CallCreateKeyIdentifierClause (
  63. SecurityToken token, SecurityTokenReferenceStyle referenceStyle)
  64. {
  65. return CreateKeyIdentifierClause (token, referenceStyle);
  66. }
  67. public void InitRequirement (SecurityTokenRequirement requirement)
  68. {
  69. InitializeSecurityTokenRequirement (requirement);
  70. }
  71. }
  72. [Test]
  73. public void DefaultValues ()
  74. {
  75. MyParameters tp = new MyParameters ();
  76. Assert.AreEqual (SecurityTokenInclusionMode.AlwaysToRecipient, tp.InclusionMode, "#1");
  77. Assert.AreEqual (SecurityTokenReferenceStyle.Internal, tp.ReferenceStyle, "#2");
  78. Assert.AreEqual (true, tp.RequireDerivedKeys, "#3");
  79. Assert.AreEqual (false, tp.HasAsymmetricKeyEx, "#4");
  80. Assert.AreEqual (false, tp.SupportsClientAuthenticationEx, "#5");
  81. Assert.AreEqual (false, tp.SupportsClientWindowsIdentityEx, "#6");
  82. Assert.AreEqual (true, tp.SupportsServerAuthenticationEx, "#7");
  83. Assert.AreEqual (false, tp.RequireCancellation, "#2-1");
  84. Assert.AreEqual (false, tp.RequireClientCertificate, "#2-2");
  85. }
  86. [Test]
  87. public void InitializeSecurityTokenParameters ()
  88. {
  89. MyParameters tp = new MyParameters ();
  90. InitiatorServiceModelSecurityTokenRequirement r =
  91. new InitiatorServiceModelSecurityTokenRequirement ();
  92. tp.InitRequirement (r);
  93. Assert.AreEqual (ServiceModelSecurityTokenTypes.AnonymousSslnego, r.TokenType, "#1");
  94. Assert.AreEqual (false, r.Properties [ReqType.SupportSecurityContextCancellationProperty], "#2");
  95. SslSecurityTokenParameters dummy;
  96. Assert.IsTrue (r.TryGetProperty<SslSecurityTokenParameters> (ReqType.IssuedSecurityTokenParametersProperty, out dummy), "#3");
  97. }
  98. [Test]
  99. [ExpectedException (typeof (ArgumentException))]
  100. public void CreateProviderNoTargetAddress ()
  101. {
  102. MyParameters tp = new MyParameters ();
  103. InitiatorServiceModelSecurityTokenRequirement r =
  104. new InitiatorServiceModelSecurityTokenRequirement ();
  105. tp.InitRequirement (r);
  106. ClientCredentials cred = new ClientCredentials ();
  107. ClientCredentialsSecurityTokenManager manager =
  108. new ClientCredentialsSecurityTokenManager (cred);
  109. manager.CreateSecurityTokenProvider (r);
  110. }
  111. [Test]
  112. [ExpectedException (typeof (ArgumentException))]
  113. public void CreateProviderNoSecurityBindingElement ()
  114. {
  115. MyParameters tp = new MyParameters ();
  116. InitiatorServiceModelSecurityTokenRequirement r =
  117. new InitiatorServiceModelSecurityTokenRequirement ();
  118. tp.InitRequirement (r);
  119. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  120. ClientCredentials cred = new ClientCredentials ();
  121. ClientCredentialsSecurityTokenManager manager =
  122. new ClientCredentialsSecurityTokenManager (cred);
  123. manager.CreateSecurityTokenProvider (r);
  124. }
  125. [Test]
  126. [ExpectedException (typeof (ArgumentException))]
  127. public void CreateProviderNoIssuerBindingContext ()
  128. {
  129. MyParameters tp = new MyParameters ();
  130. InitiatorServiceModelSecurityTokenRequirement r =
  131. new InitiatorServiceModelSecurityTokenRequirement ();
  132. tp.InitRequirement (r);
  133. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  134. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  135. ClientCredentials cred = new ClientCredentials ();
  136. ClientCredentialsSecurityTokenManager manager =
  137. new ClientCredentialsSecurityTokenManager (cred);
  138. manager.CreateSecurityTokenProvider (r);
  139. }
  140. [Test]
  141. [ExpectedException (typeof (ArgumentException))]
  142. public void CreateProviderNoMessageSecurityVersion ()
  143. {
  144. MyParameters tp = new MyParameters ();
  145. InitiatorServiceModelSecurityTokenRequirement r =
  146. new InitiatorServiceModelSecurityTokenRequirement ();
  147. tp.InitRequirement (r);
  148. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  149. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  150. r.Properties [ReqType.IssuerBindingContextProperty] =
  151. new BindingContext (new CustomBinding (),
  152. new BindingParameterCollection ());
  153. ClientCredentials cred = new ClientCredentials ();
  154. ClientCredentialsSecurityTokenManager manager =
  155. new ClientCredentialsSecurityTokenManager (cred);
  156. manager.CreateSecurityTokenProvider (r);
  157. }
  158. [Test]
  159. public void CreateProvider ()
  160. {
  161. MyParameters tp = new MyParameters ();
  162. InitiatorServiceModelSecurityTokenRequirement r =
  163. new InitiatorServiceModelSecurityTokenRequirement ();
  164. tp.InitRequirement (r);
  165. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  166. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  167. r.Properties [ReqType.IssuerBindingContextProperty] =
  168. new BindingContext (new CustomBinding (),
  169. new BindingParameterCollection ());
  170. r.MessageSecurityVersion = MessageSecurityVersion.Default.SecurityTokenVersion;
  171. ClientCredentials cred = new ClientCredentials ();
  172. ClientCredentialsSecurityTokenManager manager =
  173. new ClientCredentialsSecurityTokenManager (cred);
  174. manager.CreateSecurityTokenProvider (r);
  175. }
  176. [Test]
  177. [Ignore ("This ends up to fail to connect. Anyways it's too implementation dependent.")]
  178. public void CreateProviderGetToken ()
  179. {
  180. MyParameters tp = new MyParameters ();
  181. InitiatorServiceModelSecurityTokenRequirement r =
  182. new InitiatorServiceModelSecurityTokenRequirement ();
  183. tp.InitRequirement (r);
  184. r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  185. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  186. r.Properties [ReqType.IssuerBindingContextProperty] =
  187. new BindingContext (new CustomBinding (new HttpTransportBindingElement ()),
  188. new BindingParameterCollection ());
  189. r.MessageSecurityVersion = MessageSecurityVersion.Default.SecurityTokenVersion;
  190. // This is required at GetToken().
  191. r.SecurityAlgorithmSuite = SecurityAlgorithmSuite.Default;
  192. ClientCredentials cred = new ClientCredentials ();
  193. ClientCredentialsSecurityTokenManager manager =
  194. new ClientCredentialsSecurityTokenManager (cred);
  195. // TLS negotiation token provider is created.
  196. SecurityTokenProvider p =
  197. manager.CreateSecurityTokenProvider (r);
  198. ((ICommunicationObject) p).Open ();
  199. p.GetToken (TimeSpan.FromSeconds (5));
  200. }
  201. [Test]
  202. [ExpectedException (typeof (NotSupportedException))]
  203. public void CreateRecipientProviderAnonymous ()
  204. {
  205. CreateRecipientProviderCore (false);
  206. }
  207. [Test]
  208. [ExpectedException (typeof (NotSupportedException))]
  209. public void CreateRecipientProviderMutual ()
  210. {
  211. CreateRecipientProviderCore (true);
  212. }
  213. void CreateRecipientProviderCore (bool mutual)
  214. {
  215. MyParameters tp = new MyParameters ();
  216. tp.RequireClientCertificate = true;
  217. RecipientServiceModelSecurityTokenRequirement r =
  218. new RecipientServiceModelSecurityTokenRequirement ();
  219. tp.InitRequirement (r);
  220. r.ListenUri = new Uri ("http://localhost:8080");
  221. r.SecurityBindingElement = new SymmetricSecurityBindingElement ();
  222. r.Properties [ReqType.IssuerBindingContextProperty] =
  223. new BindingContext (new CustomBinding (),
  224. new BindingParameterCollection ());
  225. r.MessageSecurityVersion = MessageSecurityVersion.Default.SecurityTokenVersion;
  226. ClientCredentials cred = new ClientCredentials ();
  227. ClientCredentialsSecurityTokenManager manager =
  228. new ClientCredentialsSecurityTokenManager (cred);
  229. manager.CreateSecurityTokenProvider (r);
  230. }
  231. [Test]
  232. public void CreateKeyIdentifierClauseSCT ()
  233. {
  234. MyParameters tp = new MyParameters ();
  235. SecurityContextSecurityToken sct =
  236. new SecurityContextSecurityToken (new UniqueId (), new byte [32], DateTime.MinValue, DateTime.MaxValue);
  237. SecurityKeyIdentifierClause kic =
  238. tp.CallCreateKeyIdentifierClause (sct, SecurityTokenReferenceStyle.Internal);
  239. Assert.IsTrue (kic is LocalIdKeyIdentifierClause, "#1");
  240. SecurityContextKeyIdentifierClause scic = tp.CallCreateKeyIdentifierClause (sct, SecurityTokenReferenceStyle.External)
  241. as SecurityContextKeyIdentifierClause;
  242. Assert.IsNotNull (scic, "#2");
  243. Assert.IsNull (scic.Generation, "#3");
  244. }
  245. }
  246. }