Skip to content

Commit

Permalink
chore: replace ioutil with io and os (golang-jwt#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Mar 29, 2023
1 parent 3558b63 commit 0eef781
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 37 deletions.
3 changes: 1 addition & 2 deletions cmd/jwt/main.go
Expand Up @@ -11,7 +11,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"sort"
Expand Down Expand Up @@ -91,7 +90,7 @@ func loadData(p string) ([]byte, error) {
return nil, err
}
}
return ioutil.ReadAll(rdr)
return io.ReadAll(rdr)
}

// Print a json object in accordance with the prophecy (or the command line options)
Expand Down
10 changes: 5 additions & 5 deletions ecdsa_test.go
Expand Up @@ -2,7 +2,7 @@ package jwt_test

import (
"crypto/ecdsa"
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -55,7 +55,7 @@ func TestECDSAVerify(t *testing.T) {
for _, data := range ecdsaTestData {
var err error

key, _ := ioutil.ReadFile(data.keys["public"])
key, _ := os.ReadFile(data.keys["public"])

var ecdsaKey *ecdsa.PublicKey
if ecdsaKey, err = jwt.ParseECPublicKeyFromPEM(key); err != nil {
Expand All @@ -78,7 +78,7 @@ func TestECDSAVerify(t *testing.T) {
func TestECDSASign(t *testing.T) {
for _, data := range ecdsaTestData {
var err error
key, _ := ioutil.ReadFile(data.keys["private"])
key, _ := os.ReadFile(data.keys["private"])

var ecdsaKey *ecdsa.PrivateKey
if ecdsaKey, err = jwt.ParseECPrivateKeyFromPEM(key); err != nil {
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestECDSASign(t *testing.T) {

func BenchmarkECDSAParsing(b *testing.B) {
for _, data := range ecdsaTestData {
key, _ := ioutil.ReadFile(data.keys["private"])
key, _ := os.ReadFile(data.keys["private"])

b.Run(data.name, func(b *testing.B) {
b.ReportAllocs()
Expand All @@ -126,7 +126,7 @@ func BenchmarkECDSAParsing(b *testing.B) {

func BenchmarkECDSASigning(b *testing.B) {
for _, data := range ecdsaTestData {
key, _ := ioutil.ReadFile(data.keys["private"])
key, _ := os.ReadFile(data.keys["private"])

ecdsaKey, err := jwt.ParseECPrivateKeyFromPEM(key)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions ed25519_test.go
@@ -1,7 +1,7 @@
package jwt_test

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -38,7 +38,7 @@ func TestEd25519Verify(t *testing.T) {
for _, data := range ed25519TestData {
var err error

key, _ := ioutil.ReadFile(data.keys["public"])
key, _ := os.ReadFile(data.keys["public"])

ed25519Key, err := jwt.ParseEdPublicKeyFromPEM(key)
if err != nil {
Expand All @@ -62,7 +62,7 @@ func TestEd25519Verify(t *testing.T) {
func TestEd25519Sign(t *testing.T) {
for _, data := range ed25519TestData {
var err error
key, _ := ioutil.ReadFile(data.keys["private"])
key, _ := os.ReadFile(data.keys["private"])

ed25519Key, err := jwt.ParseEdPrivateKeyFromPEM(key)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions hmac_example_test.go
Expand Up @@ -2,7 +2,7 @@ package jwt_test

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/golang-jwt/jwt/v4"
Expand All @@ -15,7 +15,7 @@ var hmacSampleSecret []byte

func init() {
// Load sample key data
if keyData, e := ioutil.ReadFile("test/hmacTestKey"); e == nil {
if keyData, e := os.ReadFile("test/hmacTestKey"); e == nil {
hmacSampleSecret = keyData
} else {
panic(e)
Expand Down
4 changes: 2 additions & 2 deletions hmac_test.go
@@ -1,7 +1,7 @@
package jwt_test

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -46,7 +46,7 @@ var hmacTestData = []struct {
}

// Sample data from http://tools.ietf.org/html/draft-jones-json-web-signature-04#appendix-A.1
var hmacTestKey, _ = ioutil.ReadFile("test/hmacTestKey")
var hmacTestKey, _ = os.ReadFile("test/hmacTestKey")

func TestHMACVerify(t *testing.T) {
for _, data := range hmacTestData {
Expand Down
6 changes: 3 additions & 3 deletions http_example_test.go
Expand Up @@ -8,11 +8,11 @@ import (
"crypto/rsa"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"

Expand All @@ -34,13 +34,13 @@ var (

// read the key files before starting http handlers
func init() {
signBytes, err := ioutil.ReadFile(privKeyPath)
signBytes, err := os.ReadFile(privKeyPath)
fatal(err)

signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
fatal(err)

verifyBytes, err := ioutil.ReadFile(pubKeyPath)
verifyBytes, err := os.ReadFile(pubKeyPath)
fatal(err)

verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
Expand Down
7 changes: 4 additions & 3 deletions rsa_pss_test.go
@@ -1,10 +1,11 @@
//go:build go1.4
// +build go1.4

package jwt_test

import (
"crypto/rsa"
"io/ioutil"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -53,7 +54,7 @@ var rsaPSSTestData = []struct {
func TestRSAPSSVerify(t *testing.T) {
var err error

key, _ := ioutil.ReadFile("test/sample_key.pub")
key, _ := os.ReadFile("test/sample_key.pub")
var rsaPSSKey *rsa.PublicKey
if rsaPSSKey, err = jwt.ParseRSAPublicKeyFromPEM(key); err != nil {
t.Errorf("Unable to parse RSA public key: %v", err)
Expand All @@ -76,7 +77,7 @@ func TestRSAPSSVerify(t *testing.T) {
func TestRSAPSSSign(t *testing.T) {
var err error

key, _ := ioutil.ReadFile("test/sample_key")
key, _ := os.ReadFile("test/sample_key")
var rsaPSSKey *rsa.PrivateKey
if rsaPSSKey, err = jwt.ParseRSAPrivateKeyFromPEM(key); err != nil {
t.Errorf("Unable to parse RSA private key: %v", err)
Expand Down
24 changes: 12 additions & 12 deletions rsa_test.go
Expand Up @@ -6,7 +6,7 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -51,7 +51,7 @@ var rsaTestData = []struct {
}

func TestRSAVerify(t *testing.T) {
keyData, _ := ioutil.ReadFile("test/sample_key.pub")
keyData, _ := os.ReadFile("test/sample_key.pub")
key, _ := jwt.ParseRSAPublicKeyFromPEM(keyData)

for _, data := range rsaTestData {
Expand All @@ -69,7 +69,7 @@ func TestRSAVerify(t *testing.T) {
}

func TestRSASign(t *testing.T) {
keyData, _ := ioutil.ReadFile("test/sample_key")
keyData, _ := os.ReadFile("test/sample_key")
key, _ := jwt.ParseRSAPrivateKeyFromPEM(keyData)

for _, data := range rsaTestData {
Expand All @@ -88,7 +88,7 @@ func TestRSASign(t *testing.T) {
}

func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key.pub")
key, _ := os.ReadFile("test/sample_key.pub")
parsedKey, err := jwt.ParseRSAPublicKeyFromPEM(key)
if err != nil {
t.Fatal(err)
Expand All @@ -102,7 +102,7 @@ func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
}

func TestRSAWithPreParsedPrivateKey(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key")
key, _ := os.ReadFile("test/sample_key")
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
t.Fatal(err)
Expand All @@ -119,9 +119,9 @@ func TestRSAWithPreParsedPrivateKey(t *testing.T) {
}

func TestRSAKeyParsing(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key")
secureKey, _ := ioutil.ReadFile("test/privateSecure.pem")
pubKey, _ := ioutil.ReadFile("test/sample_key.pub")
key, _ := os.ReadFile("test/sample_key")
secureKey, _ := os.ReadFile("test/privateSecure.pem")
pubKey, _ := os.ReadFile("test/sample_key.pub")
badKey := []byte("All your base are belong to key")

// Test parsePrivateKey
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestRSAParsePublicKeyFromPEM_PKCS1(t *testing.T) {
}

func BenchmarkRSAParsing(b *testing.B) {
key, _ := ioutil.ReadFile("test/sample_key")
key, _ := os.ReadFile("test/sample_key")

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -194,7 +194,7 @@ func BenchmarkRSAParsing(b *testing.B) {
}

func BenchmarkRS256Signing(b *testing.B) {
key, _ := ioutil.ReadFile("test/sample_key")
key, _ := os.ReadFile("test/sample_key")
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
b.Fatal(err)
Expand All @@ -204,7 +204,7 @@ func BenchmarkRS256Signing(b *testing.B) {
}

func BenchmarkRS384Signing(b *testing.B) {
key, _ := ioutil.ReadFile("test/sample_key")
key, _ := os.ReadFile("test/sample_key")
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
b.Fatal(err)
Expand All @@ -214,7 +214,7 @@ func BenchmarkRS384Signing(b *testing.B) {
}

func BenchmarkRS512Signing(b *testing.B) {
key, _ := ioutil.ReadFile("test/sample_key")
key, _ := os.ReadFile("test/sample_key")
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
b.Fatal(err)
Expand Down
10 changes: 5 additions & 5 deletions test/helpers.go
Expand Up @@ -3,13 +3,13 @@ package test
import (
"crypto"
"crypto/rsa"
"io/ioutil"
"os"

"github.com/golang-jwt/jwt/v4"
)

func LoadRSAPrivateKeyFromDisk(location string) *rsa.PrivateKey {
keyData, e := ioutil.ReadFile(location)
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
Expand All @@ -21,7 +21,7 @@ func LoadRSAPrivateKeyFromDisk(location string) *rsa.PrivateKey {
}

func LoadRSAPublicKeyFromDisk(location string) *rsa.PublicKey {
keyData, e := ioutil.ReadFile(location)
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
Expand All @@ -45,7 +45,7 @@ func MakeSampleToken(c jwt.Claims, method jwt.SigningMethod, key interface{}) st
}

func LoadECPrivateKeyFromDisk(location string) crypto.PrivateKey {
keyData, e := ioutil.ReadFile(location)
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
Expand All @@ -57,7 +57,7 @@ func LoadECPrivateKeyFromDisk(location string) crypto.PrivateKey {
}

func LoadECPublicKeyFromDisk(location string) crypto.PublicKey {
keyData, e := ioutil.ReadFile(location)
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
Expand Down

0 comments on commit 0eef781

Please sign in to comment.