Skip to content

Commit

Permalink
mssim: Refactor device construction
Browse files Browse the repository at this point in the history
This introduces DeviceOption for customizing the properties of a Device.
It does break the API a bit, but the mssim package is generally only
called from unit test code.
  • Loading branch information
chrisccoulson committed Jan 20, 2025
1 parent 2976603 commit 72a3a0f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 58 deletions.
82 changes: 48 additions & 34 deletions mssim/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ const (
DefaultPort uint = 2321
)

func defaultDevice() *Device {
return NewLocalDevice(DefaultPort)
}

var (
// DefaultDevice is configured for the simulator, running locally
// with the default port for the TPM channel.
DefaultDevice *Device = defaultDevice()
// with the default port of 2321 for the TPM channel and 2322 for
// the platform channel.
DefaultDevice *Device = NewDevice()

netDial = net.Dial
)
Expand Down Expand Up @@ -56,32 +53,23 @@ type Device struct {
retryParams transportutil.RetryParams
}

// NewLocalDevice returns a new device structure for the specified port on the
// local machine. It is safe to use from multiple goroutines simultaneously. Note
// that this assumes the supplied port is for the TPM channel, and that the platform
// channel is on the subsequent port. The default retry parameters have MaxRetries
// set to 4, InitialBackoff set to 20ms and the BackoffRate set to 2.
func NewLocalDevice(port uint, opts ...DeviceOption) *Device {
return NewDevice("localhost", port, opts...)
}

