Repository navigation
Expand file tree
/
Copy pathaead_dst_contract_test.go
More file actions
92 lines (84 loc) · 2.86 KB
/
Copy pathaead_dst_contract_test.go
File metadata and controls
92 lines (84 loc) · 2.86 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: Apache-2.0 OR MIT
//go:build goexperiment.simd && amd64
package c2simd
import (
"bytes"
"crypto/rand"
"testing"
)
// Contrat du tampon de sortie de AEADSubkeyLockDst / UnlockDst : jugé à la
// CAPACITÉ, pas à la longueur. Un tampon retaillé à 0 mais assez capable est
// réutilisé (aucune allocation, la sortie aliasse le tampon fourni) ; un tampon
// de capacité insuffisante provoque une allocation et un contenu identique.
// Rend opposable le « 0 alloc » mesuré par le harnais amont.
func TestDstContract_CapacityNotLength(t *testing.T) {
key, nonce := make([]byte, 32), make([]byte, 12)
rand.Read(key)
rand.Read(nonce)
for _, n := range []int{0, 1, 64, 65, 511, 512, 4096, 8192} {
plain := make([]byte, n)
rand.Read(plain)
// Référence : tampon nil (allocation forcée).
var macRef [16]byte
ctRef, err := AEADSubkeyLockDst(nil, &macRef, key, nonce, nil, plain)
if err != nil {
t.Fatal(err)
}
// Tampon de longueur 0, capacité suffisante : aucune allocation, aliasing.
backing := make([]byte, 0, n+64)
var mac [16]byte
allocs := testing.AllocsPerRun(50, func() {
if _, err := AEADSubkeyLockDst(backing, &mac, key, nonce, nil, plain); err != nil {
t.Fatal(err)
}
})
if allocs != 0 {
t.Fatalf("n=%d : tampon capable (len 0, cap %d) → %.2f allocs, attendu 0", n, cap(backing), allocs)
}
ct, err := AEADSubkeyLockDst(backing, &mac, key, nonce, nil, plain)
if err != nil {
t.Fatal(err)
}
if n > 0 && &ct[0] != &backing[:1][0] {
t.Fatalf("n=%d : la sortie n'aliasse pas le tampon fourni", n)
}
if !bytes.Equal(ct, ctRef) || mac != macRef {
t.Fatalf("n=%d : contenu divergent entre tampon fourni et tampon alloué", n)
}
// Capacité insuffisante : allocation, contenu juste, tampon fourni intact.
if n > 0 {
small := make([]byte, n-1, n-1)
var mac2 [16]byte
ct2, err := AEADSubkeyLockDst(small, &mac2, key, nonce, nil, plain)
if err != nil {
t.Fatal(err)
}
if cap(ct2) < n || (len(small) > 0 && &ct2[0] == &small[0]) {
t.Fatalf("n=%d : capacité insuffisante non réallouée", n)
}
if !bytes.Equal(ct2, ctRef) || mac2 != macRef {
t.Fatalf("n=%d : contenu divergent après réallocation", n)
}
}
// Même contrat côté Unlock.
out := make([]byte, 0, n+64)
allocsU := testing.AllocsPerRun(50, func() {
if _, err := AEADSubkeyUnlockDst(out, key, nonce, nil, ctRef, macRef[:]); err != nil {
t.Fatal(err)
}
})
if allocsU != 0 {
t.Fatalf("n=%d : Unlock avec tampon capable → %.2f allocs, attendu 0", n, allocsU)
}
pt, err := AEADSubkeyUnlockDst(out, key, nonce, nil, ctRef, macRef[:])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(pt, plain) {
t.Fatalf("n=%d : Unlock ne restitue pas le clair", n)
}
if n > 0 && &pt[0] != &out[:1][0] {
t.Fatalf("n=%d : la sortie d'Unlock n'aliasse pas le tampon fourni", n)
}
}
}