-
Notifications
You must be signed in to change notification settings - Fork 66
/
format_test.go
52 lines (45 loc) · 1.23 KB
/
format_test.go
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
package btd
import (
"bytes"
"testing"
"github.com/privacypass/challenge-bypass-server/crypto"
)
// Tests that token arrays can be encoded correctly for all curve settings
func TestEncodeIncrement(t *testing.T) { crypto.HandleTest(t, "increment", encodeTokenArray) }
func TestEncodeSWU(t *testing.T) { crypto.HandleTest(t, "swu", encodeTokenArray) }
func encodeTokenArray(t *testing.T, h2cObj crypto.H2CObject) {
tokens := make([][]byte, 2)
for i := 0; i < len(tokens); i++ {
token, _, _, err := crypto.CreateBlindToken(h2cObj)
if err != nil {
t.Fatal(err)
}
tokens[i] = token
}
data, err := EncodeByteArrays(tokens)
if err != nil {
t.Error(err)
}
// encoding overhead for 2 elements
// 2 for [ ]
// 2 for each element's " "
// 2/2 for each ,
// For two elements: 2 + 2*2 + 2/2 = 7
// len(base64([32]byte)) = 44 * 2 = 88 + 7 = 95
if len(data) != 95 {
t.Errorf("unexpected encoding size %d expected 95", len(data))
}
values, err := DecodeByteArrays(data)
if err != nil {
t.Error(err)
}
if len(tokens) != len(values) {
t.Errorf("decoded array was a different length")
}
for i := 0; i < len(tokens); i++ {
if !bytes.Equal(tokens[i], values[i]) {
t.Errorf("json decoding is broken")
break
}
}
}