File: extension_test.go

package info (click to toggle)
golang-github-smallstep-certificates 0.20.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,144 kB
  • sloc: sh: 278; makefile: 170
file content (147 lines) | stat: -rw-r--r-- 4,536 bytes parent folder | download | duplicates (2)
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
package apiv1

import (
	"crypto/x509"
	"crypto/x509/pkix"
	"reflect"
	"testing"
)

func TestCreateCertificateAuthorityExtension(t *testing.T) {
	type args struct {
		typ           Type
		certificateID string
		keyValuePairs []string
	}
	tests := []struct {
		name    string
		args    args
		want    pkix.Extension
		wantErr bool
	}{
		{"ok", args{Type(CloudCAS), "1ac75689-cd3f-482e-a695-8a13daf39dc4", nil}, pkix.Extension{
			Id:       oidStepCertificateAuthority,
			Critical: false,
			Value: []byte{
				0x30, 0x30, 0x13, 0x08, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x63, 0x61, 0x73, 0x13, 0x24, 0x31, 0x61,
				0x63, 0x37, 0x35, 0x36, 0x38, 0x39, 0x2d, 0x63, 0x64, 0x33, 0x66, 0x2d, 0x34, 0x38, 0x32, 0x65,
				0x2d, 0x61, 0x36, 0x39, 0x35, 0x2d, 0x38, 0x61, 0x31, 0x33, 0x64, 0x61, 0x66, 0x33, 0x39, 0x64,
				0x63, 0x34,
			},
		}, false},
		{"ok", args{Type(CloudCAS), "1ac75689-cd3f-482e-a695-8a13daf39dc4", []string{"foo", "bar"}}, pkix.Extension{
			Id:       oidStepCertificateAuthority,
			Critical: false,
			Value: []byte{
				0x30, 0x3c, 0x13, 0x08, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x63, 0x61, 0x73, 0x13, 0x24, 0x31, 0x61,
				0x63, 0x37, 0x35, 0x36, 0x38, 0x39, 0x2d, 0x63, 0x64, 0x33, 0x66, 0x2d, 0x34, 0x38, 0x32, 0x65,
				0x2d, 0x61, 0x36, 0x39, 0x35, 0x2d, 0x38, 0x61, 0x31, 0x33, 0x64, 0x61, 0x66, 0x33, 0x39, 0x64,
				0x63, 0x34, 0x30, 0x0a, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x13, 0x03, 0x62, 0x61, 0x72,
			},
		}, false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := CreateCertificateAuthorityExtension(tt.args.typ, tt.args.certificateID, tt.args.keyValuePairs...)
			if (err != nil) != tt.wantErr {
				t.Errorf("CreateCertificateAuthorityExtension() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("CreateCertificateAuthorityExtension() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestFindCertificateAuthorityExtension(t *testing.T) {
	expected := pkix.Extension{
		Id:    oidStepCertificateAuthority,
		Value: []byte("fake data"),
	}
	type args struct {
		cert *x509.Certificate
	}
	tests := []struct {
		name  string
		args  args
		want  pkix.Extension
		want1 bool
	}{
		{"first", args{&x509.Certificate{Extensions: []pkix.Extension{
			expected,
			{Id: []int{1, 2, 3, 4}},
		}}}, expected, true},
		{"last", args{&x509.Certificate{Extensions: []pkix.Extension{
			{Id: []int{1, 2, 3, 4}},
			{Id: []int{2, 3, 4, 5}},
			expected,
		}}}, expected, true},
		{"fail", args{&x509.Certificate{Extensions: []pkix.Extension{
			{Id: []int{1, 2, 3, 4}},
		}}}, pkix.Extension{}, false},
		{"fail ExtraExtensions", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
			expected,
			{Id: []int{1, 2, 3, 4}},
		}}}, pkix.Extension{}, false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, got1 := FindCertificateAuthorityExtension(tt.args.cert)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("FindCertificateAuthorityExtension() got = %v, want %v", got, tt.want)
			}
			if got1 != tt.want1 {
				t.Errorf("FindCertificateAuthorityExtension() got1 = %v, want %v", got1, tt.want1)
			}
		})
	}
}

func TestRemoveCertificateAuthorityExtension(t *testing.T) {
	caExt := pkix.Extension{
		Id:    oidStepCertificateAuthority,
		Value: []byte("fake data"),
	}
	type args struct {
		cert *x509.Certificate
	}
	tests := []struct {
		name string
		args args
		want *x509.Certificate
	}{
		{"first", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
			caExt,
			{Id: []int{1, 2, 3, 4}},
		}}}, &x509.Certificate{ExtraExtensions: []pkix.Extension{
			{Id: []int{1, 2, 3, 4}},
		}}},
		{"last", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
			{Id: []int{1, 2, 3, 4}},
			caExt,
		}}}, &x509.Certificate{ExtraExtensions: []pkix.Extension{
			{Id: []int{1, 2, 3, 4}},
		}}},
		{"missing", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
			{Id: []int{1, 2, 3, 4}},
		}}}, &x509.Certificate{ExtraExtensions: []pkix.Extension{
			{Id: []int{1, 2, 3, 4}},
		}}},
		{"extensions", args{&x509.Certificate{Extensions: []pkix.Extension{
			caExt,
			{Id: []int{1, 2, 3, 4}},
		}}}, &x509.Certificate{Extensions: []pkix.Extension{
			caExt,
			{Id: []int{1, 2, 3, 4}},
		}}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			RemoveCertificateAuthorityExtension(tt.args.cert)
			if !reflect.DeepEqual(tt.args.cert, tt.want) {
				t.Errorf("RemoveCertificateAuthorityExtension() cert = %v, want %v", tt.args.cert, tt.want)
			}
		})
	}
}