SecurityMessagePropertyTest.cs 9.7 KB

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