Skip to content

Commit d00c43e

Browse files
committed
remove redundant []byte conversions
1 parent c2702ea commit d00c43e

13 files changed

+149
-150
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ linters:
1313
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
1414
- deadcode # Finds unused code [fast: true, auto-fix: false]
1515
- golint # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true, auto-fix: false]
16+
- unconvert # Remove unnecessary type conversions [fast: true, auto-fix: false]
1617

1718
disable:
1819
# TODO(ross): fix errors reported by these checkers and enable them
@@ -38,7 +39,6 @@ linters:
3839
- structcheck # Finds unused struct fields [fast: true, auto-fix: false]
3940
- stylecheck # Stylecheck is a replacement for golint [fast: false, auto-fix: false]
4041
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: true, auto-fix: false]
41-
- unconvert # Remove unnecessary type conversions [fast: true, auto-fix: false]
4242
- unparam # Reports unused function parameters [fast: false, auto-fix: false]
4343
- unused # Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
4444
- varcheck # Finds unused global variables and constants [fast: true, auto-fix: false]

identity_provider_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func mustParseURL(s string) url.URL {
5252
}
5353

5454
func mustParsePrivateKey(pemStr []byte) crypto.PrivateKey {
55-
b, _ := pem.Decode([]byte(pemStr))
55+
b, _ := pem.Decode(pemStr)
5656
if b == nil {
5757
panic("cannot parse PEM")
5858
}
@@ -64,7 +64,7 @@ func mustParsePrivateKey(pemStr []byte) crypto.PrivateKey {
6464
}
6565

6666
func mustParseCertificate(pemStr []byte) *x509.Certificate {
67-
b, _ := pem.Decode([]byte(pemStr))
67+
b, _ := pem.Decode(pemStr)
6868
if b == nil {
6969
panic("cannot parse PEM")
7070
}
@@ -83,7 +83,7 @@ func NewIdentifyProviderTest(t *testing.T) *IdentityProviderTest {
8383
}
8484
jwt.TimeFunc = TimeNow
8585
RandReader = &testRandomReader{} // TODO(ross): remove this and use the below generator
86-
xmlenc.RandReader = rand.New(rand.NewSource(0)) // deterministic random numbers for tests
86+
xmlenc.RandReader = rand.New(rand.NewSource(0)) //nolint:gosec // deterministic random numbers for tests
8787

8888
test.SPKey = mustParsePrivateKey(golden.Get(t, "sp_key.pem")).(*rsa.PrivateKey)
8989
test.SPCertificate = mustParseCertificate(golden.Get(t, "sp_cert.pem"))
@@ -261,7 +261,7 @@ func TestIDPCanHandleRequestWithNewSession(t *testing.T) {
261261

262262
decodedRequest, err := testsaml.ParseRedirectRequest(requestURL)
263263
assert.Check(t, err)
264-
assert.Check(t, is.Equal(string(golden.Get(t, "idp_authn_request.xml")), string(decodedRequest)))
264+
golden.Assert(t, string(decodedRequest), "idp_authn_request.xml")
265265
assert.Check(t, is.Equal("ThisIsTheRelayState", requestURL.Query().Get("RelayState")))
266266

267267
r, _ := http.NewRequest("GET", requestURL.String(), nil)

samlidp/samlidp_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func mustParseURL(s string) url.URL {
4242
return *rv
4343
}
4444

45-
func mustParsePrivateKey(pemStr string) crypto.PrivateKey {
46-
b, _ := pem.Decode([]byte(pemStr))
45+
func mustParsePrivateKey(pemStr []byte) crypto.PrivateKey {
46+
b, _ := pem.Decode(pemStr)
4747
if b == nil {
4848
panic("cannot parse PEM")
4949
}
@@ -54,8 +54,8 @@ func mustParsePrivateKey(pemStr string) crypto.PrivateKey {
5454
return k
5555
}
5656

57-
func mustParseCertificate(pemStr string) *x509.Certificate {
58-
b, _ := pem.Decode([]byte(pemStr))
57+
func mustParseCertificate(pemStr []byte) *x509.Certificate {
58+
b, _ := pem.Decode(pemStr)
5959
if b == nil {
6060
panic("cannot parse PEM")
6161
}
@@ -86,17 +86,17 @@ func NewServerTest(t *testing.T) *ServerTest {
8686
jwt.TimeFunc = saml.TimeNow
8787
saml.RandReader = &testRandomReader{}
8888

89-
test.SPKey = mustParsePrivateKey(string(golden.Get(t, "sp_key.pem"))).(*rsa.PrivateKey)
90-
test.SPCertificate = mustParseCertificate(string(golden.Get(t, "sp_cert.pem")))
89+
test.SPKey = mustParsePrivateKey(golden.Get(t, "sp_key.pem")).(*rsa.PrivateKey)
90+
test.SPCertificate = mustParseCertificate(golden.Get(t, "sp_cert.pem"))
9191
test.SP = saml.ServiceProvider{
9292
Key: test.SPKey,
9393
Certificate: test.SPCertificate,
9494
MetadataURL: mustParseURL("https://sp.example.com/saml2/metadata"),
9595
AcsURL: mustParseURL("https://sp.example.com/saml2/acs"),
9696
IDPMetadata: &saml.EntityDescriptor{},
9797
}
98-
test.Key = mustParsePrivateKey(string(golden.Get(t, "idp_key.pem"))).(*rsa.PrivateKey)
99-
test.Certificate = mustParseCertificate(string(golden.Get(t, "idp_cert.pem")))
98+
test.Key = mustParsePrivateKey(golden.Get(t, "idp_key.pem")).(*rsa.PrivateKey)
99+
test.Certificate = mustParseCertificate(golden.Get(t, "idp_cert.pem"))
100100

101101
test.Store = MemoryStore{}
102102

samlidp/service_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestServicesCrud(t *testing.T) {
3030
r, _ = http.NewRequest("GET", "https://idp.example.com/services/sp", nil)
3131
test.Server.ServeHTTP(w, r)
3232
assert.Check(t, is.Equal(http.StatusOK, w.Code))
33-
assert.Check(t, is.Equal(w.Body.String(), string(golden.Get(t, "sp_metadata.xml"))))
33+
golden.Assert(t, w.Body.String(), "sp_metadata.xml")
3434

3535
w = httptest.NewRecorder()
3636
r, _ = http.NewRequest("GET", "https://idp.example.com/services/", nil)

samlsp/fetch_metadata_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package samlsp
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"net/http"
78
"net/http/httptest"
89
"net/url"
9-
"strings"
1010
"testing"
1111

1212
"gotest.tools/assert"
@@ -18,7 +18,7 @@ func TestFetchMetadata(t *testing.T) {
1818

1919
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2020
assert.Check(t, is.Equal("/metadata", r.URL.String()))
21-
fmt.Fprint(w, test.IDPMetadata)
21+
w.Write(test.IDPMetadata)
2222
}))
2323

2424
fmt.Println(testServer.URL + "/metadata")
@@ -30,11 +30,12 @@ func TestFetchMetadata(t *testing.T) {
3030

3131
func TestFetchMetadataRejectsInvalid(t *testing.T) {
3232
test := NewMiddlewareTest(t)
33-
test.IDPMetadata = strings.Replace(test.IDPMetadata, "<EntityDescriptor ", "<EntityDescriptor ::foo=\"bar\"", -1)
33+
test.IDPMetadata = bytes.Replace(test.IDPMetadata,
34+
[]byte("<EntityDescriptor "), []byte("<EntityDescriptor ::foo=\"bar\""), -1)
3435

3536
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3637
assert.Check(t, is.Equal("/metadata", r.URL.String()))
37-
fmt.Fprint(w, test.IDPMetadata)
38+
w.Write(test.IDPMetadata)
3839
}))
3940

4041
fmt.Println(testServer.URL + "/metadata")

samlsp/middleware_test.go

+19-24
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import (
2828
)
2929

3030
type MiddlewareTest struct {
31-
AuthnRequest string
32-
SamlResponse string
31+
AuthnRequest []byte
32+
SamlResponse []byte
3333
Key *rsa.PrivateKey
3434
Certificate *x509.Certificate
35-
IDPMetadata string
35+
IDPMetadata []byte
3636
Middleware *Middleware
3737
expectedSessionCookie string
3838
}
@@ -59,14 +59,14 @@ func NewMiddlewareTest(t *testing.T) *MiddlewareTest {
5959
saml.Clock = dsig.NewFakeClockAt(saml.TimeNow())
6060
saml.RandReader = &testRandomReader{}
6161

62-
test.AuthnRequest = string(golden.Get(t, "authn_request.url"))
63-
test.SamlResponse = string(golden.Get(t, "saml_response.xml"))
64-
test.Key = mustParsePrivateKey(string(golden.Get(t, "key.pem"))).(*rsa.PrivateKey)
65-
test.Certificate = mustParseCertificate(string(golden.Get(t, "cert.pem")))
66-
test.IDPMetadata = string(golden.Get(t, "idp_metadata.xml"))
62+
test.AuthnRequest = golden.Get(t, "authn_request.url")
63+
test.SamlResponse = golden.Get(t, "saml_response.xml")
64+
test.Key = mustParsePrivateKey(golden.Get(t, "key.pem")).(*rsa.PrivateKey)
65+
test.Certificate = mustParseCertificate(golden.Get(t, "cert.pem"))
66+
test.IDPMetadata = golden.Get(t, "idp_metadata.xml")
6767

6868
var metadata saml.EntityDescriptor
69-
if err := xml.Unmarshal([]byte(test.IDPMetadata), &metadata); err != nil {
69+
if err := xml.Unmarshal(test.IDPMetadata, &metadata); err != nil {
7070
panic(err)
7171
}
7272

@@ -131,8 +131,7 @@ func TestMiddlewareCanProduceMetadata(t *testing.T) {
131131
assert.Check(t, is.Equal(http.StatusOK, resp.Code))
132132
assert.Check(t, is.Equal("application/samlmetadata+xml",
133133
resp.Header().Get("Content-type")))
134-
assert.Check(t, is.Equal(string(golden.Get(t, "expected_middleware_metadata.xml")),
135-
resp.Body.String()))
134+
golden.Assert(t, resp.Body.String(), "expected_middleware_metadata.xml")
136135
}
137136

138137
func TestMiddlewareFourOhFour(t *testing.T) {
@@ -168,8 +167,7 @@ func TestMiddlewareRequireAccountNoCreds(t *testing.T) {
168167
assert.Check(t, err)
169168
decodedRequest, err := testsaml.ParseRedirectRequest(redirectURL)
170169
assert.Check(t, err)
171-
assert.Check(t, is.Equal(string(golden.Get(t, "expected_authn_request.xml")),
172-
string(decodedRequest)))
170+
golden.Assert(t, string(decodedRequest), "expected_authn_request.xml")
173171
}
174172

175173
func TestMiddlewareRequireAccountNoCredsSecure(t *testing.T) {
@@ -192,8 +190,7 @@ func TestMiddlewareRequireAccountNoCredsSecure(t *testing.T) {
192190
assert.Check(t, err)
193191
decodedRequest, err := testsaml.ParseRedirectRequest(redirectURL)
194192
assert.Check(t, err)
195-
assert.Check(t, is.Equal(string(golden.Get(t, "expected_authn_request_secure.xml")),
196-
string(decodedRequest)))
193+
golden.Assert(t, string(decodedRequest), "expected_authn_request_secure.xml")
197194
}
198195

199196
func TestMiddlewareRequireAccountNoCredsPostBinding(t *testing.T) {
@@ -214,8 +211,8 @@ func TestMiddlewareRequireAccountNoCredsPostBinding(t *testing.T) {
214211
assert.Check(t, is.Equal(http.StatusOK, resp.Code))
215212
assert.Check(t, is.Equal("saml_KCosLjAyNDY4Ojw-QEJERkhKTE5QUlRWWFpcXmBiZGZoamxucHJ0dnh6="+test.makeTrackedRequest("id-00020406080a0c0e10121416181a1c1e20222426")+"; Path=/saml2/acs; Max-Age=90; HttpOnly; Secure",
216213
resp.Header().Get("Set-Cookie")))
217-
assert.Check(t, is.Equal(string(golden.Get(t, "expected_post_binding_response.html")),
218-
string(resp.Body.Bytes())))
214+
215+
golden.Assert(t, resp.Body.String(), "expected_post_binding_response.html")
219216

220217
// check that the CSP script hash is set correctly
221218
scriptContent := "document.getElementById('SAMLSubmitButton').style.visibility=\"hidden\";document.getElementById('SAMLRequestForm').submit();"
@@ -279,8 +276,7 @@ func TestMiddlewareRequireAccountBadCreds(t *testing.T) {
279276
assert.Check(t, err)
280277
decodedRequest, err := testsaml.ParseRedirectRequest(redirectURL)
281278
assert.Check(t, err)
282-
assert.Check(t, is.Equal(string(golden.Get(t, "expected_authn_request_secure.xml")),
283-
string(decodedRequest)))
279+
golden.Assert(t, string(decodedRequest), "expected_authn_request_secure.xml")
284280
}
285281

286282
func TestMiddlewareRequireAccountExpiredCreds(t *testing.T) {
@@ -310,8 +306,7 @@ func TestMiddlewareRequireAccountExpiredCreds(t *testing.T) {
310306
assert.Check(t, err)
311307
decodedRequest, err := testsaml.ParseRedirectRequest(redirectURL)
312308
assert.Check(t, err)
313-
assert.Check(t, is.Equal(string(golden.Get(t, "expected_authn_request_secure.xml")),
314-
string(decodedRequest)))
309+
golden.Assert(t, string(decodedRequest), "expected_authn_request_secure.xml")
315310
}
316311

317312
func TestMiddlewareRequireAccountPanicOnRequestToACS(t *testing.T) {
@@ -401,7 +396,7 @@ func TestMiddlewareRequireAttributeMissingAccount(t *testing.T) {
401396
func TestMiddlewareCanParseResponse(t *testing.T) {
402397
test := NewMiddlewareTest(t)
403398
v := &url.Values{}
404-
v.Set("SAMLResponse", base64.StdEncoding.EncodeToString([]byte(test.SamlResponse)))
399+
v.Set("SAMLResponse", base64.StdEncoding.EncodeToString(test.SamlResponse))
405400
v.Set("RelayState", "KCosLjAyNDY4Ojw-QEJERkhKTE5QUlRWWFpcXmBiZGZoamxucHJ0dnh6")
406401
req, _ := http.NewRequest("POST", "/saml2/acs", bytes.NewReader([]byte(v.Encode())))
407402
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
@@ -466,7 +461,7 @@ func TestMiddlewareRejectsInvalidRelayState(t *testing.T) {
466461
}
467462

468463
v := &url.Values{}
469-
v.Set("SAMLResponse", base64.StdEncoding.EncodeToString([]byte(test.SamlResponse)))
464+
v.Set("SAMLResponse", base64.StdEncoding.EncodeToString(test.SamlResponse))
470465
v.Set("RelayState", "ICIkJigqLC4wMjQ2ODo8PkBCREZISkxOUFJUVlhaXF5gYmRmaGpsbnBy")
471466
req, _ := http.NewRequest("POST", "/saml2/acs", bytes.NewReader([]byte(v.Encode())))
472467
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
@@ -489,7 +484,7 @@ func TestMiddlewareRejectsInvalidCookie(t *testing.T) {
489484
}
490485

491486
v := &url.Values{}
492-
v.Set("SAMLResponse", base64.StdEncoding.EncodeToString([]byte(test.SamlResponse)))
487+
v.Set("SAMLResponse", base64.StdEncoding.EncodeToString(test.SamlResponse))
493488
v.Set("RelayState", "KCosLjAyNDY4Ojw-QEJERkhKTE5QUlRWWFpcXmBiZGZoamxucHJ0dnh6")
494489
req, _ := http.NewRequest("POST", "/saml2/acs", bytes.NewReader([]byte(v.Encode())))
495490
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

samlsp/samlsp_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func mustParseURL(s string) url.URL {
2929
return *rv
3030
}
3131

32-
func mustParsePrivateKey(pemStr string) crypto.PrivateKey {
33-
b, _ := pem.Decode([]byte(pemStr))
32+
func mustParsePrivateKey(pemStr []byte) crypto.PrivateKey {
33+
b, _ := pem.Decode(pemStr)
3434
if b == nil {
3535
panic("cannot parse PEM")
3636
}
@@ -41,8 +41,8 @@ func mustParsePrivateKey(pemStr string) crypto.PrivateKey {
4141
return k
4242
}
4343

44-
func mustParseCertificate(pemStr string) *x509.Certificate {
45-
b, _ := pem.Decode([]byte(pemStr))
44+
func mustParseCertificate(pemStr []byte) *x509.Certificate {
45+
b, _ := pem.Decode(pemStr)
4646
if b == nil {
4747
panic("cannot parse PEM")
4848
}

samlsp/session_cookie_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"net/http/httptest"
66
"testing"
77

8-
"github.com/crewjam/saml"
98
"gotest.tools/assert"
109
is "gotest.tools/assert/cmp"
10+
11+
"github.com/crewjam/saml"
1112
)
1213

1314
func TestCookieSameSite(t *testing.T) {

0 commit comments

Comments
 (0)