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