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 namespace MonoTests.Remoting
41 public class TcpChannelTest
43 [Test] // TcpChannel (IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)
44 public void Constructor3 ()
46 const string SERVICE_URI = "MarshalSvc";
51 MarshalObject marshal = new MarshalObject ();
53 IDictionary props = new Hashtable ();
54 props ["name"] = "marshal channel";
55 props ["port"] = 1236;
56 props ["bindTo"] = IPAddress.Loopback.ToString ();
57 chn = new TcpChannel (props, null, null);
59 ChannelServices.RegisterChannel (chn);
61 Assert.AreEqual ("marshal channel", chn.ChannelName, "#A1");
62 urls = chn.GetUrlsForUri (SERVICE_URI);
63 Assert.IsNotNull (urls, "#A2");
64 Assert.AreEqual (1, urls.Length, "#A3");
65 Assert.AreEqual ("tcp://" + IPAddress.Loopback.ToString () + ":1236/" + SERVICE_URI, urls [0], "#A6");
66 ds = chn.ChannelData as ChannelDataStore;
67 Assert.IsNotNull (ds, "#A4");
68 Assert.AreEqual (1, ds.ChannelUris.Length, "#A5");
69 Assert.AreEqual ("tcp://" + IPAddress.Loopback.ToString () + ":1236", ds.ChannelUris [0], "#A6");
71 ChannelServices.UnregisterChannel (chn);
73 chn = new TcpChannel ((IDictionary) null, null, null);
75 ChannelServices.RegisterChannel (chn);
77 Assert.AreEqual ("tcp", chn.ChannelName, "#B1");
78 urls = chn.GetUrlsForUri (SERVICE_URI);
79 Assert.IsNull (urls, "#B1");
80 ds = chn.ChannelData as ChannelDataStore;
81 Assert.IsNull (ds, "#B2");
83 ChannelServices.UnregisterChannel (chn);
86 struct ParseURLTestCase {
87 public readonly string input;
88 public readonly string retval;
89 public readonly string objectURI;
91 public ParseURLTestCase (string s0, string s1, string s2)
99 ParseURLTestCase[] ParseURLTests = new ParseURLTestCase[] {
100 new ParseURLTestCase ("tcp:", "tcp:", null),
101 new ParseURLTestCase ("tcp://", "tcp://", null),
102 new ParseURLTestCase ("tcp:localhost", null, null),
103 new ParseURLTestCase ("ftp://localhost", null, null),
104 new ParseURLTestCase ("tcp://localhost", "tcp://localhost", null),
105 new ParseURLTestCase ("tCp://localhost", "tCp://localhost", null),
106 new ParseURLTestCase ("tcp://localhost:/", "tcp://localhost:", "/"),
107 new ParseURLTestCase ("tcp://localhost:9090", "tcp://localhost:9090", null),
108 new ParseURLTestCase ("tcp://localhost:9090/", "tcp://localhost:9090", "/"),
109 new ParseURLTestCase ("tcp://localhost:9090/RemoteObject.rem", "tcp://localhost:9090", "/RemoteObject.rem"),
110 new ParseURLTestCase ("tcp://localhost:q24691247abc1297/RemoteObject.rem", "tcp://localhost:q24691247abc1297", "/RemoteObject.rem"),
113 [Test] // TcpChannel.Parse ()
114 public void ParseURL ()
119 channel = new TcpChannel ();
121 for (i = 0; i < ParseURLTests.Length; i++) {
122 string retval, objectURI;
124 retval = channel.Parse (ParseURLTests[i].input, out objectURI);
126 Assert.AreEqual (ParseURLTests[i].retval, retval, "#C1");
127 Assert.AreEqual (ParseURLTests[i].objectURI, objectURI, "#C2");
131 public class MarshalObject : ContextBoundObject
133 public MarshalObject ()
138 TcpServerChannel GetServerChannel (string name, int port)
140 TcpServerChannel serverChannel = new TcpServerChannel (name + "Server", port);
141 ChannelServices.RegisterChannel (serverChannel);
143 RemotingConfiguration.RegisterWellKnownServiceType (
144 typeof (RemoteObject), "RemoteObject.rem",
145 WellKnownObjectMode.Singleton);
147 return serverChannel;
150 TcpClientChannel GetClientChannel (string name, string uri)
152 TcpClientChannel clientChannel = new TcpClientChannel (name + "Client", null);
153 ChannelServices.RegisterChannel (clientChannel);
155 WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry (
156 typeof (RemoteObject), uri + "/RemoteObject.rem");
157 RemotingConfiguration.RegisterWellKnownClientType (remoteType);
159 return clientChannel;
163 [Category ("NotWorking")] // seems to hang - "too many open files" ???
164 public void TestTcpRemoting ()
166 TcpServerChannel serverChannel = GetServerChannel ("TcpRemotingTest", 9090);
167 string uri = serverChannel.GetChannelUri ();
169 Assert.IsNotNull (uri, "Server channel URI is null");
171 TcpClientChannel clientChannel = GetClientChannel ("TcpRemotingTest", uri);
173 RemoteObject remoteObject = new RemoteObject ();
175 Assert.IsTrue (remoteObject.ReturnOne () == 1, "Invoking RemoteObject.ReturnOne() failed");