Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / NetTcpBindingTest.cs
1 //
2 // NetTcpBindingTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.ObjectModel;
30 using System.Net.Sockets;
31 using System.Net.Security;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Security;
35 using System.Threading;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.ServiceModel
39 {
40         [TestFixture]
41         public class NetTcpBindingTest
42         {
43                 [Test]
44                 public void DefaultValues ()
45                 {
46                         var n = new NetTcpBinding ();
47                         Assert.AreEqual (HostNameComparisonMode.StrongWildcard, n.HostNameComparisonMode, "#1");
48                         Assert.AreEqual (10, n.ListenBacklog, "#2");
49                         Assert.AreEqual (false, n.PortSharingEnabled, "#3");
50
51                         var tr = n.CreateBindingElements ().Find<TcpTransportBindingElement> ();
52                         Assert.IsNotNull (tr, "#tr1");
53                         Assert.AreEqual (false, tr.TeredoEnabled, "#tr2");
54                         Assert.AreEqual ("net.tcp", tr.Scheme, "#tr3");
55
56                         Assert.IsFalse (n.TransactionFlow, "#4");
57                         var tx = n.CreateBindingElements ().Find<TransactionFlowBindingElement> ();
58                         Assert.IsNotNull (tx, "#tx1");
59                 }
60
61                 [Test]
62                 public void BufferedConnection ()
63                 {
64                         var host = new ServiceHost (typeof (Foo));
65                         var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement ());
66                         host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:37564/");
67                         host.Open (TimeSpan.FromSeconds (5));
68                         try {
69                                 var bindingcli = new NetTcpBinding () { TransactionFlow = false };
70                                 bindingcli.Security.Mode = SecurityMode.None;
71                                 var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
72                                 Assert.AreEqual (5, cli.Add (1, 4));
73                                 Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
74                         } finally {
75                                 host.Close (TimeSpan.FromSeconds (5));
76                                 var t = new TcpListener (37564);
77                                 t.Start ();
78                                 t.Stop ();
79                         }
80                         Assert.IsTrue (Foo.AddCalled, "#1");
81                         Assert.IsTrue (Foo.JoinCalled, "#2");
82                 }
83
84                 [Test]
85                 public void StreamedConnection ()
86                 {
87                         var host = new ServiceHost (typeof (Foo));
88                         var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement () { TransferMode = TransferMode.Streamed });
89                         host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:37564/");
90                         host.Open (TimeSpan.FromSeconds (5));
91                         try {
92                                 var bindingcli = new NetTcpBinding () { TransactionFlow = false };
93                                 bindingcli.TransferMode = TransferMode.Streamed;
94                                 bindingcli.Security.Mode = SecurityMode.None;
95                                 var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
96                                 Assert.AreEqual (5, cli.Add (1, 4));
97                                 Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
98                         } finally {
99                                 host.Close (TimeSpan.FromSeconds (5));
100                                 var t = new TcpListener (37564);
101                                 t.Start ();
102                                 t.Stop ();
103                         }
104                         Assert.IsTrue (Foo.AddCalled, "#1");
105                         Assert.IsTrue (Foo.JoinCalled, "#2");
106                 }
107
108                 [ServiceContract]
109                 public interface IFoo
110                 {
111                         [OperationContract]
112                         int Add (short s, int i);
113                         [OperationContract]
114                         string Join (string s1, string s2);
115                 }
116
117                 public interface IFooClient : IFoo, IClientChannel
118                 {
119                 }
120
121                 public class Foo : IFoo
122                 {
123                         public static bool AddCalled;
124                         public static bool JoinCalled;
125
126                         public int Add (short s, int i)
127                         {
128                                 AddCalled = true;
129                                 return s + i;
130                         }
131
132                         public string Join (string s1, string s2)
133                         {
134                                 JoinCalled = true;
135                                 return s1 + " " + s2;
136                         }
137                 }
138         }
139 }