Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System.ServiceModel.Discovery / Test / System.ServiceModel.Discovery / IntegratedDiscoveryTest.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.Net.Sockets;
30 using System.ServiceModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33 using System.ServiceModel.Discovery;
34 using System.ServiceModel.Dispatcher;
35 using System.Threading;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.ServiceModel.Discovery
39 {
40         [TestFixture]
41         public class IntegratedDiscoveryTest
42         {
43                 void AssertTcpPortOpen (int port)
44                 {
45                         try {
46                                 var l = new TcpListener (port);
47                                 l.Start ();
48                                 l.Stop ();
49                         } catch (Exception ex) {
50                                 Assert.Fail (String.Format ("TCP port {0} is not freed", port));
51                         }
52                 }
53
54                 [Test]
55                 [Category ("NotWorking")]
56                 public void UseCase1 ()
57                 {
58                         RunCodeUnderDiscoveryHost1 (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4949"), new Uri ("http://localhost:4989"), UseCase1Core);
59                         AssertTcpPortOpen (4949);
60                         AssertTcpPortOpen (4989);
61                         AssertTcpPortOpen (37564);
62                 }
63
64                 void UseCase1Core (Uri serviceUri, AnnouncementEndpoint aEndpoint, DiscoveryEndpoint dEndpoint)
65                 {
66                         // actual service, announcing to 4989
67                         var host = new ServiceHost (typeof (TestService));
68                         var sdb = new ServiceDiscoveryBehavior ();
69                         sdb.AnnouncementEndpoints.Add (aEndpoint);
70                         host.Description.Behaviors.Add (sdb);
71                         host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), serviceUri);
72                         host.Open ();
73                         // It does not start announcement very soon, so wait for a while.
74                         Thread.Sleep (1000);
75                         try {
76                                 // actual client, with DiscoveryClientBindingElement
77                                 var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new SimpleDiscoveryEndpointProvider (dEndpoint) };
78                                 var clientBinding = new CustomBinding (new BasicHttpBinding ());
79                                 clientBinding.Elements.Insert (0, be);
80                                 var cf = new ChannelFactory<ITestService> (clientBinding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
81                                 var ch = cf.CreateChannel ();
82                                 Assert.AreEqual ("TEST", ch.Echo ("TEST"), "#1");
83                                 cf.Close ();
84                         } finally {
85                                 host.Close ();
86                         }
87                 }
88
89                 void RunCodeUnderDiscoveryHost1 (Uri serviceUri, Uri dHostUri, Uri aHostUri, Action<Uri,AnnouncementEndpoint,DiscoveryEndpoint> action)
90                 {
91                         // announcement service
92                         var abinding = new CustomBinding (new HttpTransportBindingElement ());
93                         var aAddress = new EndpointAddress (aHostUri);
94                         var aEndpoint = new AnnouncementEndpoint (abinding, aAddress);
95                         
96                         // discovery service
97                         var dbinding = new CustomBinding (new HttpTransportBindingElement ());
98                         var dAddress = new EndpointAddress (dHostUri);
99                         var dEndpoint = new DiscoveryEndpoint (dbinding, dAddress);
100                         // Without this, .NET rejects the host as if it had no service.
101                         dEndpoint.IsSystemEndpoint = false;
102
103                         // it internally hosts an AnnouncementService
104                         using (var inst = new AnnouncementBoundDiscoveryService (aEndpoint)) {
105                                 var host = new ServiceHost (inst);
106                                 host.AddServiceEndpoint (dEndpoint);
107                                 try {
108                                         host.Open ();
109                                         action (serviceUri, aEndpoint, dEndpoint);
110                                 } finally {
111                                         host.Close ();
112                                 }
113                         }
114                 }
115
116                 [Test] // Announcement: UDP, Discovery: HTTP
117                 [Category ("NotWorking")]
118                 public void UseCase2 ()
119                 {
120                         RunCodeUnderDiscoveryHost2 (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4949"), UseCase2Core);
121                         AssertTcpPortOpen (4949);
122                         AssertTcpPortOpen (37564);
123                 }
124
125                 void UseCase2Core (Uri serviceUri, AnnouncementEndpoint aEndpoint, DiscoveryEndpoint dEndpoint)
126                 {
127                         // actual service, announcing to UDP
128                         var host = new ServiceHost (typeof (TestService));
129                         var sdb = new ServiceDiscoveryBehavior ();
130                         sdb.AnnouncementEndpoints.Add (aEndpoint);
131                         host.Description.Behaviors.Add (sdb);
132                         host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), serviceUri);
133                         host.Open ();
134                         // It does not start announcement very soon, so wait for a while.
135                         Thread.Sleep (1000);
136                         try {
137                                 // actual client, with DiscoveryClientBindingElement
138                                 var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new SimpleDiscoveryEndpointProvider (dEndpoint) };
139                                 var clientBinding = new CustomBinding (new BasicHttpBinding ());
140                                 clientBinding.Elements.Insert (0, be);
141                                 var cf = new ChannelFactory<ITestService> (clientBinding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
142                                 var ch = cf.CreateChannel ();
143                                 Assert.AreEqual ("TEST", ch.Echo ("TEST"), "#1");
144                         } finally {
145                                 host.Close ();
146                         }
147                 }
148
149                 void RunCodeUnderDiscoveryHost2 (Uri serviceUri, Uri dHostUri, Action<Uri,AnnouncementEndpoint,DiscoveryEndpoint> action)
150                 {
151                         // announcement service
152                         var aEndpoint = new UdpAnnouncementEndpoint (new Uri ("soap.udp://239.255.255.250:3802/"));
153                         
154                         // discovery service
155                         var dbinding = new CustomBinding (new HttpTransportBindingElement ());
156                         var dAddress = new EndpointAddress (dHostUri);
157                         var dEndpoint = new DiscoveryEndpoint (dbinding, dAddress);
158                         // Without this, .NET rejects the host as if it had no service.
159                         dEndpoint.IsSystemEndpoint = false;
160
161                         // it internally hosts an AnnouncementService
162                         using (var inst = new AnnouncementBoundDiscoveryService (aEndpoint)) {
163                                 var host = new ServiceHost (inst);
164                                 host.AddServiceEndpoint (dEndpoint);
165                                 try {
166                                         host.Open ();
167                                         action (serviceUri, aEndpoint, dEndpoint);
168                                 } finally {
169                                         host.Close ();
170                                 }
171                         }
172                 }
173
174                 [Test]
175                 [Category ("NotWorking")]
176                 public void UseCase3 ()
177                 {
178                         RunCodeUnderDiscoveryHost3 (new Uri ("http://localhost:37564"), new Uri ("http://localhost:4989"), UseCase3Core);
179                         AssertTcpPortOpen (4989);
180                         AssertTcpPortOpen (37564);
181                 }
182
183                 void UseCase3Core (Uri serviceUri, AnnouncementEndpoint aEndpoint, DiscoveryEndpoint dEndpoint)
184                 {
185                         // actual service, announcing to 4989
186                         var host = new ServiceHost (typeof (TestService));
187                         var sdb = new ServiceDiscoveryBehavior ();
188                         sdb.AnnouncementEndpoints.Add (aEndpoint);
189                         host.Description.Behaviors.Add (sdb);
190                         host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), serviceUri);
191                         host.Open ();
192                         // It does not start announcement very soon, so wait for a while.
193                         Thread.Sleep (1000);
194                         try {
195                                 // actual client, with DiscoveryClientBindingElement
196                                 var be = new DiscoveryClientBindingElement () { DiscoveryEndpointProvider = new SimpleDiscoveryEndpointProvider (dEndpoint) };
197                                 var clientBinding = new CustomBinding (new BasicHttpBinding ());
198                                 clientBinding.Elements.Insert (0, be);
199                                 var cf = new ChannelFactory<ITestService> (clientBinding, DiscoveryClientBindingElement.DiscoveryEndpointAddress);
200                                 var ch = cf.CreateChannel ();
201                                 Assert.AreEqual ("TEST", ch.Echo ("TEST"), "#1");
202                                 cf.Close ();
203                         } finally {
204                                 host.Close ();
205                         }
206                 }
207
208                 void RunCodeUnderDiscoveryHost3 (Uri serviceUri, Uri aHostUri, Action<Uri,AnnouncementEndpoint,DiscoveryEndpoint> action)
209                 {
210                         // announcement service
211                         var abinding = new CustomBinding (new HttpTransportBindingElement ());
212                         var aAddress = new EndpointAddress (aHostUri);
213                         var aEndpoint = new AnnouncementEndpoint (abinding, aAddress);
214                         
215                         // discovery service
216                         var dEndpoint = new UdpDiscoveryEndpoint (DiscoveryVersion.WSDiscovery11, new Uri ("soap.udp://239.255.255.250:3802/"));
217                         // Without this, .NET rejects the host as if it had no service.
218                         dEndpoint.IsSystemEndpoint = false;
219
220                         // it internally hosts an AnnouncementService
221                         using (var inst = new AnnouncementBoundDiscoveryService (aEndpoint)) {
222                                 var host = new ServiceHost (inst);
223                                 host.AddServiceEndpoint (dEndpoint);
224                                 try {
225                                         host.Open ();
226                                         action (serviceUri, aEndpoint, dEndpoint);
227                                 } finally {
228                                         host.Close ();
229                                 }
230                         }
231                 }
232
233                 class SimpleDiscoveryEndpointProvider : DiscoveryEndpointProvider
234                 {
235                         public SimpleDiscoveryEndpointProvider (DiscoveryEndpoint endpoint)
236                         {
237                                 this.endpoint = endpoint;
238                         }
239                         
240                         DiscoveryEndpoint endpoint;
241                         
242                         public override DiscoveryEndpoint GetDiscoveryEndpoint ()
243                         {
244                                 return endpoint;
245                         }
246                 }
247         }
248 }