Skip to content

Commit

Permalink
remove deprecated io/ioutil usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisccoulson committed Jan 14, 2024
1 parent 42bf3ce commit b238ee2
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 38 deletions.
5 changes: 2 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"

"github.com/canonical/go-tpm2/mu"
Expand Down Expand Up @@ -98,7 +97,7 @@ func ReadCommandPacket(r io.Reader, numHandles int) (command CommandCode, handle
return 0, nil, nil, nil, fmt.Errorf("invalid tag: %v", header.Tag)
}

parameters, err = ioutil.ReadAll(lr)
parameters, err = io.ReadAll(lr)
if err != nil {
return 0, nil, nil, nil, fmt.Errorf("cannot read parameters: %w", err)
}
Expand Down Expand Up @@ -234,7 +233,7 @@ func ReadResponsePacket(r io.Reader, handle *Handle) (rc ResponseCode, parameter
authArea = append(authArea, auth)
}
case TagNoSessions:
parameters, err = ioutil.ReadAll(lr)
parameters, err = io.ReadAll(lr)
if err != nil || lr.N > 0 {
if err == nil {
err = io.ErrUnexpectedEOF
Expand Down
6 changes: 3 additions & 3 deletions internal/util/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"errors"
"fmt"
"hash"
"io/ioutil"
"io"

"github.com/canonical/go-tpm2"
internal_crypt "github.com/canonical/go-tpm2/internal/crypt"
Expand Down Expand Up @@ -41,7 +41,7 @@ func UnwrapOuter(hashAlg tpm2.HashAlgorithmId, symmetricAlg *tpm2.SymDefObject,
return nil, fmt.Errorf("cannot unmarshal integrity digest: %w", err)
}

data, _ = ioutil.ReadAll(r)
data, _ = io.ReadAll(r)

hmacKey := internal_crypt.KDFa(hashAlg.GetHash(), seed, []byte(tpm2.IntegrityKey), nil, nil, hashAlg.Size()*8)
h := hmac.New(func() hash.Hash { return hashAlg.NewHash() }, hmacKey)
Expand All @@ -64,7 +64,7 @@ func UnwrapOuter(hashAlg tpm2.HashAlgorithmId, symmetricAlg *tpm2.SymDefObject,
}
}

data, _ = ioutil.ReadAll(r)
data, _ = io.ReadAll(r)

symKey := internal_crypt.KDFa(hashAlg.GetHash(), seed, []byte(tpm2.StorageKey), name, nil, int(symmetricAlg.KeyBits.Sym))

Expand Down
5 changes: 2 additions & 3 deletions linux/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -336,7 +335,7 @@ func OpenDevice(path string) (*Transport, error) {
func tpmDeviceVersion(path string) (TPMMajorVersion, error) {
versionPath := filepath.Join(path, "tpm_version_major")

versionBytes, err := ioutil.ReadFile(versionPath)
versionBytes, err := os.ReadFile(versionPath)
switch {
case os.IsNotExist(err):
// Handle older kernels that didn't have this attribute file. There were no other
Expand Down Expand Up @@ -380,7 +379,7 @@ func probeTpmDevices() (out []*RawDevice, err error) {
}
defer f.Close()

entries, err := f.Readdir(0)
entries, err := f.ReadDir(0)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions linux/ppi_sysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -56,7 +55,7 @@ func (p *sysfsPpiImpl) SubmitOperation(op ppi.OperationId, arg *uint64) error {
}

func (p *sysfsPpiImpl) StateTransitionAction() (ppi.StateTransitionAction, error) {
actionBytes, err := ioutil.ReadFile(filepath.Join(p.sysfsPath, "transition_action"))
actionBytes, err := os.ReadFile(filepath.Join(p.sysfsPath, "transition_action"))
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -118,7 +117,7 @@ func (p *sysfsPpiImpl) OperationStatus(op ppi.OperationId) (ppi.OperationStatus,
}

func (p *sysfsPpiImpl) OperationResponse() (*ppi.OperationResponse, error) {
rspBytes, err := ioutil.ReadFile(filepath.Join(p.sysfsPath, "response"))
rspBytes, err := os.ReadFile(filepath.Join(p.sysfsPath, "response"))
switch {
case errors.Is(err, syscall.EFAULT):
return nil, ppi.ErrOperationFailed
Expand Down Expand Up @@ -148,7 +147,7 @@ func (p *sysfsPpiImpl) OperationResponse() (*ppi.OperationResponse, error) {
}

func newSysfsPpi(path string) (*sysfsPpiImpl, error) {
versionBytes, err := ioutil.ReadFile(filepath.Join(path, "version"))
versionBytes, err := os.ReadFile(filepath.Join(path, "version"))
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions objectutil/duplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"

"github.com/canonical/go-tpm2"
internal_crypt "github.com/canonical/go-tpm2/internal/crypt"
Expand Down Expand Up @@ -51,7 +50,7 @@ func duplicateToSensitive(duplicate tpm2.Private, name tpm2.Name, outerHashAlg t
return nil, fmt.Errorf("cannot unmarshal inner integrity digest: %w", err)
}

duplicate, _ = ioutil.ReadAll(r)
duplicate, _ = io.ReadAll(r)

h := name.Algorithm().NewHash()
h.Write(duplicate)
Expand Down
38 changes: 19 additions & 19 deletions testutil/suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package testutil_test

import (
"io/ioutil"
"io"

. "gopkg.in/check.v1"

Expand Down Expand Up @@ -53,7 +53,7 @@ func (s *baseTestSuite) TestCleanup(c *C) {
suite.AddCleanup(func() { suite.log = append(suite.log, c.TestName()+".test2") })
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 2 passed")
c.Check(suite.log, DeepEquals, []string{
"mockBaseTestCleanupSuite.Test1.test2",
Expand All @@ -79,7 +79,7 @@ func (s *baseTestSuite) TestSkipTests(c *C) {
c.Skip("test skipped")
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 2 skipped")
c.Check(suite.log, DeepEquals, []string{
"mockBaseTestCleanupSuite.Test1.test1",
Expand All @@ -98,7 +98,7 @@ func (s *baseTestSuite) TestSkipTestsFromFixture(c *C) {
}
suite.testCb = func(c *C) {}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 2 skipped")
c.Check(suite.log, DeepEquals, []string(nil))
}
Expand All @@ -117,7 +117,7 @@ func (s *baseTestSuite) TestFixtureCleanupError(c *C) {
c.Skip("test skipped")
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OOPS: 0 passed, 1 FAILED, 2 MISSED")
c.Check(suite.log, DeepEquals, []string{"mockBaseTestCleanupSuite.Test1.test1", "mockBaseTestCleanupSuite.Test1.fixture1"})
}
Expand All @@ -134,7 +134,7 @@ func (s *baseTestSuite) TestSkipTestFromFixtureAfterAddCleanup(c *C) {
}
suite.testCb = func(c *C) {}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OOPS: 0 passed, 1 skipped, 2 FAILED, 1 MISSED")
c.Check(suite.log, DeepEquals, []string(nil))
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (s *tpmTestSuite) TestSkipNoTPM(c *C) {
TPMBackend = TPMBackendNone
defer func() { TPMBackend = origBackend }()

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 1 skipped")
}

Expand All @@ -256,7 +256,7 @@ func (s *tpmTestSuite) TestInvalidSetUp(c *C) {
})
suite.TPM = tpm

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OOPS: 0 passed, 1 FAILED, 1 MISSED")
}

Expand All @@ -266,7 +266,7 @@ func (s *tpmTestSuite) TestLastCommandWithNoCommands(c *C) {
suite.LastCommand(c)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OOPS: 0 passed, 1 FAILED")
}

Expand All @@ -276,7 +276,7 @@ func (s *tpmTestSuite) TestRequireAlgorithm(c *C) {
suite.RequireAlgorithm(c, tpm2.AlgorithmRSA)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 1 passed")
}

Expand All @@ -286,7 +286,7 @@ func (s *tpmTestSuite) TestRequireMissingAlgorithm(c *C) {
suite.RequireAlgorithm(c, tpm2.AlgorithmError)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 1 skipped")
}

Expand All @@ -296,7 +296,7 @@ func (s *tpmTestSuite) TestRequireRSAKeySize(c *C) {
suite.RequireRSAKeySize(c, 2048)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 1 passed")
}

Expand All @@ -306,7 +306,7 @@ func (s *tpmTestSuite) TestRequireMissingRSAKeySize(c *C) {
suite.RequireRSAKeySize(c, 2047)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 1 skipped")
}

Expand All @@ -316,7 +316,7 @@ func (s *tpmTestSuite) TestRequireECCCurve(c *C) {
suite.RequireECCCurve(c, tpm2.ECCCurveNIST_P256)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 1 passed")
}

Expand All @@ -326,7 +326,7 @@ func (s *tpmTestSuite) TestRequireMissingECCCurve(c *C) {
suite.RequireECCCurve(c, tpm2.ECCCurve(0))
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 1 skipped")
}

Expand All @@ -336,7 +336,7 @@ func (s *tpmTestSuite) TestRequireSymmetricAlgorithm(c *C) {
suite.RequireSymmetricAlgorithm(c, tpm2.SymObjectAlgorithmAES, 128)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 1 passed")
}

Expand All @@ -346,7 +346,7 @@ func (s *tpmTestSuite) TestRequireMissingSymmetricAlgorithm(c *C) {
suite.RequireSymmetricAlgorithm(c, tpm2.SymObjectAlgorithmId(tpm2.AlgorithmError), 128)
}

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 1 skipped")
}

Expand Down Expand Up @@ -545,7 +545,7 @@ func (s *tpmSimulatorTestSuite) TestSkipNoTPM(c *C) {
TPMBackend = TPMBackendNone
defer func() { TPMBackend = origBackend }()

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OK: 0 passed, 1 skipped")
}

Expand All @@ -558,7 +558,7 @@ func (s *tpmSimulatorTestSuite) TestInvalidSetUp(c *C) {
})
suite.TPM = tpm

result := Run(suite, &RunConf{Output: ioutil.Discard})
result := Run(suite, &RunConf{Output: io.Discard})
c.Check(result.String(), Equals, "OOPS: 0 passed, 2 FAILED, 1 MISSED")
}

Expand Down
3 changes: 1 addition & 2 deletions testutil/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"os/exec"
Expand Down Expand Up @@ -423,7 +422,7 @@ func (c *tpmSimulatorLaunchContext) launch(opts *TPMSimulatorOptions) error {
if err := os.MkdirAll(workDirRoot, 0755); err != nil {
return fmt.Errorf("cannot create workdir root: %w", err)
}
workDir, err := ioutil.TempDir(workDirRoot, workDirPrefix)
workDir, err := os.MkdirTemp(workDirRoot, workDirPrefix)
if err != nil {
return fmt.Errorf("cannot create workdir for simulator: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"time"

"github.com/canonical/go-tpm2/mu"
Expand Down Expand Up @@ -307,7 +307,7 @@ func (t *TPMContext) RunCommandBytes(packet CommandPacket) (ResponsePacket, erro
return nil, &TransportError{"write", err}
}

resp, err := ioutil.ReadAll(t.transport)
resp, err := io.ReadAll(t.transport)
if err != nil {
return nil, &TransportError{"read", err}
}
Expand Down

0 comments on commit b238ee2

Please sign in to comment.