1 // 2 // BasicHttpBindingTest.cs 3 // 4 // Author: 5 // Atsushi Enomoto <atsushi@ximian.com> 6 // 7 // Copyright (C) 2005 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 #if !MOBILE && !XAMMAC_4_5 29 using System; 30 using System.Collections.ObjectModel; 31 using System.Net; 32 using System.Net.Security; 33 using System.ServiceModel; 34 using System.ServiceModel.Channels; 35 using System.ServiceModel.Security; 36 using NUnit.Framework; 37 using System.ServiceModel.Configuration; 38 using System.Configuration; 39 using System.Text; 40 41 namespace MonoTests.System.ServiceModel 42 { 43 [TestFixture] 44 public class BasicHttpBindingTest 45 { 46 [Test] DefaultValues()47 public void DefaultValues () 48 { 49 BasicHttpBinding b = new BasicHttpBinding (); 50 DefaultValues (b); 51 52 // BasicHttpSecurity 53 BasicHttpSecurity sec = b.Security; 54 Assert.IsNotNull (sec, "#2-1"); 55 Assert.AreEqual (BasicHttpSecurityMode.None, sec.Mode, "#2-2"); 56 BasicHttpMessageSecurity msg = sec.Message; 57 Assert.IsNotNull (msg, "#2-3-1"); 58 Assert.AreEqual (SecurityAlgorithmSuite.Default, msg.AlgorithmSuite, "#2-3-2"); 59 Assert.AreEqual (BasicHttpMessageCredentialType.UserName, msg.ClientCredentialType, "#2-3-3"); 60 HttpTransportSecurity trans = sec.Transport; 61 Assert.IsNotNull (trans, "#2-4-1"); 62 Assert.AreEqual (HttpClientCredentialType.None, trans.ClientCredentialType, "#2-4-2"); 63 Assert.AreEqual (HttpProxyCredentialType.None, trans.ProxyCredentialType, "#2-4-3"); 64 Assert.AreEqual ("", trans.Realm, "#2-4-4"); 65 66 // Binding elements 67 BindingElementCollection bec = b.CreateBindingElements (); 68 Assert.AreEqual (2, bec.Count, "#5-1"); 69 Assert.AreEqual (typeof (TextMessageEncodingBindingElement), 70 bec [0].GetType (), "#5-2"); 71 Assert.AreEqual (typeof (HttpTransportBindingElement), 72 bec [1].GetType (), "#5-3"); 73 } 74 75 [Test] DefaultValueSecurityModeMessage()76 public void DefaultValueSecurityModeMessage () 77 { 78 BasicHttpBinding b = new BasicHttpBinding (BasicHttpSecurityMode.Message); 79 b.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate; 80 DefaultValues (b); 81 82 // BasicHttpSecurity 83 BasicHttpSecurity sec = b.Security; 84 Assert.IsNotNull (sec, "#2-1"); 85 Assert.AreEqual (BasicHttpSecurityMode.Message, sec.Mode, "#2-2"); 86 BasicHttpMessageSecurity msg = sec.Message; 87 Assert.IsNotNull (msg, "#2-3-1"); 88 Assert.AreEqual (SecurityAlgorithmSuite.Default, msg.AlgorithmSuite, "#2-3-2"); 89 Assert.AreEqual (BasicHttpMessageCredentialType.Certificate, msg.ClientCredentialType, "#2-3-3"); 90 HttpTransportSecurity trans = sec.Transport; 91 Assert.IsNotNull (trans, "#2-4-1"); 92 Assert.AreEqual (HttpClientCredentialType.None, trans.ClientCredentialType, "#2-4-2"); 93 Assert.AreEqual (HttpProxyCredentialType.None, trans.ProxyCredentialType, "#2-4-3"); 94 Assert.AreEqual ("", trans.Realm, "#2-4-4"); 95 96 // Binding elements 97 BindingElementCollection bec = b.CreateBindingElements (); 98 Assert.AreEqual (3, bec.Count, "#5-1"); 99 Assert.AreEqual (typeof (AsymmetricSecurityBindingElement), 100 bec [0].GetType (), "#5-2"); 101 Assert.AreEqual (typeof (TextMessageEncodingBindingElement), 102 bec [1].GetType (), "#5-3"); 103 Assert.AreEqual (typeof (HttpTransportBindingElement), 104 bec [2].GetType (), "#5-4"); 105 } 106 DefaultValues(BasicHttpBinding b)107 void DefaultValues (BasicHttpBinding b) 108 { 109 Assert.AreEqual (false, b.BypassProxyOnLocal, "#1"); 110 Assert.AreEqual (HostNameComparisonMode.StrongWildcard, 111 b.HostNameComparisonMode, "#2"); 112 Assert.AreEqual (0x80000, b.MaxBufferPoolSize, "#3"); 113 Assert.AreEqual (0x10000, b.MaxBufferSize, "#4"); 114 Assert.AreEqual (0x10000, b.MaxReceivedMessageSize, "#5"); 115 Assert.AreEqual (WSMessageEncoding.Text, b.MessageEncoding, "#6"); 116 Assert.IsNull (b.ProxyAddress, "#7"); 117 // FIXME: test b.ReaderQuotas 118 Assert.AreEqual ("http", b.Scheme, "#8"); 119 Assert.AreEqual (EnvelopeVersion.Soap11, b.EnvelopeVersion, "#9"); 120 Assert.AreEqual (65001, b.TextEncoding.CodePage, "#10"); // utf-8 121 Assert.AreEqual (TransferMode.Buffered, b.TransferMode, "#11"); 122 Assert.AreEqual (true, b.UseDefaultWebProxy, "#12"); 123 124 /* 125 // Interfaces 126 IBindingDeliveryCapabilities ib = (IBindingDeliveryCapabilities ) b; 127 Assert.AreEqual (false, ib.AssuresOrderedDelivery, "#2-1"); 128 Assert.AreEqual (false, ib.QueuedDelivery, "#2-3"); 129 130 IBindingMulticastCapabilities imc = (IBindingMulticastCapabilities) b; 131 Assert.AreEqual (false, imc.IsMulticast, "#2.2-1"); 132 133 IBindingRuntimePreferences ir = 134 (IBindingRuntimePreferences) b; 135 Assert.AreEqual (false, ir.ReceiveSynchronously, "#3-1"); 136 137 ISecurityCapabilities ic = b as ISecurityCapabilities; 138 Assert.AreEqual (ProtectionLevel.None, 139 ic.SupportedRequestProtectionLevel, "#4-1"); 140 Assert.AreEqual (ProtectionLevel.None, 141 ic.SupportedResponseProtectionLevel, "#4-2"); 142 Assert.AreEqual (false, ic.SupportsClientAuthentication, "#4-3"); 143 Assert.AreEqual (false, ic.SupportsClientWindowsIdentity, "#4-4"); 144 Assert.AreEqual (false, ic.SupportsServerAuthentication, "#4-5"); 145 */ 146 } 147 148 [Test] 149 [ExpectedException (typeof (InvalidOperationException))] DefaultValueSecurityModeMessageError()150 public void DefaultValueSecurityModeMessageError () 151 { 152 BasicHttpBinding b = new BasicHttpBinding (BasicHttpSecurityMode.Message); 153 // "BasicHttp binding requires that BasicHttpBinding.Security.Message.ClientCredentialType be equivalent to the BasicHttpMessageCredentialType.Certificate credential type for secure messages. Select Transport or TransportWithMessageCredential security for UserName credentials." 154 b.CreateBindingElements (); 155 } 156 157 [Test] MessageEncoding()158 public void MessageEncoding () 159 { 160 BasicHttpBinding b = new BasicHttpBinding (); 161 foreach (BindingElement be in b.CreateBindingElements ()) { 162 MessageEncodingBindingElement mbe = 163 be as MessageEncodingBindingElement; 164 if (mbe != null) { 165 MessageEncoderFactory f = mbe.CreateMessageEncoderFactory (); 166 MessageEncoder e = f.Encoder; 167 168 Assert.AreEqual (typeof (TextMessageEncodingBindingElement), mbe.GetType (), "#1-1"); 169 Assert.AreEqual (MessageVersion.Soap11, f.MessageVersion, "#2-1"); 170 Assert.AreEqual ("text/xml; charset=utf-8", e.ContentType, "#3-1"); 171 Assert.AreEqual ("text/xml", e.MediaType, "#3-2"); 172 return; 173 } 174 } 175 Assert.Fail ("No message encodiing binding element."); 176 } 177 178 [Test] ApplyConfiguration()179 public void ApplyConfiguration () 180 { 181 BasicHttpBinding b = CreateBindingFromConfig (); 182 183 Assert.AreEqual (true, b.AllowCookies, "#1"); 184 Assert.AreEqual (true, b.BypassProxyOnLocal, "#2"); 185 Assert.AreEqual (HostNameComparisonMode.Exact, b.HostNameComparisonMode, "#3"); 186 Assert.AreEqual (262144, b.MaxBufferPoolSize, "#4"); 187 Assert.AreEqual (32768, b.MaxBufferSize, "#5"); 188 Assert.AreEqual (32768, b.MaxReceivedMessageSize, "#6"); 189 Assert.AreEqual ("proxy", b.ProxyAddress.ToString (), "#7"); 190 Assert.AreEqual (Encoding.Unicode, b.TextEncoding, "#7"); 191 Assert.AreEqual (TransferMode.Streamed, b.TransferMode, "#7"); 192 } 193 194 [Test] CreateBindingElements()195 public void CreateBindingElements () 196 { 197 BasicHttpBinding b = CreateBindingFromConfig (); 198 199 // Binding elements 200 BindingElementCollection bec = b.CreateBindingElements (); 201 Assert.AreEqual (2, bec.Count, "#1"); 202 Assert.AreEqual (typeof (TextMessageEncodingBindingElement), 203 bec [0].GetType (), "#2"); 204 Assert.AreEqual (typeof (HttpTransportBindingElement), 205 bec [1].GetType (), "#3"); 206 } 207 208 [Test] Elements_MessageEncodingBindingElement()209 public void Elements_MessageEncodingBindingElement () 210 { 211 BasicHttpBinding b = CreateBindingFromConfig (); 212 BindingElementCollection bec = b.CreateBindingElements (); 213 TextMessageEncodingBindingElement m = 214 (TextMessageEncodingBindingElement) bec [0]; 215 216 Assert.AreEqual (64, m.MaxReadPoolSize, "#1"); 217 Assert.AreEqual (16, m.MaxWritePoolSize, "#2"); 218 Assert.AreEqual (4096, m.ReaderQuotas.MaxArrayLength, "#3"); 219 Assert.AreEqual (8192, m.ReaderQuotas.MaxBytesPerRead, "#4"); 220 Assert.AreEqual (64, m.ReaderQuotas.MaxDepth, "#5"); 221 Assert.AreEqual (8192, m.ReaderQuotas.MaxNameTableCharCount, "#6"); 222 Assert.AreEqual (16384, m.ReaderQuotas.MaxStringContentLength, "#7"); 223 Assert.AreEqual (Encoding.Unicode, m.WriteEncoding, "#8"); 224 } 225 226 [Test] Elements_TransportBindingElement()227 public void Elements_TransportBindingElement () 228 { 229 BasicHttpBinding b = CreateBindingFromConfig (); 230 BindingElementCollection bec = b.CreateBindingElements (); 231 HttpTransportBindingElement t = 232 (HttpTransportBindingElement) bec [1]; 233 234 Assert.AreEqual (true, t.AllowCookies, "#1"); 235 Assert.AreEqual (AuthenticationSchemes.Anonymous, t.AuthenticationScheme, "#2"); 236 Assert.AreEqual (true, t.BypassProxyOnLocal, "#3"); 237 Assert.AreEqual (HostNameComparisonMode.Exact, t.HostNameComparisonMode, "#4"); 238 Assert.AreEqual (true, t.KeepAliveEnabled, "#5"); 239 Assert.AreEqual (false, t.ManualAddressing, "#6"); 240 Assert.AreEqual (262144, t.MaxBufferPoolSize, "#7"); 241 Assert.AreEqual (32768, t.MaxBufferSize, "#8"); 242 Assert.AreEqual (32768, t.MaxReceivedMessageSize, "#9"); 243 Assert.AreEqual ("proxy", t.ProxyAddress.ToString (), "#10"); 244 Assert.AreEqual (AuthenticationSchemes.Anonymous, t.ProxyAuthenticationScheme, "#11"); 245 Assert.AreEqual ("", t.Realm, "#12"); 246 Assert.AreEqual ("http", t.Scheme, "#13"); 247 Assert.AreEqual (TransferMode.Streamed, t.TransferMode, "#14"); 248 Assert.AreEqual (false, t.UnsafeConnectionNtlmAuthentication, "#15"); 249 Assert.AreEqual (false, t.UseDefaultWebProxy, "#16"); 250 } 251 252 [Test] SecurityMode()253 public void SecurityMode () 254 { 255 // hmm, against my expectation, those modes does not give Http(s)TransportBindingElement property differences.. 256 var modes = new HttpClientCredentialType [] {HttpClientCredentialType.None, HttpClientCredentialType.Basic, HttpClientCredentialType.Digest, HttpClientCredentialType.Ntlm, HttpClientCredentialType.Windows, HttpClientCredentialType.Certificate}; 257 foreach (var m in modes) { 258 var b = new BasicHttpBinding (); 259 b.Security.Mode = BasicHttpSecurityMode.Transport; 260 b.Security.Transport.ClientCredentialType = m; 261 var bec = b.CreateBindingElements (); 262 Assert.AreEqual (2, bec.Count, "#1." + m); 263 Assert.IsTrue (bec [1] is HttpsTransportBindingElement, "#2." + m); 264 var tbe = (HttpsTransportBindingElement) bec [1]; 265 if (m == HttpClientCredentialType.Certificate) 266 Assert.IsTrue (tbe.RequireClientCertificate, "#3." + m); 267 else 268 Assert.IsFalse (tbe.RequireClientCertificate, "#3." + m); 269 } 270 } 271 272 [Test] SecurityMode2()273 public void SecurityMode2 () 274 { 275 var modes = new HttpClientCredentialType [] {HttpClientCredentialType.None, HttpClientCredentialType.Basic, HttpClientCredentialType.Digest, HttpClientCredentialType.Ntlm, HttpClientCredentialType.Windows, HttpClientCredentialType.Certificate}; 276 foreach (var m in modes) { 277 var b = new BasicHttpBinding (); 278 b.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential; // gives WS-Security message security. 279 b.Security.Transport.ClientCredentialType = m; 280 var bec = b.CreateBindingElements (); 281 Assert.AreEqual (3, bec.Count, "#1." + m); 282 Assert.IsTrue (bec [0] is TransportSecurityBindingElement, "#2." + m); 283 Assert.IsTrue (bec [2] is HttpsTransportBindingElement, "#3." + m); 284 var tbe = (HttpsTransportBindingElement) bec [2]; 285 Assert.IsFalse (tbe.RequireClientCertificate, "#4." + m); 286 } 287 } 288 289 [Test] SecurityMode3()290 public void SecurityMode3 () 291 { 292 var modes = new HttpClientCredentialType [] {HttpClientCredentialType.None, HttpClientCredentialType.Basic, HttpClientCredentialType.Digest, HttpClientCredentialType.Ntlm, HttpClientCredentialType.Windows}; 293 var auths = new AuthenticationSchemes [] { AuthenticationSchemes.Anonymous, AuthenticationSchemes.Basic, AuthenticationSchemes.Digest, AuthenticationSchemes.Ntlm, AuthenticationSchemes.Negotiate }; // specifically, none->anonymous, and windows->negotiate 294 for (int i = 0; i < modes.Length; i++) { 295 var m = modes [i]; 296 var b = new BasicHttpBinding (); 297 b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; // gives WS-Security message security. 298 b.Security.Transport.ClientCredentialType = m; 299 var bec = b.CreateBindingElements (); 300 Assert.AreEqual (2, bec.Count, "#1." + m); 301 Assert.IsTrue (bec [1] is HttpTransportBindingElement, "#2." + m); 302 var tbe = (HttpTransportBindingElement) bec [1]; 303 Assert.AreEqual (auths [i], tbe.AuthenticationScheme, "#3." + m); 304 } 305 } 306 307 [Test] 308 [ExpectedException (typeof (InvalidOperationException))] SecurityMode4()309 public void SecurityMode4 () 310 { 311 var b = new BasicHttpBinding (); 312 b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; // gives WS-Security message security. 313 b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 314 var bec = b.CreateBindingElements (); 315 } 316 CreateBindingFromConfig()317 private BasicHttpBinding CreateBindingFromConfig () 318 { 319 ServiceModelSectionGroup config = (ServiceModelSectionGroup) ConfigurationManager.OpenExeConfiguration ("Test/config/basicHttpBinding").GetSectionGroup ("system.serviceModel"); 320 BindingsSection section = (BindingsSection) config.Bindings; 321 BasicHttpBindingElement el = section.BasicHttpBinding.Bindings ["BasicHttpBinding2_Service"]; 322 323 BasicHttpBinding b = new BasicHttpBinding (); 324 el.ApplyConfiguration (b); 325 326 return b; 327 } 328 } 329 } 330 #endif 331