Merge pull request #5444 from hifi/fix-tds-inputoutput
[mono.git] / mcs / class / System.ServiceModel / Test / FeatureBased / TestFixtureBase.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ServiceModel;
5 using System.ServiceModel.Channels;
6 using System.ServiceModel.Description;
7 using NUnit.Framework;
8 using System.Reflection;
9 using System.Threading;
10 using System.Configuration;
11 using System.IO;
12 using System.Net;
13 using MonoTests.stand_alone.WebHarness;
14 using System.ServiceModel.Dispatcher;
15 using System.Collections.ObjectModel;
16
17 using MonoTests.Helpers;
18
19 namespace MonoTests.Features
20 {
21         public class Configuration
22         {
23                 static Configuration() {
24                         var port = NetworkHelpers.FindFreePort ();
25                         onlyServers = Boolean.Parse (ConfigurationManager.AppSettings ["onlyServers"]  ?? "false");
26                         onlyClients = Boolean.Parse (ConfigurationManager.AppSettings ["onlyClients"]  ?? "false");
27                         endpointBase = ConfigurationManager.AppSettings ["endpointBase"] ?? $"http://localhost:{port}/";
28                         if (!endpointBase.EndsWith ("/"))
29                                 endpointBase = endpointBase + '/';
30                         logMessages = Boolean.Parse (ConfigurationManager.AppSettings ["logMessages"] ?? "false");
31                 }
32                 public static bool onlyServers;
33                 public static bool onlyClients;
34                 public static string endpointBase;
35                 public static bool logMessages;
36                 public static bool IsLocal { get { return !onlyServers && !onlyClients; } }
37         }
38
39         class LoggerMessageInspector : IDispatchMessageInspector
40         {
41                 #region IDispatchMessageInspector Members
42
43                 public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
44                         Console.WriteLine ("****Begin message received by host:");
45                         Console.WriteLine (request);
46                         Console.WriteLine ("****End message received by host:");
47                         return new object ();
48                 }
49
50                 public void BeforeSendReply (ref Message reply, object correlationState) {
51                         Console.WriteLine ("****Begin message reply from host:");
52                         Console.WriteLine (reply);
53                         Console.WriteLine ("****End message reply from host:");
54                 }
55
56                 #endregion
57         }
58         class LoggerBehavior : IServiceBehavior
59         {
60
61                 #region IServiceBehavior Members
62
63                 public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
64
65                 }
66
67                 public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
68                         ChannelDispatcher dispatcher = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
69                         dispatcher.Endpoints [0].DispatchRuntime.MessageInspectors.Add (new LoggerMessageInspector ());
70                 }
71
72                 public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
73
74                 }
75
76                 #endregion
77         }
78
79         public abstract class TestFixtureBase<TClient, TServer, IServer> where TClient : new() where TServer: new()
80         {
81                 ServiceHost _hostBase;
82                 ChannelFactory<IServer> factory;
83
84                 protected TestFixtureBase () { }                
85
86                 [TearDown]
87                 public void TearDown ()
88                 {
89                         if (_hostBase != null)
90                                 _hostBase.Close ();
91                         if (factory != null)
92                                 factory.Close ();
93                 }
94
95                 [SetUp]
96                 public virtual void Run (){
97                         bool runServer = true;
98                         bool runClient = true;
99                         if (Configuration.onlyClients)
100                                 runServer = false;
101                         if (Configuration.onlyServers)
102                                 runClient = false;
103                         Run (runServer, runClient);                     
104                 }
105
106                 public void CheckWsdlImpl () {
107                         string goldWsdl;
108                         try {
109                                 Assembly _assembly = Assembly.GetExecutingAssembly ();
110                                 StreamReader _stream = new StreamReader (_assembly.GetManifestResourceStream ("MonoTests.System.ServiceModel.Test.FeatureBased.Features.Contracts." + typeof (TServer).Name + ".xml"));
111                                 goldWsdl = _stream.ReadToEnd ();
112                         }
113                         catch {
114                                 Console.WriteLine ("Couldn't test WSDL of server " + typeof (TServer).Name + " because gold wsdl is not embedded in test !");
115                                 return;
116                         }
117                         string currentWsdl = "";
118
119                         HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create (getMexEndpoint () + "?wsdl");
120                         // Obtain a 'Stream' object associated with the response object.
121                         WebResponse response = myReq.GetResponse ();
122                         Stream ReceiveStream = response.GetResponseStream ();
123
124                         Encoding encode = global::System.Text.Encoding.GetEncoding ("utf-8");
125
126                         // Pipe the stream to a higher level stream reader with the required encoding format. 
127                         StreamReader readStream = new StreamReader (ReceiveStream, encode);
128                         Console.WriteLine ("\nResponse stream received");
129                         int maxLen = 10 * 1024;
130                         Char [] read = new Char [maxLen];
131
132                         // Read 256 charcters at a time.    
133                         int count = readStream.Read (read, 0, maxLen);
134                         while (count > 0) {
135                                 currentWsdl = currentWsdl + new String (read, 0, count);
136                                 count = readStream.Read (read, 0, 256);
137                         }
138                         readStream.Close ();
139                         response.Close ();
140
141                         XmlComparer comparer = new XmlComparer (XmlComparer.Flags.IgnoreAttribOrder, true);
142                         Assert.IsTrue (comparer.AreEqual (goldWsdl, currentWsdl), "Service WSDL does not match gold WSDL");
143
144                 }
145
146                 protected void Run (bool runServer, bool runClient) {
147
148                         if (runServer) {
149                                 _hostBase = InitializeServiceHost ();
150                                 _hostBase.Open ();
151                         }
152
153                 }
154
155         string getEndpoint()
156         {
157                         return Configuration.endpointBase + typeof(TServer).Name;
158         }
159
160                 public string getMexEndpoint () 
161                 {
162                         return getEndpoint () + "_wsdl"; // should be getEndpoint() but currently implementation is buggy
163                 }
164
165                 TClient _client;
166                 protected virtual TClient InitializeClient () {
167                         //return new TClient(new BasicHttpBinding(), new EndpointAddress( getEndpoint) );
168                         Type [] paramsTypes = new Type [] { typeof (Binding), typeof (EndpointAddress) };
169                         object [] parameters = new object [] { new BasicHttpBinding (), new EndpointAddress (getEndpoint())};
170
171                         ConstructorInfo info = typeof (TClient).GetConstructor (paramsTypes);
172                         return (TClient) info.Invoke (parameters);
173                 }
174
175                 public TClient ClientProxy {
176                         get {
177                                 if (_client == null)
178                                         _client = InitializeClient ();                  
179                                 return _client;
180                         }                       
181                 }
182
183                 public IServer Client {
184                         get {
185                                 factory = new ChannelFactory<IServer> (new BasicHttpBinding (), new EndpointAddress (getEndpoint ()));
186                                 return factory.CreateChannel ();
187                         }
188                 }
189
190                 protected virtual ServiceHost InitializeServiceHost () {
191             ServiceHost host = new ServiceHost(typeof(TServer));
192             host.AddServiceEndpoint(typeof(IServer), new BasicHttpBinding(), getEndpoint());
193                         ServiceMetadataBehavior smb = new ServiceMetadataBehavior ();
194                         smb.HttpGetEnabled = true;
195                         smb.HttpGetUrl = new Uri (getMexEndpoint ());
196                         host.Description.Behaviors.Add (smb);
197                         host.Description.Behaviors.Add (new ServiceThrottlingBehavior () { MaxConcurrentCalls = 1, MaxConcurrentSessions = 1 });
198                         if (Configuration.logMessages)
199                                 host.Description.Behaviors.Add (new LoggerBehavior ());
200             return host;
201                 }
202
203
204                 protected ServiceHost Host {
205                         get {
206                                 return _hostBase;
207                         }
208                 }
209
210                 [TearDown]
211                 protected virtual void Close () {
212                         if (!Configuration.onlyClients && !Configuration.onlyServers &&  Host.State == CommunicationState.Opened)
213                                 Host.Close ();
214                 }
215         }
216 }