Repository navigation
Expand file tree
/
Copy pathprotocol_test.go
More file actions
270 lines (251 loc) · 9.09 KB
/
Copy pathprotocol_test.go
File metadata and controls
270 lines (251 loc) · 9.09 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package dbgp
import (
"bytes"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestFileURIConversions(t *testing.T) {
tests := []struct {
name string
path string
expectURI string
}{
{
name: "unix path with spaces",
path: "/tmp/test dir/file.php",
expectURI: "file:///tmp/test%20dir/file.php",
},
{
name: "windows drive path",
path: `C:\path with spaces\file.php`,
expectURI: "file:///C:/path%20with%20spaces/file.php",
},
{
name: "windows unc path",
path: `\\server\share\dir\file.php`,
expectURI: "file://server/share/dir/file.php",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotURI := MakeFileURI(tt.path)
if gotURI != tt.expectURI {
t.Fatalf("MakeFileURI() = %q, want %q", gotURI, tt.expectURI)
}
gotPath := FormatFileURI(gotURI)
if gotPath == "" {
t.Fatalf("FormatFileURI() returned empty path")
}
})
}
}
func TestParseInitFixture(t *testing.T) {
init, err := ParseInit([]byte(fixture(t, "init")))
if err != nil {
t.Fatal(err)
}
want := InitPacket{
AppID: fixtureAppID, Language: "PHP", Protocol: "1.0", FileURI: fixtureScriptURI,
Engine: Engine{Name: "PHP Debugger", Version: "0.3.3"}, EngineVersion: "0.3.3",
}
init.XMLName = want.XMLName
if *init != want {
t.Errorf("init = %+v, want %+v", *init, want)
}
}
func TestParseInitRejectsResponse(t *testing.T) {
if _, err := ParseInit([]byte(fixture(t, "status_break"))); err == nil {
t.Error("ParseInit accepted a response packet")
}
}
// Every recorded Xdebug response must parse, with its command attribute.
func TestParseResponseFixtures(t *testing.T) {
entries, err := os.ReadDir(fixtureDir)
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
name := strings.TrimSuffix(entry.Name(), ".xml")
data := []byte(fixture(t, name))
if !bytes.Contains(data, []byte("<response")) {
continue
}
t.Run(name, func(t *testing.T) {
resp, err := ParseResponse(data)
if err != nil {
t.Fatal(err)
}
if want := attr(data, "command"); resp.Command != want {
t.Errorf("Command = %q, want %q", resp.Command, want)
}
if want := attr(data, "status"); resp.Status != want {
t.Errorf("Status = %q, want %q", resp.Status, want)
}
if wantErr := bytes.Contains(data, []byte("<error")); (resp.Error != nil) != wantErr {
t.Errorf("Error = %+v, want error: %v", resp.Error, wantErr)
}
})
}
}
func TestParseResponseRejectsOtherPackets(t *testing.T) {
for _, name := range []string{"init", "stream_stdout", "notify_breakpoint_resolved"} {
if _, err := ParseResponse([]byte(fixture(t, name))); err == nil {
t.Errorf("ParseResponse accepted %s", name)
}
}
}
func TestParseResponseFields(t *testing.T) {
parse := func(name string) *Response {
t.Helper()
resp, err := ParseResponse([]byte(fixture(t, name)))
if err != nil {
t.Fatal(err)
}
return resp
}
if r := parse("error_breakpoint_not_found"); r.Error.Code != 205 || r.Error.Message != "no such breakpoint" {
t.Errorf("error = %+v", r.Error)
}
if r := parse("breakpoint_set_line"); r.BreakpointID != 42420001 {
t.Errorf("BreakpointID = %d", r.BreakpointID)
}
if r := parse("context_get_locals"); len(r.Properties) != 3 || r.Properties[0].FullName != "$a" {
t.Errorf("Properties = %+v", r.Properties)
}
if r := parse("stack_get"); len(r.Stack) != 2 || r.Stack[1].Where != "{main}" {
t.Errorf("Stack = %+v", r.Stack)
}
if r := parse("source"); r.Encoding != "base64" || r.Value == "" {
t.Errorf("source Encoding = %q, Value = %q", r.Encoding, r.Value)
}
for name, want := range map[string]int{"run_break": lineAddBody, "step_over": lineAddReturn, "run_break_conditional": lineLoopBody} {
file, line := parse(name).ParseMessage()
if file != filepath.FromSlash("/app/basic.php") || line != want {
t.Errorf("%s: ParseMessage = %s:%d, want /app/basic.php:%d", name, file, line, want)
}
}
if file, line := parse("stop").ParseMessage(); file != "" || line != 0 {
t.Errorf("stop: ParseMessage = %q:%d, want empty", file, line)
}
}
func TestCharsetReader(t *testing.T) {
tests := []struct {
name string
encoding string
value []byte
want string
}{
{"utf-8", "UTF-8", []byte("h\xc3\xa9"), "hé"},
{"latin1", "iso-8859-1", []byte("h\xe9"), "hé"},
{"windows-1252 override", "windows-1252", []byte("\x80\x93x\x94"), "€“x”"},
{"windows-1252 latin range", "windows-1252", []byte("\xe9"), "é"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := append([]byte(`<?xml version="1.0" encoding="`+tt.encoding+`"?><response command="eval" transaction_id="1"><property type="string">`), tt.value...)
data = append(data, []byte(`</property></response>`)...)
resp, err := ParseResponse(data)
if err != nil {
t.Fatal(err)
}
if got := resp.Properties[0].Value; got != tt.want {
t.Errorf("value = %q, want %q", got, tt.want)
}
})
}
if _, err := ParseResponse([]byte(`<?xml version="1.0" encoding="koi8-r"?><response/>`)); err == nil {
t.Error("unsupported charset accepted")
}
}
func TestFileURIRoundTrip(t *testing.T) {
for _, path := range []string{"/tmp/a.php", "/tmp/test dir/file.php", "/tmp/ümlaut/#hash%.php"} {
uri := MakeFileURI(path)
if !strings.HasPrefix(uri, "file:///") {
t.Errorf("MakeFileURI(%q) = %q", path, uri)
}
if got := FormatFileURI(uri); got != filepath.FromSlash(path) {
t.Errorf("FormatFileURI(MakeFileURI(%q)) = %q (uri %q)", path, got, uri)
}
}
if got := FormatFileURI("file:///C:/path%20with%20spaces/file.php"); got != filepath.FromSlash("C:/path with spaces/file.php") {
t.Errorf("windows drive URI -> %q", got)
}
if got := FormatFileURI("file://server/share/file.php"); got != "//server"+filepath.FromSlash("/share/file.php") {
t.Errorf("UNC URI -> %q", got)
}
if got := MakeFileURI("file:///already/a%20uri.php"); got != "file:///already/a%20uri.php" {
t.Errorf("MakeFileURI(uri) = %q", got)
}
if got := MakeFileURI("relative/file.php"); got != "file:///relative/file.php" {
t.Errorf("MakeFileURI(relative) = %q", got)
}
if got := FormatFileURI("dbgp://eval/1"); got != "dbgp://eval/1" {
t.Errorf("FormatFileURI(non-file) = %q", got)
}
}
func TestFormatStack(t *testing.T) {
frames := []StackFrame{
{Level: 0, Where: "add", Filename: "file:///app/basic.php", Lineno: 24},
{Level: 1, Where: "{main}", Filename: "file:///app/basic.php", Lineno: 36},
}
file := filepath.FromSlash("/app/basic.php")
want := []string{"#0 add() at " + file + ":24", "#1 {main}() at " + file + ":36"}
if got := FormatStack(frames); !reflect.DeepEqual(got, want) {
t.Errorf("FormatStack = %q, want %q", got, want)
}
}
func TestFormatStackDecodesURI(t *testing.T) {
got := FormatStack([]StackFrame{{Where: "f", Filename: "file:///my%20app/a.php", Lineno: 1}})
if want := "#0 f() at " + filepath.FromSlash("/my app/a.php") + ":1"; got[0] != want {
t.Errorf("FormatStack = %q, want %q", got[0], want)
}
}
func TestParseBreakpoints(t *testing.T) {
resp, err := ParseResponse([]byte(fixture(t, "breakpoint_list")))
if err != nil {
t.Fatal(err)
}
want := []BreakpointInfo{
{ID: 42420001, Type: "line", Filename: fixtureScriptURI, Lineno: 24, State: "enabled", Resolved: "resolved"},
{ID: 42420002, Type: "conditional", Filename: fixtureScriptURI, Lineno: 39, State: "enabled", Resolved: "resolved", Expression: "$i == 3"},
{ID: 42420003, Type: "call", Function: "add", State: "disabled", Resolved: "resolved"},
{ID: 42420004, Type: "exception", Exception: "Exception", State: "enabled", Resolved: "resolved"},
}
if !reflect.DeepEqual(resp.Breakpoints, want) {
t.Errorf("Breakpoints =\n%+v\nwant\n%+v", resp.Breakpoints, want)
}
resp, err = ParseResponse([]byte(fixture(t, "breakpoint_get")))
if err != nil || len(resp.Breakpoints) != 1 || resp.Breakpoints[0].Expression != "$i == 3" {
t.Errorf("breakpoint_get = %+v, %v", resp, err)
}
}
func TestParseBreakpointExpressionForms(t *testing.T) {
parse := func(bp string) (*Response, error) {
return ParseResponse([]byte(`<response command="breakpoint_get" transaction_id="1">` + bp + `</response>`))
}
resp, err := parse(`<breakpoint id="1" type="conditional" expression="$a > 1"></breakpoint>`)
if err != nil || resp.Breakpoints[0].Expression != "$a > 1" {
t.Errorf("attribute form: %+v, %v", resp, err)
}
resp, err = parse(`<breakpoint id="1" type="conditional"><expression>$b</expression></breakpoint>`)
if err != nil || resp.Breakpoints[0].Expression != "$b" {
t.Errorf("plain element form: %+v, %v", resp, err)
}
if _, err := parse(`<breakpoint id="1"><expression encoding="base64">!!</expression></breakpoint>`); err == nil {
t.Error("invalid base64 expression accepted")
}
if _, err := parse(`<breakpoint id="1"><expression encoding="rot13">x</expression></breakpoint>`); err == nil {
t.Error("unknown expression encoding accepted")
}
}
func TestParseFeatureGetSupported(t *testing.T) {
for name, want := range map[string]string{"feature_get": "1", "feature_get_unsupported": "0"} {
resp, err := ParseResponse([]byte(fixture(t, name)))
if err != nil || resp.Supported != want {
t.Errorf("%s: Supported = %q, %v; want %q", name, resp.Supported, err, want)
}
}
}