Skip to content

Commit

Permalink
Merge pull request #112 from josephlr/bug
Browse files Browse the repository at this point in the history
testing: Make sure to always close simulator on cleanup
  • Loading branch information
josephlr authored Jun 23, 2021
2 parents db076d0 + c1f07d8 commit d27e86e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions client/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func CheckedClose(tb testing.TB, rwc io.ReadWriteCloser) {
} {
handles, err := Handles(rwc, t)
if err != nil {
tb.Fatalf("failed to fetch handles of type %v: %v", t, err)
tb.Errorf("failed to fetch handles of type %v: %v", t, err)
}
if len(handles) != 0 {
tb.Errorf("tests leaked handles: %v", handles)
}
}

if err := rwc.Close(); err != nil {
tb.Errorf("failed to close simulator: %v", err)
tb.Errorf("when closing simulator: %v", err)
}
}
9 changes: 9 additions & 0 deletions internal/test_tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func GetTPM(tb testing.TB) io.ReadWriteCloser {
if err != nil {
tb.Fatalf("Simulator initialization failed: %v", err)
}
// Make sure that whatever happens, we close the simulator
tb.Cleanup(func() {
if !simulator.IsClosed() {
tb.Error("simulator was not properly closed")
if err := simulator.Close(); err != nil {
tb.Errorf("when closing simulator: %v", err)
}
}
})
absPath, err := filepath.Abs("../server/test/ubuntu-2104-event-log")
if err != nil {
tb.Fatalf("failed to get abs path: %v", err)
Expand Down
10 changes: 9 additions & 1 deletion simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (
// handles, no synchronization is provided; the same simulator handle should not
// be used from multiple threads.
type Simulator struct {
buf bytes.Buffer
buf bytes.Buffer
closed bool
}

// The simulator is a global resource, so we use the variables below to make
Expand All @@ -51,6 +52,7 @@ func Get() (*Simulator, error) {
lock.Unlock()
return nil, err
}
simulator.closed = false
return simulator, nil
}

Expand Down Expand Up @@ -104,10 +106,16 @@ func (s *Simulator) Read(responseBuffer []byte) (int, error) {
// Close cleans up and stops the simulator, Close() should always be called when
// the Simulator is no longer needed, freeing up other callers to use Get().
func (s *Simulator) Close() error {
s.closed = true
defer lock.Unlock()
return s.off()
}

// IsClosed returns true if the simulator has been Closed()
func (s *Simulator) IsClosed() bool {
return s.closed
}

func (s *Simulator) on(manufactureReset bool) error {
// TPM2_Startup must be the first command the TPM receives.
if err := tpm2.Startup(s, tpm2.StartupClear); err != nil {
Expand Down

0 comments on commit d27e86e

Please sign in to comment.