-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheckpoint_signature_bug_test.go
More file actions
223 lines (196 loc) · 6.95 KB
/
Copy pathcheckpoint_signature_bug_test.go
File metadata and controls
223 lines (196 loc) · 6.95 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package nara
import (
"fmt"
"testing"
"time"
"github.com/eljojo/nara/identity"
"github.com/eljojo/nara/types"
)
// TestCheckpoint_VoteAsOfTimeMismatch verifies Issue #1 is fixed:
// Votes should sign with proposal.AsOfTime, not time.Now().Unix()
// This test intentionally creates a vote with wrong AsOfTime to show it would fail.
func TestCheckpoint_VoteAsOfTimeMismatch(t *testing.T) {
tc := NewTestCoordinator(t)
verifier := tc.AddNara("verifier", WithoutServer())
proposer := tc.AddNara("proposer", WithoutServer())
voter := tc.AddNara("voter", WithoutServer())
tc.Connect("verifier", "proposer")
tc.Connect("verifier", "voter")
proposerID := proposer.ID
voterID := voter.ID
subject := proposer.Me.Name
proposalAsOfTime := time.Now().Unix() - 10 // 10 seconds ago
// Create proposal attestation (self-attestation)
proposalAttestation := Attestation{
Version: 1,
Subject: subject,
SubjectID: proposerID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: subject,
AttesterID: proposerID,
AsOfTime: proposalAsOfTime,
}
proposalAttestation.Signature = identity.SignContent(&proposalAttestation, proposer.Keypair)
proposal := &CheckpointProposal{
Attestation: proposalAttestation,
Round: 1,
}
// CURRENT BEHAVIOR: Voter creates attestation with time.Now().Unix()
voteAsOfTime := time.Now().Unix() // Different from proposalAsOfTime!
voteAttestation := Attestation{
Version: 1,
Subject: subject,
SubjectID: proposerID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: voter.Me.Name,
AttesterID: voterID,
AsOfTime: voteAsOfTime, // BUG: Different timestamp!
}
voteAttestation.Signature = identity.SignContent(&voteAttestation, voter.Keypair)
vote := &CheckpointVote{
Attestation: voteAttestation,
ProposalTS: proposal.AsOfTime,
Round: 1,
Approved: true,
}
// Now simulate storing the checkpoint with both signatures
checkpoint := &CheckpointEventPayload{
Subject: subject,
SubjectID: proposerID,
AsOfTime: proposalAsOfTime, // Checkpoint uses proposal's AsOfTime
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
VoterIDs: []types.NaraID{proposerID, voterID},
Signatures: []string{proposal.Signature, vote.Signature},
Round: 1,
}
// Setup network for verification
ledger := NewSyncLedger(1000)
service := NewCheckpointService(verifier.Network, ledger, verifier)
// Try to verify signatures
result := service.verifyCheckpointSignatures(checkpoint)
// EXPECTATION: With the bug, voter signature fails because:
// - Vote was signed with voteAsOfTime
// - But verification uses proposalAsOfTime
// - The signable content doesn't match!
// Since only 1 of 2 signatures is valid, verification should fail (need 2+)
t.Logf("Valid signatures: %d/%d (known: %d)", result.ValidCount, result.TotalCount, result.KnownCount)
t.Logf("Proposer AsOfTime: %d", proposalAsOfTime)
t.Logf("Vote AsOfTime: %d (signed)", voteAsOfTime)
t.Logf("Checkpoint AsOfTime: %d (verified against)", checkpoint.AsOfTime)
// With Issue #2 fixed but this test using wrong AsOfTime:
// - Proposer signature should be valid (same AsOfTime)
// - Voter signature should fail (different AsOfTime)
// - Overall verification fails because we need 2+ valid signatures
if !result.Valid {
t.Log("✓ Verification correctly failed: only 1 of 2 signatures valid (need 2+)")
t.Log(" Proposer signature valid (same AsOfTime)")
t.Log(" Voter signature invalid (test used wrong AsOfTime)")
} else {
t.Error("Expected verification to fail due to AsOfTime mismatch, but it passed!")
}
}
// TestCheckpoint_SignatureFormatMismatch verifies Issue #2 is fixed:
// Both signing and verification now use "attestation:..." format
func TestCheckpoint_SignatureFormatMismatch(t *testing.T) {
tc := NewTestCoordinator(t)
verifier := tc.AddNara("verifier", WithoutServer())
proposer := tc.AddNara("proposer", WithoutServer())
voter := tc.AddNara("voter", WithoutServer())
tc.Connect("verifier", "proposer")
tc.Connect("verifier", "voter")
proposerID := proposer.ID
voterID := voter.ID
subject := proposer.Me.Name
asOfTime := time.Now().Unix()
// Create proposal attestation - signs with Attestation.SignableContent()
proposalAttestation := Attestation{
Subject: subject,
SubjectID: proposerID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: subject,
AttesterID: proposerID,
AsOfTime: asOfTime,
}
proposalAttestation.Signature = identity.SignContent(&proposalAttestation, proposer.Keypair)
proposal := &CheckpointProposal{
Attestation: proposalAttestation,
Round: 1,
}
// Create vote attestation - signs with Attestation.SignableContent()
voteAttestation := Attestation{
Version: 1,
Subject: subject,
SubjectID: proposerID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: voter.Me.Name,
AttesterID: voterID,
AsOfTime: asOfTime, // Same as proposal for now
}
voteAttestation.Signature = identity.SignContent(&voteAttestation, voter.Keypair)
vote := &CheckpointVote{
Attestation: voteAttestation,
ProposalTS: proposal.AsOfTime,
Round: 1,
Approved: true,
}
// Store checkpoint
checkpoint := &CheckpointEventPayload{
Subject: subject,
SubjectID: proposerID,
AsOfTime: asOfTime,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
VoterIDs: []types.NaraID{proposerID, voterID},
Signatures: []string{proposal.Signature, vote.Signature},
Round: 1,
}
// Setup network
ledger := NewSyncLedger(1000)
service := NewCheckpointService(verifier.Network, ledger, verifier)
// Log what was signed vs what will be verified
t.Log("SIGNED WITH:")
t.Logf(" Proposer: %s", proposalAttestation.SignableContent())
t.Logf(" Voter: %s", voteAttestation.SignableContent())
t.Log("")
t.Log("VERIFIED WITH:")
proposalVerifyContent := fmt.Sprintf("checkpoint-proposal:%s:%d:%d:%d:%d:%d",
proposerID, asOfTime, 100, 50000, 1624066568, 1)
voteVerifyContent := fmt.Sprintf("checkpoint-vote:%s:%d:%d:%d:%d:%d",
voterID, asOfTime, 100, 50000, 1624066568, 1)
t.Logf(" Proposer: %s", proposalVerifyContent)
t.Logf(" Voter: %s", voteVerifyContent)
// Try to verify
result := service.verifyCheckpointSignatures(checkpoint)
t.Logf("Valid signatures: %d/%d (known: %d)", result.ValidCount, result.TotalCount, result.KnownCount)
// With Issue #2 fixed, both signatures should be valid
if result.Valid && result.ValidCount == 2 {
t.Log("✓ Issue #2 is fixed: Both signatures valid using attestation format")
t.Log(" Signing uses: attestation:...")
t.Log(" Verification uses: attestation:...")
} else {
t.Errorf("Verification failed - expected 2 valid signatures, got %d!", result.ValidCount)
}
}