-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_certs_test.go
157 lines (139 loc) · 3 KB
/
check_certs_test.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"testing"
"time"
)
type test struct {
name string
args args
want bool
}
type args struct {
cert Cert
minCert, expires, expired int
time time.Time
}
func generateCert(t *testing.T, begin time.Time, expiry time.Time) Cert {
cert := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"Testing Ca"},
CommonName: "Testing Cert",
},
NotBefore: begin,
NotAfter: expiry,
KeyUsage: 0,
ExtKeyUsage: []x509.ExtKeyUsage{},
BasicConstraintsValid: true,
}
return Cert{
Name: "test",
Namespace: "test",
Key: "test",
X509: cert,
}
}
func shortLengthCert(t *testing.T) test {
begin := time.Now().Add(-1 * time.Hour)
expiry := begin.Add(1 * time.Hour)
return test{
name: "Short length cert",
args: args{
cert: generateCert(t, begin, expiry),
minCert: 1,
expires: 30,
expired: 90,
time: begin,
},
want: false,
}
}
func expiredCert(t *testing.T) test {
now := time.Now()
begin := now.Add(-96 * time.Hour) // Cert was issued 96 hours ago
expiry := now.Add(-1 * time.Hour) // Cert expired 1 hour ago
return test{
name: "Expired cert",
args: args{
cert: generateCert(t, begin, expiry),
minCert: 0,
expires: 30,
expired: 90,
time: now,
},
want: true,
}
}
func expiringSoonCert(t *testing.T) test {
now := time.Now()
begin := now.Add(-96 * time.Hour) // Cert was issued 96 hours ago
expiry := now.Add(24 * time.Hour) // Cert expires in 24 hours
return test{
name: "Expires in cert",
args: args{
cert: generateCert(t, begin, expiry),
minCert: 0,
expires: 30,
expired: 90,
time: now,
},
want: true,
}
}
func oldExpiredCert(t *testing.T) test {
now := time.Now()
begin := now.Add(-(180 * (24 * time.Hour))) // Cert was issued 180 days ago
expiry := now.Add(-(120 * (24 * time.Hour))) // Cert expired 120 days ago
return test{
name: "Old expired cert",
args: args{
cert: generateCert(t, begin, expiry),
minCert: 0,
expires: 30,
expired: 90,
time: now,
},
want: false,
}
}
func validCert(t *testing.T) test {
now := time.Now()
begin := now.Add(-(30 * (24 * time.Hour))) // Cert was issued 30 days ago
expiry := now.Add(60 * (24 * time.Hour)) // Cert expires in 30 days
return test{
name: "Valid cert",
args: args{
cert: generateCert(t, begin, expiry),
minCert: 0,
expires: 30,
expired: 90,
time: now,
},
want: false,
}
}
func Test_isCertFlagged(t *testing.T) {
tests := []test{
shortLengthCert(t),
expiredCert(t),
expiringSoonCert(t),
oldExpiredCert(t),
validCert(t),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got :=
isCertFlagged(
tt.args.minCert,
tt.args.expires,
tt.args.expired,
tt.args.cert,
tt.args.time); got != tt.want {
t.Errorf("isCertFlagged() = %v, want %v", got, tt.want)
}
})
}
}