-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdf_test.go
137 lines (115 loc) · 2.88 KB
/
vdf_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package vdf_test
import (
"bytes"
"reflect"
"testing"
"github.com/mdouchement/vdf"
)
var remotecache = `"429660"
{
"Save000.tobsav"
{
"root" "0"
"size" "718088"
"localtime" "1520277506"
"time" "1520277515"
"remotetime" "1520277515"
"sha" "e0c260324b8a3119629325ab5b8c4420da5e7ade"
"syncstate" "1"
"persiststate" "0"
"platformstosync2" "-1"
}
"quicksave.tobsav"
{
"root" "0"
"size" "718088"
"localtime" "1524340445"
"time" "1524340447"
"remotetime" "1524340447"
"sha" "1b2c5f42828c1f578b05f48ac75a2d031336cc3d"
"syncstate" "1"
"persiststate" "0"
"platformstosync2" "-1"
}
}
`
func TestParse(t *testing.T) {
remote, err := vdf.Parse([]byte(remotecache))
if err != nil {
t.Fatalf("Error: %v", err)
}
iface, ok := remote["429660"]
if !ok {
t.Fatalf("Expected 429660 to be present")
}
saves, ok := iface.(map[string]interface{})
if !ok {
t.Fatalf("Expected saves to be casted in map[string]interface{}")
}
_, ok = saves["Save000.tobsav"]
if !ok {
t.Fatalf("Expected Save000.tobsav to be present")
}
iface, ok = saves["quicksave.tobsav"]
if !ok {
t.Fatalf("Expected quicksave.tobsav to be present")
}
quicksave, ok := iface.(map[string]interface{})
if !ok {
t.Fatalf("Expected quicksave to be casted in map[string]interface{}")
}
if root, ok := quicksave["root"]; !ok || root.(string) != "0" {
t.Fatalf("Expected quicksave[\"root\"] to be present and have value 0")
}
if localtime, ok := quicksave["localtime"]; !ok || localtime.(string) != "1524340445" {
t.Fatalf("Expected quicksave[\"localtime\"] to be present and have value 1524340445")
}
if sha, ok := quicksave["sha"]; !ok || sha.(string) != "1b2c5f42828c1f578b05f48ac75a2d031336cc3d" {
t.Fatalf("Expected quicksave[\"sha\"] to be present and have value 1b2c5f42828c1f578b05f48ac75a2d031336cc3d")
}
}
func TestParseIO(t *testing.T) {
r := bytes.NewBufferString(remotecache)
remote, err := vdf.ParseIO(r)
if err != nil {
t.Fatalf("Error: %v", err)
}
_, ok := remote["429660"]
if !ok {
t.Fatalf("Expected 429660 to be present")
}
}
func TestGenerate(t *testing.T) {
remote, err := vdf.Parse([]byte(remotecache))
if err != nil {
t.Fatalf("Error: %v", err)
}
tmp, err := vdf.Generate(remote)
if err != nil {
t.Fatalf("Error: %v", err)
}
remote2, err := vdf.Parse(tmp)
if err != nil {
t.Fatalf("Error: %v", err)
}
if !reflect.DeepEqual(remote, remote2) {
t.Fatalf("Expected\n%v\n to equal\n%v", remote2, remote)
}
}
func TestGenerateIO(t *testing.T) {
remote, err := vdf.Parse([]byte(remotecache))
if err != nil {
t.Fatalf("Error: %v", err)
}
w := new(bytes.Buffer)
if err = vdf.GenerateIO(w, remote); err != nil {
t.Fatalf("Error: %v", err)
}
remote2, err := vdf.Parse(w.Bytes())
if err != nil {
t.Fatalf("Error: %v", err)
}
if !reflect.DeepEqual(remote, remote2) {
t.Fatalf("Expected\n%v\n to equal\n%v", remote2, remote)
}
}