-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathencoder_test.go
180 lines (159 loc) · 5 KB
/
encoder_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package php_session_decoder
import (
"encoding/json"
"strings"
"testing"
"github.com/yvasiyarov/php_session_decoder/php_serialize"
)
func TestEncodeBooleanValue(t *testing.T) {
data := PhpSession{
"login_ok": true,
}
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode boolens value %#v \n", err)
} else {
if result != "login_ok|b:1;" {
t.Errorf("Boolean value was encoded incorrectly %v \n", result)
}
}
}
func TestEncodeIntValue(t *testing.T) {
data := PhpSession{
"inteiro": 34,
}
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode int value %#v \n", err)
} else {
if result != "inteiro|i:34;" {
t.Errorf("Int value was encoded incorrectly %v \n", result)
}
}
}
func TestEncodeFloatValue(t *testing.T) {
data := PhpSession{
"float_test": 34.4679999999,
}
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode float value %#v \n", err)
} else {
// 34.467999999900002 - PHP has precision = 17 by default
if result != "float_test|d:34.467999999900002;" {
t.Errorf("Float value was encoded incorrectly %v\n", result)
}
}
}
func TestEncodeStringValue(t *testing.T) {
data := PhpSession{
"name": "some text",
}
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode string value %#v \n", err)
} else {
if result != "name|s:9:\"some text\";" {
t.Errorf("String value was encoded incorrectly %v\n", result)
}
}
}
func TestEncodeArrayValue(t *testing.T) {
data := PhpSession{
"arr": php_serialize.PhpArray{
// Zero element
//php_serialize.PhpValue(0): 5,
0: 5,
"test": true,
"test2": nil,
},
}
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode array value %#v \n", err)
} else {
if !strings.Contains(result, "i:0;i:5;") || !strings.Contains(result, "s:4:\"test\";b:1") || !strings.Contains(result, "s:5:\"test2\";N") {
t.Errorf("Array value was encoded incorrectly %v\n", result)
}
}
}
func TestEncodeObjectValue(t *testing.T) {
obj := php_serialize.NewPhpObject("TestObject")
obj.SetPublic("a", 5)
obj.SetProtected("c", 8)
obj.SetPrivate("b", "priv")
data := PhpSession{
"obj": obj,
}
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode object value %#v \n", err)
} else {
if !strings.Contains(result, "s:1:\"a\";i:5") || !strings.Contains(result, "10:\"TestObject\"") || !strings.Contains(result, "s:13:\"\x00TestObject\x00b\";s:4:\"priv\"") || !strings.Contains(result, "s:4:\"\x00*\x00c\";i:8") {
t.Errorf("Object value was encoded incorrectly %v\n", result)
}
}
}
func TestEncodeSerializableObjectValueNoFunc(t *testing.T) {
obj := php_serialize.NewPhpObjectSerialized("TestObject")
obj.SetData("a:3:{s:1:\"a\";i:5;s:1:\"b\";s:4:\"priv\";s:1:\"c\";i:8;}")
data := PhpSession{
"obj": obj,
}
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode object value %#v \n", err)
} else {
if !strings.Contains(result, "a:3:{s:1:\"a\";i:5;s:1:\"b\";s:4:\"priv\";s:1:\"c\";i:8;}") || !strings.Contains(result, "C:10:\"TestObject\"") {
t.Errorf("Object value was encoded incorrectly %v\n", result)
}
}
}
func TestEncodeSerializableObjectValue(t *testing.T) {
arr := php_serialize.PhpArray{
"a": 5,
"b": "priv",
"c": 8,
}
obj := php_serialize.NewPhpObjectSerialized("TestObject")
obj.SetValue(php_serialize.PhpValue(arr))
data := PhpSession{
"obj": obj,
}
encoder := NewPhpEncoder(data)
encoder.SetSerializedEncodeFunc(php_serialize.SerializedEncodeFunc(php_serialize.Serialize))
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode object value %#v \n", err)
} else {
if !strings.Contains(result, "C:10:\"TestObject\"") {
t.Errorf("Object value was encoded incorrectly %v\n", result)
} else if !strings.Contains(result, "s:1:\"a\";i:5;") {
t.Errorf("Object value was encoded incorrectly %v\n", result)
} else if !strings.Contains(result, "s:1:\"b\";s:4:\"priv\";") {
t.Errorf("Object value was encoded incorrectly %v\n", result)
} else if !strings.Contains(result, "s:1:\"c\";i:8;") {
t.Errorf("Object value was encoded incorrectly %v\n", result)
}
}
}
func TestEncodeSerializableObjectValueJSON(t *testing.T) {
var f php_serialize.SerializedEncodeFunc
f = func(v php_serialize.PhpValue) (string, error) {
res, err := json.Marshal(v)
return string(res), err
}
obj := php_serialize.NewPhpObjectSerialized("Bar")
obj.SetValue(map[string]string{"public": "public"})
data := PhpSession{
"bar": obj,
}
encoder := NewPhpEncoder(data)
encoder.SetSerializedEncodeFunc(f)
if result, err := encoder.Encode(); err != nil {
t.Errorf("Can not encode object value %#v \n", err)
} else {
if result != "bar|C:3:\"Bar\":19:{{\"public\":\"public\"}}" {
t.Errorf("Object value was encoded incorrectly %v\n", result)
}
}
}