-
Notifications
You must be signed in to change notification settings - Fork 3
/
letter_test.go
117 lines (96 loc) · 2.2 KB
/
letter_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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package jess
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
)
func TestSerialization(t *testing.T) {
t.Parallel()
subject := &Letter{
Version: 1,
SuiteID: SuiteComplete,
Keys: []*Seal{
{ID: "a"},
{ID: "b"},
{ID: "c"},
},
Nonce: []byte{1, 2, 3},
Data: []byte{4, 5, 6},
Mac: []byte{7, 8, 9},
ApplyKeys: true,
}
testSerialize(t, subject, true)
subject.Version = 0
subject.SuiteID = ""
testSerialize(t, subject, true)
subject.ApplyKeys = false
testSerialize(t, subject, true)
subject.Keys = nil
testSerialize(t, subject, true)
}
func testSerialize(t *testing.T, letter *Letter, wireFormat bool) { //nolint:unparam
t.Helper()
// File Format
fileData, err := letter.ToFileFormat()
if err != nil {
t.Error(err)
return
}
letter2, err := LetterFromFileFormat(fileData)
if err != nil {
t.Error(err)
return
}
err = letter.CheckEqual(letter2)
if err != nil {
t.Errorf("letters (file format) do not match: %s\n%+v\n%+v\n", err, jsonFormat(letter), jsonFormat(letter2))
return
}
// Wire Format
if !wireFormat {
return
}
wire, err := letter.ToWire()
if err != nil {
t.Error(err)
return
}
letter3, err := LetterFromWire(wire)
if err != nil {
t.Error(err)
return
}
err = letter.CheckEqual(letter3)
if err != nil {
t.Errorf("letters (wire format) do not match: %s\n%+v\n%+v\n", err, jsonFormat(letter), jsonFormat(letter3))
return
}
}
func (letter *Letter) CheckEqual(other *Letter) error {
letterValue := reflect.ValueOf(*letter)
otherValue := reflect.ValueOf(*other)
var ok bool
numElements := letterValue.NumField()
for i := 0; i < numElements; i++ {
name := letterValue.Type().Field(i).Name
switch name {
case "Data": // TODO: this required special handling in the past, leave it here for now.
ok = bytes.Equal(letter.Data, other.Data)
default:
ok = reflect.DeepEqual(letterValue.Field(i).Interface(), otherValue.Field(i).Interface())
}
if !ok {
return fmt.Errorf("field %s mismatches", name)
}
}
return nil
}
func jsonFormat(v interface{}) string {
formatted, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Sprintf("<JSON format error: %s>", err)
}
return string(formatted)
}