2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package vtpm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/simulator"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/google/go-tpm-tools/client"
|
2022-04-11 04:34:15 -04:00
|
|
|
"github.com/google/go-tpm/tpm2"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// simTPMNOPCloser is a wrapper for the generic TPM simulator with a NOP Close() method.
|
|
|
|
type simTPMNOPCloser struct {
|
|
|
|
io.ReadWriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simTPMNOPCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-17 07:50:43 -04:00
|
|
|
func TestMarkNodeAsBootstrapped(t *testing.T) {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-04-22 05:06:55 -04:00
|
|
|
tpm, err := simulator.OpenSimulatedTPM()
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
defer tpm.Close()
|
|
|
|
pcrs, err := client.ReadAllPCRs(tpm)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
assert.NoError(MarkNodeAsBootstrapped(func() (io.ReadWriteCloser, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
return &simTPMNOPCloser{tpm}, nil
|
2022-07-26 04:58:39 -04:00
|
|
|
}, []byte{0x0, 0x1, 0x2, 0x3}))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
pcrsInitialized, err := client.ReadAllPCRs(tpm)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
for i := range pcrs {
|
2022-11-24 04:57:58 -05:00
|
|
|
assert.NotEqual(pcrs[i].Pcrs[uint32(measurements.PCRIndexClusterID)], pcrsInitialized[i].Pcrs[uint32(measurements.PCRIndexClusterID)])
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailOpener(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
assert.Error(MarkNodeAsBootstrapped(func() (io.ReadWriteCloser, error) { return nil, errors.New("failed") }, []byte{0x0, 0x1, 0x2, 0x3}))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-04-11 04:34:15 -04:00
|
|
|
|
|
|
|
func TestIsNodeInitialized(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
pcrValueClusterID []byte
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInitialized bool
|
|
|
|
wantErr bool
|
2022-04-11 04:34:15 -04:00
|
|
|
}{
|
|
|
|
"uninitialized PCRs results in uninitialized node": {},
|
|
|
|
"initializing PCRs result in initialized node": {
|
|
|
|
pcrValueClusterID: []byte{0x4, 0x5, 0x6, 0x7},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantInitialized: true,
|
2022-04-11 04:34:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := require.New(t)
|
|
|
|
require := require.New(t)
|
2022-04-22 05:06:55 -04:00
|
|
|
tpm, err := simulator.OpenSimulatedTPM()
|
2022-04-11 04:34:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
defer tpm.Close()
|
|
|
|
if tc.pcrValueClusterID != nil {
|
2022-11-24 04:57:58 -05:00
|
|
|
require.NoError(tpm2.PCREvent(tpm, measurements.PCRIndexClusterID, tc.pcrValueClusterID))
|
2022-04-11 04:34:15 -04:00
|
|
|
}
|
2022-07-05 08:13:19 -04:00
|
|
|
initialized, err := IsNodeBootstrapped(func() (io.ReadWriteCloser, error) {
|
2022-04-11 04:34:15 -04:00
|
|
|
return &simTPMNOPCloser{tpm}, nil
|
|
|
|
})
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 04:34:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
require.Equal(tc.wantInitialized, initialized)
|
2022-04-11 04:34:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|