// NewDevice returns a new device structure for the specified host and port. It
// is safe to use from multiple goroutines simultaneously. Note that this assumes
// the supplied port is for the TPM channel, and that the platform channel is on
// the subsequent port. The default retry parameters have MaxRetries set to 4,
// InitialBackoff set to 20ms and the BackoffRate set to 2.
func NewDevice(host string, port uint, opts ...DeviceOption) *Device {
if host == "" {
host = "localhost"
}
// NewDevice returns a new device structure. By default, the host is localhost,
// the TPM channel port set to [DefaultPort], and it assumes that the platform
// channel port is [DefaultPort] + 1. The default retry parameters have
// MaxRetries set to 4, InitialBackoff set to 20ms and the BackoffRate set to 2.
//
// It can be customized by any of the [DeviceOption]s.
//
// The returned device is safe to use from multiple goroutines simultaneously.
func NewDevice(opts ...DeviceOption) *Device {
dev := &Device{
tpm: deviceAddr{
Host: host,
Port: port,
Host: "localhost",
Port: DefaultPort,
},
platform: deviceAddr{
Host: host,
Port: port + 1,
Host: "localhost",
Port: DefaultPort + 1,
},
retryParams: transportutil.RetryParams{
MaxRetries: 4,
Expand All @@ -95,14 +83,40 @@ func NewDevice(host string, port uint, opts ...DeviceOption) *Device {
return dev
}

// WithPlatformPort is used to customize a device if the simulator has a platform
// channel on a port that isn't the TPM channel port + 1.
// WithHost is used to customize the host address on which the simulator's
// TCP ports can be accessed. The default is localhost.
func WithHost(host string) DeviceOption {
return func(d *Device) {
d.tpm.Host = host
d.platform.Host = host
}
}

// WithPort is used to customize the TCP ports on which the TPM and platform
// channels for the simulator are accessed. It sets the platform channel port
// to the TPM channel port + 1.
func WithPort(port uint) DeviceOption {
return func(d *Device) {
d.tpm.Port = port
d.platform.Port = port + 1
}
}

// WithTPMPort is used to customize the TCP port on which the TPM channel
// for the simulator is accessed. It doesn't modify the port for the platform
// channel.
func WithTPMPort(port uint) DeviceOption {
return func(d *Device) {
d.tpm.Port = port
}
}

// WithPlatformPort is used to customize the TCP port on which the platform
// chanel for the simulator is accessed. It doesn't modify the port for the
// TPM channel
func WithPlatformPort(port uint) DeviceOption {
return func(d *Device) {
d.platform = deviceAddr{
Host: d.tpm.Host,
Port: port,
}
d.platform.Port = port
}
}

Expand Down
36 changes: 18 additions & 18 deletions mssim/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@ var (
)

func (s *deviceSuite) TestNewDevice(c *C) {
dev := NewDevice("localhost", 2321)
dev := NewDevice()
expectedDev := NewMockDevice(&DeviceAddr{Host: "localhost", Port: 2321}, &DeviceAddr{Host: "localhost", Port: 2322}, &defaultRetryParams)
c.Check(dev, DeepEquals, expectedDev)
}

func (s *deviceSuite) TestNewDeviceDifferentHost(c *C) {
dev := NewDevice("192.18.1.50", 2321)
dev := NewDevice(WithHost("192.18.1.50"))
expectedDev := NewMockDevice(&DeviceAddr{Host: "192.18.1.50", Port: 2321}, &DeviceAddr{Host: "192.18.1.50", Port: 2322}, &defaultRetryParams)
c.Check(dev, DeepEquals, expectedDev)
}

func (s *deviceSuite) TestNewDeviceDifferentPort(c *C) {
dev := NewDevice("localhost", 4444)
dev := NewDevice(WithPort(4444))
expectedDev := NewMockDevice(&DeviceAddr{Host: "localhost", Port: 4444}, &DeviceAddr{Host: "localhost", Port: 4445}, &defaultRetryParams)
c.Check(dev, DeepEquals, expectedDev)
}

func (s *deviceSuite) TestNewLocalDevice(c *C) {
dev := NewLocalDevice(2321)
expectedDev := NewMockDevice(&DeviceAddr{Host: "localhost", Port: 2321}, &DeviceAddr{Host: "localhost", Port: 2322}, &defaultRetryParams)
func (s *deviceSuite) TestNewDeviceDifferentTPMPort(c *C) {
dev := NewDevice(WithTPMPort(4444))
expectedDev := NewMockDevice(&DeviceAddr{Host: "localhost", Port: 4444}, &DeviceAddr{Host: "localhost", Port: 2322}, &defaultRetryParams)
c.Check(dev, DeepEquals, expectedDev)
}

func (s *deviceSuite) TestNewLocalDeviceDifferentPort(c *C) {
dev := NewLocalDevice(4444)
expectedDev := NewMockDevice(&DeviceAddr{Host: "localhost", Port: 4444}, &DeviceAddr{Host: "localhost", Port: 4445}, &defaultRetryParams)
func (s *deviceSuite) TestNewDeviceDifferentPlatformPort(c *C) {
dev := NewDevice(WithPlatformPort(4444))
expectedDev := NewMockDevice(&DeviceAddr{Host: "localhost", Port: 2321}, &DeviceAddr{Host: "localhost", Port: 4444}, &defaultRetryParams)
c.Check(dev, DeepEquals, expectedDev)
}

func (s *deviceSuite) TestInfoMethods(c *C) {
dev := NewLocalDevice(2321)
c.Check(dev.TPMAddr(), DeepEquals, DeviceAddr{Host: "localhost", Port: 2321})
c.Check(dev.PlatformAddr(), DeepEquals, DeviceAddr{Host: "localhost", Port: 2322})
c.Check(dev.RetryParams(), DeepEquals, defaultRetryParams)
}

func (s *deviceSuite) TestWithRetryParams(c *C) {
dev := NewLocalDevice(2321, WithRetryParams(10, 10*time.Millisecond, 3))
func (s *deviceSuite) TestNewDeviceWithDifferentRetryParams(c *C) {
dev := NewDevice(WithRetryParams(10, 10*time.Millisecond, 3))
expectedDev := NewMockDevice(&DeviceAddr{Host: "localhost", Port: 2321}, &DeviceAddr{Host: "localhost", Port: 2322}, &transportutil.RetryParams{
MaxRetries: 10,
InitialBackoff: 10 * time.Millisecond,
BackoffRate: 3,
})
c.Check(dev, DeepEquals, expectedDev)
}

func (s *deviceSuite) TestInfoMethods(c *C) {
dev := NewDevice()
c.Check(dev.TPMAddr(), DeepEquals, DeviceAddr{Host: "localhost", Port: 2321})
c.Check(dev.PlatformAddr(), DeepEquals, DeviceAddr{Host: "localhost", Port: 2322})
c.Check(dev.RetryParams(), DeepEquals, defaultRetryParams)
}
2 changes: 1 addition & 1 deletion mssim/mssim.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,6 @@ func (t *tpmMainTransport) Close() error {
//
// Deprecated: Use [NewDevice], [NewLocalDevice] or [DefaultDevice].
func OpenConnection(host string, port uint) (*Transport, error) {
device := NewDevice(host, port)
device := NewDevice(WithHost(host), WithPort(port))
return device.openInternal()
}
10 changes: 5 additions & 5 deletions testutil/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ var (
TPMDevicePath string = "/dev/tpm0"

// MssimPort defines the port number of the TPM simulator command port where TPMBackend is TPMBackendMssim.
MssimPort uint = 2321
MssimPort uint = mssim.DefaultPort

wrapMssimTransport = WrapTransport

Expand Down Expand Up @@ -266,7 +266,7 @@ func (c *tpmSimulatorLaunchContext) stopAndTerminate() (err error) {
c.captureErr("terminate", c.terminateFn(stopOk))
}()

device := mssim.NewLocalDevice(c.port)
device := mssim.NewDevice(mssim.WithPort(c.port))
tpm, err := tpm2.OpenTPMDevice(device)
if err != nil {
return fmt.Errorf("cannot open simulator connection for stop: %w", err)
Expand Down Expand Up @@ -482,7 +482,7 @@ func (c *tpmSimulatorLaunchContext) launch(opts *TPMSimulatorOptions) error {
return fmt.Errorf("cannot start simulator: %w", err)
}

device := mssim.NewLocalDevice(c.port)
device := mssim.NewDevice(mssim.WithPort(c.port))
var tpm *tpm2.TPMContext

// Give the simulator 5 seconds to start up
Expand Down Expand Up @@ -807,7 +807,7 @@ func newDevice(features TPMFeatureFlags) (tpm2.TPMDevice, error) {
}
}
case TPMBackendMssim:
device = mssim.NewLocalDevice(MssimPort)
device = mssim.NewDevice(mssim.WithPort(MssimPort))
}

return &tpmDevice{
Expand Down Expand Up @@ -1012,7 +1012,7 @@ func WrapSimulatorDevice(device *mssim.Device) tpm2.TPMDevice {
func NewSimulatorDevice() tpm2.TPMDevice {
var device *mssim.Device
if TPMBackend == TPMBackendMssim {
device = mssim.NewLocalDevice(MssimPort)
device = mssim.NewDevice(mssim.WithPort(MssimPort))
}

return &simulatorDevice{device: device}
Expand Down

0 comments on commit 72a3a0f

Please sign in to comment.