types_test.go raw
1 package types
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestEventID(t *testing.T) {
9 // Test creation from bytes
10 src := make([]byte, 32)
11 for i := range src {
12 src[i] = byte(i)
13 }
14
15 id := EventIDFromBytes(src)
16
17 // Verify contents
18 if !bytes.Equal(id[:], src) {
19 t.Errorf("EventIDFromBytes: got %x, want %x", id[:], src)
20 }
21
22 // Test Hex encoding
23 expectedHex := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
24 if got := id.Hex(); got != expectedHex {
25 t.Errorf("Hex: got %s, want %s", got, expectedHex)
26 }
27
28 // Test FromHex
29 id2, err := EventIDFromHex(expectedHex)
30 if err != nil {
31 t.Fatalf("EventIDFromHex error: %v", err)
32 }
33 if id2 != id {
34 t.Errorf("EventIDFromHex: got %x, want %x", id2[:], id[:])
35 }
36
37 // Test Copy returns independent slice
38 cp := id.Copy()
39 cp[0] = 0xFF
40 if id[0] == 0xFF {
41 t.Error("Copy: modification affected original")
42 }
43
44 // Test Bytes returns view (shares memory)
45 view := id.Bytes()
46 if &view[0] != &id[0] {
47 t.Error("Bytes: should return view sharing memory")
48 }
49
50 // Test IsZero
51 var zero EventID
52 if !zero.IsZero() {
53 t.Error("IsZero: zero value should return true")
54 }
55 if id.IsZero() {
56 t.Error("IsZero: non-zero value should return false")
57 }
58 }
59
60 func TestPubkey(t *testing.T) {
61 src := make([]byte, 32)
62 for i := range src {
63 src[i] = byte(i + 32)
64 }
65
66 pk := PubkeyFromBytes(src)
67
68 if !bytes.Equal(pk[:], src) {
69 t.Errorf("PubkeyFromBytes: got %x, want %x", pk[:], src)
70 }
71
72 // Test hex round-trip
73 hexStr := pk.Hex()
74 pk2, err := PubkeyFromHex(hexStr)
75 if err != nil {
76 t.Fatalf("PubkeyFromHex error: %v", err)
77 }
78 if pk2 != pk {
79 t.Errorf("PubkeyFromHex round-trip failed")
80 }
81 }
82
83 func TestSignature(t *testing.T) {
84 src := make([]byte, 64)
85 for i := range src {
86 src[i] = byte(i)
87 }
88
89 sig := SignatureFromBytes(src)
90
91 if !bytes.Equal(sig[:], src) {
92 t.Errorf("SignatureFromBytes: got %x, want %x", sig[:], src)
93 }
94
95 // Test size
96 if len(sig.Bytes()) != SignatureSize {
97 t.Errorf("Signature size: got %d, want %d", len(sig.Bytes()), SignatureSize)
98 }
99 }
100
101 func TestSecretKey(t *testing.T) {
102 src := make([]byte, 32)
103 for i := range src {
104 src[i] = byte(i + 100)
105 }
106
107 sk := SecretKeyFromBytes(src)
108
109 if !bytes.Equal(sk[:], src) {
110 t.Errorf("SecretKeyFromBytes: got %x, want %x", sk[:], src)
111 }
112
113 // Test Zero
114 sk.Zero()
115 if !sk.IsZero() {
116 t.Error("Zero: should clear all bytes")
117 }
118 }
119
120 func TestCopyOnAssignment(t *testing.T) {
121 // Verify that assignment creates a copy (key safety property)
122 src := make([]byte, 32)
123 for i := range src {
124 src[i] = byte(i)
125 }
126
127 id1 := EventIDFromBytes(src)
128 id2 := id1 // Should copy
129
130 // Modify id2
131 id2[0] = 0xFF
132
133 // id1 should be unchanged
134 if id1[0] == 0xFF {
135 t.Error("Assignment should create a copy, but original was modified")
136 }
137 }
138
139 func TestShortInput(t *testing.T) {
140 // Test that short inputs are zero-padded
141 short := []byte{1, 2, 3}
142 id := EventIDFromBytes(short)
143
144 if id[0] != 1 || id[1] != 2 || id[2] != 3 {
145 t.Error("Short input: first bytes should match")
146 }
147 if id[3] != 0 || id[31] != 0 {
148 t.Error("Short input: remaining bytes should be zero")
149 }
150 }
151
152 func BenchmarkEventIDCopy(b *testing.B) {
153 src := make([]byte, 32)
154 for i := range src {
155 src[i] = byte(i)
156 }
157 id := EventIDFromBytes(src)
158
159 b.ResetTimer()
160 for i := 0; i < b.N; i++ {
161 _ = id.Copy()
162 }
163 }
164
165 func BenchmarkEventIDAssignment(b *testing.B) {
166 src := make([]byte, 32)
167 for i := range src {
168 src[i] = byte(i)
169 }
170 id := EventIDFromBytes(src)
171
172 b.ResetTimer()
173 for i := 0; i < b.N; i++ {
174 id2 := id // Stack copy
175 _ = id2
176 }
177 }
178
179 func BenchmarkSliceCopy(b *testing.B) {
180 src := make([]byte, 32)
181 for i := range src {
182 src[i] = byte(i)
183 }
184
185 b.ResetTimer()
186 for i := 0; i < b.N; i++ {
187 dst := make([]byte, 32)
188 copy(dst, src)
189 _ = dst
190 }
191 }
192