Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.ServiceModel.Discovery / Test / System.ServiceModel.Discovery / DiscoveryClientBindingElementTest.cs
1 //
2 // Author: Atsushi Enomoto <atsushi@ximian.com>
3 //
4 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 using System;
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.IO;
29 using System.ServiceModel;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Discovery;
33 using System.ServiceModel.Dispatcher;
34 using System.Threading;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.ServiceModel.Discovery
38 {
39         [TestFixture]
40         public class DiscoveryClientBindingElementTest
41         {
42                 [Test]
43                 public void StaticMembers ()
44                 {
45                         Assert.AreEqual (new Uri ("http://schemas.microsoft.com/discovery/dynamic"), DiscoveryClientBindingElement.DiscoveryEndpointAddress.Uri, "#1");
46                 }
47                 
48                 DynamicEndpoint CreateDynamicEndpoint ()
49                 {
50                         var cd = ContractDescription.GetContract (typeof (ITestService));
51                         var binding = new BasicHttpBinding ();
52                         return new DynamicEndpoint (cd, binding);
53                 }
54
55                 [Test]
56                 public void Constructors ()
57                 {
58                         var de = CreateDynamicEndpoint ();
59                         var be = new DiscoveryClientBindingElement (de.DiscoveryEndpointProvider, de.FindCriteria);
60                         Assert.IsNotNull (be.FindCriteria, "#1");
61                         Assert.IsNotNull (be.DiscoveryEndpointProvider, "#2");
62                         var die = be.DiscoveryEndpointProvider.GetDiscoveryEndpoint ();
63                         Assert.AreEqual (DiscoveryVersion.WSDiscovery11, die.DiscoveryVersion, "#3");
64                         Assert.IsTrue (die is UdpDiscoveryEndpoint, "#3-2");
65                         Assert.AreEqual (ServiceDiscoveryMode.Adhoc, die.DiscoveryMode, "#4");
66                         Assert.AreEqual ("urn:docs-oasis-open-org:ws-dd:ns:discovery:2009:01", die.Address.Uri.ToString (), "#5");
67                         Assert.IsNotNull (die.Contract, "#6");
68                         Assert.AreEqual ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01", die.Contract.Namespace, "#6-2");
69                         Assert.AreEqual ("TargetService", die.Contract.Name, "#6-3");
70                         // could be either IPv4 or IPv6
71                         Assert.AreEqual (new UdpDiscoveryEndpoint ().MulticastAddress, die.ListenUri, "#7");
72                         Assert.AreEqual (ListenUriMode.Explicit, die.ListenUriMode, "#8");
73                         Assert.AreEqual (5, die.Behaviors.Count, "#9");
74
75                         // default constructor
76                         be = new DiscoveryClientBindingElement ();
77                         Assert.IsNotNull (be.FindCriteria, "#11");
78                         Assert.IsNotNull (be.DiscoveryEndpointProvider, "#12");
79                         die = be.DiscoveryEndpointProvider.GetDiscoveryEndpoint ();
80                         Assert.AreEqual (DiscoveryVersion.WSDiscovery11, die.DiscoveryVersion, "#13");
81                         Assert.IsTrue (die is UdpDiscoveryEndpoint, "#13-2");
82                 }
83
84                 [Test]
85                 public void CanBuildChannels ()
86                 {
87                         var de = CreateDynamicEndpoint ();
88                         // it is channel dependent - i.e. this binding element does not affect.
89                         var be = new DiscoveryClientBindingElement (de.DiscoveryEndpointProvider, de.FindCriteria);
90                         var bc = new BindingContext (new CustomBinding (new TcpTransportBindingElement ()), new BindingParameterCollection ());
91                         Assert.IsTrue (be.CanBuildChannelFactory<IDuplexSessionChannel> (bc), "#0");
92                         Assert.IsFalse (be.CanBuildChannelFactory<IDuplexChannel> (bc), "#1");
93                         Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (bc), "#2");
94                         Assert.IsFalse (be.CanBuildChannelFactory<IRequestChannel> (bc), "#3");
95
96                         // It always return false.
97                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (bc), "#4"); // false!
98                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (bc), "#5");
99                         Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (bc), "#6");
100                         Assert.IsFalse (be.CanBuildChannelListener<IReplyChannel> (bc), "#7");
101
102                         bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
103                         Assert.IsFalse (be.CanBuildChannelFactory<IDuplexSessionChannel> (bc), "#10");
104                         Assert.IsFalse (be.CanBuildChannelFactory<IDuplexChannel> (bc), "#11");
105                         Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (bc), "#12");
106                         Assert.IsTrue (be.CanBuildChannelFactory<IRequestChannel> (bc), "#13");
107
108                         // It always return false.
109                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (bc), "#14");
110                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (bc), "#15");
111                         Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (bc), "#16");
112                         Assert.IsFalse (be.CanBuildChannelListener<IReplyChannel> (bc), "#17");
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (InvalidOperationException))]
117                 public void BuildChannelFactory_OnlyTransport ()
118                 {
119                         var be = new DiscoveryClientBindingElement ();
120                         var bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
121                         be.BuildChannelFactory<IRequestChannel> (bc);
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentException))]
126                 public void BuildChannelFactory_CreateNonDynamicUriChannel ()
127                 {
128                         var be = new DiscoveryClientBindingElement ();
129                         var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
130                         var cf = be.BuildChannelFactory<IRequestChannel> (bc);
131                         cf.Open ();
132                         var uri = new Uri ("http://localhost:37564");
133                         cf.CreateChannel (new EndpointAddress (uri));
134                 }
135
136                 [Test]
137                 public void BuildChannelFactory ()
138                 {
139                         var be = new DiscoveryClientBindingElement ();
140                         var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
141                         var cf = be.BuildChannelFactory<IRequestChannel> (bc);
142                         cf.Open ();
143                         var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
144                         Assert.AreEqual (DiscoveryClientBindingElement.DiscoveryEndpointAddress, ch.RemoteAddress, "#1");
145                 }
146
147                 // This test takes a while, so I in face don't want to enable it ...
148                 [Test]
149                 [ExpectedException (typeof (EndpointNotFoundException))]
150                 public void RequestChannelOpenFails ()
151                 {
152                         var be = new DiscoveryClientBindingElement ();
153                         var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
154                         var cf = be.BuildChannelFactory<IRequestChannel> (bc);
155                         cf.Open ();
156                         Assert.IsNull (cf.GetProperty<DiscoveryEndpoint> (), "#1");
157                         var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
158                         Assert.IsNull (ch.GetProperty<DiscoveryEndpoint> (), "#2");
159                         ch.Open ();
160                 }
161
162                 [Test]
163                 [ExpectedException (typeof (EndpointNotFoundException))]
164                 public void RequestChannelOpenFails2 ()
165                 {
166                         var be = new DiscoveryClientBindingElement ();
167                         var bc = new BindingContext (new CustomBinding (be, new TcpTransportBindingElement ()), new BindingParameterCollection ());
168                         var cf = be.BuildChannelFactory<IDuplexSessionChannel> (bc);
169                         cf.Open ();
170                         var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
171                         ch.Open ();
172                 }
173
174                 [Test]
175                 public void CustomDiscoveryEndpointProvider ()
176                 {
177                         var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new MyDiscoveryEndpointProvider () };
178                         var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
179                         var cf = be.BuildChannelFactory<IRequestChannel> (bc);
180                         cf.Open ();
181                         var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
182                         try {
183                                 ch.Open ();
184                                 Assert.Fail ("Should try to use failing endpoint provider.");
185                         } catch (MyException) {
186                         }
187                 }
188
189                 class MyDiscoveryEndpointProvider : DiscoveryEndpointProvider
190                 {
191                         public override DiscoveryEndpoint GetDiscoveryEndpoint ()
192                         {
193                                 throw new MyException ();
194                         }
195                         
196                 }
197
198                 class MyException : Exception
199                 {
200                 }
201
202                 [Test]
203                 public void GetProperty ()
204                 {
205                         var be = new DiscoveryClientBindingElement ();
206                         var bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
207                         // so, they are not part of GetProperty<T>() return values.
208                         Assert.IsNull (be.GetProperty<FindCriteria> (bc), "#1");
209                         Assert.IsNull (be.GetProperty<DiscoveryEndpointProvider> (bc), "#2");
210
211                         Assert.IsNull (be.GetProperty<DiscoveryEndpoint> (bc), "#3");
212                 }
213         }
214 }