2 // BasicHttpBindingTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.ObjectModel;
31 using System.Net.Security;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Security;
35 using NUnit.Framework;
36 using System.ServiceModel.Configuration;
37 using System.Configuration;
40 namespace MonoTests.System.ServiceModel
43 public class BasicHttpBindingTest
46 public void DefaultValues ()
48 BasicHttpBinding b = new BasicHttpBinding ();
52 BasicHttpSecurity sec = b.Security;
53 Assert.IsNotNull (sec, "#2-1");
54 Assert.AreEqual (BasicHttpSecurityMode.None, sec.Mode, "#2-2");
55 BasicHttpMessageSecurity msg = sec.Message;
56 Assert.IsNotNull (msg, "#2-3-1");
57 Assert.AreEqual (SecurityAlgorithmSuite.Default, msg.AlgorithmSuite, "#2-3-2");
58 Assert.AreEqual (BasicHttpMessageCredentialType.UserName, msg.ClientCredentialType, "#2-3-3");
59 HttpTransportSecurity trans = sec.Transport;
60 Assert.IsNotNull (trans, "#2-4-1");
61 Assert.AreEqual (HttpClientCredentialType.None, trans.ClientCredentialType, "#2-4-2");
62 Assert.AreEqual (HttpProxyCredentialType.None, trans.ProxyCredentialType, "#2-4-3");
63 Assert.AreEqual ("", trans.Realm, "#2-4-4");
66 BindingElementCollection bec = b.CreateBindingElements ();
67 Assert.AreEqual (2, bec.Count, "#5-1");
68 Assert.AreEqual (typeof (TextMessageEncodingBindingElement),
69 bec [0].GetType (), "#5-2");
70 Assert.AreEqual (typeof (HttpTransportBindingElement),
71 bec [1].GetType (), "#5-3");
75 public void DefaultValueSecurityModeMessage ()
77 BasicHttpBinding b = new BasicHttpBinding (BasicHttpSecurityMode.Message);
78 b.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
82 BasicHttpSecurity sec = b.Security;
83 Assert.IsNotNull (sec, "#2-1");
84 Assert.AreEqual (BasicHttpSecurityMode.Message, sec.Mode, "#2-2");
85 BasicHttpMessageSecurity msg = sec.Message;
86 Assert.IsNotNull (msg, "#2-3-1");
87 Assert.AreEqual (SecurityAlgorithmSuite.Default, msg.AlgorithmSuite, "#2-3-2");
88 Assert.AreEqual (BasicHttpMessageCredentialType.Certificate, msg.ClientCredentialType, "#2-3-3");
89 HttpTransportSecurity trans = sec.Transport;
90 Assert.IsNotNull (trans, "#2-4-1");
91 Assert.AreEqual (HttpClientCredentialType.None, trans.ClientCredentialType, "#2-4-2");
92 Assert.AreEqual (HttpProxyCredentialType.None, trans.ProxyCredentialType, "#2-4-3");
93 Assert.AreEqual ("", trans.Realm, "#2-4-4");
96 BindingElementCollection bec = b.CreateBindingElements ();
97 Assert.AreEqual (3, bec.Count, "#5-1");
98 Assert.AreEqual (typeof (AsymmetricSecurityBindingElement),
99 bec [0].GetType (), "#5-2");
100 Assert.AreEqual (typeof (TextMessageEncodingBindingElement),
101 bec [1].GetType (), "#5-3");
102 Assert.AreEqual (typeof (HttpTransportBindingElement),
103 bec [2].GetType (), "#5-4");
106 void DefaultValues (BasicHttpBinding b)
108 Assert.AreEqual (false, b.BypassProxyOnLocal, "#1");
109 Assert.AreEqual (HostNameComparisonMode.StrongWildcard,
110 b.HostNameComparisonMode, "#2");
111 Assert.AreEqual (0x80000, b.MaxBufferPoolSize, "#3");
112 Assert.AreEqual (0x10000, b.MaxBufferSize, "#4");
113 Assert.AreEqual (0x10000, b.MaxReceivedMessageSize, "#5");
114 Assert.AreEqual (WSMessageEncoding.Text, b.MessageEncoding, "#6");
115 Assert.IsNull (b.ProxyAddress, "#7");
116 // FIXME: test b.ReaderQuotas
117 Assert.AreEqual ("http", b.Scheme, "#8");
118 Assert.AreEqual (EnvelopeVersion.Soap11, b.EnvelopeVersion, "#9");
119 Assert.AreEqual (65001, b.TextEncoding.CodePage, "#10"); // utf-8
120 Assert.AreEqual (TransferMode.Buffered, b.TransferMode, "#11");
121 Assert.AreEqual (true, b.UseDefaultWebProxy, "#12");
125 IBindingDeliveryCapabilities ib = (IBindingDeliveryCapabilities ) b;
126 Assert.AreEqual (false, ib.AssuresOrderedDelivery, "#2-1");
127 Assert.AreEqual (false, ib.QueuedDelivery, "#2-3");
129 IBindingMulticastCapabilities imc = (IBindingMulticastCapabilities) b;
130 Assert.AreEqual (false, imc.IsMulticast, "#2.2-1");
132 IBindingRuntimePreferences ir =
133 (IBindingRuntimePreferences) b;
134 Assert.AreEqual (false, ir.ReceiveSynchronously, "#3-1");
136 ISecurityCapabilities ic = b as ISecurityCapabilities;
137 Assert.AreEqual (ProtectionLevel.None,
138 ic.SupportedRequestProtectionLevel, "#4-1");
139 Assert.AreEqual (ProtectionLevel.None,
140 ic.SupportedResponseProtectionLevel, "#4-2");
141 Assert.AreEqual (false, ic.SupportsClientAuthentication, "#4-3");
142 Assert.AreEqual (false, ic.SupportsClientWindowsIdentity, "#4-4");
143 Assert.AreEqual (false, ic.SupportsServerAuthentication, "#4-5");
148 [ExpectedException (typeof (InvalidOperationException))]
149 public void DefaultValueSecurityModeMessageError ()
151 BasicHttpBinding b = new BasicHttpBinding (BasicHttpSecurityMode.Message);
152 // "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."
153 b.CreateBindingElements ();
157 public void MessageEncoding ()
159 BasicHttpBinding b = new BasicHttpBinding ();
160 foreach (BindingElement be in b.CreateBindingElements ()) {
161 MessageEncodingBindingElement mbe =
162 be as MessageEncodingBindingElement;
164 MessageEncoderFactory f = mbe.CreateMessageEncoderFactory ();
165 MessageEncoder e = f.Encoder;
167 Assert.AreEqual (typeof (TextMessageEncodingBindingElement), mbe.GetType (), "#1-1");
168 Assert.AreEqual (MessageVersion.Soap11, f.MessageVersion, "#2-1");
169 Assert.AreEqual ("text/xml; charset=utf-8", e.ContentType, "#3-1");
170 Assert.AreEqual ("text/xml", e.MediaType, "#3-2");
174 Assert.Fail ("No message encodiing binding element.");
178 public void ApplyConfiguration ()
180 BasicHttpBinding b = CreateBindingFromConfig ();
182 Assert.AreEqual (true, b.AllowCookies, "#1");
183 Assert.AreEqual (true, b.BypassProxyOnLocal, "#2");
184 Assert.AreEqual (HostNameComparisonMode.Exact, b.HostNameComparisonMode, "#3");
185 Assert.AreEqual (262144, b.MaxBufferPoolSize, "#4");
186 Assert.AreEqual (32768, b.MaxBufferSize, "#5");
187 Assert.AreEqual (32768, b.MaxReceivedMessageSize, "#6");
188 Assert.AreEqual ("proxy", b.ProxyAddress.ToString (), "#7");
189 Assert.AreEqual (Encoding.Unicode, b.TextEncoding, "#7");
190 Assert.AreEqual (TransferMode.Streamed, b.TransferMode, "#7");
194 public void CreateBindingElements ()
196 BasicHttpBinding b = CreateBindingFromConfig ();
199 BindingElementCollection bec = b.CreateBindingElements ();
200 Assert.AreEqual (2, bec.Count, "#1");
201 Assert.AreEqual (typeof (TextMessageEncodingBindingElement),
202 bec [0].GetType (), "#2");
203 Assert.AreEqual (typeof (HttpTransportBindingElement),
204 bec [1].GetType (), "#3");
208 public void Elements_MessageEncodingBindingElement ()
210 BasicHttpBinding b = CreateBindingFromConfig ();
211 BindingElementCollection bec = b.CreateBindingElements ();
212 TextMessageEncodingBindingElement m =
213 (TextMessageEncodingBindingElement) bec [0];
215 Assert.AreEqual (64, m.MaxReadPoolSize, "#1");
216 Assert.AreEqual (16, m.MaxWritePoolSize, "#2");
217 Assert.AreEqual (4096, m.ReaderQuotas.MaxArrayLength, "#3");
218 Assert.AreEqual (8192, m.ReaderQuotas.MaxBytesPerRead, "#4");
219 Assert.AreEqual (64, m.ReaderQuotas.MaxDepth, "#5");
220 Assert.AreEqual (8192, m.ReaderQuotas.MaxNameTableCharCount, "#6");
221 Assert.AreEqual (16384, m.ReaderQuotas.MaxStringContentLength, "#7");
222 Assert.AreEqual (Encoding.Unicode, m.WriteEncoding, "#8");
226 public void Elements_TransportBindingElement ()
228 BasicHttpBinding b = CreateBindingFromConfig ();
229 BindingElementCollection bec = b.CreateBindingElements ();
230 HttpTransportBindingElement t =
231 (HttpTransportBindingElement) bec [1];
233 Assert.AreEqual (true, t.AllowCookies, "#1");
234 Assert.AreEqual (AuthenticationSchemes.Anonymous, t.AuthenticationScheme, "#2");
235 Assert.AreEqual (true, t.BypassProxyOnLocal, "#3");
236 Assert.AreEqual (HostNameComparisonMode.Exact, t.HostNameComparisonMode, "#4");
237 Assert.AreEqual (true, t.KeepAliveEnabled, "#5");
238 Assert.AreEqual (false, t.ManualAddressing, "#6");
239 Assert.AreEqual (262144, t.MaxBufferPoolSize, "#7");
240 Assert.AreEqual (32768, t.MaxBufferSize, "#8");
241 Assert.AreEqual (32768, t.MaxReceivedMessageSize, "#9");
242 Assert.AreEqual ("proxy", t.ProxyAddress.ToString (), "#10");
243 Assert.AreEqual (AuthenticationSchemes.Anonymous, t.ProxyAuthenticationScheme, "#11");
244 Assert.AreEqual ("", t.Realm, "#12");
245 Assert.AreEqual ("http", t.Scheme, "#13");
246 Assert.AreEqual (TransferMode.Streamed, t.TransferMode, "#14");
247 Assert.AreEqual (false, t.UnsafeConnectionNtlmAuthentication, "#15");
248 Assert.AreEqual (false, t.UseDefaultWebProxy, "#16");
252 public void SecurityMode ()
254 // hmm, against my expectation, those modes does not give Http(s)TransportBindingElement property differences..
255 var modes = new HttpClientCredentialType [] {HttpClientCredentialType.None, HttpClientCredentialType.Basic, HttpClientCredentialType.Digest, HttpClientCredentialType.Ntlm, HttpClientCredentialType.Windows, HttpClientCredentialType.Certificate};
256 foreach (var m in modes) {
257 var b = new BasicHttpBinding ();
258 b.Security.Mode = BasicHttpSecurityMode.Transport;
259 b.Security.Transport.ClientCredentialType = m;
260 var bec = b.CreateBindingElements ();
261 Assert.AreEqual (2, bec.Count, "#1." + m);
262 Assert.IsTrue (bec [1] is HttpsTransportBindingElement, "#2." + m);
263 var tbe = (HttpsTransportBindingElement) bec [1];
264 if (m == HttpClientCredentialType.Certificate)
265 Assert.IsTrue (tbe.RequireClientCertificate, "#3." + m);
267 Assert.IsFalse (tbe.RequireClientCertificate, "#3." + m);
272 public void SecurityMode2 ()
274 var modes = new HttpClientCredentialType [] {HttpClientCredentialType.None, HttpClientCredentialType.Basic, HttpClientCredentialType.Digest, HttpClientCredentialType.Ntlm, HttpClientCredentialType.Windows, HttpClientCredentialType.Certificate};
275 foreach (var m in modes) {
276 var b = new BasicHttpBinding ();
277 b.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential; // gives WS-Security message security.
278 b.Security.Transport.ClientCredentialType = m;
279 var bec = b.CreateBindingElements ();
280 Assert.AreEqual (3, bec.Count, "#1." + m);
281 Assert.IsTrue (bec [0] is TransportSecurityBindingElement, "#2." + m);
282 Assert.IsTrue (bec [2] is HttpsTransportBindingElement, "#3." + m);
283 var tbe = (HttpsTransportBindingElement) bec [2];
284 Assert.IsFalse (tbe.RequireClientCertificate, "#4." + m);
289 public void SecurityMode3 ()
291 var modes = new HttpClientCredentialType [] {HttpClientCredentialType.None, HttpClientCredentialType.Basic, HttpClientCredentialType.Digest, HttpClientCredentialType.Ntlm, HttpClientCredentialType.Windows};
292 var auths = new AuthenticationSchemes [] { AuthenticationSchemes.Anonymous, AuthenticationSchemes.Basic, AuthenticationSchemes.Digest, AuthenticationSchemes.Ntlm, AuthenticationSchemes.Negotiate }; // specifically, none->anonymous, and windows->negotiate
293 for (int i = 0; i < modes.Length; i++) {
295 var b = new BasicHttpBinding ();
296 b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; // gives WS-Security message security.
297 b.Security.Transport.ClientCredentialType = m;
298 var bec = b.CreateBindingElements ();
299 Assert.AreEqual (2, bec.Count, "#1." + m);
300 Assert.IsTrue (bec [1] is HttpTransportBindingElement, "#2." + m);
301 var tbe = (HttpTransportBindingElement) bec [1];
302 Assert.AreEqual (auths [i], tbe.AuthenticationScheme, "#3." + m);
307 [ExpectedException (typeof (InvalidOperationException))]
308 public void SecurityMode4 ()
310 var b = new BasicHttpBinding ();
311 b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; // gives WS-Security message security.
312 b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
313 var bec = b.CreateBindingElements ();
316 private BasicHttpBinding CreateBindingFromConfig ()
318 ServiceModelSectionGroup config = (ServiceModelSectionGroup) ConfigurationManager.OpenExeConfiguration ("Test/config/basicHttpBinding").GetSectionGroup ("system.serviceModel");
319 BindingsSection section = (BindingsSection) config.Bindings;
320 BasicHttpBindingElement el = section.BasicHttpBinding.Bindings ["BasicHttpBinding2_Service"];
322 BasicHttpBinding b = new BasicHttpBinding ();
323 el.ApplyConfiguration (b);