Skip to content

Commit

Permalink
Fix timezone to UTC in test
Browse files Browse the repository at this point in the history
  • Loading branch information
genkiroid committed Apr 10, 2018
1 parent 4eaeb49 commit 534ede5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
20 changes: 13 additions & 7 deletions cert_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
)

func ExampleCerts_String() {
UTC = true
enableUTC()
defer disableUTC()

stubCert()
certs, _ := NewCerts([]string{"example.com"})

Expand All @@ -14,31 +16,35 @@ func ExampleCerts_String() {
// DomainName: example.com
// IP: 127.0.0.1
// Issuer: CA for test
// NotBefore: 2016-12-31 15:00:00 +0000 UTC
// NotAfter: 2017-12-31 15:00:00 +0000 UTC
// NotBefore: 2017-01-01 00:00:00 +0000 UTC
// NotAfter: 2018-01-01 00:00:00 +0000 UTC
// CommonName: example.com
// SANs: [example.com www.example.com]
// Error:
}

func ExampleCerts_Markdown() {
UTC = true
enableUTC()
defer disableUTC()

stubCert()
certs, _ := NewCerts([]string{"example.com"})

fmt.Printf("%s", certs.Markdown())
// Output:
// DomainName | IP | Issuer | NotBefore | NotAfter | CN | SANs | Error
// --- | --- | --- | --- | --- | --- | --- | ---
// example.com | 127.0.0.1 | CA for test | 2016-12-31 15:00:00 +0000 UTC | 2017-12-31 15:00:00 +0000 UTC | example.com | example.com<br/>www.example.com<br/> |
// example.com | 127.0.0.1 | CA for test | 2017-01-01 00:00:00 +0000 UTC | 2018-01-01 00:00:00 +0000 UTC | example.com | example.com<br/>www.example.com<br/> |
}

func ExampleCerts_JSON() {
UTC = true
enableUTC()
defer disableUTC()

stubCert()
certs, _ := NewCerts([]string{"example.com"})

fmt.Printf("%s", certs.JSON())
// Output:
// [{"domainName":"example.com","ip":"127.0.0.1","issuer":"CA for test","commonName":"example.com","sans":["example.com","www.example.com"],"notBefore":"2016-12-31 15:00:00 +0000 UTC","notAfter":"2017-12-31 15:00:00 +0000 UTC","error":""}]
// [{"domainName":"example.com","ip":"127.0.0.1","issuer":"CA for test","commonName":"example.com","sans":["example.com","www.example.com"],"notBefore":"2017-01-01 00:00:00 +0000 UTC","notAfter":"2018-01-01 00:00:00 +0000 UTC","error":""}]
}
44 changes: 38 additions & 6 deletions cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (
"time"
)

func enableUTC() {
UTC = true
}

func disableUTC() {
UTC = false
}

func stubCert() {
serverCert = func(host, port string) ([]*x509.Certificate, string, error) {
return []*x509.Certificate{
Expand All @@ -19,8 +27,8 @@ func stubCert() {
CommonName: host,
},
DNSNames: []string{host, "www." + host},
NotBefore: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.Local),
NotAfter: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.Local),
NotBefore: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC),
NotAfter: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC),
},
&x509.Certificate{
Issuer: pkix.Name{
Expand All @@ -30,8 +38,8 @@ func stubCert() {
CommonName: host,
},
DNSNames: []string{host, "www." + host},
NotBefore: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.Local),
NotAfter: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.Local),
NotBefore: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC),
NotAfter: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC),
},
}, "127.0.0.1", nil
}
Expand Down Expand Up @@ -87,6 +95,9 @@ func TestSplitHostPort(t *testing.T) {
}

func TestNewCert(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()

input := "example.com"
Expand Down Expand Up @@ -131,6 +142,9 @@ func TestNewCert(t *testing.T) {
}

func TestNewCerts(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()

input := []string{"example.com"}
Expand All @@ -143,6 +157,9 @@ func TestNewCerts(t *testing.T) {
}

func TestCertsAsString(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()

certChain, _, _ := serverCert("example.com", defaultPort)
Expand All @@ -168,6 +185,9 @@ Error:
}

func TestCertsAsMarkdown(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()

certChain, _, _ := serverCert("example.com", defaultPort)
Expand All @@ -187,6 +207,9 @@ example.com | 127.0.0.1 | CA for test | %s | %s | example.com | example.com<br/>
}

func TestCertsAsJSON(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()

certChain, _, _ := serverCert("example.com", defaultPort)
Expand All @@ -212,8 +235,8 @@ func TestCertsEscapeStarInSANs(t *testing.T) {
CommonName: host,
},
DNSNames: []string{host, "*." + host}, // include star
NotBefore: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.Local),
NotAfter: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.Local),
NotBefore: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC),
NotAfter: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC),
},
}, "127.0.0.1", nil
}
Expand All @@ -228,6 +251,9 @@ func TestCertsEscapeStarInSANs(t *testing.T) {
}

func TestSetUserTempl(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()
_ = SetUserTempl("{{range .}}Issuer: {{.Issuer}}{{end}}")
expected := "Issuer: CA for test"
Expand All @@ -242,6 +268,9 @@ func TestSetUserTempl(t *testing.T) {
}

func TestDetail(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()

input := "example.com"
Expand All @@ -261,6 +290,9 @@ func TestDetail(t *testing.T) {
}

func TestCertChain(t *testing.T) {
enableUTC()
defer disableUTC()

stubCert()

input := "example.com"
Expand Down

0 comments on commit 534ede5

Please sign in to comment.