SecurityMessagePropertyTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // SecurityMessagePropertyTest.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.Net;
  31. using System.Net.Security;
  32. using System.Security.Principal;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Channels;
  36. using System.ServiceModel.Description;
  37. using System.ServiceModel.Security;
  38. using System.ServiceModel.Security.Tokens;
  39. using System.Security.Cryptography.Xml;
  40. using System.Threading;
  41. using NUnit.Framework;
  42. using MonoTests.System.ServiceModel.Channels;
  43. using MonoTests.Helpers;
  44. namespace MonoTests.System.ServiceModel.Security
  45. {
  46. [TestFixture]
  47. public class SecurityMessagePropertyTest
  48. {
  49. static X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  50. static X509Certificate2 cert2 = new X509Certificate2 ("Test/Resources/test2.pfx", "mono");
  51. [ServiceContract]
  52. public interface ICalc
  53. {
  54. [OperationContract]
  55. int Sum (int a, int b);
  56. [OperationContract (AsyncPattern = true)]
  57. IAsyncResult BeginSum (int a, int b, AsyncCallback cb, object state);
  58. int EndSum (IAsyncResult result);
  59. }
  60. public class CalcProxy : ClientBase<ICalc>, ICalc
  61. {
  62. public CalcProxy (Binding binding, EndpointAddress address)
  63. : base (binding, address)
  64. {
  65. }
  66. public int Sum (int a, int b)
  67. {
  68. return Channel.Sum (a, b);
  69. }
  70. public IAsyncResult BeginSum (int a, int b, AsyncCallback cb, object state)
  71. {
  72. return Channel.BeginSum (a, b, cb, state);
  73. }
  74. public int EndSum (IAsyncResult result)
  75. {
  76. return Channel.EndSum (result);
  77. }
  78. }
  79. public class CalcService : ICalc
  80. {
  81. public int Sum (int a, int b)
  82. {
  83. return a + b;
  84. }
  85. public IAsyncResult BeginSum (int a, int b, AsyncCallback cb, object state)
  86. {
  87. return new CalcAsyncResult (a, b, cb, state);
  88. }
  89. public int EndSum (IAsyncResult result)
  90. {
  91. CalcAsyncResult c = (CalcAsyncResult) result;
  92. return c.A + c.B;
  93. }
  94. }
  95. class CalcAsyncResult : IAsyncResult
  96. {
  97. public int A, B;
  98. AsyncCallback callback;
  99. object state;
  100. public CalcAsyncResult (int a, int b, AsyncCallback cb, object state)
  101. {
  102. A = a;
  103. B = b;
  104. callback = cb;
  105. this.state = state;
  106. }
  107. public object AsyncState {
  108. get { return state; }
  109. }
  110. public WaitHandle AsyncWaitHandle {
  111. get { return null; }
  112. }
  113. public bool CompletedSynchronously {
  114. get { return true; }
  115. }
  116. public bool IsCompleted {
  117. get { return true; }
  118. }
  119. }
  120. [Test]
  121. public void GetOrCreateNonSecureMessage ()
  122. {
  123. Message m = Message.CreateMessage (MessageVersion.Default, "urn:myaction");
  124. SecurityMessageProperty p =
  125. SecurityMessageProperty.GetOrCreate (m);
  126. Assert.IsNull (p.InitiatorToken, "#1");
  127. Assert.IsNull (p.RecipientToken, "#2");
  128. Assert.IsNull (p.ProtectionToken, "#3");
  129. Assert.IsNull (p.TransportToken, "#4");
  130. Assert.IsNull (p.ExternalAuthorizationPolicies, "#5");
  131. // Assert.AreEqual (0, p.ExternalAuthorizationPolicies.Count, "#5");
  132. Assert.IsFalse (p.HasIncomingSupportingTokens, "#6");
  133. Assert.IsNotNull (p.IncomingSupportingTokens, "#6-2");
  134. Assert.AreEqual ("_", p.SenderIdPrefix, "#6-3");
  135. ServiceSecurityContext ssc = p.ServiceSecurityContext;
  136. Assert.IsNotNull (ssc, "#7");
  137. // not sure if it is worthy of testing though ...
  138. GenericIdentity identity = ssc.PrimaryIdentity as GenericIdentity;
  139. Assert.IsNotNull (identity, "#8-1");
  140. Assert.AreEqual ("", identity.Name, "#8-2");
  141. Assert.AreEqual ("", identity.AuthenticationType, "#8-3");
  142. Assert.AreEqual (0, ssc.AuthorizationPolicies.Count, "#9");
  143. Assert.IsTrue (ssc.IsAnonymous, "#10");
  144. }
  145. [Test]
  146. [Ignore ("This hangs on .NET")]
  147. // not sure how "good" this test is ... if it fails at
  148. // service side, it just results in timeout error.
  149. // The assertion makes sure that it passes all the tests, but
  150. // in case it failed, there is almost no hint ...
  151. public void GetOrCreateSecureMessage ()
  152. {
  153. bool passed = false;
  154. ServiceHost host = new ServiceHost (typeof (CalcService));
  155. InterceptorRequestContextHandler handler = delegate (MessageBuffer src) {
  156. Message msg = src.CreateMessage ();
  157. GetOrCreateSecureMessageAtService (msg);
  158. passed = true;
  159. };
  160. try {
  161. SymmetricSecurityBindingElement clisbe =
  162. new SymmetricSecurityBindingElement ();
  163. clisbe.ProtectionTokenParameters =
  164. new X509SecurityTokenParameters (X509KeyIdentifierClauseType.Thumbprint, SecurityTokenInclusionMode.Never);
  165. BindingElement transport = new HttpTransportBindingElement ();
  166. BindingElement sintercept = new InterceptorBindingElement (handler);
  167. CustomBinding b_res = new CustomBinding (clisbe,
  168. sintercept,
  169. transport);
  170. b_res.ReceiveTimeout = b_res.SendTimeout = TimeSpan.FromSeconds (5);
  171. host.AddServiceEndpoint (typeof (ICalc), b_res, "http://localhost:" + NetworkHelpers.FindFreePort ());
  172. ServiceCredentials cred = new ServiceCredentials ();
  173. cred.ServiceCertificate.Certificate = cert;
  174. cred.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
  175. host.Description.Behaviors.Add (cred);
  176. host.Open ();
  177. ProcessClient ();
  178. } finally {
  179. if (host.State == CommunicationState.Opened)
  180. host.Close ();
  181. }
  182. if (!passed)
  183. Assert.Fail ("Didn't pass the interceptor.");
  184. }
  185. void ProcessClient ()
  186. {
  187. SymmetricSecurityBindingElement svcsbe =
  188. new SymmetricSecurityBindingElement ();
  189. svcsbe.ProtectionTokenParameters =
  190. new X509SecurityTokenParameters (X509KeyIdentifierClauseType.Thumbprint, SecurityTokenInclusionMode.Never);
  191. BindingElement cintercept = new InterceptorBindingElement (null);
  192. CustomBinding b_req = new CustomBinding (svcsbe,
  193. cintercept,
  194. new HttpTransportBindingElement ());
  195. b_req.ReceiveTimeout = b_req.SendTimeout = TimeSpan.FromSeconds (5);
  196. EndpointAddress remaddr = new EndpointAddress (
  197. new Uri ("http://localhost:" + NetworkHelpers.FindFreePort ()),
  198. new X509CertificateEndpointIdentity (cert));
  199. CalcProxy proxy = new CalcProxy (b_req, remaddr);
  200. proxy.ClientCredentials.ClientCertificate.Certificate = cert2;
  201. proxy.Sum (1, 2);
  202. proxy.Close ();
  203. }
  204. static void GetOrCreateSecureMessageAtClient (Message msg)
  205. {
  206. foreach (object o in msg.Properties)
  207. if (o is SecurityMessageProperty)
  208. Assert.Fail ("The input msg should not contain SecurityMessageProperty yet.");
  209. SecurityMessageProperty p = SecurityMessageProperty.GetOrCreate (msg);
  210. Assert.AreEqual (null, p.InitiatorToken, "#1");
  211. Assert.AreEqual (null, p.RecipientToken, "#2");
  212. Assert.IsNull (p.ProtectionToken, "#3");
  213. Assert.IsNull (p.TransportToken, "#4");
  214. Assert.IsNull (p.ExternalAuthorizationPolicies, "#5");
  215. // Assert.AreEqual (0, p.ExternalAuthorizationPolicies.Count, "#5");
  216. Assert.IsFalse (p.HasIncomingSupportingTokens, "#6");
  217. Assert.IsNotNull (p.IncomingSupportingTokens, "#6-2");
  218. Assert.AreEqual ("_", p.SenderIdPrefix, "#6-3");
  219. ServiceSecurityContext ssc = p.ServiceSecurityContext;
  220. Assert.IsNotNull (ssc, "#7");
  221. // not sure if it is worthy of testing though ...
  222. GenericIdentity identity = ssc.PrimaryIdentity as GenericIdentity;
  223. Assert.IsNotNull (identity, "#8-1");
  224. Assert.AreEqual ("", identity.Name, "#8-2");
  225. Assert.AreEqual ("", identity.AuthenticationType, "#8-3");
  226. Assert.AreEqual (0, ssc.AuthorizationPolicies.Count, "#9");
  227. Assert.IsTrue (ssc.IsAnonymous, "#10");
  228. }
  229. static void GetOrCreateSecureMessageAtService (Message msg)
  230. {
  231. Assert.IsNull (msg.Properties.Security, "#0");
  232. foreach (object o in msg.Properties)
  233. if (o is SecurityMessageProperty)
  234. Assert.Fail ("The input msg should not contain SecurityMessageProperty yet.");
  235. SecurityMessageProperty p = SecurityMessageProperty.GetOrCreate (msg);
  236. Assert.IsNotNull (msg.Properties.Security, "#0-2");
  237. Assert.AreEqual (null, p.InitiatorToken, "#1");
  238. Assert.AreEqual (null, p.RecipientToken, "#2");
  239. Assert.IsNull (p.ProtectionToken, "#3");
  240. Assert.IsNull (p.TransportToken, "#4");
  241. Assert.IsNull (p.ExternalAuthorizationPolicies, "#5");
  242. // Assert.AreEqual (0, p.ExternalAuthorizationPolicies.Count, "#5");
  243. Assert.IsFalse (p.HasIncomingSupportingTokens, "#6");
  244. Assert.IsNotNull (p.IncomingSupportingTokens, "#6-2");
  245. Assert.AreEqual ("_", p.SenderIdPrefix, "#6-3");
  246. ServiceSecurityContext ssc = p.ServiceSecurityContext;
  247. Assert.IsNotNull (ssc, "#7");
  248. // not sure if it is worthy of testing though ...
  249. GenericIdentity identity = ssc.PrimaryIdentity as GenericIdentity;
  250. Assert.IsNotNull (identity, "#8-1");
  251. Assert.AreEqual ("", identity.Name, "#8-2");
  252. Assert.AreEqual ("", identity.AuthenticationType, "#8-3");
  253. Assert.AreEqual (0, ssc.AuthorizationPolicies.Count, "#9");
  254. Assert.IsTrue (ssc.IsAnonymous, "#10");
  255. }
  256. }
  257. }