Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions powershell/src/Public/Create-SqlServerCertificate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ function Create-SqlServerCertificate{

[Parameter()]
[System.DateTimeOffset]
$NotBefore = [System.DateTimeOffset]::UtcNow,
$NotBefore = [System.DateTimeOffset]::Now,

[Parameter()]
[System.DateTimeOffset]
$NotAfter = [System.DateTimeOffset]::UtcNow.AddDays(3285)
$NotAfter = [System.DateTimeOffset]::Now.AddDays(3285)
)

Write-Information -MessageData "Creating a certificate for SqlServer with '$($SignerCertificate.Thumbprint)' signer." -InformationAction Continue
Expand Down
41 changes: 41 additions & 0 deletions powershell/test/Public/Create-SqlServerCertificate.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,46 @@
$certificate.Issuer | Should -Be $signerCert.Subject
}
}

Context 'When creating a self-signed certificate with DEFAULT validity period' {
It 'creates a certificate with the correct start and end dates' {
# Arrange
$commonName = 'test.sql.server'
$dnsName = 'test.sql.server'

# Act
$certificate = Create-SqlServerCertificate -CommonName $commonName -DnsName $dnsName -SignerCertificate $signerCert

# Assert
$certificate | Should -Not -BeNullOrEmpty

# Check default validity period
$now = [System.DateTimeOffset]::Now

$certificate.NotBefore | Should -BeGreaterThan ($now.DateTime.AddSeconds(-5))
$certificate.NotBefore | Should -BeLessThan ($now.DateTime.AddSeconds(5))

$certificate.NotAfter | Should -BeGreaterThan ($now.AddDays(3285).DateTime.AddSeconds(-5))
$certificate.NotAfter | Should -BeLessThan ($now.AddDays(3285).DateTime.AddSeconds(5))
}
}

Context 'When creating a self-signed certificate with CUSTOM validity period' {
It 'creates a certificate with the correct start and end dates' {
# Arrange
$commonName = 'test.sql.server'
$dnsName = 'test.sql.server'
$notBefore = [System.DateTimeOffset]::UtcNow.AddDays(-1)
$notAfter = [System.DateTimeOffset]::UtcNow.AddDays(365)

# Act
$certificate = Create-SqlServerCertificate -CommonName $commonName -DnsName $dnsName -SignerCertificate $signerCert -NotBefore $notBefore -NotAfter $notAfter

# Assert
$certificate | Should -Not -BeNullOrEmpty
$certificate.NotBefore | Should -BeGreaterThan ($notBefore.DateTime.AddSeconds(-1))
$certificate.NotAfter | Should -BeLessThan ($notAfter.DateTime.AddSeconds(1))
}
}
}
}