-
Notifications
You must be signed in to change notification settings - Fork 10
/
cmds_rng_test.go
63 lines (45 loc) · 1.35 KB
/
cmds_rng_test.go
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
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package tpm2_test
import (
"crypto/rand"
. "gopkg.in/check.v1"
. "github.com/canonical/go-tpm2"
internal_testutil "github.com/canonical/go-tpm2/internal/testutil"
"github.com/canonical/go-tpm2/mu"
"github.com/canonical/go-tpm2/testutil"
)
type rngSuite struct {
testutil.TPMTest
}
func (s *rngSuite) SetUpSuite(c *C) {
s.TPMFeatures = testutil.TPMFeatureNV
}
var _ = Suite(&rngSuite{})
func (s *rngSuite) testGetRandom(c *C, bytesRequested uint16) {
data, err := s.TPM.GetRandom(bytesRequested)
c.Check(err, IsNil)
c.Check(data, internal_testutil.LenEquals, int(bytesRequested))
_, _, rpBytes, _ := s.LastCommand(c).UnmarshalResponse(c)
var expected Digest
_, err = mu.UnmarshalFromBytes(rpBytes, &expected)
c.Check(err, IsNil)
c.Check(data, DeepEquals, expected)
}
func (s *rngSuite) TestGetRandom32(c *C) {
s.testGetRandom(c, 32)
}
func (s *rngSuite) TestGetRandom20(c *C) {
s.testGetRandom(c, 32)
}
func (s *rngSuite) TestStirRandom(c *C) {
inData := make([]byte, 32)
rand.Read(inData)
c.Check(s.TPM.StirRandom(inData), IsNil)
_, _, cpBytes := s.LastCommand(c).UnmarshalCommand(c)
var expected []byte
_, err := mu.UnmarshalFromBytes(cpBytes, &expected)
c.Check(err, IsNil)
c.Check(inData, DeepEquals, expected)
}