Skip to content

Commit

Permalink
Merge pull request #10 from difi/UtvidetSertifikatvalidering
Browse files Browse the repository at this point in the history
Utvidet sertifikatvalidering
  • Loading branch information
asjafjell authored Sep 22, 2016
2 parents 95f4d77 + 4363acc commit 56e7223
Show file tree
Hide file tree
Showing 19 changed files with 347 additions and 256 deletions.
91 changes: 58 additions & 33 deletions Difi.Felles.Utility.Tester/CertificateChainValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using ApiClientShared;
using Difi.Felles.Utility.Exceptions;
using Difi.Felles.Utility.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
using Assert = Xunit.Assert;

namespace Difi.Felles.Utility.Tester
{
[TestClass]

public class CertificateChainValidatorTests
{
private static readonly ResourceUtility ResourceUtility = new ResourceUtility("Difi.Felles.Utility.Tester.Testdata.Sertifikater");

[TestClass]

public class ErGyldigSertifikatkjedeMethod : CertificateChainValidatorTests
{
[TestMethod]
public void ErGyldigSertifikatkjedeMedProduksjonssertifikater()
[Fact]
public void Gyldig_produksjonssertifikat_når_validerer_mot_produksjonskjede()
{
//Arrange
var produksjonssertifikat = SertifikatUtility.GetProduksjonsMottakerSertifikatOppslagstjenesten();
Expand All @@ -25,11 +25,11 @@ public void ErGyldigSertifikatkjedeMedProduksjonssertifikater()
var erGyldigResponssertifikat = sertifikatValidator.ErGyldigSertifikatkjede(produksjonssertifikat);

//Assert
Assert.IsTrue(erGyldigResponssertifikat);
Assert.True(erGyldigResponssertifikat);
}

[TestMethod]
public void ErGyldigSertifikatkjedeMedFunksjoneltTestmiljøsertifikater()
[Fact]
public void Gyldig_testsertifikat_når_validerer_mot_testkjede()
{
//Arrange
var testSertifikat = SertifikatUtility.GetFunksjoneltTestmiljøMottakerSertifikatOppslagstjenesten();
Expand All @@ -39,11 +39,11 @@ public void ErGyldigSertifikatkjedeMedFunksjoneltTestmiljøsertifikater()
var erGyldigResponssertifikat = sertifikatValidator.ErGyldigSertifikatkjede(testSertifikat);

//Assert
Assert.IsTrue(erGyldigResponssertifikat);
Assert.True(erGyldigResponssertifikat);
}

[TestMethod]
public void ErGyldigSertifikatkjedeOgKjedestatusMedProduksjonssertifikater()
[Fact]
public void Gyldig_produksjonssertifikat_og_kjedestatus_når_validerer_mot_produksjonskjede()
{
//Arrange
var produksjonssertifikat = SertifikatUtility.GetProduksjonsMottakerSertifikatOppslagstjenesten();
Expand All @@ -55,12 +55,12 @@ public void ErGyldigSertifikatkjedeOgKjedestatusMedProduksjonssertifikater()

//Assert
const int forventetAntallStatusElementer = 0;
Assert.IsTrue(erGyldigResponssertifikat);
Assert.AreEqual(forventetAntallStatusElementer, kjedestatus.Length);
Assert.True(erGyldigResponssertifikat);
Assert.Equal(forventetAntallStatusElementer, kjedestatus.Length);
}

[TestMethod]
public void ErGyldigSertifikatkjedeOgKjedestatusMedFunksjoneltTestmiljøsertifikater()
[Fact]
public void Gyldig_testsertifikat_og_kjedestatus_når_validerer_mot_testkjede()
{
//Arrange
var testSertifikat = SertifikatUtility.GetFunksjoneltTestmiljøMottakerSertifikatOppslagstjenesten();
Expand All @@ -71,12 +71,12 @@ public void ErGyldigSertifikatkjedeOgKjedestatusMedFunksjoneltTestmiljøsertifik
var erGyldigResponssertifikat = sertifikatValidator.ErGyldigSertifikatkjede(testSertifikat, out kjedestatus);

//Assert
Assert.IsTrue(erGyldigResponssertifikat);
Assert.IsTrue(kjedestatus.Length == 0 || kjedestatus.ElementAt(0).Status == X509ChainStatusFlags.UntrustedRoot);
Assert.True(erGyldigResponssertifikat);
Assert.True((kjedestatus.Length == 0) || (kjedestatus.ElementAt(0).Status == X509ChainStatusFlags.UntrustedRoot));
}

[TestMethod]
public void FeilerMedSertifikatUtenGyldigKjedeMedProduksjonssertifikater()
[Fact]
public void Feiler_med_selvsignert_sertifikat_når_validerer_mot_produksjonskjede()
{
//Arrange
var selvsignertSertifikat = SertifikatUtility.GetEnhetstesterSelvsignertSertifikat();
Expand All @@ -87,11 +87,11 @@ public void FeilerMedSertifikatUtenGyldigKjedeMedProduksjonssertifikater()
var erGyldigResponssertifikat = sertifikatValidator.ErGyldigSertifikatkjede(selvsignertSertifikat);

//Assert
Assert.IsFalse(erGyldigResponssertifikat);
Assert.False(erGyldigResponssertifikat);
}

[TestMethod]
public void FeilerMedSertifikatUtenGyldigKjedeMedFunksjoneltTestmiljøsertifikater()
[Fact]
public void Feiler_med_selvsignert_sertifikat_når_validerer_mot_testkjede()
{
//Arrange
var selvsignertSertifikat = SertifikatUtility.GetEnhetstesterSelvsignertSertifikat();
Expand All @@ -102,11 +102,11 @@ public void FeilerMedSertifikatUtenGyldigKjedeMedFunksjoneltTestmiljøsertifikat
var erGyldigResponssertifikat = sertifikatValidator.ErGyldigSertifikatkjede(selvsignertSertifikat);

//Assert
Assert.IsFalse(erGyldigResponssertifikat);
Assert.False(erGyldigResponssertifikat);
}

[TestMethod]
public void FeilerMedSertifikatUtenGyldigKjedeReturnererKjedestatusMedProduksjonssertifikater()
[Fact]
public void Feiler_med_selvsignert_sertifikat_og_kjedestatus_når_validerer_mot_produksjonskjede()
{
//Arrange
var selvsignertSertifikat = SertifikatUtility.GetEnhetstesterSelvsignertSertifikat();
Expand All @@ -118,23 +118,48 @@ public void FeilerMedSertifikatUtenGyldigKjedeReturnererKjedestatusMedProduksjon
var erGyldigResponssertifikat = sertifikatValidator.ErGyldigSertifikatkjede(selvsignertSertifikat, out kjedestatus);

//Assert
Assert.IsFalse(erGyldigResponssertifikat);
Assert.IsTrue(kjedestatus.ElementAt(0).Status == X509ChainStatusFlags.UntrustedRoot);
Assert.False(erGyldigResponssertifikat);
Assert.True(kjedestatus.ElementAt(0).Status == X509ChainStatusFlags.UntrustedRoot);
}

[TestMethod]
public void FeilerMedSertifikatUtenGyldigKjedeReturnererKjedestatusMedFunksjoneltTestmiljøsertifikater()
[Fact]
public void Feiler_med_selvsignert_sertifikat_og_kjedestatus_når_validerer_mot_testkjede()
{
var selvsignertSertifikat = SertifikatUtility.GetEnhetstesterSelvsignertSertifikat();

//Act
var sertifikatValidator = new CertificateChainValidator(CertificateChainUtility.FunksjoneltTestmiljøSertifikater());

X509ChainStatus[] kjedestatus;
var erGyldigResponssertifikat = sertifikatValidator.ErGyldigSertifikatkjede(selvsignertSertifikat, out kjedestatus);

//Assert
Assert.IsFalse(erGyldigResponssertifikat);
Assert.IsTrue(kjedestatus.ElementAt(0).Status == X509ChainStatusFlags.UntrustedRoot);
Assert.False(erGyldigResponssertifikat);
Assert.True(kjedestatus.ElementAt(0).Status == X509ChainStatusFlags.UntrustedRoot);
}

[Fact]
public void Feiler_med_produksjonssertifikat_når_validerer_mot_testkjede()
{
//Arrange
var produksjonssertifikat = SertifikatUtility.GetProduksjonsMottakerSertifikatOppslagstjenesten();

//Act
var sertifikatValidator = new CertificateChainValidator(CertificateChainUtility.FunksjoneltTestmiljøSertifikater());

Assert.Throws<CertificateChainValidationException>(() => sertifikatValidator.ErGyldigSertifikatkjede(produksjonssertifikat));
}

[Fact]
public void Feiler_med_testsertifikat_når_validerer_mot_produksjonskjede()
{
//Arrange
var testsertifikat = SertifikatUtility.GetFunksjoneltTestmiljøMottakerSertifikatOppslagstjenesten();

//Act
var sertifikatValidator = new CertificateChainValidator(CertificateChainUtility.ProduksjonsSertifikater());

Assert.Throws<CertificateChainValidationException>(() => sertifikatValidator.ErGyldigSertifikatkjede(testsertifikat));
}
}
}
Expand Down
28 changes: 15 additions & 13 deletions Difi.Felles.Utility.Tester/CertificateValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;


using Xunit;

namespace Difi.Felles.Utility.Tester
{
[TestClass]

public class CertificateValidatorTests
{
[TestClass]

public class IsValidServerSertifikatMethod : CertificateValidatorTests
{
[TestMethod]
[Fact]
public void ReturnsFalseWithNullCertificate()
{
//Arrange
Expand All @@ -18,10 +20,10 @@ public void ReturnsFalseWithNullCertificate()
var isValid = CertificateValidator.IsValidCertificate(null, certificateOrganizationNumber);

//Assert
Assert.IsFalse(isValid);
Assert.False(isValid);
}

[TestMethod]
[Fact]
public void ReturnsFalseIfNotIssuedToServerOrganizationNumber()
{
//Arrange
Expand All @@ -31,10 +33,10 @@ public void ReturnsFalseIfNotIssuedToServerOrganizationNumber()
var isValid = CertificateValidator.IsValidCertificate(SertifikatUtility.TestIntegrasjonssertifikat(), sertifikatOrganisasjonsnummer);

//Assert
Assert.IsFalse(isValid);
Assert.False(isValid);
}

[TestMethod]
[Fact]
public void ReturnsFalseIfNotActivated()
{
//Arrange
Expand All @@ -44,10 +46,10 @@ public void ReturnsFalseIfNotActivated()
var isValid = CertificateValidator.IsValidCertificate(SertifikatUtility.NotActivatedTestCertificate(), sertifikatOrganisasjonsnummer);

//Assert
Assert.IsFalse(isValid);
Assert.False(isValid);
}

[TestMethod]
[Fact]
public void ReturnsFalseIfExpired()
{
//Arrange
Expand All @@ -57,10 +59,10 @@ public void ReturnsFalseIfExpired()
var isValid = CertificateValidator.IsValidCertificate(SertifikatUtility.GetExpiredTestCertificate(), sertifikatOrganisasjonsnummer);

//Assert
Assert.IsFalse(isValid);
Assert.False(isValid);
}

[TestMethod]
[Fact]
public void ReturnsTrueForCorrectCertificate()
{
//Arrange
Expand All @@ -70,7 +72,7 @@ public void ReturnsTrueForCorrectCertificate()
var isValid = CertificateValidator.IsValidCertificate(SertifikatUtility.GetPostenCertificate(), sertifikatOrganisasjonsnummer);

//Assert
Assert.IsTrue(isValid);
Assert.True(isValid);
}
}
}
Expand Down
27 changes: 24 additions & 3 deletions Difi.Felles.Utility.Tester/Difi.Felles.Utility.Tester.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -16,6 +17,8 @@
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -41,13 +44,25 @@
<AssemblyOriginatorKeyFile>C:\Keys\digipost.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="ApiClientShared, Version=1.0.5941.27271, Culture=neutral, PublicKeyToken=683b8efceae684a6, processorArchitecture=MSIL">
<HintPath>..\packages\api-client-shared.1.0.5941.27271\lib\net45\ApiClientShared.dll</HintPath>
<Reference Include="ApiClientShared, Version=1.0.5968.19413, Culture=neutral, PublicKeyToken=683b8efceae684a6, processorArchitecture=MSIL">
<HintPath>..\packages\api-client-shared.1.0.5968.19413\lib\net45\ApiClientShared.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Security" />
<Reference Include="System.Xml" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.0.0.2929, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.assert.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.0.0.2929, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.core.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
Expand All @@ -72,7 +87,7 @@
<Compile Include="CertificateChainValidatorTests.cs" />
<Compile Include="CertificateValidatorTests.cs" />
<Compile Include="Testdata\Transportkvittering.cs" />
<Compile Include="Utilities\SertifikatkjedeUtilityTester.cs" />
<Compile Include="Utilities\CertificateChainUtilityTests.cs" />
<Compile Include="SertifikatUtility.cs" />
<Compile Include="Utilities\XmlUtility.cs" />
<Compile Include="Validation\TestGenerator.cs" />
Expand Down Expand Up @@ -142,6 +157,12 @@
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
24 changes: 2 additions & 22 deletions Difi.Felles.Utility.Tester/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

[assembly: AssemblyTitle("Difi.Felles.UtilityTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Difi.Felles.UtilityTests")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("663926ad-e1e6-4008-831f-0201eb029671")]
[assembly: AssemblyTitle("Difi.Felles.Utility.Tester")]
[assembly: ComVisible(false)]
Loading

0 comments on commit 56e7223

Please sign in to comment.