forked from TrueCloudLab/certificates
Add unit test for profileDefaultDuration.
This commit is contained in:
parent
f06db4099e
commit
7e33aeb8d3
2 changed files with 42 additions and 1 deletions
|
@ -194,7 +194,7 @@ func (v profileDefaultDuration) Option(so Options) x509util.WithOption {
|
||||||
var backdate time.Duration
|
var backdate time.Duration
|
||||||
notBefore := so.NotBefore.Time()
|
notBefore := so.NotBefore.Time()
|
||||||
if notBefore.IsZero() {
|
if notBefore.IsZero() {
|
||||||
notBefore = time.Now()
|
notBefore = now()
|
||||||
backdate = -1 * so.Backdate
|
backdate = -1 * so.Backdate
|
||||||
}
|
}
|
||||||
notAfter := so.NotAfter.RelativeTime(notBefore)
|
notAfter := so.NotAfter.RelativeTime(notBefore)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"crypto/x509/pkix"
|
"crypto/x509/pkix"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -357,3 +358,43 @@ func Test_profileLimitDuration_Option(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_profileDefaultDuration_Option(t *testing.T) {
|
||||||
|
tm, fn := mockNow()
|
||||||
|
defer fn()
|
||||||
|
|
||||||
|
v := profileDefaultDuration(24 * time.Hour)
|
||||||
|
type args struct {
|
||||||
|
so Options
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
v profileDefaultDuration
|
||||||
|
args args
|
||||||
|
want *x509.Certificate
|
||||||
|
}{
|
||||||
|
{"default", v, args{Options{}}, &x509.Certificate{NotBefore: tm, NotAfter: tm.Add(24 * time.Hour)}},
|
||||||
|
{"backdate", v, args{Options{Backdate: 1 * time.Minute}}, &x509.Certificate{NotBefore: tm.Add(-1 * time.Minute), NotAfter: tm.Add(24 * time.Hour)}},
|
||||||
|
{"notBefore", v, args{Options{NotBefore: NewTimeDuration(tm.Add(10 * time.Second))}}, &x509.Certificate{NotBefore: tm.Add(10 * time.Second), NotAfter: tm.Add(24*time.Hour + 10*time.Second)}},
|
||||||
|
{"notAfter", v, args{Options{NotAfter: NewTimeDuration(tm.Add(1 * time.Hour))}}, &x509.Certificate{NotBefore: tm, NotAfter: tm.Add(1 * time.Hour)}},
|
||||||
|
{"notBefore and notAfter", v, args{Options{NotBefore: NewTimeDuration(tm.Add(10 * time.Second)), NotAfter: NewTimeDuration(tm.Add(1 * time.Hour))}},
|
||||||
|
&x509.Certificate{NotBefore: tm.Add(10 * time.Second), NotAfter: tm.Add(1 * time.Hour)}},
|
||||||
|
{"notBefore and backdate", v, args{Options{Backdate: 1 * time.Minute, NotBefore: NewTimeDuration(tm.Add(10 * time.Second))}},
|
||||||
|
&x509.Certificate{NotBefore: tm.Add(10 * time.Second), NotAfter: tm.Add(24*time.Hour + 10*time.Second)}},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
cert := &x509.Certificate{}
|
||||||
|
profile := &x509util.Leaf{}
|
||||||
|
profile.SetSubject(cert)
|
||||||
|
|
||||||
|
fn := tt.v.Option(tt.args.so)
|
||||||
|
if err := fn(profile); err != nil {
|
||||||
|
t.Errorf("profileDefaultDuration.Option() error %v", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(cert, tt.want) {
|
||||||
|
t.Errorf("profileDefaultDuration.Option() = %v, \nwant %v", cert, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue