2008-09-12 Jeffrey Stedfast <fejj@novell.com>
[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.System.Runtime.Remoting.Channels.Tcp
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 ("tcp://localhost", "tcp://localhost", null),
104                         new ParseURLTestCase ("tCp://localhost", "tCp://localhost", null),
105                         new ParseURLTestCase ("tcp://localhost:/", "tcp://localhost:", "/"),
106                         new ParseURLTestCase ("tcp://localhost:9090", "tcp://localhost:9090", null),
107                         new ParseURLTestCase ("tcp://localhost:9090/", "tcp://localhost:9090", "/"),
108                         new ParseURLTestCase ("tcp://localhost:9090/RemoteObject.rem", "tcp://localhost:9090", "/RemoteObject.rem"),
109                         new ParseURLTestCase ("tcp://localhost:q24691247abc1297/RemoteObject.rem", "tcp://localhost:q24691247abc1297", "/RemoteObject.rem"),
110                 };
111                 
112                 [Test] // TcpChannel.Parse ()
113                 public void ParseURL ()
114                 {
115                         TcpChannel channel;
116                         int i;
117                         
118                         channel = new TcpChannel ();
119                         
120                         for (i = 0; i < ParseURLTests.Length; i++) {
121                                 string retval, objectURI;
122                                 
123                                 retval = channel.Parse (ParseURLTests[i].input, out objectURI);
124                                 
125                                 Assert.AreEqual (ParseURLTests[i].retval, retval, "#C1");
126                                 Assert.AreEqual (ParseURLTests[i].objectURI, objectURI, "#C2");
127                         }
128                 }
129                 
130                 public class MarshalObject : ContextBoundObject
131                 {
132                         public MarshalObject ()
133                         {
134                         }
135                 }
136         }
137 }