context_test.go raw
1 package p256k1
2
3 import (
4 "crypto/rand"
5 "testing"
6 )
7
8 func TestContextCreate(t *testing.T) {
9 // Test creating context with different flags
10 testCases := []struct {
11 name string
12 flags uint
13 }{
14 {"none", ContextNone},
15 {"sign", ContextSign},
16 {"verify", ContextVerify},
17 {"both", ContextSign | ContextVerify},
18 }
19
20 for _, tc := range testCases {
21 t.Run(tc.name, func(t *testing.T) {
22 ctx := ContextCreate(tc.flags)
23 if ctx == nil {
24 t.Error("ContextCreate should not return nil")
25 }
26 if ctx.flags != tc.flags {
27 t.Errorf("context flags should be %d, got %d", tc.flags, ctx.flags)
28 }
29
30 // Check capabilities
31 expectedCanSign := (tc.flags & ContextSign) != 0
32 expectedCanVerify := (tc.flags & ContextVerify) != 0
33
34 if ctx.canSign() != expectedCanSign {
35 t.Errorf("canSign() should be %v", expectedCanSign)
36 }
37 if ctx.canVerify() != expectedCanVerify {
38 t.Errorf("canVerify() should be %v", expectedCanVerify)
39 }
40
41 // Clean up
42 ContextDestroy(ctx)
43 })
44 }
45 }
46
47 func TestContextDestroy(t *testing.T) {
48 // Test destroying nil context (should not panic)
49 ContextDestroy(nil)
50
51 // Test destroying valid context
52 ctx := ContextCreate(ContextSign | ContextVerify)
53 ContextDestroy(ctx)
54
55 // After destruction, context should be cleared
56 if ctx.flags != 0 {
57 t.Error("context flags should be cleared after destruction")
58 }
59 if ctx.ecmultGenCtx != nil {
60 t.Error("ecmult_gen context should be cleared after destruction")
61 }
62 }
63
64 func TestContextRandomize(t *testing.T) {
65 ctx := ContextCreate(ContextSign | ContextVerify)
66 defer ContextDestroy(ctx)
67
68 // Test with nil seed (should generate random seed)
69 err := ContextRandomize(ctx, nil)
70 if err != nil {
71 t.Errorf("ContextRandomize with nil seed failed: %v", err)
72 }
73
74 // Test with provided seed
75 seed := make([]byte, 32)
76 if _, err := rand.Read(seed); err != nil {
77 t.Fatal(err)
78 }
79
80 err = ContextRandomize(ctx, seed)
81 if err != nil {
82 t.Errorf("ContextRandomize with seed failed: %v", err)
83 }
84
85 // Test with invalid seed length
86 invalidSeed := make([]byte, 16) // Wrong length
87 err = ContextRandomize(ctx, invalidSeed)
88 if err == nil {
89 t.Error("ContextRandomize should fail with invalid seed length")
90 }
91
92 // Test with nil context
93 err = ContextRandomize(nil, seed)
94 if err == nil {
95 t.Error("ContextRandomize should fail with nil context")
96 }
97 }
98
99 func TestContextStatic(t *testing.T) {
100 // Test that static context exists and has correct properties
101 if ContextStatic == nil {
102 t.Error("ContextStatic should not be nil")
103 }
104
105 if ContextStatic.flags != ContextVerify {
106 t.Errorf("ContextStatic should have ContextVerify flag, got %d", ContextStatic.flags)
107 }
108
109 if !ContextStatic.canVerify() {
110 t.Error("ContextStatic should be able to verify")
111 }
112
113 if ContextStatic.canSign() {
114 t.Error("ContextStatic should not be able to sign")
115 }
116 }
117
118 func TestContextCapabilities(t *testing.T) {
119 // Test signing context
120 signCtx := ContextCreate(ContextSign)
121 defer ContextDestroy(signCtx)
122
123 if !signCtx.canSign() {
124 t.Error("sign context should be able to sign")
125 }
126 if signCtx.canVerify() {
127 t.Error("sign-only context should not be able to verify")
128 }
129
130 // Test verify context
131 verifyCtx := ContextCreate(ContextVerify)
132 defer ContextDestroy(verifyCtx)
133
134 if verifyCtx.canSign() {
135 t.Error("verify-only context should not be able to sign")
136 }
137 if !verifyCtx.canVerify() {
138 t.Error("verify context should be able to verify")
139 }
140
141 // Test combined context
142 bothCtx := ContextCreate(ContextSign | ContextVerify)
143 defer ContextDestroy(bothCtx)
144
145 if !bothCtx.canSign() {
146 t.Error("combined context should be able to sign")
147 }
148 if !bothCtx.canVerify() {
149 t.Error("combined context should be able to verify")
150 }
151
152 // Test none context
153 noneCtx := ContextCreate(ContextNone)
154 defer ContextDestroy(noneCtx)
155
156 if noneCtx.canSign() {
157 t.Error("none context should not be able to sign")
158 }
159 if noneCtx.canVerify() {
160 t.Error("none context should not be able to verify")
161 }
162 }
163
164 func BenchmarkContextCreate(b *testing.B) {
165 b.ResetTimer()
166 for i := 0; i < b.N; i++ {
167 ctx := ContextCreate(ContextSign | ContextVerify)
168 ContextDestroy(ctx)
169 }
170 }
171
172 func BenchmarkContextRandomize(b *testing.B) {
173 ctx := ContextCreate(ContextSign | ContextVerify)
174 defer ContextDestroy(ctx)
175
176 seed := make([]byte, 32)
177 rand.Read(seed)
178
179 b.ResetTimer()
180 for i := 0; i < b.N; i++ {
181 ContextRandomize(ctx, seed)
182 }
183 }
184