-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrub_test.go
More file actions
118 lines (107 loc) · 3.13 KB
/
Copy pathscrub_test.go
File metadata and controls
118 lines (107 loc) · 3.13 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package secmem
import (
"bytes"
"errors"
"testing"
)
func TestScrub_RunsFn(t *testing.T) {
t.Parallel()
ran := false
Scrub(func() { ran = true })
if !ran {
t.Fatal("Scrub did not invoke fn")
}
}
func TestScrubErr_PropagatesError(t *testing.T) {
t.Parallel()
sentinel := errors.New("boom")
err := ScrubErr(func() error { return sentinel })
if !errors.Is(err, sentinel) {
t.Fatalf("ScrubErr error = %v, want %v", err, sentinel)
}
}
func TestScrubErr_NilOnSuccess(t *testing.T) {
t.Parallel()
if err := ScrubErr(func() error { return nil }); err != nil {
t.Fatalf("ScrubErr = %v, want nil", err)
}
}
// TestScrub_ResultSurvives is the guard against the runtime/secret erasure
// zeroing a result we still need. A value produced inside Scrub and kept
// referenced (assigned to an outer variable) MUST remain intact after Scrub
// returns. Run under GOEXPERIMENT=runtimesecret to exercise the real erasure:
//
// GOEXPERIMENT=runtimesecret CGO_ENABLED=0 go test -run ResultSurvives ./...
func TestScrub_ResultSurvives(t *testing.T) {
t.Parallel()
want := make([]byte, 64)
for i := range want {
want[i] = byte(i*7 + 1)
}
// Produced inside Scrub, retained via the outer variable `got`.
var got []byte
Scrub(func() {
got = make([]byte, 64)
for i := range got {
got[i] = byte(i*7 + 1)
}
})
if !bytes.Equal(got, want) {
t.Fatalf("result not preserved across Scrub: got %x", got)
}
// Same via the error-returning form and a copy-out into a caller buffer
// (the documented best practice for results that must survive).
out := make([]byte, 64)
err := ScrubErr(func() error {
tmp := make([]byte, 64)
for i := range tmp {
tmp[i] = byte(i*7 + 1)
}
copy(out, tmp)
return nil
})
if err != nil {
t.Fatalf("ScrubErr: %v", err)
}
if !bytes.Equal(out, want) {
t.Fatalf("copied-out result not preserved: got %x", out)
}
}
func TestScrub_PanicPropagates(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic to propagate through Scrub")
}
}()
Scrub(func() { panic("kaboom") })
}
// TestAssertRuntimeSecret_ConsistentWithActive verifies the posture policy:
// AssertRuntimeSecret returns nil iff the erasure layer is active OR the
// platform does not support it. On a supported-but-inactive build it returns
// ErrRuntimeSecretInactive.
func TestAssertRuntimeSecret_ConsistentWithActive(t *testing.T) {
t.Parallel()
err := AssertRuntimeSecret()
if RuntimeSecretActive() {
// Active implies supported, so the assertion must pass.
if err != nil {
t.Fatalf("runtime secret active but AssertRuntimeSecret = %v", err)
}
return
}
// Inactive: error must be nil (unsupported platform) or the sentinel
// (supported platform misbuild). It must never be some other error.
if err != nil && !errors.Is(err, ErrRuntimeSecretInactive) {
t.Fatalf("unexpected AssertRuntimeSecret error: %v", err)
}
}
// TestScrub_NilIsNoop verifies Scrub(nil)/ScrubErr(nil) do not panic
// and that ScrubErr(nil) returns nil.
func TestScrub_NilIsNoop(t *testing.T) {
t.Parallel()
Scrub(nil) // must not panic
if err := ScrubErr(nil); err != nil {
t.Errorf("ScrubErr(nil) = %v, want nil", err)
}
}