aboutsummaryrefslogtreecommitdiff
path: root/internal/cryptocore/randprefetch_test.go
blob: b263ef3f4d220929649f0ff550f4963d74cc9229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cryptocore

import (
	"bytes"
	"compress/flate"
	"runtime"
	"sync"
	"testing"
)

// TestRandPrefetch hammers the randPrefetcher with 100 goroutines and verifies
// that the result is incompressible
func TestRandPrefetch(t *testing.T) {
	runtime.GOMAXPROCS(10)
	p := 100
	l := 200
	vec := make([][]byte, p)
	var wg sync.WaitGroup
	for i := 0; i < p; i++ {
		wg.Add(1)
		go func(i int) {
			var tmp []byte
			for x := 0; x < l; x++ {
				tmp = append(tmp, randPrefetcher.read(l)...)
			}
			vec[i] = tmp
			wg.Done()
		}(i)
	}
	wg.Wait()
	var b bytes.Buffer
	fw, _ := flate.NewWriter(&b, flate.BestCompression)
	for _, v := range vec {
		fw.Write(v)
	}
	fw.Close()
	if b.Len() < p*l*l {
		t.Errorf("random data should be incompressible, but: in=%d compressed=%d\n", p*l*l, b.Len())
	}
}

func BenchmarkRandPrefetch(b *testing.B) {
	// 16-byte nonces are default since gocryptfs v0.7
	b.SetBytes(16)
	for i := 0; i < b.N; i++ {
		randPrefetcher.read(16)
	}
}