File: encode_test.go

package info (click to toggle)
golang-github-smallstep-crypto 0.63.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,800 kB
  • sloc: sh: 66; makefile: 50
file content (159 lines) | stat: -rw-r--r-- 4,059 bytes parent folder | download | duplicates (3)
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
158
159
package tss2

import (
	"encoding/pem"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
	type args struct {
		pub  []byte
		priv []byte
		opts []TPMOption
	}
	tests := []struct {
		name string
		args args
		want *TPMKey
	}{
		{"ok", args{[]byte("public"), []byte("private"), nil}, &TPMKey{
			Type:       oidLoadableKey,
			EmptyAuth:  true,
			Parent:     0x40000001,
			PublicKey:  append([]byte{0, 6}, []byte("public")...),
			PrivateKey: append([]byte{0, 7}, []byte("private")...),
		}},
		{"ok with options", args{[]byte("public"), []byte("private"), []TPMOption{
			func(k *TPMKey) {
				k.EmptyAuth = false
			},
			func(k *TPMKey) {
				k.Policy = append(k.Policy, TPMPolicy{CommandCode: 1, CommandPolicy: []byte("command-policy")})
			},
		}}, &TPMKey{
			Type:       oidLoadableKey,
			EmptyAuth:  false,
			Policy:     []TPMPolicy{{CommandCode: 1, CommandPolicy: []byte("command-policy")}},
			Parent:     0x40000001,
			PublicKey:  append([]byte{0, 6}, []byte("public")...),
			PrivateKey: append([]byte{0, 7}, []byte("private")...),
		}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, New(tt.args.pub, tt.args.priv, tt.args.opts...))
		})
	}
}

func TestTPMKey_Encode(t *testing.T) {
	tests := []struct {
		name      string
		tpmKey    *TPMKey
		want      *pem.Block
		assertion assert.ErrorAssertionFunc
	}{
		{"ok", New([]byte("public"), []byte("private")), &pem.Block{
			Type: "TSS2 PRIVATE KEY",
			Bytes: []byte{
				0x30, 0x28,
				0x6, 0x6, 0x67, 0x81, 0x5, 0xa, 0x1, 0x3,
				0xa0, 0x3, 0x1, 0x1, 0xff,
				0x2, 0x4, 0x40, 0x0, 0x0, 0x1,
				0x4, 0x8, 0x0, 0x6, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63,
				0x4, 0x9, 0x0, 0x7, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65,
			},
		}, assert.NoError},
		{"fail", nil, nil, assert.Error},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := tt.tpmKey.Encode()
			tt.assertion(t, err)
			assert.Equal(t, tt.want, got)
		})
	}
}

func TestTPMKey_EncodeToMemory(t *testing.T) {
	tests := []struct {
		name      string
		tpmKey    *TPMKey
		want      []byte
		assertion assert.ErrorAssertionFunc
	}{
		{"ok", New([]byte("public"), []byte("private")), []byte(`-----BEGIN TSS2 PRIVATE KEY-----
MCgGBmeBBQoBA6ADAQH/AgRAAAABBAgABnB1YmxpYwQJAAdwcml2YXRl
-----END TSS2 PRIVATE KEY-----
`), assert.NoError},
		{"fail", nil, nil, assert.Error},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := tt.tpmKey.EncodeToMemory()
			tt.assertion(t, err)
			assert.Equal(t, tt.want, got)
		})
	}
}

func TestEncode(t *testing.T) {
	type args struct {
		pub  []byte
		priv []byte
		opts []TPMOption
	}
	tests := []struct {
		name      string
		args      args
		want      *pem.Block
		assertion assert.ErrorAssertionFunc
	}{
		{"ok", args{[]byte("public"), []byte("private"), nil}, &pem.Block{
			Type: "TSS2 PRIVATE KEY",
			Bytes: []byte{
				0x30, 0x28,
				0x6, 0x6, 0x67, 0x81, 0x5, 0xa, 0x1, 0x3,
				0xa0, 0x3, 0x1, 0x1, 0xff,
				0x2, 0x4, 0x40, 0x0, 0x0, 0x1,
				0x4, 0x8, 0x0, 0x6, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63,
				0x4, 0x9, 0x0, 0x7, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65,
			},
		}, assert.NoError},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := Encode(tt.args.pub, tt.args.priv, tt.args.opts...)
			tt.assertion(t, err)
			assert.Equal(t, tt.want, got)
		})
	}
}

func TestEncodeToMemory(t *testing.T) {
	type args struct {
		pub  []byte
		priv []byte
		opts []TPMOption
	}
	tests := []struct {
		name      string
		args      args
		want      []byte
		assertion assert.ErrorAssertionFunc
	}{
		{"ok", args{[]byte("public"), []byte("private"), nil}, []byte(`-----BEGIN TSS2 PRIVATE KEY-----
MCgGBmeBBQoBA6ADAQH/AgRAAAABBAgABnB1YmxpYwQJAAdwcml2YXRl
-----END TSS2 PRIVATE KEY-----
`), assert.NoError},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := EncodeToMemory(tt.args.pub, tt.args.priv, tt.args.opts...)
			tt.assertion(t, err)
			assert.Equal(t, tt.want, got)
		})
	}
}