2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
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:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
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.
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
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;
37 namespace MonoTests.System.ServiceModel.Discovery
40 public class DiscoveryClientBindingElementTest
43 public void StaticMembers ()
45 Assert.AreEqual (new Uri ("http://schemas.microsoft.com/discovery/dynamic"), DiscoveryClientBindingElement.DiscoveryEndpointAddress.Uri, "#1");
48 DynamicEndpoint CreateDynamicEndpoint ()
50 var cd = ContractDescription.GetContract (typeof (ITestService));
51 var binding = new BasicHttpBinding ();
52 return new DynamicEndpoint (cd, binding);
56 public void Constructors ()
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");
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");
85 public void CanBuildChannels ()
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");
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");
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");
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");
116 [ExpectedException (typeof (InvalidOperationException))]
117 public void BuildChannelFactory_OnlyTransport ()
119 var be = new DiscoveryClientBindingElement ();
120 var bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
121 be.BuildChannelFactory<IRequestChannel> (bc);
125 [ExpectedException (typeof (ArgumentException))]
126 public void BuildChannelFactory_CreateNonDynamicUriChannel ()
128 var be = new DiscoveryClientBindingElement ();
129 var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
130 var cf = be.BuildChannelFactory<IRequestChannel> (bc);
132 var uri = new Uri ("http://localhost:37564");
133 cf.CreateChannel (new EndpointAddress (uri));
137 public void BuildChannelFactory ()
139 var be = new DiscoveryClientBindingElement ();
140 var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
141 var cf = be.BuildChannelFactory<IRequestChannel> (bc);
143 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
144 Assert.AreEqual (DiscoveryClientBindingElement.DiscoveryEndpointAddress, ch.RemoteAddress, "#1");
147 // This test takes a while, so I in face don't want to enable it ...
149 [ExpectedException (typeof (EndpointNotFoundException))]
150 public void RequestChannelOpenFails ()
152 var be = new DiscoveryClientBindingElement ();
153 var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
154 var cf = be.BuildChannelFactory<IRequestChannel> (bc);
156 Assert.IsNull (cf.GetProperty<DiscoveryEndpoint> (), "#1");
157 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
158 Assert.IsNull (ch.GetProperty<DiscoveryEndpoint> (), "#2");
163 [ExpectedException (typeof (EndpointNotFoundException))]
164 public void RequestChannelOpenFails2 ()
166 var be = new DiscoveryClientBindingElement ();
167 var bc = new BindingContext (new CustomBinding (be, new TcpTransportBindingElement ()), new BindingParameterCollection ());
168 var cf = be.BuildChannelFactory<IDuplexSessionChannel> (bc);
170 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
175 public void CustomDiscoveryEndpointProvider ()
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);
181 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
184 Assert.Fail ("Should try to use failing endpoint provider.");
185 } catch (MyException) {
189 class MyDiscoveryEndpointProvider : DiscoveryEndpointProvider
191 public override DiscoveryEndpoint GetDiscoveryEndpoint ()
193 throw new MyException ();
198 class MyException : Exception
203 public void GetProperty ()
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");
211 Assert.IsNull (be.GetProperty<DiscoveryEndpoint> (bc), "#3");