Browse Source

test: enhance rand data builder

feature/assets
Dnomd343 6 months ago
parent
commit
7d51419bda
  1. 23
      next/assets/extract_test.go

23
next/assets/extract_test.go

@ -11,9 +11,10 @@ import (
"testing" "testing"
) )
const testMin = 1024 * 1024 // 1MiB const testMinSize = 16 * 1024 // 16MiB
const testMax = 4096 * 1024 // 4Mib const testMaxSize = 64 * 1024 // 64MiB
// randBytes generates a specified number of random bytes.
func randBytes(size int) []byte { func randBytes(size int) []byte {
tmp := make([]byte, size) tmp := make([]byte, size)
_, _ = rand.Read(tmp) _, _ = rand.Read(tmp)
@ -24,6 +25,16 @@ func randInt(min int, max int) int {
return min + int(mrand.Float64()*float64(max-min)) return min + int(mrand.Float64()*float64(max-min))
} }
func randData() []byte {
raw := randBytes(1024)
size := randInt(testMinSize, testMaxSize)
var buffer bytes.Buffer
for i := 0; i < size; i++ {
buffer.Write(raw)
}
return buffer.Bytes()
}
func gzipCompress(data []byte) []byte { func gzipCompress(data []byte) []byte {
buf := bytes.Buffer{} buf := bytes.Buffer{}
gw := gzip.NewWriter(&buf) gw := gzip.NewWriter(&buf)
@ -51,7 +62,7 @@ func xzCompress(data []byte) []byte {
} }
func TestGzipExtract(t *testing.T) { func TestGzipExtract(t *testing.T) {
raw := randBytes(randInt(testMin, testMax)) raw := randData()
gzOk := gzipCompress(raw) gzOk := gzipCompress(raw)
gzErr := append(gzOk, randBytes(randInt(1, 16))...) gzErr := append(gzOk, randBytes(randInt(1, 16))...)
@ -63,7 +74,7 @@ func TestGzipExtract(t *testing.T) {
} }
func TestBzip2Extract(t *testing.T) { func TestBzip2Extract(t *testing.T) {
raw := randBytes(randInt(testMin, testMax)) raw := randData()
bz2Ok := bzip2Compress(raw) bz2Ok := bzip2Compress(raw)
bz2Err := append(bz2Ok, randBytes(randInt(1, 16))...) bz2Err := append(bz2Ok, randBytes(randInt(1, 16))...)
@ -75,7 +86,7 @@ func TestBzip2Extract(t *testing.T) {
} }
func TestXzExtract(t *testing.T) { func TestXzExtract(t *testing.T) {
raw := randBytes(randInt(testMin, testMax)) raw := randData()
xzOk := xzCompress(raw) xzOk := xzCompress(raw)
xzErr := append(xzOk, randBytes(randInt(1, 16))...) xzErr := append(xzOk, randBytes(randInt(1, 16))...)
@ -87,7 +98,7 @@ func TestXzExtract(t *testing.T) {
} }
func TestExtract(t *testing.T) { func TestExtract(t *testing.T) {
raw := randBytes(randInt(testMin, testMax)) raw := randData()
ret, err := tryExtract(raw) ret, err := tryExtract(raw)
assert.Nil(t, err) assert.Nil(t, err)

Loading…
Cancel
Save