context: //
  // ClientCredentialsSecurityTokenManagerTest.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.
  //
  using System;
  using System.IdentityModel.Selectors;
  using System.IdentityModel.Tokens;
  using System.Security.Cryptography;
  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.Xml;
  using NUnit.Framework;
  
  using MonoTests.System.ServiceModel.Channels;
  
  using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
  
  using MonoTests.Helpers;
  
  namespace MonoTests.System.ServiceModel
  {
  	[TestFixture]
  	public class ClientCredentialsSecurityTokenManagerTest
  	{
  		MyManager def_c;
  		X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  		X509Certificate2 certpub = new X509Certificate2 ("Test/Resources/test.cer");
  
  		[SetUp]
  		public void Initialize ()
  		{
  			def_c = new MyManager (new MyClientCredentials ());
  		}
  
  		[Test]
  		public void IsIssuedSecurityTokenRequirement ()
  		{
  			ServiceModelSecurityTokenRequirement r;
  			MyManager mgr = new MyManager (new MyClientCredentials ());
  
  			r = new InitiatorServiceModelSecurityTokenRequirement ();
  			MySslSecurityTokenParameters ssl =
  				new MySslSecurityTokenParameters ();
  			ssl.InitRequirement (r);
  			Assert.IsFalse (mgr.IsIssued (r), "ssl");
  
  			r = new InitiatorServiceModelSecurityTokenRequirement ();
  			MySspiSecurityTokenParameters sspi =
  				new MySspiSecurityTokenParameters ();
  			sspi.InitRequirement (r);
  			Assert.IsFalse (mgr.IsIssued (r), "sspi");
  
  			r = new InitiatorServiceModelSecurityTokenRequirement ();
  			MyIssuedSecurityTokenParameters issued =
  				new MyIssuedSecurityTokenParameters ();
  			issued.InitRequirement (r);
  			Assert.IsTrue (mgr.IsIssued (r), "issued");
  
  //			r = new InitiatorServiceModelSecurityTokenRequirement ();
  //			MySecureConversationSecurityTokenParameters sc =
  //				new MySecureConversationSecurityTokenParameters ();
  //			sc.InitRequirement (r);
  //			Assert.IsFalse (mgr.IsIssued (r), "sc");
  		}
  
  		[Test]
  		[ExpectedException (typeof (NotSupportedException))]
  		public void CreateProviderDefault ()
  		{
  			SecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		[ExpectedException (typeof (InvalidOperationException))]
  		public void CreateProviderUserNameWithoutName ()
  		{
  			SecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.UserName;
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		public void CreateProviderUserName ()
  		{
  			SecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.UserName;
  			def_c.ClientCredentials.UserName.UserName = "mono";
  			UserNameSecurityTokenProvider p =
  				def_c.CreateSecurityTokenProvider (r)
  				as UserNameSecurityTokenProvider;
  			Assert.IsNotNull (p, "#1");
  		}
  
  		[Test]
  		[ExpectedException (typeof (NotSupportedException))]
  		public void CreateProviderRsaDefault ()
  		{
  			// actually is Rsa usable here??
  
  			SecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.Rsa;
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		[ExpectedException (typeof (InvalidOperationException))]
  		public void CreateProviderX509WithoutCert ()
  		{
  			SecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.X509Certificate;
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		[ExpectedException (typeof (InvalidOperationException))]
  		public void CreateProviderX509WithX509EndpointIdentity ()
  		{
  			InitiatorServiceModelSecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.X509Certificate;
  			// X509CertificateEndpointIdentity does not work like
  			// a client certificate; it still requires client cert
  			r.TargetAddress = new EndpointAddress (
  				new Uri ("http://localhost:8080"),
  				new X509CertificateEndpointIdentity (cert));
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		public void CreateProviderX509WithX509IdentityKeyExchange ()
  		{
  			InitiatorServiceModelSecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.X509Certificate;
  			// ... however when it is KeyExchange mode, this
  			// endpoint identity is used.
  			r.KeyUsage = SecurityKeyUsage.Exchange;
  			r.TargetAddress = new EndpointAddress (
  				new Uri ("http://localhost:8080"),
  				new X509CertificateEndpointIdentity (cert));
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		[ExpectedException (typeof (InvalidOperationException))]
  		public void CreateProviderX509WithClientCertKeyExchange ()
  		{
  			InitiatorServiceModelSecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.X509Certificate;
  			// ... and in such case ClientCertificate makes no sense.
  			r.KeyUsage = SecurityKeyUsage.Exchange;
  			def_c.ClientCredentials.ClientCertificate.Certificate = cert;
  			r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		public void CreateProviderX509 ()
  		{
  			SecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.X509Certificate;
  			def_c.ClientCredentials.ClientCertificate.Certificate = cert;
  			X509SecurityTokenProvider p =
  				def_c.CreateSecurityTokenProvider (r)
  				as X509SecurityTokenProvider;
  			Assert.IsNotNull (p, "#1");
  		}
  
  		[Test]
  		[ExpectedException (typeof (NotSupportedException))]
  		public void CreateProviderX509RecipientNoKeyUsage ()
  		{
  			RecipientServiceModelSecurityTokenRequirement r =
  				new RecipientServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.X509Certificate;
  			def_c.ClientCredentials.ClientCertificate.Certificate = cert;
  
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		public void CreateProviderX509Recipient ()
  		{
  			RecipientServiceModelSecurityTokenRequirement r =
  				new RecipientServiceModelSecurityTokenRequirement ();
  			r.TokenType = SecurityTokenTypes.X509Certificate;
  			r.KeyUsage = SecurityKeyUsage.Exchange;
  			def_c.ClientCredentials.ClientCertificate.Certificate = cert;
  
  			X509SecurityTokenProvider p =
  				def_c.CreateSecurityTokenProvider (r)
  				as X509SecurityTokenProvider;
  			Assert.IsNotNull (p, "#1");
  		}
  
  		[Test]
  		[ExpectedException (typeof (ArgumentException))]
  		public void CreateProviderAnonSslNoTargetAddress ()
  		{
  			SecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  //			r.TokenType = ServiceModelSecurityTokenTypes.AnonymousSslnego;
  			new MySslSecurityTokenParameters ().InitRequirement (r);
  			def_c.CreateSecurityTokenProvider (r);
  		}
  
  		[Test]
  		[ExpectedException (typeof (ArgumentException))]
  		public void CreateProviderAnonSslNoBindingElement ()
  		{
  			InitiatorServiceModelSecurityTokenRequirement r =
  				new InitiatorServiceModelSecurityTokenRequirement ();
  			r.TokenType = ServiceModelSecurityTokenTypes.AnonymousSslnego;
  			r.TargetAddress = new EndpointAddress ("http://localhost:8080");
  			SecurityTokenProvider p =
  				def_c.CreateSecurityTokenProvider (r);
  			Assert.IsNotNull (p, "#1");
  		}
  
  		[Test]
  		[ExpectedException (typeof (ArgumentException))]
  		public void CreateProviderAnonSslNoIssuerBindingContext ()
  		{
  			InitiatorServiceModelSecurityTokenRequirement r =
  
gt: 				new InitiatorServiceModelSecurityTokenRequirement ();