oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package webread
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "net"
8 "net/http"
9 "net/netip"
10 "os"
11 "strings"
12 "testing"
13)
14
15func TestMainWritesTheBundle(t *testing.T) {
16 mux := http.NewServeMux()
17 mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("hello")) })
18 r := newRig(t, mux)
19 r.host("shop.example.com", public1)
20 raw, _ := json.Marshal(plan(t, "https://shop.example.com/"))
21 dir := t.TempDir()
22 var stderr bytes.Buffer
23 limited := false
24 if code := mainWith([]string{dir}, bytes.NewReader(raw), &stderr, r.d, func() error { limited = true; return nil }); code != 0 {
25 t.Fatalf("exit %d: %s", code, stderr.String())
26 }
27 if !limited {
28 t.Fatal("limits not applied")
29 }
30 b, err := ReadBundle(dir)
31 if err != nil || len(b.Files) != 1 || string(b.Files[0].Data) != "hello" {
32 t.Fatalf("bundle %+v %v", b, err)
33 }
34}
35
36func TestMainRefusesBadInput(t *testing.T) {
37 ok, _ := json.Marshal(plan(t, "https://shop.example.com/"))
38 for name, c := range map[string]struct {
39 args []string
40 input string
41 }{
42 "no outdir": {nil, string(ok)},
43 "two outdirs": {[]string{t.TempDir(), t.TempDir()}, string(ok)},
44 "not json": {[]string{t.TempDir()}, "{"},
45 "unknown field": {[]string{t.TempDir()}, strings.Replace(string(ok), `{"sources"`, `{"extra":1,"sources"`, 1)},
46 "trailing": {[]string{t.TempDir()}, string(ok) + "{}"},
47 "too large": {[]string{t.TempDir()}, strings.Repeat(" ", MaxPlanBytes+1)},
48 "bad source": {[]string{t.TempDir()}, `{"sources":[{"id":"s1","url":{"scheme":"https","host":"127.0.0.1","port":443,"path":"/"},"origin":"paul"}]}`},
49 } {
50 var stderr bytes.Buffer
51 d := defaults()
52 d.lookup = func(context.Context, string) ([]netip.Addr, error) {
53 t.Errorf("%s: looked up", name)
54 return nil, net.ErrClosed
55 }
56 if code := mainWith(c.args, strings.NewReader(c.input), &stderr, d, func() error { return nil }); code == 0 {
57 t.Errorf("%s: exit 0", name)
58 }
59 if len(c.args) == 1 {
60 if names, _ := os.ReadDir(c.args[0]); len(names) != 0 {
61 t.Errorf("%s: wrote %v", name, names)
62 }
63 }
64 }
65}
66
67func TestLocalAddressesKeepsGlobalUnicast(t *testing.T) {
68 var in []net.Addr
69 for _, s := range []string{"127.0.0.1/8", "::1/128", "fe80::1/64", "169.254.3.4/16", "192.168.1.5/24", "151.101.1.1/32", "2a01:4f8::5/64", "224.0.0.1/4", "151.101.1.1/24"} {
70 ip, n, _ := net.ParseCIDR(s)
71 n.IP = ip
72 in = append(in, n)
73 }
74 got, err := localAddresses(func() ([]net.Addr, error) { return in, nil })
75 if err != nil {
76 t.Fatal(err)
77 }
78 var s []string
79 for _, a := range got {
80 s = append(s, a.String())
81 }
82 if strings.Join(s, " ") != "151.101.1.1 192.168.1.5 2a01:4f8::5" {
83 t.Fatalf("got %v", s)
84 }
85 if _, err := localAddresses(func() ([]net.Addr, error) { return nil, net.ErrClosed }); err == nil {
86 t.Fatal("an unreadable interface list is not an empty one")
87 }
88 // The real host's list reads, and holds no loopback.
89 real, err := LocalAddresses()
90 if err != nil {
91 t.Fatal(err)
92 }
93 for _, a := range real {
94 if a.IsLoopback() || a.IsLinkLocalUnicast() {
95 t.Fatalf("LocalAddresses gave %s", a)
96 }
97 }
98}
99