From d9008ecc79c78e45d706c18b5f53acfc9716721d Mon Sep 17 00:00:00 2001 From: Herman Slatman Date: Wed, 22 Feb 2023 16:06:47 +0100 Subject: [PATCH] Return the actual number of bytes written to through command buffer The `Write` function returned the number of bytes written to the internal command response buffer in the simulator. Depending on the type of command executed against the TPM, this would usually result in a different number of bytes being written than the original input. The fix is useful if one wants to wrap the io.ReadWriterCloser functions with implementations that perform strict checks. An example is using an io.MultiWriter to capture the bytes sent to the TPM for debugging purposes, which will fail if the number of bytes written does not equal the original length of the input bytes. --- simulator/simulator.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/simulator/simulator.go b/simulator/simulator.go index eed7e2ab2..a434f4ac6 100644 --- a/simulator/simulator.go +++ b/simulator/simulator.go @@ -109,7 +109,14 @@ func (s *Simulator) Write(commandBuffer []byte) (int, error) { if err != nil { return 0, err } - return s.buf.Write(resp) + n, err := s.buf.Write(resp) + if err != nil { + return 0, err + } + if n != len(resp) { + return 0, fmt.Errorf("expected %d bytes to be written to command response buffer, but actual number is %d", n, len(resp)) + } + return len(commandBuffer), nil } // Read gets the response of a command previously issued by calling Write().