Skip to content

Commit 35f4265

Browse files
cuishuanggopherbot
authored andcommitted
all: replace io/ioutil with io and os package
For golang/go#45557 Change-Id: I447530cc66896aef7a8d528ccb8d095b80e3cf47 GitHub-Last-Rev: 5f385ff GitHub-Pull-Request: #230 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/430797 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Meng Zhuo <mzh@golangcn.org> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
1 parent c86fa9a commit 35f4265

30 files changed

+60
-83
lines changed

acme/autocert/autocert_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"crypto/x509/pkix"
1818
"encoding/asn1"
1919
"fmt"
20-
"io/ioutil"
20+
"io"
2121
"math/big"
2222
"net/http"
2323
"net/http/httptest"
@@ -941,7 +941,7 @@ func TestEndToEndALPN(t *testing.T) {
941941
t.Fatal(err)
942942
}
943943
defer res.Body.Close()
944-
b, err := ioutil.ReadAll(res.Body)
944+
b, err := io.ReadAll(res.Body)
945945
if err != nil {
946946
t.Fatal(err)
947947
}
@@ -995,7 +995,7 @@ func TestEndToEndHTTP(t *testing.T) {
995995
t.Fatal(err)
996996
}
997997
defer res.Body.Close()
998-
b, err := ioutil.ReadAll(res.Body)
998+
b, err := io.ReadAll(res.Body)
999999
if err != nil {
10001000
t.Fatal(err)
10011001
}

