[utils] Fix inet_pton fallback.
[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 namespace MonoTests.Remoting
39 {
40         [TestFixture]
41         public class TcpChannelTest
42         {
43                 [Test] // TcpChannel (IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)
44                 public void Constructor3 ()
45                 {
46                         const string SERVICE_URI = "MarshalSvc";
47                         string [] urls;
48                         ChannelDataStore ds;
49                         TcpChannel chn;
50
51                         MarshalObject marshal = new MarshalObject ();
52
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);
58
59                         ChannelServices.RegisterChannel (chn);
60
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");
70
71                         ChannelServices.UnregisterChannel (chn);
72                         
73                         chn = new TcpChannel ((IDictionary) null, null, null);
74
75                         ChannelServices.RegisterChannel (chn);
76
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");
82
83                         ChannelServices.UnregisterChannel (chn);
84                 }
85                 
86                 struct ParseURLTestCase {
87                         public readonly string input;
88                         public readonly string retval;
89                         public readonly string objectURI;
90                         
91                         public ParseURLTestCase (string s0, string s1, string s2)
92                         {
93                                 input = s0;
94                                 retval = s1;
95                                 objectURI = s2;
96                         }
97                 };
98                 
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"),
111                 };
112                 
113                 [Test] // TcpChannel.Parse ()
114                 public void ParseURL ()
115                 {
116                         TcpChannel channel;
117                         int i;
118                         
119                         channel = new TcpChannel ();
120                         
121                         for (i = 0; i < ParseURLTests.Length; i++) {
122                                 string retval, objectURI;
123                                 
124                                 retval = channel.Parse (ParseURLTests[i].input, out objectURI);
125                                 
126                                 Assert.AreEqual (ParseURLTests[i].retval, retval, "#C1");
127                                 Assert.AreEqual (ParseURLTests[i].objectURI, objectURI, "#C2");
128                         }
129                 }
130                 
131                 public class MarshalObject : ContextBoundObject
132                 {
133                         public MarshalObject ()
134                         {
135                         }
136                 }
137                 
138                 TcpServerChannel GetServerChannel (string name, int port)
139                 {
140                         TcpServerChannel serverChannel = new TcpServerChannel (name + "Server", port);
141                         ChannelServices.RegisterChannel (serverChannel);
142                         
143                         RemotingConfiguration.RegisterWellKnownServiceType (
144                                 typeof (RemoteObject), "RemoteObject.rem", 
145                                 WellKnownObjectMode.Singleton);
146                         
147                         return serverChannel;
148                 }
149                 
150                 TcpClientChannel GetClientChannel (string name, string uri)
151                 {
152                         TcpClientChannel clientChannel = new TcpClientChannel (name + "Client", null);
153                         ChannelServices.RegisterChannel (clientChannel);
154                         
155                         WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry (
156                                 typeof (RemoteObject), uri + "/RemoteObject.rem");
157                         RemotingConfiguration.RegisterWellKnownClientType (remoteType);
158                         
159                         return clientChannel;
160                 }
161                 
162                 [Test]
163                 [Category ("NotWorking")]  // seems to hang - "too many open files" ???
164                 [Ignore ("Fails on MS")]
165                 public void TestTcpRemoting ()
166                 {
167                         TcpServerChannel serverChannel = GetServerChannel ("TcpRemotingTest", 9090);
168                         string uri = serverChannel.GetChannelUri ();
169                         
170                         Assert.IsNotNull (uri, "Server channel URI is null");
171                         
172                         TcpClientChannel clientChannel = GetClientChannel ("TcpRemotingTest", uri);
173                         
174                         RemoteObject remoteObject = new RemoteObject (); 
175                         
176                         Assert.IsTrue (remoteObject.ReturnOne () == 1, "Invoking RemoteObject.ReturnOne() failed");
177                 }
178         }
179 }