﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;

namespace WcfService
{
    [TestServiceDefinition(Schema = ServiceSchema.NETTCP, BasePath = "TcpTransportSecurityWithSsl.svc")]
    public class TcpTransportSecurityWithSslTestServiceHost : TestServiceHostBase<IWcfService>
    {
        protected override string Address { get { return "tcp-server-ssl-security"; } }

        protected override Binding GetBinding()
        {
            return new CustomBinding(
                new SslStreamSecurityBindingElement(),
                new BinaryMessageEncodingBindingElement(),
                new TcpTransportBindingElement()
                );
        }

        protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();
            {
                string thumbprint = TestHost.CertificateFromFriendlyName(StoreName.My, StoreLocation.LocalMachine, "WCF Bridge - Machine certificate generated by the CertificateManager").Thumbprint;

                this.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine,
                                                          StoreName.My,
                                                          X509FindType.FindByThumbprint,
                                                          thumbprint);

                this.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
            }
        }

        public TcpTransportSecurityWithSslTestServiceHost(params Uri[] baseAddresses)
            : base(typeof(WcfService), baseAddresses)
        {
        }
    }
}
