-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstratum_msgs_test.go
115 lines (106 loc) · 2.46 KB
/
stratum_msgs_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
package main
import (
"testing"
"errors"
"os"
"encoding/json"
)
func TestCheckJsonErrors(t *testing.T) {
var exited bool = false
exiter = func(code int) {
exited = true
}
e := errors.New("test")
CheckJsonErrors(e)
if exited == false {
t.Errorf("Error in CheckJsonErrors()")
}
exiter = os.Exit
}
func TestStratumCommandMsgNormal(t *testing.T) {
var exited bool = false
exiter = func(code int) {
exited = true
}
var input string = "{\"id\":9999,\"params\":[\"asdf\",\"asdf2\"],\"method\":\"cmd\"}"
var scm = new(Stratum_command_msg)
scm.FromJsonString(input)
if exited == true {
t.Errorf("Error in FromJsonString")
}
if scm.Id != 9999 {
t.Errorf("Error parsing Id")
}
if scm.Method != "cmd" {
t.Errorf("Error parsing Method")
}
switch p := scm.Params.(type) {
case []interface{}:
switch p0 := p[0].(type) {
case string:
if p0 != "asdf" {
t.Errorf("Error parsing Params value")
}
default:
t.Errorf("Error parsing Params string type")
}
default:
t.Errorf("Error parsing Params slice type")
}
}
func TestStratumCommandMsgMissingParams(t *testing.T) {
var exited bool = false
exiter = func(code int) {
exited = true
}
var input string = "{\"id\":9999,\"method\":\"cmd\"}"
var scm = new(Stratum_command_msg)
scm.FromJsonString(input)
if exited == true {
t.Errorf("Error in FromJsonString")
}
if scm.Id != 9999 {
t.Errorf("Error parsing Id")
}
if scm.Method != "cmd" {
t.Errorf("Error parsing Method")
}
if scm.Params != nil {
t.Errorf("Error parsing Params")
}
}
func TestStratumCommandMsgInvalidId(t *testing.T) {
var exited bool = false
exiter = func(code int) {
exited = true
}
var input string = "{\"id\":\"999\"}"
var scm = new(Stratum_command_msg)
scm.FromJsonString(input)
if exited == false {
t.Errorf("Error in FromJsonString")
}
}
func TestStratumCommandMsgMarshal(t *testing.T) {
var expected string = "{\"id\":9999,\"params\":null,\"method\":\"cmd\"}"
var scm = Stratum_command_msg{
Id: 9999,
Method: "cmd",
}
output, _ := json.Marshal(scm)
if string(output) != expected {
t.Errorf("Error Marshalling Json, output: %s", output)
}
}
func TestStratumCommandResp(t *testing.T) {
var input string = "{\"id\":9999,\"result\":\"response\"}"
var scr = new(Stratum_command_resp)
scr.FromJsonString(input)
if scr.Id != 9999 {
t.Errorf("Error parsing stratum response")
}
output, _ := json.Marshal(scr)
if string(output) != input {
t.Errorf("Error Marshalling Json, output: %s", output)
}
}