open-nomad/api/ioutil_test.go
Seth Hoenig cd75858f4a
api: purge testify and pretty dependencies (#15627)
* api: swap testify for test (acl)

* api: swap testify for test (agent)

 Please enter the commit message for your changes. Lines starting

* api: swap testify for test (allocations)

* api: swap testify for test (api)

* api: swap testify for test (compose)

* api: swap testify for test (constraint)

* api: swap testify for test (consul)

* api: swap testify for test (csi)

* api: swap testify for test (evaluations)

* api: swap testify for test (event stream)

* api: swap testify for test (fs)

* api: swap testify for test (ioutil)

* api: swap testify for test (jobs)

* api: swap testify for test (keyring)

* api: swap testify for test (operator_ent)

* api: swap testify for test (operator_metrics)

* api: swap testify for test (operator)

* api: swap testify for test (quota)

* api: swap testify for test (resources)

* api: swap testify for test (fix operator_metrics)

* api: swap testify for test (scaling)

* api: swap testify for test (search)

* api: swap testify for test (sentinel)

* api: swap testify for test (services)

* api: swap testify for test (status)

* api: swap testify for test (system)

* api: swap testify for test (tasks)

* api: swap testify for test (utils)

* api: swap testify for test (variables)

* api: remove dependencies on testify and pretty
2023-01-01 12:57:26 -06:00

90 lines
1.9 KiB
Go

package api
import (
"bytes"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"errors"
"hash"
"io"
"io/ioutil"
"math/rand"
"testing"
"testing/iotest"
"github.com/hashicorp/nomad/api/internal/testutil"
"github.com/shoenig/test/must"
)
func TestChecksumValidatingReader(t *testing.T) {
testutil.Parallel(t)
data := make([]byte, 4096)
_, err := rand.Read(data)
must.NoError(t, err)
cases := []struct {
algo string
hash hash.Hash
}{
{"sha-256", sha256.New()},
{"sha-512", sha512.New()},
}
for _, c := range cases {
t.Run("valid: "+c.algo, func(t *testing.T) {
_, err = c.hash.Write(data)
must.NoError(t, err)
checksum := c.hash.Sum(nil)
digest := c.algo + "=" + base64.StdEncoding.EncodeToString(checksum)
r := iotest.HalfReader(bytes.NewReader(data))
cr, err := newChecksumValidatingReader(ioutil.NopCloser(r), digest)
must.NoError(t, err)
_, err = io.Copy(ioutil.Discard, cr)
must.NoError(t, err)
})
t.Run("invalid: "+c.algo, func(t *testing.T) {
_, err = c.hash.Write(data)
must.NoError(t, err)
checksum := c.hash.Sum(nil)
// mess up checksum
checksum[0]++
digest := c.algo + "=" + base64.StdEncoding.EncodeToString(checksum)
r := iotest.HalfReader(bytes.NewReader(data))
cr, err := newChecksumValidatingReader(ioutil.NopCloser(r), digest)
must.NoError(t, err)
_, err = io.Copy(ioutil.Discard, cr)
must.ErrorIs(t, err, errMismatchChecksum)
})
}
}
func TestChecksumValidatingReader_PropagatesError(t *testing.T) {
testutil.Parallel(t)
pr, pw := io.Pipe()
defer func() { _ = pr.Close() }()
defer func() { _ = pw.Close() }()
expectedErr := errors.New("some error")
go func() {
_, _ = pw.Write([]byte("some input"))
_ = pw.CloseWithError(expectedErr)
}()
cr, err := newChecksumValidatingReader(pr, "sha-256=aaaa")
must.NoError(t, err)
_, err = io.Copy(ioutil.Discard, cr)
must.ErrorIs(t, err, expectedErr)
}