File: rand_simulator_test.go

package info (click to toggle)
golang-github-smallstep-crypto 0.57.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,284 kB
  • sloc: sh: 53; makefile: 36
file content (52 lines) | stat: -rw-r--r-- 1,088 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
//go:build tpmsimulator
// +build tpmsimulator

package rand

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rsa"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.step.sm/crypto/tpm"
	"go.step.sm/crypto/tpm/simulator"
)

func withSimulator(t *testing.T) tpm.NewTPMOption {
	t.Helper()
	var sim simulator.Simulator
	t.Cleanup(func() {
		if sim == nil {
			return
		}
		err := sim.Close()
		require.NoError(t, err)
	})
	sim, err := simulator.New()
	require.NoError(t, err)
	err = sim.Open()
	require.NoError(t, err)
	return tpm.WithSimulator(sim)
}

func TestNew(t *testing.T) {
	r, err := New(withSimulator(t))
	require.NoError(t, err)
	require.NotNil(t, r)

	ecdsaKey, err := ecdsa.GenerateKey(elliptic.P256(), r)
	require.NoError(t, err)
	if assert.NotNil(t, ecdsaKey) {
		size := (ecdsaKey.D.BitLen() + 7) / 8
		require.Equal(t, 32, size)
	}

	rsaKey, err := rsa.GenerateKey(r, 2048)
	require.NoError(t, err)
	if assert.NotNil(t, rsaKey) {
		require.Equal(t, 256, rsaKey.Size()) // 2048 bits; 256 bytes expected to have been read
	}
}