-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding_test.go
More file actions
77 lines (65 loc) · 1.67 KB
/
encoding_test.go
File metadata and controls
77 lines (65 loc) · 1.67 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
package shamir
import (
"bytes"
"testing"
)
func TestBinaryEncodingRoundTrip(t *testing.T) {
share := Share{
X: 3,
Y: []byte{0x10, 0x20, 0x30, 0x40},
}
enc, err := EncodeShare(share)
if err != nil {
t.Fatalf("EncodeShare failed: %v", err)
}
dec, err := DecodeShare(enc)
if err != nil {
t.Fatalf("DecodeShare failed: %v", err)
}
if dec.X != share.X {
t.Fatalf("X mismatch: got %d, want %d", dec.X, share.X)
}
if !bytes.Equal(dec.Y, share.Y) {
t.Fatalf("Y mismatch")
}
}
func TestTextEncodingRoundTrip(t *testing.T) {
share := Share{
X: 7,
Y: []byte("encoded payload"),
}
text, err := MarshalText(share)
if err != nil {
t.Fatalf("MarshalText failed: %v", err)
}
dec, err := UnmarshalText(text)
if err != nil {
t.Fatalf("UnmarshalText failed: %v", err)
}
if dec.X != share.X {
t.Fatalf("X mismatch after text round-trip")
}
if !bytes.Equal(dec.Y, share.Y) {
t.Fatalf("Y mismatch after text round-trip")
}
}
func TestInvalidPrefix(t *testing.T) {
_, err := UnmarshalText("invalidprefix:abcd")
if err == nil {
t.Fatalf("expected error for invalid prefix")
}
}
func TestMalformedBinaryEncoding(t *testing.T) {
// Missing payload length
data := []byte{0x01, 0x02, 0x03}
_, err := DecodeShare(data)
if err == nil {
t.Fatalf("expected error for malformed binary encoding")
}
}
func TestEmptyShareEncoding(t *testing.T) {
_, err := EncodeShare(Share{X: 1, Y: nil})
if err == nil {
t.Fatalf("expected error for empty share payload")
}
}