oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package websource
2
3import (
4 "errors"
5 "strings"
6 "testing"
7)
8
9func TestParseCanonicalParts(t *testing.T) {
10 for _, c := range []struct {
11 raw string
12 want URL
13 str string
14 }{
15 {"https://Example.COM", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/"}, "https://example.com/"},
16 {"HTTPS://example.com./a/b?x=1#frag", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/a/b", RawQuery: "x=1"}, "https://example.com/a/b?x=1"},
17 {"https://example.com:443/", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/"}, "https://example.com/"},
18 {"http://shop.example.com:80/menu", URL{Scheme: "http", Host: "shop.example.com", Port: 80, EscapedPath: "/menu"}, "http://shop.example.com/menu"},
19 {"https://example.com/caf%C3%A9?q=a%20b", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/caf%C3%A9", RawQuery: "q=a%20b"}, "https://example.com/caf%C3%A9?q=a%20b"},
20 {"https://example.com/?", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/"}, "https://example.com/"},
21 {"https://xn--caf-dma.example/", URL{}, ""}, // .example refused, see below
22 {"https://xn--caf-dma.com/", URL{Scheme: "https", Host: "xn--caf-dma.com", Port: 443, EscapedPath: "/"}, "https://xn--caf-dma.com/"},
23 {"https://example.com:/x", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/x"}, "https://example.com/x"},
24 } {
25 got, err := Parse(c.raw)
26 if c.str == "" {
27 if err == nil {
28 t.Errorf("Parse(%q) = %+v, want refusal", c.raw, got)
29 }
30 continue
31 }
32 if err != nil {
33 t.Errorf("Parse(%q): %v", c.raw, err)
34 continue
35 }
36 if got != c.want {
37 t.Errorf("Parse(%q) = %+v, want %+v", c.raw, got, c.want)
38 }
39 if got.String() != c.str {
40 t.Errorf("Parse(%q).String() = %q, want %q", c.raw, got.String(), c.str)
41 }
42 again, err := Parse(got.String())
43 if err != nil || again != got {
44 t.Errorf("round trip of %q: %+v, %v", got.String(), again, err)
45 }
46 }
47}
48
49func TestParseRefusals(t *testing.T) {
50 long := "https://example.com/" + strings.Repeat("a", MaxURL)
51 for _, c := range []struct {
52 raw string
53 reason string
54 }{
55 {"", "bad_url"},
56 {long, "url_too_long"},
57 {"ftp://example.com/", "scheme_refused"},
58 {"file:///etc/passwd", "scheme_refused"},
59 {"javascript:alert(1)", "scheme_refused"},
60 {"//example.com/", "scheme_refused"},
61 {"https:example.com", "bad_url"},
62 {"https:///path", "bad_url"},
63 {"https://user:pass@example.com/", "userinfo_refused"},
64 {"https://user@example.com/", "userinfo_refused"},
65 {"https://example.com:8080/", "port_refused"},
66 {"https://example.com:80/", "port_refused"},
67 {"http://example.com:443/", "port_refused"},
68 {"https://example.com:0443/", "port_refused"},
69 {"https://example.com/a b", "bad_url"},
70 {"https://example.com/a\\b", "bad_url"},
71 {"https://example.com/\x00", "bad_url"},
72 {"https://example.com/café", "bad_url"},
73 {"https://exämple.com/", "non_ascii_host"},
74 // IP literals in every form.
75 {"https://127.0.0.1/", "ip_literal"},
76 {"https://93.184.216.34/", "ip_literal"},
77 {"http://2130706433/", "ip_literal"},
78 {"http://0177.0.0.1/", "ip_literal"},
79 {"http://0x7f.0.0.1/", "ip_literal"},
80 {"http://0x7f000001/", "ip_literal"},
81 {"http://1.0x7f/", "ip_literal"},
82 {"http://127.1/", "ip_literal"},
83 {"https://[::1]/", "ip_literal"},
84 {"https://[::ffff:10.0.0.1]/", "ip_literal"},
85 {"https://[fe80::1%25eth0]/", "ip_literal"},
86 {"https://%31%32%37.0.0.1/", "bad_url"},
87 // Names.
88 {"https://localhost/", "host_refused"},
89 {"https://localhost./", "host_refused"},
90 {"https://app.localhost/", "host_refused"},
91 {"https://printer.local/", "host_refused"},
92 {"https://db.internal/", "host_refused"},
93 {"https://nas.lan/", "host_refused"},
94 {"https://router.home.arpa/", "host_refused"},
95 {"https://1.0.0.127.in-addr.arpa/", "host_refused"},
96 {"https://box.tailnet-example.ts.net/", "host_refused"},
97 {"https://abc.onion/", "host_refused"},
98 {"https://shop.test/", "host_refused"},
99 {"https://shop.invalid/", "host_refused"},
100 {"https://shop.example/", "host_refused"},
101 {"https://intranet/", "host_refused"},
102 {"https://example.123/", "host_refused"},
103 {"https://-bad.com/", "host_refused"},
104 {"https://bad-.com/", "host_refused"},
105 {"https://a..com/", "host_refused"},
106 {"https://example.com../", "host_refused"},
107 {"https://under_score.com/", "host_refused"},
108 {"https://" + strings.Repeat("a", 64) + ".com/", "host_refused"},
109 } {
110 u, err := Parse(c.raw)
111 if err == nil {
112 t.Errorf("Parse(%q) = %+v, want %s", c.raw, u, c.reason)
113 continue
114 }
115 if got := Reason(err); got != c.reason {
116 t.Errorf("Parse(%q) reason %q (%v), want %q", c.raw, got, err, c.reason)
117 }
118 }
119}
120
121func TestHostname(t *testing.T) {
122 for h, want := range map[string]bool{
123 "example.com": true, "a-b.example.com": true, "Example.com": false, "example": false,
124 "1.2.3.4": false, "::1": false, "-a.com": false, "a_b.com": false, "": false,
125 strings.Repeat("a.", 127) + "com": false,
126 } {
127 if got := Hostname(h); got != want {
128 t.Errorf("Hostname(%q) = %v, want %v", h, got, want)
129 }
130 }
131}
132
133func TestResolve(t *testing.T) {
134 base, err := Parse("https://shop.example.com/menu/today?x=1")
135 if err != nil {
136 t.Fatal(err)
137 }
138 for ref, want := range map[string]string{
139 "/style.css": "https://shop.example.com/style.css",
140 "logo.png": "https://shop.example.com/menu/logo.png",
141 "../about": "https://shop.example.com/about",
142 "?page=2": "https://shop.example.com/menu/today?page=2",
143 "//cdn.example.net/a.css": "https://cdn.example.net/a.css",
144 "http://other.example.org/x#top": "http://other.example.org/x",
145 "": "https://shop.example.com/menu/today?x=1",
146 } {
147 got, err := Resolve(base, ref)
148 if err != nil {
149 t.Errorf("Resolve(%q): %v", ref, err)
150 continue
151 }
152 if got.String() != want {
153 t.Errorf("Resolve(%q) = %q, want %q", ref, got.String(), want)
154 }
155 }
156 for ref, reason := range map[string]string{
157 "javascript:alert(1)": "scheme_refused",
158 "mailto:a@example.com": "scheme_refused",
159 "data:image/png;base64,AA": "scheme_refused",
160 "http://127.0.0.1/": "ip_literal",
161 "//localhost/": "host_refused",
162 "https://example.com:8080/": "port_refused",
163 "https://u:p@example.com/": "userinfo_refused",
164 "/" + strings.Repeat("a", MaxURL): "url_too_long",
165 } {
166 if u, err := Resolve(base, ref); err == nil || Reason(err) != reason {
167 t.Errorf("Resolve(%q) = %+v, %v; want %s", ref, u, err, reason)
168 }
169 }
170}
171
172func TestProposal(t *testing.T) {
173 if u, err := Proposal("https://shop.example.com/about"); err != nil || u.String() != "https://shop.example.com/about" {
174 t.Fatalf("Proposal: %+v %v", u, err)
175 }
176 for raw, reason := range map[string]string{
177 "https://shop.example.com/?q=1": "query_refused",
178 "https://shop.example.com/" + strings.Repeat("ab-", 34): "path_too_long",
179 "https://u@shop.example.com/": "userinfo_refused",
180 "https://shop.example.com/ghp_abcdefghijklmnopqrstuvwx": "secret_refused",
181 "https://127.0.0.1/": "ip_literal",
182 } {
183 if u, err := Proposal(raw); err == nil || Reason(err) != reason {
184 t.Errorf("Proposal(%q) = %+v, %v; want %s", raw, u, err, reason)
185 }
186 }
187 // Exactly 100 bytes of path is allowed.
188 if _, err := Proposal("https://shop.example.com/" + strings.Repeat("ab-", 33)); err != nil {
189 t.Errorf("100-byte path: %v", err)
190 }
191}
192
193func TestLooksSecret(t *testing.T) {
194 for _, s := range []string{
195 "sk-ant-api03-abcdefghijklmnop",
196 "sk_live_abcdefghijklmnop1234",
197 "ghp_abcdefghijklmnopqrstuvwxyz0123",
198 "github_pat_11ABCDEFG0123456789",
199 "gho_abcdefghijklmnop1234",
200 "glpat-abcdefghijklmnop1234",
201 "xoxb-123456789012-abcdefghij",
202 "AKIAIOSFODNN7EXAMPLE",
203 "-----BEGIN OPENSSH PRIVATE KEY-----",
204 "/x/-----BEGIN",
205 "AIzaSyA1234567890abcdefghijklmnopqrstu",
206 "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig",
207 "/api/token=abc123",
208 "/p/password:hunter2",
209 "/a/Q2xhdWRlIGlzIGEgZ29vZCBib3QgMTIz",
210 "/k/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
211 "/p/%67hp_abcdefghijklmnopqrstuvwxyz0123",
212 } {
213 if !LooksSecret(s) {
214 t.Errorf("LooksSecret(%q) = false", s)
215 }
216 }
217 for _, s := range []string{
218 "https://shop.example.com/about",
219 "/blog/how-to-make-the-best-coffee-at-home-in-2026",
220 "/products/supercalifragilisticexpialidocious",
221 "/menu/opening-hours",
222 "/images/logo-2024.png",
223 "/skills/sk-ills",
224 } {
225 if LooksSecret(s) {
226 t.Errorf("LooksSecret(%q) = true", s)
227 }
228 }
229}
230
231func TestReasonOfForeignError(t *testing.T) {
232 if Reason(errors.New("x")) != "bad_url" || Reason(nil) != "" {
233 t.Fatal("Reason fallback")
234 }
235}
236