Merge pull request #5428 from kumpera/wasm-support-p2
[mono.git] / mcs / class / System.Runtime.Remoting / Test / System.Runtime.Remoting.Channels.Tcp / TcpChannelTest.cs
1 //
2 // TcpChanneltest.cs
3 //
4 // Author:
5 //      Gert Driesen <drieseng@users.sourceforge.net>
6 //
7 // Copyright (C) 2008 Gert Driesen
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
29 using System;
30 using System.Collections;
31 using System.Net;
32 using System.Runtime.Remoting;
33 using System.Runtime.Remoting.Channels;
34 using System.Runtime.Remoting.Channels.Tcp;
35
36 using NUnit.Framework;
37
38 using MonoTests.Helpers;
39
40 namespace MonoTests.Remoting
41 {
42         [TestFixture]
43         public class TcpChannelTest
44         {
45                 [Test] // TcpChannel (IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)
46                 public void Constructor3 ()
47                 {
48                         const string SERVICE_URI = "MarshalSvc";
49                         string [] urls;
50                         ChannelDataStore ds;
51                         TcpChannel chn;
52
53                         MarshalObject marshal = new MarshalObject ();
54
55                         var port = NetworkHelpers.FindFreePort ();
56                         IDictionary props = new Hashtable ();
57                         props ["name"] = "marshal channel";
58                         props ["port"] = port;
59                         props ["bindTo"] = IPAddress.Loopback.ToString ();
60                         chn = new TcpChannel (props, null, null);
61
62                         ChannelServices.RegisterChannel (chn);
63
64                         Assert.AreEqual ("marshal channel", chn.ChannelName, "#A1");
65                         urls = chn.GetUrlsForUri (SERVICE_URI);
66                         Assert.IsNotNull (urls, "#A2");
67                         Assert.AreEqual (1, urls.Length, "#A3");
68                         Assert.AreEqual ($"tcp://{IPAddress.Loopback.ToString ()}:{port}/{SERVICE_URI}", urls [0], "#A6");
69                         ds = chn.ChannelData as ChannelDataStore;
70                         Assert.IsNotNull (ds, "#A4");
71                         Assert.AreEqual (1, ds.ChannelUris.Length, "#A5");
72                         Assert.AreEqual ($"tcp://{IPAddress.Loopback.ToString ()}:{port}", ds.ChannelUris [0], "#A6");
73
74                         ChannelServices.UnregisterChannel (chn);
75                         
76                         chn = new TcpChannel ((IDictionary) null, null, null);
77
78                         ChannelServices.RegisterChannel (chn);
79
80                         Assert.AreEqual ("tcp", chn.ChannelName, "#B1");
81                         urls = chn.GetUrlsForUri (SERVICE_URI);
82                         Assert.IsNull (urls, "#B1");
83                         ds = chn.ChannelData as ChannelDataStore;
84                         Assert.IsNull (ds, "#B2");
85
86                         ChannelServices.UnregisterChannel (chn);
87                 }
88                 
89                 struct ParseURLTestCase {
90                         public readonly string input;
91                         public readonly string retval;
92                         public readonly string objectURI;
93                         
94                         public ParseURLTestCase (string s0, string s1, string s2)
95                         {
96                                 input = s0;
97                                 retval = s1;
98                                 objectURI = s2;
99                         }
100                 };
101                 
102                 ParseURLTestCase[] ParseURLTests = new ParseURLTestCase[] {
103                         new ParseURLTestCase ("tcp:", "tcp:", null),
104                         new ParseURLTestCase ("tcp://", "tcp://", null),
105                         new ParseURLTestCase ("tcp:localhost", null, null),
106                         new ParseURLTestCase ("ftp://localhost", null, null),
107                         new ParseURLTestCase ("tcp://localhost", "tcp://localhost", null),
108                         new ParseURLTestCase ("tCp://localhost", "tCp://localhost", null),
109                         new ParseURLTestCase ("tcp://localhost:/", "tcp://localhost:", "/"),
110                         new ParseURLTestCase ("tcp://localhost:9090", "tcp://localhost:9090", null),
111                         new ParseURLTestCase ("tcp://localhost:9090/", "tcp://localhost:9090", "/"),
112                         new ParseURLTestCase ("tcp://localhost:9090/RemoteObject.rem", "tcp://localhost:9090", "/RemoteObject.rem"),
113                         new ParseURLTestCase ("tcp://localhost:q24691247abc1297/RemoteObject.rem", "tcp://localhost:q24691247abc1297", "/RemoteObject.rem"),
114                 };
115                 
116                 [Test] // TcpChannel.Parse ()
117                 public void ParseURL ()
118                 {
119                         TcpChannel channel;
120                         int i;
121                         
122                         channel = new TcpChannel ();
123                         
124                         for (i = 0; i < ParseURLTests.Length; i++) {
125                                 string retval, objectURI;
126                                 
127                                 retval = channel.Parse (ParseURLTests[i].input, out objectURI);
128                                 
129                                 Assert.AreEqual (ParseURLTests[i].retval, retval, "#C1");
130                                 Assert.AreEqual (ParseURLTests[i].objectURI, objectURI, "#C2");
131                         }
132                 }
133                 
134                 public class MarshalObject : ContextBoundObject
135                 {
136                         public MarshalObject ()
137                         {
138                         }
139                 }
140                 
141                 TcpServerChannel GetServerChannel (string name, int port)
142                 {
143                         TcpServerChannel serverChannel = new TcpServerChannel (name + "Server", port);
144                         ChannelServices.RegisterChannel (serverChannel);
145                         
146                         RemotingConfiguration.RegisterWellKnownServiceType (
147                                 typeof (RemoteObject), "RemoteObject.rem", 
148                                 WellKnownObjectMode.Singleton);
149                         
150                         return serverChannel;
151                 }
152                 
153                 TcpClientChannel GetClientChannel (string name, string uri)
154                 {
155                         TcpClientChannel clientChannel = new TcpClientChannel (name + "Client", null);
156                         ChannelServices.RegisterChannel (clientChannel);
157                         
158                         WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry (
159                                 typeof (RemoteObject), uri + "/RemoteObject.rem");
160                         RemotingConfiguration.RegisterWellKnownClientType (remoteType);
161                         
162                         return clientChannel;
163                 }
164                 
165                 [Test]
166                 [Category ("NotWorking")]  // seems to hang - "too many open files" ???
167                 [Ignore ("Fails on MS")]
168                 public void TestTcpRemoting ()
169                 {
170                         TcpServerChannel serverChannel = GetServerChannel ("TcpRemotingTest", 9090);
171                         string uri = serverChannel.GetChannelUri ();
172                         
173                         Assert.IsNotNull (uri, "Server channel URI is null");
174                         
175                         TcpClientChannel clientChannel = GetClientChannel ("TcpRemotingTest", uri);
176                         
177                         RemoteObject remoteObject = new RemoteObject (); 
178                         
179                         Assert.IsTrue (remoteObject.ReturnOne () == 1, "Invoking RemoteObject.ReturnOne() failed");
180                 }
181         }
182 }