Fix wrong async completion handling in UDP channel receive.
[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                         // 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");
75
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");
83                 }
84
85                 [Test]
86                 public void CanBuildChannels ()
87                 {
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");
96
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");
102
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");
108
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");
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (InvalidOperationException))]
118                 public void BuildChannelFactory_OnlyTransport ()
119                 {
120                         var be = new DiscoveryClientBindingElement ();
121                         var bc = new BindingContext (new CustomBinding (new HttpTransportBindingElement ()), new BindingParameterCollection ());
122                         be.BuildChannelFactory<IRequestChannel> (bc);
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (ArgumentException))]
127                 public void BuildChannelFactory_CreateNonDynamicUriChannel ()
128                 {
129                         var be = new DiscoveryClientBindingElement ();
130                         var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
131                         var cf = be.BuildChannelFactory<IRequestChannel> (bc);
132                         cf.Open ();
133                         var uri = new Uri ("http://localhost:37564");
134                         cf.CreateChannel (new EndpointAddress (uri));
135                 }
136
137                 [Test]
138                 public void BuildChannelFactory ()
139                 {
140                         var be = new DiscoveryClientBindingElement ();
141                         var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
142                         var cf = be.BuildChannelFactory<IRequestChannel> (bc);
143                         cf.Open ();
144                         var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
145                         Assert.AreEqual (DiscoveryClientBindingElement.DiscoveryEndpointAddress, ch.RemoteAddress, "#1");
146                 }
147
148                 // This test takes a while, so I in fact don't want to enable it ...
149                 [Test]
150                 [ExpectedException (typeof (EndpointNotFoundException))]
151                 public void RequestChannelOpenFails ()
152                 {
153                         var be = new DiscoveryClientBindingElement ();
154                         var bc = new BindingContext (new CustomBinding (be, new HttpTransportBindingElement ()), new BindingParameterCollection ());
155                         var cf = be.BuildChannelFactory<IRequestChannel> (bc);
156                         cf.Open ();
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));
161                 }
162
163                 // This test takes a while, so I in fact don't want to enable it ...
164                 [Test]
165                 [ExpectedException (typeof (EndpointNotFoundException))]
166                 public void RequestChannelOpenFails2 ()
167                 {
168                         var be = new DiscoveryClientBindingElement ();
169                         var bc = new BindingContext (new CustomBinding (be, new TcpTransportBindingElement ()), new BindingParameterCollection ());
170                         var cf = be.BuildChannelFactory<IDuplexSessionChannel> (bc);
171                         cf.Open ();
172                         var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
173                         ch.Open (TimeSpan.FromSeconds (80));
174                 }
175
176                 [Test]
177                 public void CustomDiscoveryEndpointProvider ()
178                 {
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);
182                         cf.Open ();
183                         var ch = cf.CreateChannel (DiscoveryClientBindingElement.DiscoveryEndpointAddress);
184                         try {
185                                 ch.Open ();
186                                 Assert.Fail ("Should try to use failing endpoint provider.");
187                         } catch (MyException) {
188                         }
189                 }
190
191                 class MyDiscoveryEndpointProvider : DiscoveryEndpointProvider
192                 {
193                         public override DiscoveryEndpoint GetDiscoveryEndpoint ()
194                         {
195                                 throw new MyException ();
196                         }
197                         
198                 }
199
200                 class MyException : Exception
201                 {
202                 }
203
204                 [Test]
205                 public void GetProperty ()
206                 {
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");
212
213                         Assert.IsNull (be.GetProperty<DiscoveryEndpoint> (bc), "#3");
214                 }
215         }
216 }