-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
123 lines (106 loc) · 2.64 KB
/
main_test.go
File metadata and controls
123 lines (106 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
var update = flag.Bool("update", false, "update golden files")
func TestPEMFormat(t *testing.T) {
tests := []struct {
file string
}{
{"example.com.crt"},
}
for _, tt := range tests {
path := filepath.Join("testdata", tt.file)
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
bundle, err := Load(path)
if err != nil {
t.Fatalf("load: %v", err)
}
var report Report
for _, c := range bundle {
report = append(report, &Record{Cert: c})
}
f := &PEMFormatter{}
got, err := f.Format(report)
if err != nil {
t.Fatalf("format: %v", err)
}
// Cut trailing newlines for comparison, since some PEM encoders add
// them and some don't.
want := strings.TrimRight(string(raw), "\n")
got = strings.TrimRight(got, "\n")
if got != want {
t.Errorf("PEM round-trip mismatch for %s\n--- want %d bytes ---\n%s\n--- got %d bytes ---\n%s",
tt.file, len(want), want, len(got), got)
}
}
}
func TestTextFormat(t *testing.T) {
tests := []struct {
file string
time time.Time
verbosity OutputLevel
golden string
}{
{
file: "example.com.crt",
time: time.Date(2026, 2, 16, 12, 0, 0, 0, time.UTC),
verbosity: CompactOutput,
golden: "example.com.crt.golden",
},
{
file: "example.com.crt",
time: time.Date(2026, 2, 16, 12, 0, 0, 0, time.UTC),
verbosity: VerboseOutput,
golden: "example.com.crt.verbose.golden",
},
{
file: "example.com.crt",
time: time.Date(2026, 2, 16, 12, 0, 0, 0, time.UTC),
verbosity: FullOutput,
golden: "example.com.crt.full.golden",
},
}
// Fix timezone for deterministic output
time.Local = time.UTC
for _, tt := range tests {
bundle, err := Load(filepath.Join("testdata", tt.file))
if err != nil {
t.Fatalf("load %s: %v", tt.file, err)
}
report, err := Verify(bundle, &VerifyOptions{
Time: tt.time,
})
if err != nil {
t.Fatalf("verify: %v", err)
}
f := &TextFormatter{Verbosity: tt.verbosity}
got, err := f.Format(report)
if err != nil {
t.Fatalf("format: %v", err)
}
goldenPath := filepath.Join("testdata", tt.golden)
if *update {
if err := os.WriteFile(goldenPath, []byte(got), 0644); err != nil {
t.Fatalf("write golden: %v", err)
}
return
}
wantBytes, err := os.ReadFile(goldenPath)
if err != nil {
t.Fatalf("read golden (run with -update to create): %v", err)
}
want := string(wantBytes)
if got != want {
t.Errorf("output doesn't match golden file %s\n--- want ---\n%s\n--- got ---\n%s", tt.golden, want, got)
}
}
}