acme/autocert/cache.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package autocert
77
import (
88
"context"
99
"errors"
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
)
@@ -48,7 +47,7 @@ func (d DirCache) Get(ctx context.Context, name string) ([]byte, error) {
4847
done = make(chan struct{})
4948
)
5049
go func() {
51-
data, err = ioutil.ReadFile(name)
50+
data, err = os.ReadFile(name)
5251
close(done)
5352
}()
5453
select {
@@ -119,7 +118,7 @@ func (d DirCache) Delete(ctx context.Context, name string) error {
119118
// writeTempFile writes b to a temporary file, closes the file and returns its path.
120119
func (d DirCache) writeTempFile(prefix string, b []byte) (name string, reterr error) {
121120
// TempFile uses 0600 permissions
122-
f, err := ioutil.TempFile(string(d), prefix)
121+
f, err := os.CreateTemp(string(d), prefix)
123122
if err != nil {
124123
return "", err
125124
}

acme/autocert/cache_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package autocert
66

77
import (
88
"context"
9-
"io/ioutil"
109
"os"
1110
"path/filepath"
1211
"reflect"
@@ -17,7 +16,7 @@ import (
1716
var _ Cache = DirCache("/")
1817

1918
func TestDirCache(t *testing.T) {
20-
dir, err := ioutil.TempDir("", "autocert")
19+
dir, err := os.MkdirTemp("", "autocert")
2120
if err != nil {
2221
t.Fatal(err)
2322
}

acme/http.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"encoding/json"
1313
"errors"
1414
"fmt"
15-
"io/ioutil"
15+
"io"
1616
"math/big"
1717
"net/http"
1818
"strconv"
@@ -310,7 +310,7 @@ func isRetriable(code int) bool {
310310
func responseError(resp *http.Response) error {
311311
// don't care if ReadAll returns an error:
312312
// json.Unmarshal will fail in that case anyway
313-
b, _ := ioutil.ReadAll(resp.Body)
313+
b, _ := io.ReadAll(resp.Body)
314314
e := &wireError{Status: resp.StatusCode}
315315
if err := json.Unmarshal(b, e); err != nil {
316316
// this is not a regular error response:

acme/http_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package acme
77
import (
88
"context"
99
"fmt"
10-
"io/ioutil"
10+
"io"
1111
"net/http"
1212
"net/http/httptest"
1313
"reflect"
@@ -54,7 +54,7 @@ func TestErrorResponse(t *testing.T) {
5454
res := &http.Response{
5555
StatusCode: 400,
5656
Status: "400 Bad Request",
57-
Body: ioutil.NopCloser(strings.NewReader(s)),
57+
Body: io.NopCloser(strings.NewReader(s)),
5858
Header: http.Header{"X-Foo": {"bar"}},
5959
}
6060
err := responseError(res)

acme/rfc8555.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"errors"
1414
"fmt"
1515
"io"
16-
"io/ioutil"
1716
"net/http"
1817
"time"
1918
)
@@ -390,7 +389,7 @@ func (c *Client) fetchCertRFC(ctx context.Context, url string, bundle bool) ([][
390389
// Get all the bytes up to a sane maximum.
391390
// Account very roughly for base64 overhead.
392391
const max = maxCertChainSize + maxCertChainSize/33
393-
b, err := ioutil.ReadAll(io.LimitReader(res.Body, max+1))
392+
b, err := io.ReadAll(io.LimitReader(res.Body, max+1))
394393
if err != nil {
395394
return nil, fmt.Errorf("acme: fetch cert response stream: %v", err)
396395
}
@@ -469,7 +468,7 @@ func (c *Client) ListCertAlternates(ctx context.Context, url string) ([]string,
469468

470469
// We don't need the body but we need to discard it so we don't end up
471470
// preventing keep-alive
472-
if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
471+
if _, err := io.Copy(io.Discard, res.Body); err != nil {
473472
return nil, fmt.Errorf("acme: cert alternates response stream: %v", err)
474473
}
475474
alts := linkHeader(res.Header, "alternate")

acme/rfc8555_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"encoding/pem"
1818
"errors"
1919
"fmt"
20-
"io/ioutil"
20+
"io"
2121
"math/big"
2222
"net/http"
2323
"net/http/httptest"
@@ -148,7 +148,7 @@ func TestRFC_postKID(t *testing.T) {
148148
w.Header().Set("Location", "/account-1")
149149
w.Write([]byte(`{"status":"valid"}`))
150150
case "/post":
151-
b, _ := ioutil.ReadAll(r.Body) // check err later in decodeJWSxxx
151+
b, _ := io.ReadAll(r.Body) // check err later in decodeJWSxxx
152152
head, err := decodeJWSHead(bytes.NewReader(b))
153153
if err != nil {
154154
t.Errorf("decodeJWSHead: %v", err)
@@ -194,7 +194,7 @@ func TestRFC_postKID(t *testing.T) {
194194
t.Fatal(err)
195195
}
196196
defer res.Body.Close()
197-
b, _ := ioutil.ReadAll(res.Body) // don't care about err - just checking b
197+
b, _ := io.ReadAll(res.Body) // don't care about err - just checking b
198198
if string(b) != "pong" {
199199
t.Errorf("res.Body = %q; want pong", b)
200200
}
@@ -297,7 +297,7 @@ func TestRFC_Register(t *testing.T) {
297297
"orders": %q
298298
}`, email, s.url("/accounts/1/orders"))
299299

300-
b, _ := ioutil.ReadAll(r.Body) // check err later in decodeJWSxxx
300+
b, _ := io.ReadAll(r.Body) // check err later in decodeJWSxxx
301301
head, err := decodeJWSHead(bytes.NewReader(b))
302302
if err != nil {
303303
t.Errorf("decodeJWSHead: %v", err)
@@ -528,7 +528,7 @@ func TestRFC_UpdateReg(t *testing.T) {
528528
w.WriteHeader(http.StatusOK)
529529
w.Write([]byte(`{"status": "valid"}`))
530530

531-
b, _ := ioutil.ReadAll(r.Body) // check err later in decodeJWSxxx
531+
b, _ := io.ReadAll(r.Body) // check err later in decodeJWSxxx
532532
head, err := decodeJWSHead(bytes.NewReader(b))
533533
if err != nil {
534534
t.Errorf("decodeJWSHead: %v", err)
@@ -668,7 +668,7 @@ func TestRFC_DeactivateReg(t *testing.T) {
668668
Orders: s.url("/accounts/1/orders"),
669669
})
670670

671-
b, _ := ioutil.ReadAll(r.Body) // check err later in decodeJWSxxx
671+
b, _ := io.ReadAll(r.Body) // check err later in decodeJWSxxx
672672
head, err := decodeJWSHead(bytes.NewReader(b))
673673
if err != nil {
674674
t.Errorf("decodeJWSHead: %v", err)
@@ -700,7 +700,7 @@ func TestRFC_DeactivateReg(t *testing.T) {
700700
})
701701
}
702702
var req account
703-
b, _ := ioutil.ReadAll(r.Body) // check err later in decodeJWSxxx
703+
b, _ := io.ReadAll(r.Body) // check err later in decodeJWSxxx
704704
head, err := decodeJWSHead(bytes.NewReader(b))
705705
if err != nil {
706706
t.Errorf("decodeJWSHead: %v", err)

internal/wycheproof/wycheproof_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"encoding/hex"
1313
"encoding/json"
1414
"fmt"
15-
"io/ioutil"
1615
"log"
1716
"os"
1817
"os/exec"
@@ -62,7 +61,7 @@ func TestMain(m *testing.M) {
6261
}
6362

6463
func readTestVector(t *testing.T, f string, dest interface{}) {
65-
b, err := ioutil.ReadFile(filepath.Join(wycheproofTestVectorsDir, f))
64+
b, err := os.ReadFile(filepath.Join(wycheproofTestVectorsDir, f))
6665
if err != nil {
6766
t.Fatalf("failed to read json file: %v", err)
6867
}

openpgp/armor/armor_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package armor
77
import (
88
"bytes"
99
"hash/adler32"
10-
"io/ioutil"
10+
"io"
1111
"testing"
1212
)
1313

@@ -29,7 +29,7 @@ func TestDecodeEncode(t *testing.T) {
2929
t.Errorf("result.Header: got:%#v", result.Header)
3030
}
3131

32-
contents, err := ioutil.ReadAll(result.Body)
32+
contents, err := io.ReadAll(result.Body)
3333
if err != nil {
3434
t.Error(err)
3535
}

openpgp/packet/compressed_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/hex"
1010
"io"
11-
"io/ioutil"
1211
"testing"
1312
)
1413

@@ -25,7 +24,7 @@ func TestCompressed(t *testing.T) {
2524
return
2625
}
2726

28-
contents, err := ioutil.ReadAll(c.Body)
27+
contents, err := io.ReadAll(c.Body)
2928
if err != nil && err != io.EOF {
3029
t.Error(err)
3130
return

openpgp/packet/opaque.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package packet
77
import (
88
"bytes"
99
"io"
10-
"io/ioutil"
1110

1211
"golang.org/x/crypto/openpgp/errors"
1312
)
@@ -26,7 +25,7 @@ type OpaquePacket struct {
2625
}
2726

2827
func (op *OpaquePacket) parse(r io.Reader) (err error) {
29-
op.Contents, err = ioutil.ReadAll(r)
28+
op.Contents, err = io.ReadAll(r)
3029
return
3130
}
3231

openpgp/packet/packet_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"golang.org/x/crypto/openpgp/errors"
1212
"io"
13-
"io/ioutil"
1413
"testing"
1514
)
1615

@@ -100,7 +99,7 @@ var partialLengthReaderTests = []struct {
10099
func TestPartialLengthReader(t *testing.T) {
101100
for i, test := range partialLengthReaderTests {
102101
r := &partialLengthReader{readerFromHex(test.hexInput), 0, true}
103-
out, err := ioutil.ReadAll(r)
102+
out, err := io.ReadAll(r)
104103
if test.err != nil {
105104
if err != test.err {
106105
t.Errorf("%d: expected different error got:%s want:%s", i, err, test.err)
@@ -172,7 +171,7 @@ func TestReadHeader(t *testing.T) {
172171
continue
173172
}
174173

175-
body, err := ioutil.ReadAll(contents)
174+
body, err := io.ReadAll(contents)
176175
if err != nil {
177176
if !test.unexpectedEOF || err != io.ErrUnexpectedEOF {
178177
t.Errorf("%d: unexpected error from contents: %s", i, err)

openpgp/packet/private_key.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"crypto/rsa"
1414
"crypto/sha1"
1515
"io"
16-
"io/ioutil"
1716
"math/big"
1817
"strconv"
1918
"time"
@@ -133,7 +132,7 @@ func (pk *PrivateKey) parse(r io.Reader) (err error) {
133132
}
134133
}
135134

136-
pk.encryptedData, err = ioutil.ReadAll(r)
135+
pk.encryptedData, err = io.ReadAll(r)
137136
if err != nil {
138137
return
139138
}

openpgp/packet/signature_v3_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"crypto"
1010
"encoding/hex"
1111
"io"
12-
"io/ioutil"
1312
"testing"
1413

1514
"golang.org/x/crypto/openpgp/armor"
@@ -45,7 +44,7 @@ func TestSignatureV3Reserialize(t *testing.T) {
4544
t.Errorf("error reserializing: %s", err)
4645
return
4746
}
48-
expected, err := ioutil.ReadAll(v3KeyReader(t))
47+
expected, err := io.ReadAll(v3KeyReader(t))
4948
if err != nil {
5049
t.Error(err)
5150
return

openpgp/packet/symmetric_key_encrypted_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/hex"
1010
"io"
11-
"io/ioutil"
1211
"testing"
1312
)
1413

@@ -46,7 +45,7 @@ func TestSymmetricKeyEncrypted(t *testing.T) {
4645
return
4746
}
4847

49-
contents, err := ioutil.ReadAll(r)
48+
contents, err := io.ReadAll(r)
5049
if err != nil && err != io.EOF {
5150
t.Error(err)
5251
return

openpgp/packet/symmetrically_encrypted.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (w *seMDCWriter) Close() (err error) {
236236
return w.w.Close()
237237
}
238238

239-
// noOpCloser is like an ioutil.NopCloser, but for an io.Writer.
239+
// noOpCloser is like an io.NopCloser, but for an io.Writer.
240240
type noOpCloser struct {
241241
w io.Writer
242242
}

openpgp/packet/symmetrically_encrypted_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/hex"
1111
"golang.org/x/crypto/openpgp/errors"
1212
"io"
13-
"io/ioutil"
1413
"testing"
1514
)
1615

@@ -42,7 +41,7 @@ func testMDCReader(t *testing.T) {
4241
for stride := 1; stride < len(mdcPlaintext)/2; stride++ {
4342
r := &testReader{data: mdcPlaintext, stride: stride}
4443
mdcReader := &seMDCReader{in: r, h: sha1.New()}
45-
body, err := ioutil.ReadAll(mdcReader)
44+
body, err := io.ReadAll(mdcReader)
4645
if err != nil {
4746
t.Errorf("stride: %d, error: %s", stride, err)
4847
continue
@@ -62,7 +61,7 @@ func testMDCReader(t *testing.T) {
6261

6362
r := &testReader{data: mdcPlaintext, stride: 2}
6463
mdcReader := &seMDCReader{in: r, h: sha1.New()}
65-
_, err := ioutil.ReadAll(mdcReader)
64+
_, err := io.ReadAll(mdcReader)
6665
if err != nil {
6766
t.Errorf("corruption test, error: %s", err)
6867
return

openpgp/packet/userattribute.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"image"
1010
"image/jpeg"
1111
"io"
12-
"io/ioutil"
1312
)
1413

1514
const UserAttrImageSubpacket = 1
@@ -56,7 +55,7 @@ func NewUserAttribute(contents ...*OpaqueSubpacket) *UserAttribute {
5655

5756
func (uat *UserAttribute) parse(r io.Reader) (err error) {
5857
// RFC 4880, section 5.13
59-
b, err := ioutil.ReadAll(r)
58+
b, err := io.ReadAll(r)
6059
if err != nil {
6160
return
6261
}

0 commit comments

Comments
 (0)