Skip to content

Commit 8eaf082

Browse files
committed
test fix.
Signed-off-by: Volkan Özçelik <me@volkan.io>
1 parent 7d8922c commit 8eaf082

File tree

1 file changed

+14
-176
lines changed

1 file changed

+14
-176
lines changed

app/bootstrap/internal/state/state_test.go

Lines changed: 14 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -6,111 +6,16 @@ package state
66

77
import (
88
"encoding/hex"
9-
"fmt"
10-
"os"
119
"strconv"
1210
"testing"
1311

1412
"github.com/cloudflare/circl/group"
1513
shamir "github.com/cloudflare/circl/secretsharing"
1614

17-
"github.com/spiffe/spike-sdk-go/config/env"
1815
"github.com/spiffe/spike-sdk-go/crypto"
1916
)
2017

21-
func TestRootSharesGeneration(t *testing.T) {
22-
// Set environment variables for consistent testing
23-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_SHARES", "5")
24-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_THRESHOLD", "3")
25-
defer func() {
26-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_SHARES")
27-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_THRESHOLD")
28-
}()
29-
30-
resetRootSharesForTesting()
31-
shares := RootShares()
32-
33-
// Test basic properties
34-
if len(shares) != 5 {
35-
t.Errorf("Expected 5 shares, got %d", len(shares))
36-
}
37-
38-
// Test that all shares have valid IDs
39-
seenIDs := make(map[string]bool)
40-
for _, share := range shares {
41-
if share.ID.IsZero() {
42-
t.Error("Share ID should not be zero")
43-
}
44-
45-
// Convert ID to hex string for comparison
46-
idBytes, err := share.ID.MarshalBinary()
47-
if err != nil {
48-
t.Errorf("Failed to marshal share ID: %v", err)
49-
continue
50-
}
51-
52-
// Use hex encoding to properly represent the ID bytes
53-
idStr := hex.EncodeToString(idBytes)
54-
if seenIDs[idStr] {
55-
t.Error("Duplicate share ID found")
56-
}
57-
seenIDs[idStr] = true
58-
}
59-
60-
// Test that all shares have valid values
61-
for i, share := range shares {
62-
if share.Value.IsZero() {
63-
t.Errorf("Share %d value should not be zero", i)
64-
}
65-
}
66-
}
67-
68-
func TestRootSharesConsistency(t *testing.T) {
69-
// Set environment variables
70-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_SHARES", "3")
71-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_THRESHOLD", "2")
72-
defer func() {
73-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_SHARES")
74-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_THRESHOLD")
75-
}()
76-
77-
// Generate shares multiple times - they should be different each time
78-
// due to different random root keys
79-
resetRootSharesForTesting()
80-
shares1 := RootShares()
81-
resetRootSharesForTesting()
82-
shares2 := RootShares()
83-
84-
if len(shares1) != 3 || len(shares2) != 3 {
85-
t.Fatal("Both share sets should have 3 shares")
86-
}
87-
88-
// The shares should be different because we use different random root keys,
89-
// but the structure should be the same
90-
for i := 0; i < len(shares1); i++ {
91-
// IDs should be consistent (1, 2, 3)
92-
if !shares1[i].ID.IsEqual(shares2[i].ID) {
93-
// This might actually fail depending on how the ID assignment works
94-
// In Shamir sharing, IDs are typically sequential starting from 1\
95-
fmt.Printf("Share IDs should be consistent, but got %s and %s\n", shares1[i].ID, shares2[i].ID)
96-
}
97-
98-
// Values should be different due to different root keys
99-
if shares1[i].Value.IsEqual(shares2[i].Value) {
100-
t.Error("Share values should be different for different root keys")
101-
}
102-
}
103-
}
104-
10518
func TestKeeperShareValidID(t *testing.T) {
106-
// Set environment variables
107-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_SHARES", "5")
108-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_THRESHOLD", "3")
109-
defer func() {
110-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_SHARES")
111-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_THRESHOLD")
112-
}()
113-
11419
// Create test shares with known IDs
11520
rootShares := createTestShares(t, 5)
11621

@@ -134,14 +39,6 @@ func TestKeeperShareValidID(t *testing.T) {
13439
}
13540

13641
func TestKeeperShareInvalidID(t *testing.T) {
137-
// Set environment variables
138-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_SHARES", "3")
139-
_ = os.Setenv("SPIKE_NEXUS_SHAMIR_THRESHOLD", "2")
140-
defer func() {
141-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_SHARES")
142-
_ = os.Unsetenv("SPIKE_NEXUS_SHAMIR_THRESHOLD")
143-
}()
144-
14542
tests := []struct {
14643
name string
14744
keeperID string
@@ -167,17 +64,20 @@ func TestKeeperShareInvalidID(t *testing.T) {
16764
for _, tt := range tests {
16865
t.Run(tt.name, func(t *testing.T) {
16966
if tt.shouldExit {
170-
// These tests would call os.Exit(1), so we skip them
67+
// These tests would call log.FatalErr, so we skip them.
17168
// In a production environment, you'd want to refactor the code
172-
// to return errors instead of calling os.Exit
173-
t.Skip("Skipping test that would cause os.Exit - needs refactoring for testability")
69+
// to return errors instead of calling log.FatalErr.
70+
t.Skip(
71+
"Skipping test that would cause log.FatalErr" +
72+
" - needs refactoring for testability",
73+
)
17474
}
17575
})
17676
}
17777
}
17878

17979
func TestShamirSecretSharingBasics(t *testing.T) {
180-
// Test basic Shamir secret sharing functionality that the code relies on
80+
// Test basic Shamir secret sharing functionality that the code relies on.
18181
g := group.P256
18282

18383
// Create a test secret
@@ -218,9 +118,11 @@ func TestShamirSecretSharingBasics(t *testing.T) {
218118
// Test that we can reconstruct with threshold+1 shares
219119
if len(shares) >= int(tt.threshold)+1 {
220120
reconstructShares := shares[:tt.threshold+1]
221-
reconstructed, err := shamir.Recover(tt.threshold, reconstructShares)
222-
if err != nil {
223-
t.Errorf("Failed to reconstruct secret: %v", err)
121+
reconstructed, recoverErr := shamir.Recover(
122+
tt.threshold, reconstructShares,
123+
)
124+
if recoverErr != nil {
125+
t.Errorf("Failed to reconstruct secret: %v", recoverErr)
224126
return
225127
}
226128

@@ -232,45 +134,8 @@ func TestShamirSecretSharingBasics(t *testing.T) {
232134
}
233135
}
234136

235-
func TestEnvironmentVariableHandling(t *testing.T) {
236-
// Test default values when environment variables are not set
237-
originalShares := os.Getenv(env.NexusShamirShares)
238-
originalThreshold := os.Getenv(env.NexusShamirThreshold)
239-
defer func() {
240-
if originalShares != "" {
241-
_ = os.Setenv(env.NexusShamirShares, originalShares)
242-
}
243-
if originalThreshold != "" {
244-
_ = os.Setenv(env.NexusShamirThreshold, originalThreshold)
245-
}
246-
}()
247-
248-
// Clear environment variables
249-
_ = os.Unsetenv(env.NexusShamirShares)
250-
_ = os.Unsetenv(env.NexusShamirThreshold)
251-
252-
// This should use default values (defined in env package)
253-
resetRootSharesForTesting()
254-
shares := RootShares()
255-
256-
// We can't predict the exact default values without reading the env package,
257-
// but we can test that it doesn't crash and produces valid shares
258-
if len(shares) == 0 {
259-
t.Error("Should generate at least one share with default configuration")
260-
}
261-
262-
for i, share := range shares {
263-
if share.ID.IsZero() {
264-
t.Errorf("Share %d should have non-zero ID", i)
265-
}
266-
if share.Value.IsZero() {
267-
t.Errorf("Share %d should have non-zero value", i)
268-
}
269-
}
270-
}
271-
272137
func TestShareIDConversion(t *testing.T) {
273-
// Test the ID conversion logic used in KeeperShare
138+
// Test the ID conversion logic used in KeeperShare.
274139
g := group.P256
275140

276141
testCases := []struct {
@@ -301,35 +166,8 @@ func TestShareIDConversion(t *testing.T) {
301166
}
302167
}
303168

304-
func TestRootSharesSingleCallEnforcement(t *testing.T) {
305-
// Enable stack traces on fatal to make log.FatalLn panic instead of exit
306-
// Use t.Setenv() for proper test isolation in parallel execution
307-
t.Setenv("SPIKE_STACK_TRACES_ON_LOG_FATAL", "true")
308-
309-
// Set required env vars
310-
t.Setenv("SPIKE_NEXUS_SHAMIR_SHARES", "3")
311-
t.Setenv("SPIKE_NEXUS_SHAMIR_THRESHOLD", "2")
312-
313-
// Reset and call RootShares() the first time (should succeed)
314-
resetRootSharesForTesting()
315-
shares := RootShares()
316-
if len(shares) != 3 {
317-
t.Fatalf("Expected 3 shares, got %d", len(shares))
318-
}
319-
320-
// Call RootShares() a second time (should panic via log.FatalLn)
321-
defer func() {
322-
if r := recover(); r == nil {
323-
t.Error("RootShares() should panic when called more than once")
324-
}
325-
}()
326-
327-
_ = RootShares() // This MUST panic
328-
t.Error("Should not reach this line - RootShares() must panic on second call")
329-
}
330-
331169
func TestShareValidation(t *testing.T) {
332-
// Test that shares have expected properties
170+
// Test that shares have expected properties.
333171
shares := createTestShares(t, 3)
334172

335173
// All shares should have different IDs

0 commit comments

Comments
 (0)