2020-05-21 22:52:19 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/base64"
|
2022-08-05 15:05:47 +00:00
|
|
|
"errors"
|
2020-05-21 22:52:19 +00:00
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"testing/iotest"
|
|
|
|
|
2022-03-17 13:34:57 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2023-01-01 18:57:26 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2020-05-21 22:52:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestChecksumValidatingReader(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2020-05-21 22:52:19 +00:00
|
|
|
data := make([]byte, 4096)
|
|
|
|
_, err := rand.Read(data)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-05-21 22:52:19 +00:00
|
|
|
|
|
|
|
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) {
|
2023-01-01 18:57:26 +00:00
|
|
|
_, err = c.hash.Write(data)
|
|
|
|
must.NoError(t, err)
|
2020-05-21 22:52:19 +00:00
|
|
|
|
|
|
|
checksum := c.hash.Sum(nil)
|
|
|
|
digest := c.algo + "=" + base64.StdEncoding.EncodeToString(checksum)
|
|
|
|
|
|
|
|
r := iotest.HalfReader(bytes.NewReader(data))
|
2023-03-08 16:25:09 +00:00
|
|
|
cr, err := newChecksumValidatingReader(io.NopCloser(r), digest)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-05-21 22:52:19 +00:00
|
|
|
|
2023-03-08 16:25:09 +00:00
|
|
|
_, err = io.Copy(io.Discard, cr)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-05-21 22:52:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid: "+c.algo, func(t *testing.T) {
|
2023-01-01 18:57:26 +00:00
|
|
|
_, err = c.hash.Write(data)
|
|
|
|
must.NoError(t, err)
|
2020-05-21 22:52:19 +00:00
|
|
|
|
|
|
|
checksum := c.hash.Sum(nil)
|
|
|
|
// mess up checksum
|
|
|
|
checksum[0]++
|
|
|
|
digest := c.algo + "=" + base64.StdEncoding.EncodeToString(checksum)
|
|
|
|
|
|
|
|
r := iotest.HalfReader(bytes.NewReader(data))
|
2023-03-08 16:25:09 +00:00
|
|
|
cr, err := newChecksumValidatingReader(io.NopCloser(r), digest)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-05-21 22:52:19 +00:00
|
|
|
|
2023-03-08 16:25:09 +00:00
|
|
|
_, err = io.Copy(io.Discard, cr)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.ErrorIs(t, err, errMismatchChecksum)
|
2020-05-21 22:52:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChecksumValidatingReader_PropagatesError(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2020-05-21 22:52:19 +00:00
|
|
|
pr, pw := io.Pipe()
|
2023-01-01 18:57:26 +00:00
|
|
|
defer func() { _ = pr.Close() }()
|
|
|
|
defer func() { _ = pw.Close() }()
|
2020-05-21 22:52:19 +00:00
|
|
|
|
2022-08-05 15:05:47 +00:00
|
|
|
expectedErr := errors.New("some error")
|
2020-05-21 22:52:19 +00:00
|
|
|
|
|
|
|
go func() {
|
2023-01-01 18:57:26 +00:00
|
|
|
_, _ = pw.Write([]byte("some input"))
|
|
|
|
_ = pw.CloseWithError(expectedErr)
|
2020-05-21 22:52:19 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
cr, err := newChecksumValidatingReader(pr, "sha-256=aaaa")
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-05-21 22:52:19 +00:00
|
|
|
|
2023-03-08 16:25:09 +00:00
|
|
|
_, err = io.Copy(io.Discard, cr)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.ErrorIs(t, err, expectedErr)
|
2020-05-21 22:52:19 +00:00
|
|
|
}
|