File: recipient_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 (57 lines) | stat: -rw-r--r-- 1,255 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
package plugin

import (
	"crypto/ecdh"
	"crypto/ecdsa"
	"crypto/elliptic"
	"math/big"
	"reflect"
	"strings"
	"testing"
)

func bigInt(s string) *big.Int {
	ret := big.NewInt(0)
	ret.SetString(s, 10)
	return ret
}

func mustECDH(e *ecdsa.PublicKey) *ecdh.PublicKey {
	ret, _ := e.ECDH()
	return ret
}

var cases = []struct {
	pubKey    *TPMRecipient
	recipient string
}{{
	pubKey: NewTPMRecipient(mustECDH(
		&ecdsa.PublicKey{
			Curve: elliptic.P256(),
			X:     bigInt("89354244803538158909979995955747079783816134516555582017998279936143319776423"),
			Y:     bigInt("44449113766368004535934930895165275911452797542884597880018495457858036318074"),
		},
	)),

	recipient: "age1tpm1qtzcedwcyuemjkynrvucs5wyhue4h528vv7s2z9k8xvr78ky6c72wff0tz2",
}}

func TestDecodeRecipient(t *testing.T) {
	for _, c := range cases {
		pubkey, err := ParseTPMRecipient(c.recipient)
		if err != nil {
			t.Fatalf("failed decoding recipient: %v", err)
		}
		if !reflect.DeepEqual(pubkey, c.pubKey) {
			t.Fatalf("Did not parse the correct key")
		}
	}
}

func TestEncodeRecipient(t *testing.T) {
	for _, c := range cases {
		if !strings.EqualFold(c.pubKey.String(), c.recipient) {
			t.Fatalf("did not get the recipient back. expected %v, got %v", c.recipient, s)
		}
	}
}