1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
//
// SecurityMessagePropertyTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2006 Novell, Inc. http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if !MOBILE && !XAMMAC_4_5
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.Security;
using System.Security.Principal;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
using System.Security.Cryptography.Xml;
using System.Threading;
using NUnit.Framework;
using MonoTests.System.ServiceModel.Channels;
using MonoTests.Helpers;
namespace MonoTests.System.ServiceModel.Security
{
[TestFixture]
public class SecurityMessagePropertyTest
{
static X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
static X509Certificate2 cert2 = new X509Certificate2 ("Test/Resources/test2.pfx", "mono");
[ServiceContract]
public interface ICalc
{
[OperationContract]
int Sum (int a, int b);
[OperationContract (AsyncPattern = true)]
IAsyncResult BeginSum (int a, int b, AsyncCallback cb, object state);
int EndSum (IAsyncResult result);
}
public class CalcProxy : ClientBase<ICalc>, ICalc
{
public CalcProxy (Binding binding, EndpointAddress address)
: base (binding, address)
{
}
public int Sum (int a, int b)
{
return Channel.Sum (a, b);
}
public IAsyncResult BeginSum (int a, int b, AsyncCallback cb, object state)
{
return Channel.BeginSum (a, b, cb, state);
}
public int EndSum (IAsyncResult result)
{
return Channel.EndSum (result);
}
}
public class CalcService : ICalc
{
public int Sum (int a, int b)
{
return a + b;
}
public IAsyncResult BeginSum (int a, int b, AsyncCallback cb, object state)
{
return new CalcAsyncResult (a, b, cb, state);
}
public int EndSum (IAsyncResult result)
{
CalcAsyncResult c = (CalcAsyncResult) result;
return c.A + c.B;
}
}
class CalcAsyncResult : IAsyncResult
{
public int A, B;
AsyncCallback callback;
object state;
public CalcAsyncResult (int a, int b, AsyncCallback cb, object state)
{
A = a;
B = b;
callback = cb;
this.state = state;
}
public object AsyncState {
get { return state; }
}
public WaitHandle AsyncWaitHandle {
get { return null; }
}
public bool CompletedSynchronously {
get { return true; }
}
public bool IsCompleted {
get { return true; }
}
}
[Test]
public void GetOrCreateNonSecureMessage ()
{
Message m = Message.CreateMessage (MessageVersion.Default, "urn:myaction");
SecurityMessageProperty p =
SecurityMessageProperty.GetOrCreate (m);
Assert.IsNull (p.InitiatorToken, "#1");
Assert.IsNull (p.RecipientToken, "#2");
Assert.IsNull (p.ProtectionToken, "#3");
Assert.IsNull (p.TransportToken, "#4");
Assert.IsNull (p.ExternalAuthorizationPolicies, "#5");
// Assert.AreEqual (0, p.ExternalAuthorizationPolicies.Count, "#5");
Assert.IsFalse (p.HasIncomingSupportingTokens, "#6");
Assert.IsNotNull (p.IncomingSupportingTokens, "#6-2");
Assert.AreEqual ("_", p.SenderIdPrefix, "#6-3");
ServiceSecurityContext ssc = p.ServiceSecurityContext;
Assert.IsNotNull (ssc, "#7");
// not sure if it is worthy of testing though ...
GenericIdentity identity = ssc.PrimaryIdentity as GenericIdentity;
Assert.IsNotNull (identity, "#8-1");
Assert.AreEqual ("", identity.Name, "#8-2");
Assert.AreEqual ("", identity.AuthenticationType, "#8-3");
Assert.AreEqual (0, ssc.AuthorizationPolicies.Count, "#9");
Assert.IsTrue (ssc.IsAnonymous, "#10");
}
[Test]
[Ignore ("This hangs on .NET")]
// not sure how "good" this test is ... if it fails at
// service side, it just results in timeout error.
// The assertion makes sure that it passes all the tests, but
// in case it failed, there is almost no hint ...
public void GetOrCreateSecureMessage ()
{
bool passed = false;
ServiceHost host = new ServiceHost (typeof (CalcService));
InterceptorRequestContextHandler handler = delegate (MessageBuffer src) {
Message msg = src.CreateMessage ();
GetOrCreateSecureMessageAtService (msg);
passed = true;
};
try {
SymmetricSecurityBindingElement clisbe =
new SymmetricSecurityBindingElement ();
clisbe.ProtectionTokenParameters =
new X509SecurityTokenParameters (X509KeyIdentifierClauseType.Thumbprint, SecurityTokenInclusionMode.Never);
BindingElement transport = new HttpTransportBindingElement ();
BindingElement sintercept = new InterceptorBindingElement (handler);
CustomBinding b_res = new CustomBinding (clisbe,
sintercept,
transport);
b_res.ReceiveTimeout = b_res.SendTimeout = TimeSpan.FromSeconds (5);
host.AddServiceEndpoint (typeof (ICalc), b_res, "http://localhost:" + NetworkHelpers.FindFreePort ());
ServiceCredentials cred = new ServiceCredentials ();
cred.ServiceCertificate.Certificate = cert;
cred.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
host.Description.Behaviors.Add (cred);
host.Open ();
ProcessClient ();
} finally {
if (host.State == CommunicationState.Opened)
host.Close ();
}
if (!passed)
Assert.Fail ("Didn't pass the interceptor.");
}
void ProcessClient ()
{
SymmetricSecurityBindingElement svcsbe =
new SymmetricSecurityBindingElement ();
svcsbe.ProtectionTokenParameters =
new X509SecurityTokenParameters (X509KeyIdentifierClauseType.Thumbprint, SecurityTokenInclusionMode.Never);
BindingElement cintercept = new InterceptorBindingElement (null);
CustomBinding b_req = new CustomBinding (svcsbe,
cintercept,
new HttpTransportBindingElement ());
b_req.ReceiveTimeout = b_req.SendTimeout = TimeSpan.FromSeconds (5);
EndpointAddress remaddr = new EndpointAddress (
new Uri ("http://localhost:" + NetworkHelpers.FindFreePort ()),
new X509CertificateEndpointIdentity (cert));
CalcProxy proxy = new CalcProxy (b_req, remaddr);
proxy.ClientCredentials.ClientCertificate.Certificate = cert2;
proxy.Sum (1, 2);
proxy.Close ();
}
static void GetOrCreateSecureMessageAtClient (Message msg)
{
foreach (object o in msg.Properties)
if (o is SecurityMessageProperty)
Assert.Fail ("The input msg should not contain SecurityMessageProperty yet.");
SecurityMessageProperty p = SecurityMessageProperty.GetOrCreate (msg);
Assert.AreEqual (null, p.InitiatorToken, "#1");
Assert.AreEqual (null, p.RecipientToken, "#2");
Assert.IsNull (p.ProtectionToken, "#3");
Assert.IsNull (p.TransportToken, "#4");
Assert.IsNull (p.ExternalAuthorizationPolicies, "#5");
// Assert.AreEqual (0, p.ExternalAuthorizationPolicies.Count, "#5");
Assert.IsFalse (p.HasIncomingSupportingTokens, "#6");
Assert.IsNotNull (p.IncomingSupportingTokens, "#6-2");
Assert.AreEqual ("_", p.SenderIdPrefix, "#6-3");
ServiceSecurityContext ssc = p.ServiceSecurityContext;
Assert.IsNotNull (ssc, "#7");
// not sure if it is worthy of testing though ...
GenericIdentity identity = ssc.PrimaryIdentity as GenericIdentity;
Assert.IsNotNull (identity, "#8-1");
Assert.AreEqual ("", identity.Name, "#8-2");
Assert.AreEqual ("", identity.AuthenticationType, "#8-3");
Assert.AreEqual (0, ssc.AuthorizationPolicies.Count, "#9");
Assert.IsTrue (ssc.IsAnonymous, "#10");
}
static void GetOrCreateSecureMessageAtService (Message msg)
{
Assert.IsNull (msg.Properties.Security, "#0");
foreach (object o in msg.Properties)
if (o is SecurityMessageProperty)
Assert.Fail ("The input msg should not contain SecurityMessageProperty yet.");
SecurityMessageProperty p = SecurityMessageProperty.GetOrCreate (msg);
Assert.IsNotNull (msg.Properties.Security, "#0-2");
Assert.AreEqual (null, p.InitiatorToken, "#1");
Assert.AreEqual (null, p.RecipientToken, "#2");
Assert.IsNull (p.ProtectionToken, "#3");
Assert.IsNull (p.TransportToken, "#4");
Assert.IsNull (p.ExternalAuthorizationPolicies, "#5");
// Assert.AreEqual (0, p.ExternalAuthorizationPolicies.Count, "#5");
Assert.IsFalse (p.HasIncomingSupportingTokens, "#6");
Assert.IsNotNull (p.IncomingSupportingTokens, "#6-2");
Assert.AreEqual ("_", p.SenderIdPrefix, "#6-3");
ServiceSecurityContext ssc = p.ServiceSecurityContext;
Assert.IsNotNull (ssc, "#7");
// not sure if it is worthy of testing though ...
GenericIdentity identity = ssc.PrimaryIdentity as GenericIdentity;
Assert.IsNotNull (identity, "#8-1");
Assert.AreEqual ("", identity.Name, "#8-2");
Assert.AreEqual ("", identity.AuthenticationType, "#8-3");
Assert.AreEqual (0, ssc.AuthorizationPolicies.Count, "#9");
Assert.IsTrue (ssc.IsAnonymous, "#10");
}
}
}
#endif
|