From 534ede5365d1e589562a4cb38e5b6c63e570b032 Mon Sep 17 00:00:00 2001 From: genkiroid Date: Wed, 11 Apr 2018 06:52:43 +0900 Subject: [PATCH] Fix timezone to UTC in test --- cert_example_test.go | 20 +++++++++++++------- cert_test.go | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/cert_example_test.go b/cert_example_test.go index ce7e934..be31355 100644 --- a/cert_example_test.go +++ b/cert_example_test.go @@ -5,7 +5,9 @@ import ( ) func ExampleCerts_String() { - UTC = true + enableUTC() + defer disableUTC() + stubCert() certs, _ := NewCerts([]string{"example.com"}) @@ -14,15 +16,17 @@ 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"}) @@ -30,15 +34,17 @@ func ExampleCerts_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
www.example.com
| + // 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
www.example.com
| } 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":""}] } diff --git a/cert_test.go b/cert_test.go index 1c8548c..22bd625 100644 --- a/cert_test.go +++ b/cert_test.go @@ -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{ @@ -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{ @@ -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 } @@ -87,6 +95,9 @@ func TestSplitHostPort(t *testing.T) { } func TestNewCert(t *testing.T) { + enableUTC() + defer disableUTC() + stubCert() input := "example.com" @@ -131,6 +142,9 @@ func TestNewCert(t *testing.T) { } func TestNewCerts(t *testing.T) { + enableUTC() + defer disableUTC() + stubCert() input := []string{"example.com"} @@ -143,6 +157,9 @@ func TestNewCerts(t *testing.T) { } func TestCertsAsString(t *testing.T) { + enableUTC() + defer disableUTC() + stubCert() certChain, _, _ := serverCert("example.com", defaultPort) @@ -168,6 +185,9 @@ Error: } func TestCertsAsMarkdown(t *testing.T) { + enableUTC() + defer disableUTC() + stubCert() certChain, _, _ := serverCert("example.com", defaultPort) @@ -187,6 +207,9 @@ example.com | 127.0.0.1 | CA for test | %s | %s | example.com | example.com
} func TestCertsAsJSON(t *testing.T) { + enableUTC() + defer disableUTC() + stubCert() certChain, _, _ := serverCert("example.com", defaultPort) @@ -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 } @@ -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" @@ -242,6 +268,9 @@ func TestSetUserTempl(t *testing.T) { } func TestDetail(t *testing.T) { + enableUTC() + defer disableUTC() + stubCert() input := "example.com" @@ -261,6 +290,9 @@ func TestDetail(t *testing.T) { } func TestCertChain(t *testing.T) { + enableUTC() + defer disableUTC() + stubCert() input := "example.com"