New tests.
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / BasicHttpBindingTest.cs
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 using System;
29 using System.Collections.ObjectModel;
30 using System.Net;
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;
38 using System.Text;
39
40 namespace MonoTests.System.ServiceModel
41 {
42         [TestFixture]
43         public class BasicHttpBindingTest
44         {
45                 [Test]
46                 public void DefaultValues ()
47                 {
48                         BasicHttpBinding b = new BasicHttpBinding ();
49                         DefaultValues (b);
50
51                         // BasicHttpSecurity
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");
64
65                         // Binding elements
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");
72                 }
73
74                 [Test]
75                 public void DefaultValueSecurityModeMessage ()
76                 {
77                         BasicHttpBinding b = new BasicHttpBinding (BasicHttpSecurityMode.Message);
78                         b.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
79                         DefaultValues (b);
80
81                         // BasicHttpSecurity
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");
94
95                         // Binding elements
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");
104                 }
105
106                 void DefaultValues (BasicHttpBinding b)
107                 {
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");
122
123 /*
124                         // Interfaces
125                         IBindingDeliveryCapabilities ib = (IBindingDeliveryCapabilities ) b;
126                         Assert.AreEqual (false, ib.AssuresOrderedDelivery, "#2-1");
127                         Assert.AreEqual (false, ib.QueuedDelivery, "#2-3");
128
129                         IBindingMulticastCapabilities imc = (IBindingMulticastCapabilities) b;
130                         Assert.AreEqual (false, imc.IsMulticast, "#2.2-1");
131
132                         IBindingRuntimePreferences ir =
133                                 (IBindingRuntimePreferences) b;
134                         Assert.AreEqual (false, ir.ReceiveSynchronously, "#3-1");
135
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");
144 */
145                 }
146
147                 [Test]
148                 [ExpectedException (typeof (InvalidOperationException))]
149                 public void DefaultValueSecurityModeMessageError ()
150                 {
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 ();
154                 }
155
156                 [Test]
157                 public void MessageEncoding ()
158                 {
159                         BasicHttpBinding b = new BasicHttpBinding ();
160                         foreach (BindingElement be in b.CreateBindingElements ()) {
161                                 MessageEncodingBindingElement mbe =
162                                         be as MessageEncodingBindingElement;
163                                 if (mbe != null) {
164                                         MessageEncoderFactory f = mbe.CreateMessageEncoderFactory ();
165                                         MessageEncoder e = f.Encoder;
166
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");
171                                         return;
172                                 }
173                         }
174                         Assert.Fail ("No message encodiing binding element.");
175                 }
176
177                 [Test]
178                 public void ApplyConfiguration ()
179                 {
180                         BasicHttpBinding b = CreateBindingFromConfig ();
181
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");
191                 }
192
193                 [Test]
194                 public void CreateBindingElements ()
195                 {
196                         BasicHttpBinding b = CreateBindingFromConfig ();
197
198                         // Binding elements
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");
205                 }
206
207                 [Test]
208                 public void Elements_MessageEncodingBindingElement ()
209                 {
210                         BasicHttpBinding b = CreateBindingFromConfig ();
211                         BindingElementCollection bec = b.CreateBindingElements ();
212                         TextMessageEncodingBindingElement m =
213                                 (TextMessageEncodingBindingElement) bec [0];
214
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");
223                 }
224
225                 [Test]
226                 public void Elements_TransportBindingElement ()
227                 {
228                         BasicHttpBinding b = CreateBindingFromConfig ();
229                         BindingElementCollection bec = b.CreateBindingElements ();
230                         HttpTransportBindingElement t =
231                                 (HttpTransportBindingElement) bec [1];
232
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");
249                 }
250
251                 [Test]
252                 public void SecurityMode ()
253                 {
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);
266                                 else
267                                         Assert.IsFalse (tbe.RequireClientCertificate, "#3." + m);
268                         }
269                 }
270
271                 [Test]
272                 public void SecurityMode2 ()
273                 {
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);
285                         }
286                 }
287
288                 [Test]
289                 public void SecurityMode3 ()
290                 {
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++) {
294                                 var m = modes [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);
303                         }
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (InvalidOperationException))]
308                 public void SecurityMode4 ()
309                 {
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 ();
314                 }
315
316                 private BasicHttpBinding CreateBindingFromConfig ()
317                 {
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"];
321
322                         BasicHttpBinding b = new BasicHttpBinding ();
323                         el.ApplyConfiguration (b);
324
325                         return b;
326                 }
327         }
328 }