1 //
2 // PeerTransportBindingElementTest.cs
3 //
4 // Author:
5 //	Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc.  http://www.novell.com
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 #if !MOBILE && !XAMMAC_4_5
29 using System;
30 using System.Collections.ObjectModel;
31 using System.Net;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.Text;
36 using NUnit.Framework;
37 
38 namespace MonoTests.System.ServiceModel.Channels
39 {
40 	[TestFixture]
41 	public class PeerTransportBindingElementTest
42 	{
43 		[Test]
CanBuildChannelFactoryListener()44 		public void CanBuildChannelFactoryListener ()
45 		{
46 			var be = new PeerTransportBindingElement ();
47 			var binding = new CustomBinding (new HandlerTransportBindingElement (null));
48 			var ctx = new BindingContext (binding, new BindingParameterCollection ());
49 			Assert.IsFalse (be.CanBuildChannelFactory<IRequestChannel> (ctx), "#1");
50 			Assert.IsTrue (be.CanBuildChannelFactory<IOutputChannel> (ctx), "#2");
51 			Assert.IsTrue  (be.CanBuildChannelFactory<IDuplexChannel> (ctx), "#3");
52 			Assert.IsFalse (be.CanBuildChannelFactory<IRequestSessionChannel> (ctx), "#4");
53 			Assert.IsFalse (be.CanBuildChannelFactory<IOutputSessionChannel> (ctx), "#5"); // oh?
54 			Assert.IsFalse (be.CanBuildChannelFactory<IDuplexSessionChannel> (ctx), "#6"); // really?
55 
56 			Assert.IsFalse (be.CanBuildChannelListener<IReplyChannel> (ctx), "#7");
57 			Assert.IsTrue (be.CanBuildChannelListener<IInputChannel> (ctx), "#8");
58 			Assert.IsTrue (be.CanBuildChannelListener<IDuplexChannel> (ctx), "#9");
59 			Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#10");
60 			Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#11"); // hrm...
61 			Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (ctx), "#12"); // ...k.
62 		}
63 
64 		[Test]
65 		[ExpectedException (typeof (ArgumentException))]
BuildRequestChannelFactory()66 		public void BuildRequestChannelFactory ()
67 		{
68 			// IRequestChannel is invalid
69 			PeerTransportBindingElement be =
70 				new PeerTransportBindingElement ();
71 			be.Security.Mode = SecurityMode.None;
72 			CustomBinding binding = new CustomBinding (
73 				new HandlerTransportBindingElement (null));
74 			BindingContext ctx = new BindingContext (
75 				binding, new BindingParameterCollection ());
76 
77 			var f = be.BuildChannelFactory<IRequestChannel> (ctx);
78 			Assert.IsNotNull (f.GetProperty<IOnlineStatus> (), "#1");
79 			Assert.IsNotNull (f.GetProperty<PeerNode> (), "#2");
80 		}
81 
82 		[Test]
BuildOutputChannelFactory()83 		public void BuildOutputChannelFactory ()
84 		{
85 			PeerTransportBindingElement be =
86 				new PeerTransportBindingElement ();
87 			be.Security.Mode = SecurityMode.None;
88 			CustomBinding binding = new CustomBinding (
89 				new HandlerTransportBindingElement (null));
90 			BindingContext ctx = new BindingContext (
91 				binding, new BindingParameterCollection ());
92 			be.BuildChannelFactory<IOutputChannel> (ctx);
93 		}
94 		[Test]
95 		[ExpectedException (typeof (ArgumentException))]
BuildReplyChannelListener()96 		public void BuildReplyChannelListener ()
97 		{
98 			// IReplyChannel is invalid
99 			PeerTransportBindingElement be =
100 				new PeerTransportBindingElement ();
101 			be.Security.Mode = SecurityMode.None;
102 			CustomBinding binding = new CustomBinding (
103 				new HandlerTransportBindingElement (null));
104 			BindingContext ctx = new BindingContext (
105 				binding, new BindingParameterCollection ());
106 			be.BuildChannelListener<IReplyChannel> (ctx);
107 		}
108 
109 		[Test]
BuildInputChannelListener()110 		public void BuildInputChannelListener ()
111 		{
112 			PeerTransportBindingElement be =
113 				new PeerTransportBindingElement ();
114 			be.Security.Mode = SecurityMode.None;
115 			CustomBinding binding = new CustomBinding (
116 				new HandlerTransportBindingElement (null));
117 			BindingContext ctx = new BindingContext (
118 				binding, new BindingParameterCollection ());
119 			ctx.ListenUriBaseAddress = new Uri ("net.p2p:foobar");
120 			be.BuildChannelListener<IInputChannel> (ctx);
121 		}
122 
123 		[Test]
124 		[ExpectedException (typeof (ArgumentException))]
125 		[Category ("NotWorking")]
InvalidListenIPAddress()126 		public void InvalidListenIPAddress ()
127 		{
128 			PeerTransportBindingElement be =
129 				new PeerTransportBindingElement ();
130 			be.Security.Mode = SecurityMode.None;
131 			be.ListenIPAddress = IPAddress.Parse ("127.0.0.1");
132 			CustomBinding binding = new CustomBinding (
133 				new HandlerTransportBindingElement (null));
134 			BindingContext ctx = new BindingContext (
135 				binding, new BindingParameterCollection ());
136 			ctx.ListenUriBaseAddress = new Uri ("net.p2p:foobar");
137 			be.BuildChannelListener<IInputChannel> (ctx);
138 		}
139 
140 		[Test]
141 		[Ignore ("It is documented that MaxBufferPoolSize must be greater than MaxReceivedMessageSize, but not really checked (at least here)")]
MaxBufferPoolSizeTooSmall()142 		public void MaxBufferPoolSizeTooSmall ()
143 		{
144 			PeerTransportBindingElement be =
145 				new PeerTransportBindingElement ();
146 			be.MaxBufferPoolSize = 0x1000;
147 			be.Security.Mode = SecurityMode.None;
148 			CustomBinding binding = new CustomBinding (
149 				new HandlerTransportBindingElement (null));
150 			BindingContext ctx = new BindingContext (
151 				binding, new BindingParameterCollection ());
152 			ctx.ListenUriBaseAddress = new Uri ("net.p2p:foobar");
153 			be.BuildChannelListener<IInputChannel> (ctx);
154 		}
155 	}
156 }
157 #endif
158