-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity_unit_test.go
More file actions
156 lines (122 loc) · 4.37 KB
/
security_unit_test.go
File metadata and controls
156 lines (122 loc) · 4.37 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// security_unit_test.go — focused tests for BasicSecurityContext.
//
// We rely on the real capability‑context helpers that exist in the repo
// (MakeRootTrustContext, MakeCapabilityContext, …) to avoid hand‑rolled stubs
// that might drift from the interface definition.
package actor
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/depinkit/crypto"
"github.com/depinkit/ucan"
)
// newCtxPair creates two independent BasicSecurityContexts rooted in their own
// trust trees; they can be used as source/destination handles in the tests.
func newCtxPair(t *testing.T) (*BasicSecurityContext, *BasicSecurityContext) {
t.Helper()
// root A
rootADID, rootATrust := MakeRootTrustContext(t)
// root B
rootBDID, rootBTrust := MakeRootTrustContext(t)
// actor alice
privA, pubA, _ := crypto.GenerateKeyPair(crypto.Ed25519)
aliceDID, aliceTrust := MakeTrustContext(t, privA)
capA := MakeCapabilityContext(t, aliceDID, rootADID, aliceTrust, rootATrust)
aliceCtx, _ := NewBasicSecurityContext(pubA, privA, capA)
// actor bob
privB, pubB, _ := crypto.GenerateKeyPair(crypto.Ed25519)
bobDID, bobTrust := MakeTrustContext(t, privB)
capB := MakeCapabilityContext(t, bobDID, rootBDID, bobTrust, rootBTrust)
bobCtx, _ := NewBasicSecurityContext(pubB, privB, capB)
return aliceCtx, bobCtx
}
// makeHandle wraps a security‑context into a Handle (same pattern as other tests).
func makeHandle(t *testing.T, s *BasicSecurityContext, host, box string) Handle {
t.Helper()
return Handle{
ID: s.ID(),
DID: s.DID(),
Address: Address{
HostID: host,
InboxAddress: box,
},
}
}
func TestSecurityProvideRequireHappyAndBadSender(t *testing.T) {
t.Parallel()
aliceCtx, bobCtx := newCtxPair(t)
alice := makeHandle(t, aliceCtx, "hostAlice", "inAlice")
bob := makeHandle(t, bobCtx, "hostBob", "inBob")
// Happy path: sender and receiver are the same handle
selfMsg, err := Message(alice, alice, "/self", nil)
require.NoError(t, err)
require.NoError(t, aliceCtx.Provide(&selfMsg, nil, nil))
require.NoError(t, aliceCtx.Require(selfMsg, nil))
// Error path: missing capabilities when talking to another actor
msg, _ := Message(alice, bob, "/be", nil)
err = aliceCtx.Provide(&msg, nil, nil)
require.Error(t, err)
// Sign() bad‑sender branch
bad := selfMsg
bad.From = bob
require.ErrorIs(t, aliceCtx.Sign(&bad), ErrBadSender)
}
func TestSecurityBroadcastTopicMismatch(t *testing.T) {
t.Parallel()
aliceCtx, _ := newCtxPair(t)
alice := makeHandle(t, aliceCtx, "hostAlice", "inAlice")
bcast, _ := Message(alice, Handle{}, "/b", nil, WithMessageTopic("topic‑X"))
require.True(t, bcast.IsBroadcast())
require.NoError(t, aliceCtx.ProvideBroadcast(&bcast, "topic‑X", []Capability{"/b"}))
require.Error(t, aliceCtx.RequireBroadcast(bcast, "otherTopic", []Capability{"/b"}))
}
func TestSecurityVerifyExpiresAndTamper(t *testing.T) {
t.Parallel()
aliceCtx, _ := newCtxPair(t)
alice := makeHandle(t, aliceCtx, "h", "i")
// expired
expired, _ := Message(alice, alice, "/x", nil,
WithMessageExpiry(uint64(time.Now().Add(-time.Second).UnixNano())))
require.ErrorIs(t, aliceCtx.Verify(expired), ErrMessageExpired)
// tamper signature
msg, _ := Message(alice, alice, "/x", nil)
require.NoError(t, aliceCtx.Sign(&msg))
msg.Message = []byte("evil") // tamper after signing
require.ErrorIs(t, aliceCtx.Verify(msg), ErrSignatureVerification)
}
func TestSecurityNonceConcurrency(t *testing.T) {
t.Parallel()
aliceCtx, _ := newCtxPair(t)
const threads = 64
var wg sync.WaitGroup
seen := make(map[uint64]struct{})
var mx sync.Mutex
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
n := aliceCtx.Nonce()
mx.Lock()
seen[n] = struct{}{}
mx.Unlock()
}()
}
wg.Wait()
require.Len(t, seen, threads)
}
func TestSecurityGrantAddRootsError(t *testing.T) {
t.Parallel()
rootDID, rootTrust := MakeRootTrustContext(t)
priv, pub, _ := crypto.GenerateKeyPair(crypto.Ed25519)
aliceDID, aliceTrust := MakeTrustContext(t, priv)
capCtx := MakeCapabilityContext(t, aliceDID, rootDID, aliceTrust, rootTrust)
aliceCtx, _ := NewBasicSecurityContext(pub, priv, capCtx)
// Success path
require.NoError(t, aliceCtx.Grant(aliceCtx.DID(), rootDID,
[]ucan.Capability{"/do/something"}, time.Minute))
// force an error with a negative expiry
err := aliceCtx.Grant(aliceCtx.DID(), rootDID, nil, -24*time.Hour)
require.Error(t, err)
}