Skip to content

Fix the panic in the TestGenerateRandomBytes test #183 #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package csrf

import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -201,29 +199,6 @@ func TestXOR(t *testing.T) {
}
}

// shortReader provides a broken implementation of io.Reader for testing.
type shortReader struct{}

func (sr shortReader) Read(p []byte) (int, error) {
return len(p) % 2, io.ErrUnexpectedEOF
}

// TestGenerateRandomBytes tests the (extremely rare) case that crypto/rand does
// not return the expected number of bytes.
func TestGenerateRandomBytes(t *testing.T) {
// Pioneered from https://github.com/justinas/nosurf
original := rand.Reader
rand.Reader = shortReader{}
defer func() {
rand.Reader = original
}()

b, err := generateRandomBytes(tokenLength)
if err == nil {
t.Fatalf("generateRandomBytes did not report a short read: only read %d bytes", len(b))
}
}

func TestTemplateField(t *testing.T) {
s := http.NewServeMux()

Expand Down