5 // Gert Driesen <drieseng@users.sourceforge.net>
7 // Copyright (C) 2008 Gert Driesen
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
32 using System.Runtime.Remoting;
33 using System.Runtime.Remoting.Channels;
34 using System.Runtime.Remoting.Channels.Tcp;
36 using NUnit.Framework;
38 using MonoTests.Helpers;
40 namespace MonoTests.Remoting
43 public class TcpChannelTest
45 [Test] // TcpChannel (IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)
46 public void Constructor3 ()
48 const string SERVICE_URI = "MarshalSvc";
53 MarshalObject marshal = new MarshalObject ();
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);
62 ChannelServices.RegisterChannel (chn);
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");
74 ChannelServices.UnregisterChannel (chn);
76 chn = new TcpChannel ((IDictionary) null, null, null);
78 ChannelServices.RegisterChannel (chn);
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");
86 ChannelServices.UnregisterChannel (chn);
89 struct ParseURLTestCase {
90 public readonly string input;
91 public readonly string retval;
92 public readonly string objectURI;
94 public ParseURLTestCase (string s0, string s1, string s2)
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"),
116 [Test] // TcpChannel.Parse ()
117 public void ParseURL ()
122 channel = new TcpChannel ();
124 for (i = 0; i < ParseURLTests.Length; i++) {
125 string retval, objectURI;
127 retval = channel.Parse (ParseURLTests[i].input, out objectURI);
129 Assert.AreEqual (ParseURLTests[i].retval, retval, "#C1");
130 Assert.AreEqual (ParseURLTests[i].objectURI, objectURI, "#C2");
134 public class MarshalObject : ContextBoundObject
136 public MarshalObject ()
141 TcpServerChannel GetServerChannel (string name, int port)
143 TcpServerChannel serverChannel = new TcpServerChannel (name + "Server", port);
144 ChannelServices.RegisterChannel (serverChannel);
146 RemotingConfiguration.RegisterWellKnownServiceType (
147 typeof (RemoteObject), "RemoteObject.rem",
148 WellKnownObjectMode.Singleton);
150 return serverChannel;
153 TcpClientChannel GetClientChannel (string name, string uri)
155 TcpClientChannel clientChannel = new TcpClientChannel (name + "Client", null);
156 ChannelServices.RegisterChannel (clientChannel);
158 WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry (
159 typeof (RemoteObject), uri + "/RemoteObject.rem");
160 RemotingConfiguration.RegisterWellKnownClientType (remoteType);
162 return clientChannel;
166 [Category ("NotWorking")] // seems to hang - "too many open files" ???
167 [Ignore ("Fails on MS")]
168 public void TestTcpRemoting ()
170 TcpServerChannel serverChannel = GetServerChannel ("TcpRemotingTest", 9090);
171 string uri = serverChannel.GetChannelUri ();
173 Assert.IsNotNull (uri, "Server channel URI is null");
175 TcpClientChannel clientChannel = GetClientChannel ("TcpRemotingTest", uri);
177 RemoteObject remoteObject = new RemoteObject ();
179 Assert.IsTrue (remoteObject.ReturnOne () == 1, "Invoking RemoteObject.ReturnOne() failed");