-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostDataWebGo_test.go
More file actions
160 lines (153 loc) · 4.4 KB
/
postDataWebGo_test.go
File metadata and controls
160 lines (153 loc) · 4.4 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/golang/protobuf/proto"
"github.com/superryanguo/postDataWebGo/myobject"
)
func TestPostDataHandler(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(PostDataHandler))
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Errorf("Error occured while constructing request: %s", err)
}
w := httptest.NewRecorder()
PostDataHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("Actual status: (%d); Expected status:(%d)", w.Code, http.StatusOK)
}
}
func TestMarshalRight(t *testing.T) {
u := myobject.User{
Id: proto.Int64(1),
Name: proto.String("Ryan"),
Email: proto.String("999dingguagua@hotml.com"),
}
data, err := proto.Marshal(&u)
if err != nil {
t.Fatal("marshaling error: ", err)
}
ur := &myobject.User{}
err = proto.Unmarshal(data, ur)
if err != nil {
t.Fatal("unmarshaling error: ", err)
}
//a better way to read the data member?
if *u.Id != *ur.Id || *u.Name != *ur.Name || *u.Email != *ur.Email {
t.Error("Unmatch data found")
}
}
func TestParseGpbNormalMode(t *testing.T) {
u := myobject.User{
Id: proto.Int64(1),
Name: proto.String("Ryan"),
Email: proto.String("999dingguagua@hotml.com"),
}
data, err := proto.Marshal(&u)
if err != nil {
t.Fatal("marshaling error: ", err)
}
p, err := ParseGpbNormalMode(data, "User", "./myobject/myobject.proto")
if err != nil {
t.Error(err.Error())
} else {
out := fmt.Sprintf("%s", p)
if !strings.Contains(out, "Ryan") || !strings.Contains(out, "999dingguagua@hotml.com") {
t.Error("Data parse fail")
}
}
}
func TestHardcoreDecode(t *testing.T) {
u := myobject.User{
Id: proto.Int64(1),
Name: proto.String("Ryan"),
Email: proto.String("999dingguagua@hotml.com"),
}
data, err := proto.Marshal(&u)
if err != nil {
t.Fatal("marshaling error: ", err)
}
p, err := HardcoreDecode("./myobject/myobject.proto", data)
if err != nil {
t.Error(err.Error())
} else {
out := fmt.Sprintf("%s", p)
if !strings.Contains(out, "Ryan") || !strings.Contains(out, "999dingguagua@hotml.com") {
t.Error("Data parse fail")
}
}
}
func TestHardcoreDecodeHexNoSpace(t *testing.T) {
data, err := CheckAndFilterDataInput("0801121739393964696e6767756167756140686f746d6c2e636f6d1a045279616e")
if err != nil {
t.Error(err.Error())
} else {
p, err := HardcoreDecode("./myobject/myobject.proto", data)
if err != nil {
t.Error(err.Error())
} else {
out := fmt.Sprintf("%s", p)
if !strings.Contains(out, "Ryan") || !strings.Contains(out, "999dingguagua@hotml.com") {
t.Error("Data parse fail")
}
}
}
}
func TestHardcoreDecodeHexSpace(t *testing.T) {
dataSpace := "08 01 12 17 39 39 39 64 69 6e 67 67 75 61 67 75 61 40 68 6f 74 6d 6c 2e 63 6f 6d 1a 04 52 79 61 6e"
data, err := CheckAndFilterDataInput(dataSpace)
if err != nil {
t.Error(err.Error())
} else {
p, err := HardcoreDecode("./myobject/myobject.proto", data)
if err != nil {
t.Error(err.Error())
} else {
out := fmt.Sprintf("%s", p)
if !strings.Contains(out, "Ryan") || !strings.Contains(out, "999dingguagua@hotml.com") {
t.Error("Data parse fail")
}
}
}
}
func TestEscapeHardcoreDecodeHexSpace(t *testing.T) {
dataSpace := "02 01 08 01 12 17 39 39 39 64 69 6e 67 67 75 61 67 75 61 40 68 6f 74 6d 6c 2e 63 6f 6d 1a 04 52 79 61 6e"
data, err := CheckAndFilterDataInput(dataSpace)
if err != nil {
t.Error(err.Error())
} else {
p, err := HardcoreDecode("./myobject/myobject.proto", data)
if err != nil {
t.Error(err.Error())
} else {
out := fmt.Sprintf("%s", p)
if !strings.Contains(out, "Ryan") || !strings.Contains(out, "999dingguagua@hotml.com") {
t.Error("Data parse fail")
}
}
}
}
func TestFilterDecDataString(t *testing.T) {
data := "[0]=8, [1]=0,[3]=5,[4]=9,[5]=7,[5]=c,[5]=a"
expect := "80597ca"
if expect != FilterDecDataString(data) {
t.Error("Filter Data String Error!")
}
}
func TestDecStringToHex(t *testing.T) {
data := "[0] = 65, [1] = 67, [2] = 5, [3] = 5, [4] = 0, [5] = 0, [6] = 0, [7] = 0, [8] = 0, [9] = 0, [10] = 0, [11] = 9, [12] = 0, [13] = 0, [14] = 0, [15] = 0, [16] = 8, [17] = 129, [18] = 128"
//first divide to strings slice
//then atoi
//then printf%x
b, err := ConvertDecToHexDataString(data)
if err != nil {
t.Errorf("Convert found err=%s", err.Error())
}
if "41430505000000000000000900000000088180" != fmt.Sprintf("%x", b) {
t.Error("Data mismatch")
}
}