-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_path_unsafe_test.go
More file actions
182 lines (160 loc) · 4.05 KB
/
Copy pathrequest_path_unsafe_test.go
File metadata and controls
182 lines (160 loc) · 4.05 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package httpserver
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHTTPRequestUnsafeLayout(t *testing.T) {
actualRequest := reflect.TypeFor[http.Request]()
mirrorRequest := reflect.TypeFor[httpRequest]()
// These are the Request fields we actually dereference through
// the unsafe mirror.
for _, name := range []string{"pat", "matches"} {
actualField, ok := actualRequest.FieldByName(name)
if !ok {
t.Fatalf("net/http.Request no longer has field %q", name)
}
mirrorField, ok := mirrorRequest.FieldByName(name)
if !ok {
t.Fatalf("httpRequest mirror no longer has field %q", name)
}
if actualField.Offset != mirrorField.Offset {
t.Fatalf(
"http.Request.%s offset changed: net/http=%d mirror=%d",
name,
actualField.Offset,
mirrorField.Offset,
)
}
if actualField.Type.Size() != mirrorField.Type.Size() ||
actualField.Type.Align() != mirrorField.Type.Align() {
t.Fatalf(
"http.Request.%s layout changed: "+
"net/http type=%v size=%d align=%d; "+
"mirror type=%v size=%d align=%d",
name,
actualField.Type,
actualField.Type.Size(),
actualField.Type.Align(),
mirrorField.Type,
mirrorField.Type.Size(),
mirrorField.Type.Align(),
)
}
}
// Verify the private *http.pattern that Request.pat points to.
patField, _ := actualRequest.FieldByName("pat")
actualPattern := patField.Type.Elem()
assertStructLayout(
t,
"net/http.pattern",
actualPattern,
reflect.TypeFor[httpPattern](),
)
// Verify the private http.segment layout too.
segmentsField, ok := actualPattern.FieldByName("segments")
if !ok {
t.Fatal("net/http.pattern no longer has field \"segments\"")
}
if segmentsField.Type.Kind() != reflect.Slice {
t.Fatalf(
"net/http.pattern.segments is no longer a slice: %v",
segmentsField.Type,
)
}
assertStructLayout(
t,
"net/http.segment",
segmentsField.Type.Elem(),
reflect.TypeFor[httpSegment](),
)
}
func assertStructLayout(
t *testing.T,
name string,
actual, mirror reflect.Type,
) {
t.Helper()
if actual.Kind() != reflect.Struct ||
mirror.Kind() != reflect.Struct {
t.Fatalf(
"%s: expected structs: actual=%v mirror=%v",
name,
actual,
mirror,
)
}
if actual.Size() != mirror.Size() ||
actual.Align() != mirror.Align() {
t.Fatalf(
"%s size/alignment changed: "+
"actual size=%d align=%d; "+
"mirror size=%d align=%d",
name,
actual.Size(),
actual.Align(),
mirror.Size(),
mirror.Align(),
)
}
if actual.NumField() != mirror.NumField() {
t.Fatalf(
"%s field count changed: actual=%d mirror=%d",
name,
actual.NumField(),
mirror.NumField(),
)
}
for i := 0; i < actual.NumField(); i++ {
af := actual.Field(i)
mf := mirror.Field(i)
if af.Name != mf.Name {
t.Fatalf(
"%s field %d changed: actual=%q mirror=%q",
name,
i,
af.Name,
mf.Name,
)
}
if af.Offset != mf.Offset {
t.Fatalf(
"%s.%s offset changed: actual=%d mirror=%d",
name,
af.Name,
af.Offset,
mf.Offset,
)
}
// Exact Type equality won't work for unexported
// net/http types such as http.segment, so compare
// the properties that matter for unsafe access.
if af.Type.Kind() != mf.Type.Kind() ||
af.Type.Size() != mf.Type.Size() ||
af.Type.Align() != mf.Type.Align() {
t.Fatalf(
"%s.%s type layout changed: actual=%v mirror=%v",
name,
af.Name,
af.Type,
mf.Type,
)
}
}
}
// TestGetPathValues_UnmatchedRequest_ReturnsNil exercises the
// `request.pat == nil` branch in [getPathValues]: a request that has not been
// dispatched through a [http.ServeMux] has no matched pattern, so the result
// must be nil.
func TestGetPathValues_UnmatchedRequest_ReturnsNil(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
values := getPathValues(req)
assert.Nil(t, values)
}