BindingTestAssertions.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. //
  2. // BindingTestAssertions.cs
  3. //
  4. // Author:
  5. // Martin Baulig <[email protected]>
  6. //
  7. // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Net;
  28. using System.Net.Security;
  29. using System.Xml;
  30. using System.Xml.XPath;
  31. using System.Text;
  32. using System.Collections.Generic;
  33. using System.ServiceModel;
  34. using System.ServiceModel.Security;
  35. using System.ServiceModel.Channels;
  36. using System.ServiceModel.Description;
  37. using NUnit.Framework;
  38. using NUnit.Framework.Constraints;
  39. using QName = System.Xml.XmlQualifiedName;
  40. using WS = System.Web.Services.Description;
  41. namespace MonoTests.System.ServiceModel.MetadataTests {
  42. public static class BindingTestAssertions {
  43. const string WspNamespace = "http://schemas.xmlsoap.org/ws/2004/09/policy";
  44. const string WsuNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
  45. const string MsbNamespace = "http://schemas.microsoft.com/ws/06/2004/mspolicy/netbinary1";
  46. const string WsawNamespace = "http://www.w3.org/2006/05/addressing/wsdl";
  47. const string MsfNamespace = "http://schemas.microsoft.com/ws/2006/05/framing/policy";
  48. const string SpNamespace = "http://schemas.xmlsoap.org/ws/2005/07/securitypolicy";
  49. const string WsrmNamespace = "http://schemas.xmlsoap.org/ws/2005/02/rm/policy";
  50. const string HttpNamespace = "http://schemas.microsoft.com/ws/06/2004/policy/http";
  51. const string WsomaNamespace = "http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization";
  52. const string Wsa10Namespace = "http://www.w3.org/2005/08/addressing";
  53. static readonly QName BinaryEncodingQName = new QName ("BinaryEncoding", MsbNamespace);
  54. static readonly QName UsingAddressingQName = new QName ("UsingAddressing", WsawNamespace);
  55. static readonly QName StreamedTransferQName = new QName ("Streamed", MsfNamespace);
  56. static readonly QName ReliableSessionQName = new QName ("RMAssertion", WsrmNamespace);
  57. static readonly QName TransportBindingQName = new QName ("TransportBinding", SpNamespace);
  58. static readonly QName AsymmetricBindingQName = new QName ("AsymmetricBinding", SpNamespace);
  59. static readonly QName SymmetricBindingQName = new QName ("SymmetricBinding", SpNamespace);
  60. static readonly QName EndorsingSupportingQName = new QName ("EndorsingSupportingTokens", SpNamespace);
  61. static readonly QName SignedSupportingQName = new QName ("SignedSupportingTokens", SpNamespace);
  62. static readonly QName Wss10QName = new QName ("Wss10", SpNamespace);
  63. static readonly QName Wss11QName = new QName ("Wss11", SpNamespace);
  64. static readonly QName Trust10QName = new QName ("Trust10", SpNamespace);
  65. static readonly QName NtlmAuthenticationQName = new QName ("NtlmAuthentication", HttpNamespace);
  66. static readonly QName MtomEncodingQName = new QName ("OptimizedMimeSerialization", WsomaNamespace);
  67. public static void CheckImportErrors (WsdlImporter importer, TestLabel label)
  68. {
  69. bool foundErrors = false;
  70. foreach (var error in importer.Errors) {
  71. if (error.IsWarning)
  72. Console.WriteLine ("WARNING ({0}): {1}", label, error.Message);
  73. else {
  74. Console.WriteLine ("ERROR ({0}): {1}", label, error.Message);
  75. foundErrors = true;
  76. }
  77. }
  78. if (foundErrors)
  79. Assert.Fail ("Found import errors", label);
  80. }
  81. static void CheckSoapBinding (object extension, string transport, TestLabel label)
  82. {
  83. label.EnterScope ("soap");
  84. Assert.That (extension, Is.AssignableTo<WS.SoapBinding>(), label.Get ());
  85. var soap = (WS.SoapBinding)extension;
  86. Assert.That (soap.Style, Is.EqualTo (WS.SoapBindingStyle.Document), label.Get ());
  87. Assert.That (soap.Transport, Is.EqualTo (transport), label.Get ());
  88. Assert.That (soap.Required, Is.False, label.Get ());
  89. label.LeaveScope ();
  90. }
  91. public static void CheckBasicHttpBinding (
  92. Binding binding, string scheme, BasicHttpSecurityMode security,
  93. WSMessageEncoding encoding, HttpClientCredentialType clientCred,
  94. AuthenticationSchemes authScheme, TestLabel label)
  95. {
  96. label.EnterScope ("http");
  97. if (security == BasicHttpSecurityMode.Message) {
  98. Assert.IsInstanceOfType (typeof(CustomBinding), binding, label.Get ());
  99. } else {
  100. Assert.IsInstanceOfType (typeof(BasicHttpBinding), binding, label.Get ());
  101. var basicHttp = (BasicHttpBinding)binding;
  102. Assert.That (basicHttp.EnvelopeVersion, Is.EqualTo (EnvelopeVersion.Soap11), label.Get ());
  103. Assert.That (basicHttp.MessageVersion, Is.EqualTo (MessageVersion.Soap11), label.Get ());
  104. Assert.That (basicHttp.Scheme, Is.EqualTo (scheme), label.Get ());
  105. Assert.That (basicHttp.TransferMode, Is.EqualTo (TransferMode.Buffered), label.Get ());
  106. Assert.That (basicHttp.MessageEncoding, Is.EqualTo (encoding), label.Get ());
  107. Assert.That (basicHttp.Security, Is.Not.Null, label.Get ());
  108. Assert.That (basicHttp.Security.Mode, Is.EqualTo (security), label.Get ());
  109. Assert.That (basicHttp.Security.Transport.ClientCredentialType, Is.EqualTo (clientCred), label.Get ());
  110. Assert.That (basicHttp.Security.Message.AlgorithmSuite, Is.EqualTo (SecurityAlgorithmSuite.Basic256), label.Get ());
  111. }
  112. label.EnterScope ("elements");
  113. var elements = binding.CreateBindingElements ();
  114. Assert.That (elements, Is.Not.Null, label.Get ());
  115. if ((security == BasicHttpSecurityMode.Message) ||
  116. (security == BasicHttpSecurityMode.TransportWithMessageCredential))
  117. Assert.That (elements.Count, Is.EqualTo (3), label.Get ());
  118. else
  119. Assert.That (elements.Count, Is.EqualTo (2), label.Get ());
  120. TextMessageEncodingBindingElement textElement = null;
  121. TransportSecurityBindingElement securityElement = null;
  122. HttpTransportBindingElement transportElement = null;
  123. AsymmetricSecurityBindingElement asymmSecurityElement = null;
  124. MtomMessageEncodingBindingElement mtomElement = null;
  125. foreach (var element in elements) {
  126. if (element is TextMessageEncodingBindingElement)
  127. textElement = (TextMessageEncodingBindingElement)element;
  128. else if (element is HttpTransportBindingElement)
  129. transportElement = (HttpTransportBindingElement)element;
  130. else if (element is TransportSecurityBindingElement)
  131. securityElement = (TransportSecurityBindingElement)element;
  132. else if (element is AsymmetricSecurityBindingElement)
  133. asymmSecurityElement = (AsymmetricSecurityBindingElement)element;
  134. else if (element is MtomMessageEncodingBindingElement)
  135. mtomElement = (MtomMessageEncodingBindingElement)element;
  136. else
  137. Assert.Fail (string.Format (
  138. "Unknown element: {0}", element.GetType ()), label.Get ());
  139. }
  140. label.EnterScope ("text");
  141. if (encoding == WSMessageEncoding.Text) {
  142. Assert.That (textElement, Is.Not.Null, label.Get ());
  143. Assert.IsInstanceOfType (typeof(UTF8Encoding), textElement.WriteEncoding, label.Get ());
  144. } else {
  145. Assert.That (textElement, Is.Null, label.Get ());
  146. }
  147. label.LeaveScope ();
  148. label.EnterScope ("mtom");
  149. if (encoding == WSMessageEncoding.Mtom) {
  150. Assert.That (mtomElement, Is.Not.Null, label.Get ());
  151. } else {
  152. Assert.That (mtomElement, Is.Null, label.Get ());
  153. }
  154. label.LeaveScope ();
  155. label.EnterScope ("security");
  156. if (security == BasicHttpSecurityMode.TransportWithMessageCredential) {
  157. Assert.That (securityElement, Is.Not.Null, label.Get ());
  158. Assert.That (securityElement.SecurityHeaderLayout,
  159. Is.EqualTo (SecurityHeaderLayout.Lax), label.Get ());
  160. } else {
  161. Assert.That (securityElement, Is.Null, label.Get ());
  162. }
  163. label.LeaveScope ();
  164. label.EnterScope ("asymmetric");
  165. if (security == BasicHttpSecurityMode.Message) {
  166. Assert.That (asymmSecurityElement, Is.Not.Null, label.Get ());
  167. } else {
  168. Assert.That (asymmSecurityElement, Is.Null, label.Get ());
  169. }
  170. label.LeaveScope ();
  171. label.EnterScope ("transport");
  172. Assert.That (transportElement, Is.Not.Null, label.Get ());
  173. Assert.That (transportElement.Realm, Is.Empty, label.Get ());
  174. Assert.That (transportElement.Scheme, Is.EqualTo (scheme), label.Get ());
  175. Assert.That (transportElement.TransferMode, Is.EqualTo (TransferMode.Buffered), label.Get ());
  176. label.EnterScope ("auth");
  177. Assert.That (transportElement.AuthenticationScheme, Is.EqualTo (authScheme), label.Get ());
  178. label.LeaveScope (); // auth
  179. label.LeaveScope (); // transport
  180. label.LeaveScope (); // elements
  181. label.LeaveScope (); // http
  182. }
  183. static void CheckEndpoint (ServiceEndpoint endpoint, string uri, TestLabel label)
  184. {
  185. label.EnterScope ("endpoint");
  186. Assert.That (endpoint.ListenUri, Is.EqualTo (new Uri (uri)), label.Get ());
  187. Assert.That (endpoint.ListenUriMode, Is.EqualTo (ListenUriMode.Explicit), label.Get ());
  188. Assert.That (endpoint.Contract, Is.Not.Null, label.Get ());
  189. Assert.That (endpoint.Contract.Name, Is.EqualTo ("MyContract"), label.Get ());
  190. Assert.That (endpoint.Address, Is.Not.Null, label.Get ());
  191. Assert.That (endpoint.Address.Uri, Is.EqualTo (new Uri (uri)), label.Get ());
  192. Assert.That (endpoint.Address.Identity, Is.Null, label.Get ());
  193. Assert.That (endpoint.Address.Headers, Is.Not.Null, label.Get ());
  194. Assert.That (endpoint.Address.Headers.Count, Is.EqualTo (0), label.Get ());
  195. label.LeaveScope ();
  196. }
  197. public static void BasicHttpBinding (
  198. TestContext context, MetadataSet doc, WSMessageEncoding encoding, TestLabel label)
  199. {
  200. BasicHttpBinding (
  201. context, doc, BasicHttpSecurityMode.None, encoding,
  202. HttpClientCredentialType.None, AuthenticationSchemes.Anonymous,
  203. label);
  204. }
  205. public static void BasicHttpBinding (
  206. TestContext context, MetadataSet doc, BasicHttpSecurityMode security, TestLabel label)
  207. {
  208. BasicHttpBinding (
  209. context, doc, security, WSMessageEncoding.Text,
  210. HttpClientCredentialType.None, AuthenticationSchemes.Anonymous,
  211. label);
  212. }
  213. public static void BasicHttpBinding (
  214. TestContext context, MetadataSet doc, BasicHttpSecurityMode security,
  215. WSMessageEncoding encoding, HttpClientCredentialType clientCred,
  216. AuthenticationSchemes authScheme, TestLabel label)
  217. {
  218. label.EnterScope ("basicHttpBinding");
  219. BasicHttpBinding_inner (
  220. context, doc, security, encoding, clientCred,
  221. authScheme, false, label);
  222. label.LeaveScope ();
  223. }
  224. public static void BasicHttpsBinding (
  225. TestContext context, MetadataSet doc, BasicHttpSecurityMode security,
  226. WSMessageEncoding encoding, HttpClientCredentialType clientCred,
  227. AuthenticationSchemes authScheme, TestLabel label)
  228. {
  229. label.EnterScope ("basicHttpsBinding");
  230. BasicHttpBinding_inner (
  231. context, doc, security, encoding, clientCred,
  232. authScheme, true, label);
  233. label.LeaveScope ();
  234. }
  235. static void BasicHttpBinding_inner (
  236. TestContext context, MetadataSet doc, BasicHttpSecurityMode security,
  237. WSMessageEncoding encoding, HttpClientCredentialType clientCred,
  238. AuthenticationSchemes authScheme, bool isHttps, TestLabel label)
  239. {
  240. var sd = (WS.ServiceDescription)doc.MetadataSections [0].Metadata;
  241. label.EnterScope ("wsdl");
  242. label.EnterScope ("bindings");
  243. Assert.That (sd.Bindings.Count, Is.EqualTo (1), label.Get ());
  244. var binding = sd.Bindings [0];
  245. Assert.That (binding.ExtensibleAttributes, Is.Null, label.Get ());
  246. Assert.That (binding.Extensions, Is.Not.Null, label.Get ());
  247. bool hasPolicyXml;
  248. switch (security) {
  249. case BasicHttpSecurityMode.None:
  250. if (isHttps)
  251. throw new InvalidOperationException ();
  252. hasPolicyXml = encoding == WSMessageEncoding.Mtom;
  253. break;
  254. case BasicHttpSecurityMode.Message:
  255. case BasicHttpSecurityMode.Transport:
  256. case BasicHttpSecurityMode.TransportWithMessageCredential:
  257. if (encoding == WSMessageEncoding.Mtom)
  258. throw new InvalidOperationException ();
  259. hasPolicyXml = true;
  260. break;
  261. case BasicHttpSecurityMode.TransportCredentialOnly:
  262. if (isHttps)
  263. throw new InvalidOperationException ();
  264. hasPolicyXml = true;
  265. break;
  266. default:
  267. throw new InvalidOperationException ();
  268. }
  269. label.LeaveScope ();
  270. WS.SoapBinding soap = null;
  271. XmlElement xml = null;
  272. foreach (var ext in binding.Extensions) {
  273. if (ext is WS.SoapBinding)
  274. soap = (WS.SoapBinding)ext;
  275. else if (ext is XmlElement)
  276. xml = (XmlElement)ext;
  277. }
  278. CheckSoapBinding (soap, WS.SoapBinding.HttpTransport, label);
  279. label.LeaveScope ();
  280. label.EnterScope ("policy-xml");
  281. if (!hasPolicyXml)
  282. Assert.That (xml, Is.Null, label.Get ());
  283. else {
  284. Assert.That (xml, Is.Not.Null, label.Get ());
  285. var assertions = AssertPolicy (sd, xml, label);
  286. Assert.That (assertions, Is.Not.Null, label.Get ());
  287. if (clientCred == HttpClientCredentialType.Ntlm)
  288. AssertPolicy (assertions, NtlmAuthenticationQName, label);
  289. if (encoding == WSMessageEncoding.Mtom)
  290. AssertPolicy (assertions, MtomEncodingQName, label);
  291. switch (security) {
  292. case BasicHttpSecurityMode.Message:
  293. AssertPolicy (assertions, AsymmetricBindingQName, label);
  294. AssertPolicy (assertions, Wss10QName, label);
  295. break;
  296. case BasicHttpSecurityMode.Transport:
  297. AssertPolicy (assertions, TransportBindingQName, label);
  298. break;
  299. case BasicHttpSecurityMode.TransportWithMessageCredential:
  300. AssertPolicy (assertions, SignedSupportingQName, label);
  301. AssertPolicy (assertions, TransportBindingQName, label);
  302. AssertPolicy (assertions, Wss10QName, label);
  303. break;
  304. default:
  305. break;
  306. }
  307. Assert.That (assertions.Count, Is.EqualTo (0), label.Get ());
  308. }
  309. label.LeaveScope ();
  310. label.EnterScope ("services");
  311. Assert.That (sd.Services, Is.Not.Null, label.Get ());
  312. Assert.That (sd.Services.Count, Is.EqualTo (1), label.Get ());
  313. var service = sd.Services [0];
  314. Assert.That (service.Ports, Is.Not.Null, label.Get ());
  315. Assert.That (service.Ports.Count, Is.EqualTo (1), label.Get ());
  316. var port = service.Ports [0];
  317. label.EnterScope ("port");
  318. Assert.That (port.Extensions, Is.Not.Null, label.Get ());
  319. Assert.That (port.Extensions.Count, Is.EqualTo (1), label.Get ());
  320. WS.SoapAddressBinding soap_addr_binding = null;
  321. foreach (var extension in port.Extensions) {
  322. if (extension is WS.SoapAddressBinding)
  323. soap_addr_binding = (WS.SoapAddressBinding)extension;
  324. else
  325. Assert.Fail (label.Get ());
  326. }
  327. Assert.That (soap_addr_binding, Is.Not.Null, label.Get ());
  328. label.LeaveScope ();
  329. label.LeaveScope (); // wsdl
  330. var importer = new WsdlImporter (doc);
  331. label.EnterScope ("bindings");
  332. var bindings = importer.ImportAllBindings ();
  333. CheckImportErrors (importer, label);
  334. Assert.That (bindings, Is.Not.Null, label.Get ());
  335. Assert.That (bindings.Count, Is.EqualTo (1), label.Get ());
  336. string scheme;
  337. if ((security == BasicHttpSecurityMode.Transport) ||
  338. (security == BasicHttpSecurityMode.TransportWithMessageCredential))
  339. scheme = "https";
  340. else
  341. scheme = "http";
  342. CheckBasicHttpBinding (
  343. bindings [0], scheme, security, encoding, clientCred,
  344. authScheme, label);
  345. label.LeaveScope ();
  346. label.EnterScope ("endpoints");
  347. var endpoints = importer.ImportAllEndpoints ();
  348. CheckImportErrors (importer, label);
  349. Assert.That (endpoints, Is.Not.Null, label.Get ());
  350. Assert.That (endpoints.Count, Is.EqualTo (1), label.Get ());
  351. var uri = isHttps ? MetadataSamples.HttpsUri : MetadataSamples.HttpUri;
  352. CheckEndpoint (endpoints [0], uri, label);
  353. label.LeaveScope ();
  354. }
  355. public static void CheckNetTcpBinding (
  356. Binding binding, SecurityMode security, bool reliableSession,
  357. TransferMode transferMode, TestLabel label)
  358. {
  359. label.EnterScope ("net-tcp");
  360. if (security == SecurityMode.Message) {
  361. Assert.IsInstanceOfType (typeof(CustomBinding), binding, label.Get ());
  362. } else {
  363. Assert.IsInstanceOfType (typeof(NetTcpBinding), binding, label.Get ());
  364. var netTcp = (NetTcpBinding)binding;
  365. Assert.That (netTcp.EnvelopeVersion, Is.EqualTo (EnvelopeVersion.Soap12), label.Get ());
  366. Assert.That (netTcp.MessageVersion, Is.EqualTo (MessageVersion.Soap12WSAddressing10), label.Get ());
  367. Assert.That (netTcp.Scheme, Is.EqualTo ("net.tcp"), label.Get ());
  368. Assert.That (netTcp.TransferMode, Is.EqualTo (transferMode), label.Get ());
  369. label.EnterScope ("security");
  370. Assert.That (netTcp.Security, Is.Not.Null, label.Get ());
  371. Assert.That (netTcp.Security.Mode, Is.EqualTo (security), label.Get ());
  372. Assert.That (netTcp.Security.Transport, Is.Not.Null, label.Get ());
  373. Assert.That (netTcp.Security.Transport.ProtectionLevel, Is.EqualTo (ProtectionLevel.EncryptAndSign), label.Get ());
  374. Assert.That (netTcp.Security.Transport.ClientCredentialType, Is.EqualTo (TcpClientCredentialType.Windows), label.Get ());
  375. label.LeaveScope ();
  376. }
  377. label.EnterScope ("elements");
  378. var elements = binding.CreateBindingElements ();
  379. Assert.That (elements, Is.Not.Null, label.Get ());
  380. TcpTransportBindingElement transportElement = null;
  381. TransactionFlowBindingElement transactionFlowElement = null;
  382. BinaryMessageEncodingBindingElement encodingElement = null;
  383. WindowsStreamSecurityBindingElement windowsStreamElement = null;
  384. ReliableSessionBindingElement reliableSessionElement = null;
  385. TransportSecurityBindingElement transportSecurityElement = null;
  386. SslStreamSecurityBindingElement sslStreamElement = null;
  387. SymmetricSecurityBindingElement symmSecurityElement = null;
  388. foreach (var element in elements) {
  389. if (element is TcpTransportBindingElement)
  390. transportElement = (TcpTransportBindingElement)element;
  391. else if (element is TransactionFlowBindingElement)
  392. transactionFlowElement = (TransactionFlowBindingElement)element;
  393. else if (element is BinaryMessageEncodingBindingElement)
  394. encodingElement = (BinaryMessageEncodingBindingElement)element;
  395. else if (element is WindowsStreamSecurityBindingElement)
  396. windowsStreamElement = (WindowsStreamSecurityBindingElement)element;
  397. else if (element is ReliableSessionBindingElement)
  398. reliableSessionElement = (ReliableSessionBindingElement)element;
  399. else if (element is TransportSecurityBindingElement)
  400. transportSecurityElement = (TransportSecurityBindingElement)element;
  401. else if (element is SslStreamSecurityBindingElement)
  402. sslStreamElement = (SslStreamSecurityBindingElement)element;
  403. else if (element is SymmetricSecurityBindingElement)
  404. symmSecurityElement = (SymmetricSecurityBindingElement)element;
  405. else
  406. Assert.Fail (string.Format (
  407. "Unknown element `{0}'.", element.GetType ()), label.Get ());
  408. }
  409. label.EnterScope ("windows-stream");
  410. if (security == SecurityMode.Transport) {
  411. Assert.That (windowsStreamElement, Is.Not.Null, label.Get ());
  412. Assert.That (windowsStreamElement.ProtectionLevel, Is.EqualTo (ProtectionLevel.EncryptAndSign), label.Get ());
  413. } else {
  414. Assert.That (windowsStreamElement, Is.Null, label.Get ());
  415. }
  416. label.LeaveScope ();
  417. label.EnterScope ("reliable-session");
  418. if (reliableSession) {
  419. Assert.That (reliableSessionElement, Is.Not.Null, label.Get ());
  420. } else {
  421. Assert.That (reliableSessionElement, Is.Null, label.Get ());
  422. }
  423. label.LeaveScope ();
  424. label.EnterScope ("encoding");
  425. Assert.That (encodingElement, Is.Not.Null, label.Get ());
  426. label.LeaveScope ();
  427. label.EnterScope ("transaction");
  428. if (security == SecurityMode.Message) {
  429. Assert.That (transactionFlowElement, Is.Null, label.Get ());
  430. } else {
  431. Assert.That (transactionFlowElement, Is.Not.Null, label.Get ());
  432. }
  433. label.LeaveScope ();
  434. label.EnterScope ("transport");
  435. Assert.That (transportElement, Is.Not.Null, label.Get ());
  436. Assert.That (transportElement.Scheme, Is.EqualTo ("net.tcp"), label.Get ());
  437. Assert.That (transportElement.TransferMode, Is.EqualTo (transferMode), label.Get ());
  438. label.LeaveScope (); // transport
  439. label.EnterScope ("security");
  440. switch (security) {
  441. case SecurityMode.None:
  442. case SecurityMode.Transport:
  443. Assert.That (transportSecurityElement, Is.Null, label.Get ());
  444. Assert.That (sslStreamElement, Is.Null, label.Get ());
  445. Assert.That (symmSecurityElement, Is.Null, label.Get ());
  446. break;
  447. case SecurityMode.TransportWithMessageCredential:
  448. Assert.That (transportSecurityElement, Is.Not.Null, label.Get ());
  449. Assert.That (sslStreamElement, Is.Not.Null, label.Get ());
  450. Assert.That (symmSecurityElement, Is.Null, label.Get ());
  451. break;
  452. case SecurityMode.Message:
  453. Assert.That (transportSecurityElement, Is.Null, label.Get ());
  454. Assert.That (sslStreamElement, Is.Null, label.Get ());
  455. Assert.That (symmSecurityElement, Is.Not.Null, label.Get ());
  456. break;
  457. default:
  458. throw new InvalidOperationException ();
  459. }
  460. label.LeaveScope ();
  461. label.LeaveScope (); // elements
  462. label.LeaveScope (); // net-tcp
  463. }
  464. public static void NetTcpBinding (
  465. TestContext context, MetadataSet doc, SecurityMode security,
  466. bool reliableSession, TransferMode transferMode, TestLabel label)
  467. {
  468. label.EnterScope ("netTcpBinding");
  469. var sd = (WS.ServiceDescription)doc.MetadataSections [0].Metadata;
  470. label.EnterScope ("wsdl");
  471. label.EnterScope ("bindings");
  472. Assert.That (sd.Bindings.Count, Is.EqualTo (1), label.Get ());
  473. var binding = sd.Bindings [0];
  474. Assert.That (binding.ExtensibleAttributes, Is.Null, label.Get ());
  475. Assert.That (binding.Extensions, Is.Not.Null, label.Get ());
  476. WS.Soap12Binding soap = null;
  477. XmlElement xml = null;
  478. foreach (var ext in binding.Extensions) {
  479. if (ext is WS.Soap12Binding)
  480. soap = (WS.Soap12Binding)ext;
  481. else if (ext is XmlElement)
  482. xml = (XmlElement)ext;
  483. }
  484. CheckSoapBinding (soap, "http://schemas.microsoft.com/soap/tcp", label);
  485. label.EnterScope ("policy-xml");
  486. Assert.That (xml, Is.Not.Null, label.Get ());
  487. var assertions = AssertPolicy (sd, xml, label);
  488. Assert.That (assertions, Is.Not.Null, label.Get ());
  489. AssertPolicy (assertions, BinaryEncodingQName, label);
  490. AssertPolicy (assertions, UsingAddressingQName, label);
  491. if (transferMode == TransferMode.Streamed)
  492. AssertPolicy (assertions, StreamedTransferQName, label);
  493. switch (security) {
  494. case SecurityMode.Message:
  495. AssertPolicy (assertions, SymmetricBindingQName, label);
  496. AssertPolicy (assertions, Wss11QName, label);
  497. AssertPolicy (assertions, Trust10QName, label);
  498. break;
  499. case SecurityMode.Transport:
  500. AssertPolicy (assertions, TransportBindingQName, label);
  501. break;
  502. case SecurityMode.TransportWithMessageCredential:
  503. AssertPolicy (assertions, TransportBindingQName, label);
  504. AssertPolicy (assertions, EndorsingSupportingQName, label);
  505. AssertPolicy (assertions, Wss11QName, label);
  506. AssertPolicy (assertions, Trust10QName, label);
  507. break;
  508. default:
  509. break;
  510. }
  511. if (reliableSession)
  512. AssertPolicy (assertions, ReliableSessionQName, label);
  513. Assert.That (assertions.Count, Is.EqualTo (0), label.Get ());
  514. label.LeaveScope ();
  515. label.EnterScope ("services");
  516. Assert.That (sd.Services, Is.Not.Null, label.Get ());
  517. Assert.That (sd.Services.Count, Is.EqualTo (1), label.Get ());
  518. var service = sd.Services [0];
  519. Assert.That (service.Ports, Is.Not.Null, label.Get ());
  520. Assert.That (service.Ports.Count, Is.EqualTo (1), label.Get ());
  521. var port = service.Ports [0];
  522. label.EnterScope ("port");
  523. Assert.That (port.Extensions, Is.Not.Null, label.Get ());
  524. Assert.That (port.Extensions.Count, Is.EqualTo (2), label.Get ());
  525. WS.Soap12AddressBinding soap_addr_binding = null;
  526. XmlElement port_xml = null;
  527. foreach (var extension in port.Extensions) {
  528. if (extension is WS.Soap12AddressBinding)
  529. soap_addr_binding = (WS.Soap12AddressBinding)extension;
  530. else if (extension is XmlElement)
  531. port_xml = (XmlElement)extension;
  532. else
  533. Assert.Fail (label.Get ());
  534. }
  535. Assert.That (soap_addr_binding, Is.Not.Null, label.Get ());
  536. Assert.That (port_xml, Is.Not.Null, label.Get ());
  537. Assert.That (port_xml.NamespaceURI, Is.EqualTo (Wsa10Namespace), label.Get ());
  538. Assert.That (port_xml.LocalName, Is.EqualTo ("EndpointReference"), label.Get ());
  539. label.LeaveScope ();
  540. label.LeaveScope ();
  541. label.LeaveScope (); // wsdl
  542. var importer = new WsdlImporter (doc);
  543. label.EnterScope ("bindings");
  544. var bindings = importer.ImportAllBindings ();
  545. CheckImportErrors (importer, label);
  546. Assert.That (bindings, Is.Not.Null, label.Get ());
  547. Assert.That (bindings.Count, Is.EqualTo (1), label.Get ());
  548. CheckNetTcpBinding (
  549. bindings [0], security, reliableSession,
  550. transferMode, label);
  551. label.LeaveScope ();
  552. label.EnterScope ("endpoints");
  553. var endpoints = importer.ImportAllEndpoints ();
  554. CheckImportErrors (importer, label);
  555. Assert.That (endpoints, Is.Not.Null, label.Get ());
  556. Assert.That (endpoints.Count, Is.EqualTo (1), label.Get ());
  557. CheckEndpoint (endpoints [0], MetadataSamples.NetTcpUri, label);
  558. label.LeaveScope ();
  559. label.LeaveScope ();
  560. }
  561. public static void Dump (PolicyAssertionCollection assertions)
  562. {
  563. foreach (var assertion in assertions)
  564. Console.WriteLine ("ASSERTION: {0}", assertion.OuterXml);
  565. }
  566. public static void AssertPolicy (
  567. PolicyAssertionCollection assertions, QName qname, TestLabel label)
  568. {
  569. var assertion = assertions.Find (qname.Name, qname.Namespace);
  570. label.EnterScope (qname.Name);
  571. Assert.That (assertion, Is.Not.Null, label.ToString ());
  572. assertions.Remove (assertion);
  573. label.LeaveScope ();
  574. }
  575. static XmlElement ResolvePolicy (WS.ServiceDescription sd, XmlElement policy)
  576. {
  577. if (policy.LocalName.Equals ("Policy"))
  578. return policy;
  579. var uri = policy.GetAttribute ("URI");
  580. if (!uri.StartsWith ("#"))
  581. return null;
  582. foreach (var sext in sd.Extensions) {
  583. var sxml = sext as XmlElement;
  584. if (sxml == null)
  585. continue;
  586. if (!sxml.NamespaceURI.Equals (WspNamespace))
  587. continue;
  588. if (!sxml.LocalName.Equals ("Policy"))
  589. continue;
  590. var id = sxml.GetAttribute ("Id", WsuNamespace);
  591. if (uri.Substring (1).Equals (id))
  592. return sxml;
  593. }
  594. return null;
  595. }
  596. public static PolicyAssertionCollection AssertPolicy (
  597. WS.Binding binding, TestLabel label)
  598. {
  599. label.EnterScope ("FindPolicy");
  600. XmlElement policy = null;
  601. foreach (var extension in binding.Extensions) {
  602. var xml = extension as XmlElement;
  603. if (xml == null)
  604. continue;
  605. Assert.That (policy, Is.Null, label.Get ());
  606. policy = xml;
  607. }
  608. Assert.That (policy, Is.Not.Null, label.Get ());
  609. try {
  610. return AssertPolicy (binding.ServiceDescription, policy, label);
  611. } finally {
  612. label.LeaveScope ();
  613. }
  614. }
  615. static XmlElement AssertExactlyOneChildElement (XmlElement element)
  616. {
  617. XmlElement found = null;
  618. foreach (var node in element.ChildNodes) {
  619. if (node is XmlWhitespace)
  620. continue;
  621. var e = node as XmlElement;
  622. if (e == null)
  623. return null;
  624. if (found != null)
  625. return null;
  626. found = e;
  627. }
  628. return found;
  629. }
  630. public static PolicyAssertionCollection AssertPolicy (
  631. WS.ServiceDescription sd, XmlElement element, TestLabel label)
  632. {
  633. label.EnterScope ("wsp:Policy");
  634. Assert.That (element.NamespaceURI, Is.EqualTo (WspNamespace), label.Get ());
  635. Assert.That (element.LocalName, Is.EqualTo ("Policy") | Is.EqualTo ("PolicyReference"), label.Get ());
  636. var policy = ResolvePolicy (sd, element);
  637. Assert.That (policy, Is.Not.Null, label.Get ());
  638. label.EnterScope ("wsp:ExactlyOne");
  639. var exactlyOne = AssertExactlyOneChildElement (policy);
  640. Assert.That (exactlyOne, Is.Not.Null, label.Get ());
  641. Assert.That (exactlyOne.NamespaceURI, Is.EqualTo (WspNamespace), label.Get ());
  642. Assert.That (exactlyOne.LocalName, Is.EqualTo ("ExactlyOne"), label.Get ());
  643. label.LeaveScope ();
  644. label.EnterScope ("wsp:Any");
  645. var all = AssertExactlyOneChildElement (exactlyOne);
  646. Assert.That (all, Is.Not.Null, label.Get ());
  647. Assert.That (all.NamespaceURI, Is.EqualTo (WspNamespace), label.Get ());
  648. Assert.That (all.LocalName, Is.EqualTo ("All"), label.Get ());
  649. label.LeaveScope ();
  650. var collection = new PolicyAssertionCollection ();
  651. label.EnterScope ("assertions");
  652. foreach (var node in all.ChildNodes) {
  653. if (node is XmlWhitespace)
  654. continue;
  655. Assert.IsInstanceOfType (typeof (XmlElement), node, label.ToString ());
  656. collection.Add ((XmlElement)node);
  657. }
  658. label.LeaveScope ();
  659. label.LeaveScope ();
  660. return collection;
  661. }
  662. public static void TestOperation (MetadataSet metadata, bool soap12, TestLabel label)
  663. {
  664. label.EnterScope ("TestOperation");
  665. label.EnterScope ("metadata");
  666. WS.ServiceDescription sd = null;
  667. foreach (var ms in metadata.MetadataSections) {
  668. if (!ms.Dialect.Equals ("http://schemas.xmlsoap.org/wsdl/"))
  669. continue;
  670. sd = ms.Metadata as WS.ServiceDescription;
  671. }
  672. Assert.That (sd, Is.Not.Null, label.Get ());
  673. Assert.That (sd.Bindings, Is.Not.Null, label.Get ());
  674. Assert.That (sd.Bindings.Count, Is.EqualTo (1), label.Get ());
  675. var binding = sd.Bindings [0];
  676. label.LeaveScope ();
  677. label.EnterScope ("operation");
  678. Assert.That (binding.Operations, Is.Not.Null, label.Get ());
  679. Assert.That (binding.Operations.Count, Is.EqualTo (1), label.Get ());
  680. var op = binding.Operations [0];
  681. Assert.That (op.Name, Is.EqualTo ("Hello"), label.Get ());
  682. Assert.That (op.ExtensibleAttributes, Is.Null, label.Get ());
  683. label.EnterScope ("extensions");
  684. Assert.That (op.Extensions, Is.Not.Null, label.Get ());
  685. Assert.That (op.Extensions.Count, Is.EqualTo (1), label.Get ());
  686. Assert.That (op.Extensions [0], Is.AssignableTo<WS.SoapOperationBinding>(), label.Get ());
  687. var soap = (WS.SoapOperationBinding)op.Extensions [0];
  688. TestSoap (soap, soap12, label);
  689. label.LeaveScope ();
  690. TestSoapMessage (op.Input, soap12, label);
  691. TestSoapMessage (op.Output, soap12, label);
  692. label.LeaveScope (); // operation
  693. label.LeaveScope ();
  694. }
  695. static void TestSoap (WS.SoapOperationBinding soap, bool soap12, TestLabel label)
  696. {
  697. label.EnterScope ("soap");
  698. var type = soap12 ? typeof (WS.Soap12OperationBinding) : typeof (WS.SoapOperationBinding);
  699. Assert.That (soap.GetType (), Is.EqualTo (type), label.Get ());
  700. Assert.That (soap.Style, Is.EqualTo (WS.SoapBindingStyle.Document), label.Get ());
  701. Assert.That (soap.SoapAction, Is.EqualTo ("http://tempuri.org/IMyContract/Hello"), label.Get ());
  702. Assert.That (soap.Required, Is.False, label.Get ());
  703. label.LeaveScope ();
  704. }
  705. static void TestSoapMessage (WS.MessageBinding binding, bool soap12, TestLabel label)
  706. {
  707. label.EnterScope (binding is WS.InputBinding ? "input" : "output");
  708. Assert.That (binding, Is.Not.Null, label.Get ());
  709. Assert.That (binding.Name, Is.Null, label.Get ());
  710. Assert.That (binding.ExtensibleAttributes, Is.Null, label.Get ());
  711. Assert.That (binding.Extensions, Is.Not.Null, label.Get ());
  712. Assert.That (binding.Extensions.Count, Is.EqualTo (1), label.Get ());
  713. Assert.That (binding.Extensions [0], Is.AssignableTo<WS.SoapBodyBinding> (), label.Get ());
  714. var body = (WS.SoapBodyBinding)binding.Extensions [0];
  715. TestSoapBody (body, soap12, label);
  716. label.LeaveScope ();
  717. }
  718. static void TestSoapBody (WS.SoapBodyBinding soap, bool soap12, TestLabel label)
  719. {
  720. label.EnterScope ("soap-body");
  721. var type = soap12 ? typeof (WS.Soap12BodyBinding) : typeof (WS.SoapBodyBinding);
  722. Assert.That (soap.GetType (), Is.EqualTo (type), label.Get ());
  723. Assert.That (soap.Encoding, Is.Empty, label.Get ());
  724. Assert.That (soap.Namespace, Is.Empty, label.Get ());
  725. Assert.That (soap.Parts, Is.Null, label.Get ());
  726. Assert.That (soap.Use, Is.EqualTo (WS.SoapBindingUse.Literal), label.Get ());
  727. label.LeaveScope ();
  728. }
  729. public static void AssertConfig (MetadataSet metadata, XmlDocument xml, TestLabel label)
  730. {
  731. label.EnterScope ("import");
  732. var importer = new WsdlImporter (metadata);
  733. var endpoints = importer.ImportAllEndpoints ();
  734. CheckImportErrors (importer, label);
  735. Assert.That (endpoints.Count, Is.AtLeast (1), label.Get ());
  736. label.LeaveScope ();
  737. var nav = xml.CreateNavigator ();
  738. // FIXME: Check endpoints.
  739. label.EnterScope ("endpoints");
  740. var endpointIter = nav.Select ("/configuration/system.serviceModel/client/endpoint");
  741. Assert.That (endpointIter.Count, Is.EqualTo (endpoints.Count), label.Get ());
  742. label.LeaveScope ();
  743. }
  744. }
  745. }