Skip to content

Commit

Permalink
refactor: Replace deprecated ioutil commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lubux committed Jan 18, 2024
1 parent d94a5cc commit cabb822
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 55 deletions.
5 changes: 2 additions & 3 deletions armor/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package armor
import (
"bytes"
"io"
"io/ioutil"

"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/gopenpgp/v3/constants"
Expand Down Expand Up @@ -105,7 +104,7 @@ func Unarmor(input string) ([]byte, error) {
if err != nil {
return nil, errors.Wrap(err, "armor: unable to unarmor")
}
return ioutil.ReadAll(b.Body)
return io.ReadAll(b.Body)
}

// UnarmorBytes unarmors an armored input into a byte array.
Expand All @@ -114,7 +113,7 @@ func UnarmorBytes(input []byte) ([]byte, error) {
if err != nil {
return nil, errors.Wrap(err, "armor: unable to unarmor")
}
return ioutil.ReadAll(b.Body)
return io.ReadAll(b.Body)
}

func ArmorPGPSignatureBinary(signature []byte) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions crypto/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package crypto

import (
"crypto/rsa"
"io/ioutil"
"math/big"
"os"
"strings"
"testing"

Expand All @@ -21,7 +21,7 @@ var testPGP *PGPHandle
var testProfiles []*profile.Custom

func readTestFile(name string, trimNewlines bool) string {
data, err := ioutil.ReadFile("testdata/" + name) //nolint
data, err := os.ReadFile("testdata/" + name) //nolint
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions crypto/encrypt_decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -966,7 +965,7 @@ func testEncryptSplitDecryptStream(
if err == nil {
t.Fatal("Expected verify error not read all, got nil")
}
decryptedBytes, err := ioutil.ReadAll(decryptedReader)
decryptedBytes, err := io.ReadAll(decryptedReader)
if err != nil {
t.Fatal("Expected no error while reading the decrypted data, got:", err)
}
Expand Down Expand Up @@ -1038,7 +1037,7 @@ func testEncryptDecryptStream(
if err == nil {
t.Fatal("Expected an error while verifying the signature before reading the data, got nil")
}
decryptedBytes, err := ioutil.ReadAll(decryptedReader)
decryptedBytes, err := io.ReadAll(decryptedReader)
if err != nil {
t.Fatal("Expected no error while reading the decrypted data, got:", err)
}
Expand Down
6 changes: 3 additions & 3 deletions crypto/key_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package crypto

import (
"io/ioutil"
"io"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -278,12 +278,12 @@ func TestGetArmoredPublicKey(t *testing.T) {

assert.Exactly(t, expected.Type, block.Type)

b, err := ioutil.ReadAll(block.Body)
b, err := io.ReadAll(block.Body)
if err != nil {
t.Fatal("Expected no error while reading armored public key body, got:", err)
}

eb, err := ioutil.ReadAll(expected.Body)
eb, err := io.ReadAll(expected.Body)
if err != nil {
t.Fatal("Expected no error while reading expected armored public key body, got:", err)
}
Expand Down
3 changes: 1 addition & 2 deletions crypto/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
goerrors "errors"
"io"
"io/ioutil"
"regexp"

"github.com/ProtonMail/go-crypto/openpgp/packet"
Expand Down Expand Up @@ -87,7 +86,7 @@ func NewPGPMessageFromArmored(armored string) (*PGPMessage, error) {
return nil, errors.Wrap(err, "gopenpgp: error in unarmoring message")
}

message, err := ioutil.ReadAll(encryptedIO.Body)
message, err := io.ReadAll(encryptedIO.Body)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in reading armored message")
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"os"
"testing"
"time"

Expand Down Expand Up @@ -91,7 +91,7 @@ func TestTextMixedMessageDecryptionWithPassword(t *testing.T) {
t.Fatal("Expected no error when decrypting, got:", err)
}

expected, err := ioutil.ReadFile("testdata/message_mixedPasswordPublicExpected")
expected, err := os.ReadFile("testdata/message_mixedPasswordPublicExpected")
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/sessionkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package crypto
import (
"encoding/base64"
"encoding/hex"
"io/ioutil"
"os"
"testing"

"github.com/ProtonMail/gopenpgp/v3/constants"
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestSessionKeyClear(t *testing.T) {
}

func TestAEADDataPacketDecryption(t *testing.T) {
pgpMessageData, err := ioutil.ReadFile("testdata/gpg2.3-aead-pgp-message.pgp")
pgpMessageData, err := os.ReadFile("testdata/gpg2.3-aead-pgp-message.pgp")
if err != nil {
t.Fatal("Expected no error when reading message data, got:", err)
}
Expand Down
26 changes: 13 additions & 13 deletions crypto/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/hex"
"io"
"io/ioutil"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -185,18 +185,18 @@ func Test_KeyRing_GetVerifiedSignatureTimestampSuccess(t *testing.T) {
}

func Test_KeyRing_GetVerifiedSignatureWithTwoKeysTimestampSuccess(t *testing.T) {
publicKey1Armored, err := ioutil.ReadFile("testdata/signature/publicKey1")
publicKey1Armored, err := os.ReadFile("testdata/signature/publicKey1")
if err != nil {
t.Errorf("Couldn't read the public key file: %v", err)
}
publicKey1 := parseKey(t, string(publicKey1Armored))
publicKey2Armored, err := ioutil.ReadFile("testdata/signature/publicKey2")
publicKey2Armored, err := os.ReadFile("testdata/signature/publicKey2")
if err != nil {
t.Errorf("Couldn't read the public key file: %v", err)
}
publicKey2 := parseKey(t, string(publicKey2Armored))
message := []byte("hello world")
signatureArmored, err := ioutil.ReadFile("testdata/signature/detachedSigSignedTwice")
signatureArmored, err := os.ReadFile("testdata/signature/detachedSigSignedTwice")
if err != nil {
t.Errorf("Couldn't read the signature file: %v", err)
}
Expand Down Expand Up @@ -394,7 +394,7 @@ func Test_SignDetachedWithCriticalContext(t *testing.T) {
func Test_VerifyWithUnknownCriticalContext(t *testing.T) {
// given

signatureArmored, err := ioutil.ReadFile("testdata/signature/critical_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/critical_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand All @@ -419,7 +419,7 @@ func Test_VerifyWithUnknownCriticalContext(t *testing.T) {
func Test_VerifyWithUnKnownNonCriticalContext(t *testing.T) {
// given

signatureArmored, err := ioutil.ReadFile("testdata/signature/non_critical_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/non_critical_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand All @@ -445,7 +445,7 @@ func Test_VerifyWithUnKnownNonCriticalContext(t *testing.T) {
func Test_VerifyWithKnownCriticalContext(t *testing.T) {
// given

signatureArmored, err := ioutil.ReadFile("testdata/signature/critical_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/critical_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -477,7 +477,7 @@ func Test_VerifyWithKnownCriticalContext(t *testing.T) {
func Test_VerifyWithWrongContext(t *testing.T) {
// given

signatureArmored, err := ioutil.ReadFile("testdata/signature/critical_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/critical_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -507,7 +507,7 @@ func Test_VerifyWithWrongContext(t *testing.T) {
func Test_VerifyWithMissingNonRequiredContext(t *testing.T) {
// given

signatureArmored, err := ioutil.ReadFile("testdata/signature/no_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/no_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -539,7 +539,7 @@ func Test_VerifyWithMissingNonRequiredContext(t *testing.T) {
func Test_VerifyWithMissingRequiredContext(t *testing.T) {
// given

signatureArmored, err := ioutil.ReadFile("testdata/signature/no_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/no_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -568,7 +568,7 @@ func Test_VerifyWithMissingRequiredContext(t *testing.T) {

func Test_VerifyWithMissingRequiredContextBeforeCutoff(t *testing.T) {
// given
signatureArmored, err := ioutil.ReadFile("testdata/signature/no_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/no_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -607,7 +607,7 @@ func Test_VerifyWithMissingRequiredContextBeforeCutoff(t *testing.T) {

func Test_VerifyWithMissingRequiredContextAfterCutoff(t *testing.T) {
// given
signatureArmored, err := ioutil.ReadFile("testdata/signature/no_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/no_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -644,7 +644,7 @@ func Test_VerifyWithMissingRequiredContextAfterCutoff(t *testing.T) {

func Test_VerifyWithDoubleContext(t *testing.T) {
// given
signatureArmored, err := ioutil.ReadFile("testdata/signature/double_critical_context_detached_sig")
signatureArmored, err := os.ReadFile("testdata/signature/double_critical_context_detached_sig")
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions crypto/verify_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package crypto
import (
"bytes"
"io"
"io/ioutil"

"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/go-crypto/openpgp/clearsign"
Expand Down Expand Up @@ -84,7 +83,7 @@ func (vh *verifyHandle) VerifyDetached(data, signature []byte, encoding int8) (v
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: verifying signature failed")
}
_, err = io.Copy(ioutil.Discard, ptReader)
_, err = io.Copy(io.Discard, ptReader)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: reading data to verify signature failed")
}
Expand Down Expand Up @@ -146,7 +145,7 @@ func (vh *verifyHandle) verifyDetachedSignature(
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: verify signature failed")
}
_, err = io.Copy(ioutil.Discard, ptReader)
_, err = io.Copy(io.Discard, ptReader)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: reading all data from plaintext reader failed")
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func (vh *verifyHandle) verifyCleartext(cleartext []byte) (*VerifyCleartextResul
if len(bytes.TrimSpace(rest)) > 0 {
return nil, errors.New("gopenpgp: cleartext message has trailing text")
}
signature, err := ioutil.ReadAll(block.ArmoredSignature.Body)
signature, err := io.ReadAll(block.ArmoredSignature.Body)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: signature not parsable in cleartext")
}
Expand Down
5 changes: 2 additions & 3 deletions crypto/verify_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package crypto

import (
"io"
"io/ioutil"

openpgp "github.com/ProtonMail/go-crypto/openpgp/v2"
"github.com/pkg/errors"
Expand Down Expand Up @@ -63,12 +62,12 @@ func (msg *VerifyDataReader) VerifySignature() (result *VerifyResult, err error)
// ReadAll reads all plaintext data from the reader
// and returns it as a byte slice.
func (msg *VerifyDataReader) ReadAll() (plaintext []byte, err error) {
return ioutil.ReadAll(msg)
return io.ReadAll(msg)
}

// DiscardAll reads all data from the reader and discards it.
func (msg *VerifyDataReader) DiscardAll() (err error) {
_, err = io.Copy(ioutil.Discard, msg)
_, err = io.Copy(io.Discard, msg)
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/utf8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/hex"
"errors"
"io"
"io/ioutil"
"os"
"strings"
"testing"
)
Expand All @@ -27,7 +27,7 @@ func (c *noOpCloser) Close() (err error) {
}

func loadLargeData(t *testing.T) {
data, err := ioutil.ReadFile("testdata/utf8Valid.txt")
data, err := os.ReadFile("testdata/utf8Valid.txt")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions mime/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mime

import (
"bytes"
"io/ioutil"
"io"
"net/mail"
"net/textproto"

Expand Down Expand Up @@ -99,7 +99,7 @@ func parseMIME(
}

h := textproto.MIMEHeader(mm.Header)
mmBodyData, err := ioutil.ReadAll(mm.Body)
mmBodyData, err := io.ReadAll(mm.Body)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "mime: error in reading message body data")
}
Expand Down
10 changes: 5 additions & 5 deletions mime/mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mime

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -101,7 +101,7 @@ func (tc *testMIMECallbacks) OnError(err error) {
}

func loadPrivateKeyRing(file string, passphrase string) (*crypto.KeyRing, error) {
armored, err := ioutil.ReadFile(filepath.Clean(file))
armored, err := os.ReadFile(filepath.Clean(file))
if err != nil {
return nil, err
}
Expand All @@ -117,7 +117,7 @@ func loadPrivateKeyRing(file string, passphrase string) (*crypto.KeyRing, error)
}

func loadPublicKeyRing(file string) (*crypto.KeyRing, error) {
armored, err := ioutil.ReadFile(filepath.Clean(file))
armored, err := os.ReadFile(filepath.Clean(file))
if err != nil {
return nil, err
}
Expand All @@ -143,7 +143,7 @@ func loadPublicKeyRing(file string) (*crypto.KeyRing, error) {
}

func loadMessage(file string) ([]byte, error) {
armored, err := ioutil.ReadFile(filepath.Clean(file))
armored, err := os.ReadFile(filepath.Clean(file))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -332,7 +332,7 @@ func readTestFile(name string, trimNewlines bool) string {
}

func readTestFileBytes(name string) []byte {
data, err := ioutil.ReadFile(filepath.Join("testdata/", name)) //nolint:gosec
data, err := os.ReadFile(filepath.Join("testdata/", name)) //nolint:gosec
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit cabb822

Please sign in to comment.