File: ClientBaseTest.cs

package info (click to toggle)
mono 5.18.0.240%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,253,216 kB
  • sloc: cs: 10,925,936; xml: 2,804,987; ansic: 643,970; cpp: 120,384; perl: 59,272; asm: 21,383; sh: 20,162; makefile: 18,157; python: 4,715; pascal: 924; sql: 859; sed: 16; php: 1
file content (590 lines) | stat: -rw-r--r-- 15,641 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//
// ClientBaseTest.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2006 Novell, Inc.  http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if !MOBILE && !XAMMAC_4_5
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using NUnit.Framework;

using MonoTests.Helpers;

namespace MonoTests.System.ServiceModel
{
	[TestFixture]
	public class ClientBaseTest
	{
/*
		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void GenericTypeArgumentIsServiceContract ()
		{
			new MyClientBase<ICloneable> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:4126"));
		}
*/

/*
		public class MyClientBase<T> : ClientBase<T>
		{
			public MyClientBase (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}
		}

		public class MyClientBase1 : ClientBase<TestService>
		{
			public MyClientBase1 (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}
		}

		[Test]
		public void ClassTypeArg ()
		{
			new MyClientBase1 (new BasicHttpBinding (), new EndpointAddress ("urn:dummy"));
		}
*/

		[ServiceContract]
		public interface ITestService
		{
			[OperationContract]
			string Foo (string arg);
		}

		public class TestService : ITestService
		{
			public string Foo (string arg)
			{
				return arg;
			}
		}

		[ServiceContract]
		public interface ITestService2
		{
			[OperationContract]
			void Bar (string arg);
		}

		public class TestService2 : ITestService2
		{
			public void Bar (string arg)
			{
			}
		}

		[Test]
		[Ignore ("hmm, this test shows that MSDN documentation does not match the fact.")]
		public void Foo ()
		{
			Type t = typeof (ClientBase<ITestService>).GetGenericTypeDefinition ().GetGenericArguments () [0];
			Assert.IsTrue (t.IsGenericParameter);
			Assert.AreEqual (GenericParameterAttributes.None, t.GenericParameterAttributes);
		}

		class MyChannelFactory<T> : ChannelFactory<T>
		{
			public MyChannelFactory (Binding b, EndpointAddress e)
				: base (b, e)
			{
			}

			public IChannelFactory GimmeFactory ()
			{
				return CreateFactory ();
			}
		}

		#region UseCase1

