File: identity_test.go

package info (click to toggle)
age-plugin-tpm 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: makefile: 23
file content (114 lines) | stat: -rw-r--r-- 2,412 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
package plugin

import (
	"errors"
	"io"
	"reflect"
	"testing"

	"github.com/google/go-tpm/tpm2"
	"github.com/google/go-tpm/tpm2/transport/simulator"
)

func mustPublic(data []byte) tpm2.TPM2BPublic {
	return tpm2.BytesAs2B[tpm2.TPMTPublic](data)
}

func mustPrivate(data []byte) tpm2.TPM2BPrivate {
	return tpm2.TPM2BPrivate{
		Buffer: data,
	}
}

func mustSRK(data []byte) *tpm2.TPM2BName {
	return &tpm2.TPM2BName{
		Buffer: data,
	}
}

var data = []struct {
	key string
	t   *Identity
}{
	{
		key: "AGE-PLUGIN-TPM-1QYQQQPNSW43XC6TRQQRHQUNFWESHGEGN0E0FM",
		t: &Identity{
			Version: 1,
			Public:  mustPublic([]byte("public")),
			Private: mustPrivate([]byte("private")),
		},
	},
	{
		key: "AGE-PLUGIN-TPM-1QYQSQPNSW43XC6TRQQRHQUNFWESHGEGWKR32R",
		t: &Identity{
			Version: 1,
			PIN:     HasPIN,
			Public:  mustPublic([]byte("public")),
			Private: mustPrivate([]byte("private")),
		},
	},
	{
		key: "AGE-PLUGIN-TPM-1QGQQQPNSW43XC6TRQQRHQUNFWESHGEGQQDEHY6CUT4TFU",
		t: &Identity{
			Version: 2,
			Public:  mustPublic([]byte("public")),
			Private: mustPrivate([]byte("private")),
			SRKName: mustSRK([]byte("srk")),
		},
	},
	{
		key: "AGE-PLUGIN-TPM-1QGQSQPNSW43XC6TRQQRHQUNFWESHGEGQQDEHY6CHRM9KS",
		t: &Identity{
			Version: 2,
			PIN:     HasPIN,
			Public:  mustPublic([]byte("public")),
			Private: mustPrivate([]byte("private")),
			SRKName: mustSRK([]byte("srk")),
		},
	},
}

func TestIdentityIdentityGeneration(t *testing.T) {
	for _, d := range data {
		k := d.t.String()
		if !reflect.DeepEqual(k, d.key) {
			t.Fatalf("no the same. Got %v expected %v", k, d.key)
		}
	}
}

// func TestIdentityDecode(t *testing.T) {
// 	for _, d := range data {
// 		k, err := DecodeIdentity(d.key)
// 		if err != nil {
// 			t.Fatalf("failed to decode key: %v", err)
// 		}
// 		if !reflect.DeepEqual(k, d.t) {
// 			t.Fatalf("no the same")
// 		}
// 	}
// }

func TestIdentityCreateEncodeDecode(t *testing.T) {
	tpm, err := simulator.OpenSimulator()
	if err != nil {
		t.Fatalf("failed opening tpm: %v", err)
	}
	defer tpm.Close()

	SetLogger(io.Discard)
	identity, _, err1 := CreateIdentity(tpm, nil)
	identity.Callbacks(nil, tpm, func() ([]byte, error) { return nil, nil })

	k := identity.String()

	identity2, err2 := DecodeIdentity(k)

	if err = errors.Join(err1, err2); err != nil {
		t.Fatalf("failed test: %v", err)
	}

	if identity2.String() != k {
		t.Fatalf("failed to parse identityes")
	}
}