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 // FIXME: enable (but the number should not matter; the functionality should rather matter. Those behaviors are internal in .NET)
74 // Assert.AreEqual (5, die.Behaviors.Count, "#9");
76 // default constructor
77 be = new DiscoveryClientBindingElement ();
78 Assert.IsNotNull (be.FindCriteria, "#11");
79 Assert.IsNotNull (be.DiscoveryEndpointProvider, "#12");
80 die = be.DiscoveryEndpointProvider.GetDiscoveryEndpoint ();
81 Assert.AreEqual (DiscoveryVersion.WSDiscovery11, die.DiscoveryVersion, "#13");
82 Assert.IsTrue (die is UdpDiscoveryEndpoint, "#13-2");
86 public void CanBuildChannels ()
88 var de = CreateDynamicEndpoint ();
89 // it is channel dependent - i.e. this binding element does not affect.
90 var be = new DiscoveryClientBindingElement (de.DiscoveryEndpointProvider, de.FindCriteria);
91 var bc = new BindingContext (new CustomBinding (new TcpTransportBindingElement ()), new BindingParameterCollection ());
92 Assert.IsTrue (be.CanBuildChannelFactory<IDuplexSessionChannel> (bc), "#0");
93 Assert.IsFalse (be.CanBuildChannelFactory<IDuplexChannel> (bc), "#1");
94 Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (bc), "#2");
95 Assert.IsFalse (be.CanBuildChannelFactory<IRequestChannel> (bc), "#3");
97 // It always return false.
98 Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (bc), "#4"); // false!
99 Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (bc), "#5");
100 Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (bc), "#6");
101 Assert.IsFalse (be.CanBuildChannelListener<IReplyChannel> (bc), "#7");
103 bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
104 Assert.IsFalse (be.CanBuildChannelFactory<IDuplexSessionChannel> (bc), "#10");
105 Assert.IsFalse (be.CanBuildChannelFactory<IDuplexChannel> (bc), "#11");
106 Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (bc), "#12");
107 Assert.IsTrue (be.CanBuildChannelFactory<IRequestChannel> (bc), "#13");
109 // It always return false.
110 Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (bc), "#14");
111 Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (bc), "#15");
112 Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (bc), "#16");
113 Assert.IsFalse (be.CanBuildChannelListener<IReplyChannel> (bc), "#17");
117 [ExpectedException (typeof (InvalidOperationException))]
118 public void BuildChannelFactory_OnlyTransport ()
120 var be = new DiscoveryClientBindingElement ();
121 var bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
122 be.BuildChannelFactory<IRequestChannel> (bc);
126 [ExpectedException (typeof (ArgumentException))]
127 public void BuildChannelFactory_CreateNonDynamicUriChannel ()
129 var be = new DiscoveryClientBindingElement ();
130 var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
131 var cf = be.BuildChannelFactory<IRequestChannel> (bc);
133 var uri = new Uri ("http://localhost:37564");
134 cf.CreateChannel (new EndpointAddress (uri));
138 public void BuildChannelFactory ()
140 var be = new DiscoveryClientBindingElement ();
141 var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
142 var cf = be.BuildChannelFactory<IRequestChannel> (bc);
144 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
145 Assert.AreEqual (DiscoveryClientBindingElement.DiscoveryEndpointAddress, ch.RemoteAddress, "#1");
148 // This test takes a while, so I in fact don't want to enable it ...
150 [ExpectedException (typeof (EndpointNotFoundException))]
151 public void RequestChannelOpenFails ()
153 var be = new DiscoveryClientBindingElement ();
154 var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
155 var cf = be.BuildChannelFactory<IRequestChannel> (bc);
157 Assert.IsNull (cf.GetProperty<DiscoveryEndpoint> (), "#1");
158 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
159 Assert.IsNull (ch.GetProperty<DiscoveryEndpoint> (), "#2");
160 ch.Open (TimeSpan.FromSeconds (80));
163 // This test takes a while, so I in fact don't want to enable it ...
165 [ExpectedException (typeof (EndpointNotFoundException))]
166 public void RequestChannelOpenFails2 ()
168 var be = new DiscoveryClientBindingElement ();
169 var bc = new BindingContext (new CustomBinding (be, new TcpTransportBindingElement ()), new BindingParameterCollection ());
170 var cf = be.BuildChannelFactory<IDuplexSessionChannel> (bc);
172 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
173 ch.Open (TimeSpan.FromSeconds (80));
177 public void CustomDiscoveryEndpointProvider ()
179 var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new MyDiscoveryEndpointProvider () };
180 var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
181 var cf = be.BuildChannelFactory<IRequestChannel> (bc);
183 var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
186 Assert.Fail ("Should try to use failing endpoint provider.");
187 } catch (MyException) {
191 class MyDiscoveryEndpointProvider : DiscoveryEndpointProvider
193 public override DiscoveryEndpoint GetDiscoveryEndpoint ()
195 throw new MyException ();
200 class MyException : Exception
205 public void GetProperty ()
207 var be = new DiscoveryClientBindingElement ();
208 var bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
209 // so, they are not part of GetProperty<T>() return values.
210 Assert.IsNull (be.GetProperty<FindCriteria> (bc), "#1");
211 Assert.IsNull (be.GetProperty<DiscoveryEndpointProvider> (bc), "#2");
213 Assert.IsNull (be.GetProperty<DiscoveryEndpoint> (bc), "#3");