		ServiceHost host;

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ClientBaseCtorArgsTest1 ()
		{
			new CtorUseCase1 (null, new BasicHttpBinding (), new EndpointAddress ("http://test"));
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ClientBaseCtorArgsTest2 ()
		{
			new CtorUseCase1 (null, new EndpointAddress ("http://test"));
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ClientBaseCtorArgsTest3 ()
		{
			new CtorUseCase1 (null, "http://test");
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ClientBaseCtorArgsTest4 ()
		{
			new CtorUseCase1 ("CtorUseCase1_1", null);
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void ClientBaseCtorArgsTest5 ()
		{
			new CtorUseCase1 (new BasicHttpBinding (), null);
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void ClientBaseCtorArgsTest6 ()
		{
			new CtorUseCase1 ("CtorUseCase1_Incorrect");
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void ClientBaseCtorArgsTest7 ()
		{
			new CtorUseCase3 ();
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void ClientBaseCtorConfigAmbiguityTest ()
		{
			new CtorUseCase2 ();
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void ClientBaseCtorConfigAmbiguityTest2 ()
		{
			new CtorUseCase2 ("*");
		}

		[Test]
		[Ignore ("fails under .NET; I never bothered to fix the test")]
		public void ClientBaseConfigEmptyCtor ()
		{
			new CtorUseCase1 ();
		}

		[Test]
		[Ignore ("fails under .NET; I never bothered to fix the test")]
		public void ClientBaseConfigCtor ()
		{
			new CtorUseCase1 ("CtorUseCase1_1");
		}

		[Test]
		[Ignore ("fails under .NET; I never bothered to fix the test")]
		public void ClientBaseConfigCtorUsingDefault ()
		{
			new CtorUseCase1 ("*");
		}

		[Test]
		[Ignore ("With Orcas it does not work fine")]
		public void UseCase1Test ()
		{
			int port = NetworkHelpers.FindFreePort ();
			// almost equivalent to samples/clientbase/samplesvc.cs
			using (host = new ServiceHost (typeof (UseCase1))) {
				Binding binding = new BasicHttpBinding ();
				binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
				host.AddServiceEndpoint (typeof (IUseCase1).FullName, binding, new Uri ("http://localhost:" + port));

				host.Open ();
				// almost equivalent to samples/clientbase/samplecli.cs
				using (UseCase1Proxy proxy = new UseCase1Proxy (
					new BasicHttpBinding (),
					new EndpointAddress ("http://localhost:" + port))) {
					proxy.Open ();
					Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"));
				}
			}
			EnsurePortNonBlocking (port);
		}

		void EnsurePortNonBlocking (int port)
		{
			TcpListener l = new TcpListener (port);
			l.ExclusiveAddressUse = true;
			l.Start ();
			l.Stop ();
		}

		[ServiceContract]
		public interface IUseCase1
		{
			[OperationContract]
			string Echo (string msg);
		}

		public class UseCase1 : IUseCase1
		{
			public string Echo (string msg)
			{
				return msg + msg;
			}
		}

		public class UseCase1Proxy : ClientBase<IUseCase1>, IUseCase1
		{
			public UseCase1Proxy (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}

			public string Echo (string msg)
			{
				return Channel.Echo (msg);
			}
		}

		public class CtorUseCase1 : ClientBase<ICtorUseCase1>, ICtorUseCase1
		{
			public CtorUseCase1 ()
				: base ()
			{
			}

			public CtorUseCase1 (string configName)
				: base (configName)
			{
			}

			public CtorUseCase1 (string configName, string address)
				: base (configName, address)
			{
			}

			public CtorUseCase1 (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}

			public CtorUseCase1 (InstanceContext i, Binding binding, EndpointAddress address)
				: base (i, binding, address)
			{
			}

			public string Echo (string msg)
			{
				return Channel.Echo (msg);
			}
		}

		public class CtorUseCase2 : ClientBase<ICtorUseCase2>, ICtorUseCase2
		{
			public CtorUseCase2 ()
				: base ()
			{
			}

			public CtorUseCase2 (string configName)
				: base (configName)
			{
			}

			public CtorUseCase2 (string configName, string address)
				: base (configName, address)
			{
			}

			public CtorUseCase2 (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}

			public CtorUseCase2 (InstanceContext i, Binding binding, EndpointAddress address)
				: base (i, binding, address)
			{
			}

			public string Echo (string msg)
			{
				return Channel.Echo (msg);
			}
		}

		public class CtorUseCase3 : ClientBase<ICtorUseCase3>, ICtorUseCase3
		{
			public string Echo (string msg)
			{
				return Channel.Echo (msg);
			}
		}

		#endregion

		// For contract that directly receives and sends Message instances.
		#region UseCase2
		[Test]
		[Ignore ("With Orcas it does not work fine")]
		public void UseCase2Test ()
		{
			// almost equivalent to samples/clientbase/samplesvc2.cs
			ServiceHost host = new ServiceHost (typeof (UseCase2));
			Binding binding = new BasicHttpBinding ();
			binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
			int port = NetworkHelpers.FindFreePort ();
			host.AddServiceEndpoint (typeof (IUseCase2).FullName,
			binding, new Uri ("http://localhost:" + port));

			try {
				host.Open ();
				// almost equivalent to samples/clientbase/samplecli2.cs
				Binding b = new BasicHttpBinding ();
				b.SendTimeout = TimeSpan.FromSeconds (15);
				b.ReceiveTimeout = TimeSpan.FromSeconds (15);
				UseCase2Proxy proxy = new UseCase2Proxy (
					b,
					new EndpointAddress ("http://localhost:" + port + "/"));
				proxy.Open ();
				Message req = Message.CreateMessage (MessageVersion.Soap11, "http://tempuri.org/IUseCase2/Echo");
				Message res = proxy.Echo (req);
				using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
					res.WriteMessage (w);
				}
			} finally {
				if (host.State == CommunicationState.Opened)
					host.Close ();
				EnsurePortNonBlocking (port);
			}
		}

		[ServiceContract]
		public interface IUseCase2
		{
			[OperationContract]
			Message Echo (Message request);
		}

		class UseCase2 : IUseCase2
		{
			public Message Echo (Message request)
			{
				Message msg = Message.CreateMessage (request.Version, request.Headers.Action + "Response");
				msg.Headers.Add (MessageHeader.CreateHeader ("hoge", "urn:hoge", "heh"));
				//msg.Headers.Add (MessageHeader.CreateHeader ("test", "http://schemas.microsoft.com/ws/2005/05/addressing/none", "testing"));
				return msg;
			}
		}

		public class UseCase2Proxy : ClientBase<IUseCase2>, IUseCase2
		{
			public UseCase2Proxy (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}

			public Message Echo (Message request)
			{
				return Channel.Echo (request);
			}
		}

		#endregion

		[Test]
		[Ignore ("With Orcas it does not work fine")]
		public void UseCase3 ()
		{
			// almost equivalent to samples/clientbase/samplesvc3.cs
			ServiceHost host = new ServiceHost (typeof (MetadataExchange));
			host.Description.Behaviors.Find<ServiceDebugBehavior> ()
				.IncludeExceptionDetailInFaults = true;
			Binding bs = new BasicHttpBinding ();
			bs.SendTimeout = TimeSpan.FromSeconds (5);
			bs.ReceiveTimeout = TimeSpan.FromSeconds (5);
			int port = NetworkHelpers.FindFreePort ();
			// magic name that does not require fully qualified name ...
			host.AddServiceEndpoint ("IMetadataExchange",
			        bs, new Uri ("http://localhost:" + port));
			try {
				host.Open ();
				// almost equivalent to samples/clientbase/samplecli3.cs
				Binding bc = new BasicHttpBinding ();
				bc.SendTimeout = TimeSpan.FromSeconds (5);
				bc.ReceiveTimeout = TimeSpan.FromSeconds (5);
				MetadataExchangeProxy proxy = new MetadataExchangeProxy (
					bc,
					new EndpointAddress ("http://localhost:" + port + "/"));
				proxy.Open ();

				Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
				Message res = proxy.Get (req);
				using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
					res.WriteMessage (w);
				}
			} finally {
				if (host.State == CommunicationState.Opened)
					host.Close ();
				EnsurePortNonBlocking (port);
			}
		}

		class MetadataExchange : IMetadataExchange
		{
			public Message Get (Message request)
			{
				XmlDocument doc = new XmlDocument ();
				doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
				return Message.CreateMessage (request.Version,
				"http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
				new XmlNodeReader (doc));
			}

			public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
			{
				throw new NotImplementedException ();
			}

			public Message EndGet (IAsyncResult result)
			{
				throw new NotImplementedException ();
			}
		}

		[Test]
		public void ConstructorServiceEndpoint ()
		{
			// It is okay to pass ServiceEndpoint that does not have Binding or EndpointAddress.
			new MyClient (new ServiceEndpoint (ContractDescription.GetContract (typeof (IMetadataExchange)), null, null));
			try {
				new MyClient ((Binding) null, (EndpointAddress) null);
				Assert.Fail ("ArgumentNullException is expected");
			} catch (ArgumentNullException) {
			}
		}

		class MyClient : ClientBase<IMetadataExchange>
		{
			public MyClient (ServiceEndpoint endpoint)
				: base (endpoint)
			{
			}
			
			public MyClient (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}
		}

		[SerializableAttribute ()]
		[XmlTypeAttribute (Namespace = "http://mono.com/")]
		public class ValueWithXmlAttributes
		{
			[XmlElementAttribute (ElementName = "Name")]
			public string FakeName;
		}

		[ServiceContractAttribute (Namespace = "http://mono.com/")]
		public interface IXmlSerializerFormatService
		{
			[OperationContractAttribute (Action = "http://mono.com/Send", ReplyAction = "*")]
			[XmlSerializerFormatAttribute ()]
			void SendValueWithXmlAttributes (ValueWithXmlAttributes v);
		}

		class XmlSerializerFormatClient : ClientBase<IXmlSerializerFormatService>
		{
			public XmlSerializerFormatClient (Binding binding, EndpointAddress address)
				: base (binding, address)
			{
			}

			public void SendValue ()
			{
				var v = new ValueWithXmlAttributes () { FakeName = "name" };
				base.Channel.SendValueWithXmlAttributes (v);
			}
		}

		[Test]
		public void TestXmlAttributes ()
		{
			int port = NetworkHelpers.FindFreePort();
			var endpoint = new EndpointAddress ("http://localhost:" + port);
			var binding = new BasicHttpBinding ();
			var client = new XmlSerializerFormatClient (binding, endpoint);

			var server = new TcpListener (IPAddress.Any, port);
			server.Start ();

			var acceptTask = server.AcceptTcpClientAsync ();

			var t1 = new Task( () => client.SendValue ());
			t1.Start();

			if (!acceptTask.Wait (2000))
				Assert.Fail ("No request from client.");

			var netStream = acceptTask.Result.GetStream ();

			byte[] buffer = new byte [1024];
			int numBytesRead = 0;
			var message = new StringBuilder ();
			
			do {
				numBytesRead = netStream.Read (buffer, 0, buffer.Length);
				var str =  Encoding.UTF8.GetString (buffer, 0, numBytesRead);
				message.AppendFormat ("{0}", str);
				if (str.EndsWith ("</s:Envelope>", StringComparison.InvariantCulture))
					break;
			} while (numBytesRead > 0);

			var messageStr = message.ToString ();
			var envelopeIndex = messageStr.IndexOf ("<s:Envelope");
			if (envelopeIndex < 0)
				Assert.Fail ("Soap envelope was not received.");
			
			var envelope = messageStr.Substring (envelopeIndex);

			var expected = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SendValueWithXmlAttributes xmlns=\"http://mono.com/\"><v><Name>name</Name></v></SendValueWithXmlAttributes></s:Body></s:Envelope>";

			Assert.AreEqual (expected, envelope);

			server.Stop ();
		}
	}
}
#endif