This repository was archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_check_test.go
107 lines (96 loc) · 3.29 KB
/
send_check_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
package main
import (
"encoding/base64"
"goprotobuf.googlecode.com/hg/proto"
"log"
"testing"
"json"
)
type jsonTest struct {
cr *CheckResult
json string
}
var jsonTests = []jsonTest{
{&CheckResult{Hostname: proto.String("testhost.foo.bar"),
ServiceName: proto.String("test-service"),
Status: NewCheckStatus(CheckStatus_OK),
StartTimestamp: proto.Int64(151892832),
EndTimestamp: proto.Int64(151895000)},
"{\"Hostname\":\"testhost.foo.bar\",\"ServiceName\":\"test-service\",\"Status\":0,\"CheckPassive\":null,\"CheckScheduled\":null,\"CheckOutput\":null,\"StartTimestamp\":151892832,\"EndTimestamp\":151895000,\"XXX_unrecognized\":\"\"}"}}
var jsonStr = "{\"Hostname\":\"testhost.foo.bar\",\"ServiceName\":\"test-service\",\"Status\":0,\"CheckPassive\":null,\"CheckScheduled\":null,\"CheckOutput\":null,\"StartTimestamp\":151892832,\"EndTimestamp\":151895000,\"XXX_unrecognized\":\"\"}"
var cr = CheckResult{
Hostname: proto.String("testhost.foo.bar"),
ServiceName: proto.String("test-service"),
Status: NewCheckStatus(CheckStatus_OK),
StartTimestamp: proto.Int64(151892832),
EndTimestamp: proto.Int64(151895000),
}
var crs = CheckResultSet{
Results: []*CheckResult{&cr},
}
func TestMarshalUnmarshal(t *testing.T) {
t.Log("marshalling protobuf")
buf, err := proto.Marshal(&cr)
if err != nil {
log.Fatal("marshal error: ", err)
}
t.Log("marshalled")
rcr := new(CheckResult)
t.Log("unmarshalling")
err = proto.Unmarshal(buf, rcr)
t.Log(rcr)
}
func TestJsonToProto(t *testing.T) {
for i, v := range jsonTests {
ncr := &CheckResult{}
jsonBytes := []byte(v.json)
t.Logf("test %d: unmarshalling json to protobuf struct", i)
err := json.Unmarshal(jsonBytes, ncr)
if err != nil {
t.Errorf("test %d: %s", i, err)
}
if proto.CompactTextString(ncr) != proto.CompactTextString(v.cr) {
t.Errorf("test %d: mismatch\noutput: %s\nexpected: %s", i, ncr, v.cr)
}
t.Logf("%s\n", proto.CompactTextString(ncr))
}
}
func TestJsonOutput(t *testing.T) {
t.Log("marshalling protobuf as json")
buf, err := json.Marshal(&crs)
t.Logf("%#v\n", string(buf))
t.Log(err)
}
func TestMarshalUnmarshalBase64(t *testing.T) {
var encbuf []byte
var decbuf []byte
t.Logf("start with buf %s\n", proto.CompactTextString(&cr))
t.Log("marshalling protobuf")
buf, err := proto.Marshal(&cr)
if err != nil {
t.Error("marshal error: ", err)
}
t.Log("marshalled")
t.Log("urlencoding")
t.Logf("need %d size buffer\n", base64.URLEncoding.EncodedLen(len(buf)-1))
t.Log(buf)
t.Logf("%v %s\n", buf, buf)
encbuf = make([]byte, base64.URLEncoding.EncodedLen(len(buf)), base64.URLEncoding.EncodedLen(len(buf)))
base64.URLEncoding.Encode(encbuf, buf)
t.Log("urlencoded")
t.Log("urldecoding")
t.Logf("need %d size buffer\n", base64.URLEncoding.DecodedLen(len(encbuf)))
t.Logf("%v %s\n", encbuf, encbuf)
decbuf = make([]byte, base64.URLEncoding.DecodedLen(len(encbuf)), base64.URLEncoding.DecodedLen(len(encbuf)))
n, err := base64.URLEncoding.Decode(decbuf, encbuf)
t.Logf("wrote %d bytes from encbuf to decbuf. len(encbuf)=%d, len(buf)=%d\n", n, len(encbuf), len(buf))
if err != nil {
t.Error("urldecode error: ", err)
}
t.Log("urldecoded")
t.Log(buf, decbuf)
rcr := &CheckResult{}
t.Log("unmarshalling")
err = proto.Unmarshal(decbuf, rcr)
t.Logf("%s\n", proto.CompactTextString(rcr))
}