Просмотр исходного кода

Add unit test for profileDefaultDuration.

Mariano Cano 7 лет назад
Родитель
Сommit
7e33aeb8d3
2 измененных файлов с 42 добавлено и 1 удалено
  1. 1 1
      authority/provisioner/sign_options.go
  2. 41 0
      authority/provisioner/sign_options_test.go

+ 1 - 1
authority/provisioner/sign_options.go

@@ -194,7 +194,7 @@ func (v profileDefaultDuration) Option(so Options) x509util.WithOption {
 	var backdate time.Duration
 	notBefore := so.NotBefore.Time()
 	if notBefore.IsZero() {
-		notBefore = time.Now()
+		notBefore = now()
 		backdate = -1 * so.Backdate
 	}
 	notAfter := so.NotAfter.RelativeTime(notBefore)

+ 41 - 0
authority/provisioner/sign_options_test.go

@@ -5,6 +5,7 @@ import (
 	"crypto/x509/pkix"
 	"net"
 	"net/url"
+	"reflect"
 	"testing"
 	"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)
+			}
+		})
+	}
+}