gunk-dev/the-valley

body_test.go

sourcecc441a0b6b45main's published tip history
3.8 KiB
1package export_test
2
3import (
4 "bytes"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "os"
9 "path/filepath"
10 "testing"
11
12 "the-valley/webui/export"
13)
14
15func putBody(body io.Reader) *http.Request {
16 return httptest.NewRequest("PUT", "/", body)
17}
18
19// untouched is a body that records whether anything read it.
20type untouched struct{ read bool }
21
22func (u *untouched) Read(p []byte) (int, error) {
23 u.read = true
24 return 0, io.EOF
25}
26
27// A body is kept under its hash once its size and hash match. Every
28// refusal leaves the staging directory as it was.
29func TestStageBody(t *testing.T) {
30 data := []byte("the body's bytes\n")
31 want := export.BodyOf("export.json", data)
32 const limit = 1 << 20
33
34 t.Run("a body that verifies", func(t *testing.T) {
35 dir := t.TempDir()
36 path, err := export.StageBody(dir, want, limit, putBody(bytes.NewReader(data)))
37 if err != nil {
38 t.Fatal(err)
39 }
40 if path != filepath.Join(dir, want.SHA256) {
41 t.Errorf("staged at %s", path)
42 }
43 if got, _ := os.ReadFile(path); !bytes.Equal(got, data) {
44 t.Errorf("staged %q", got)
45 }
46 // Sent again, it is accepted without being read.
47 again := &untouched{}
48 if _, err := export.StageBody(dir, want, limit, putBody(again)); err != nil || again.read {
49 t.Errorf("a staged body sent again: %v, read %v", err, again.read)
50 }
51 if left := entries(t, dir); len(left) != 1 {
52 t.Errorf("the directory holds %v", left)
53 }
54 })
55
56 t.Run("a body declared over the cap is not read", func(t *testing.T) {
57 dir := t.TempDir()
58 body := &untouched{}
59 _, err := export.StageBody(dir, want, int64(len(data))-1, putBody(body))
60 wantCode(t, err, export.CodeTooLarge)
61 if body.read {
62 t.Error("the body was read")
63 }
64 })
65
66 refusals := []struct {
67 name string
68 want export.Body
69 req func() *http.Request
70 code export.Code
71 }{
72 {"a Content-Encoding", want, func() *http.Request {
73 r := putBody(bytes.NewReader(data))
74 r.Header.Set("Content-Encoding", "gzip")
75 return r
76 }, export.CodeEncoding},
77 {"Content-Encoding identity", want, func() *http.Request {
78 r := putBody(bytes.NewReader(data))
79 r.Header.Set("Content-Encoding", "identity")
80 return r
81 }, export.CodeEncoding},
82 {"a Content-Length other than the declared size", want, func() *http.Request {
83 r := putBody(bytes.NewReader(data))
84 r.ContentLength = int64(len(data)) + 1
85 return r
86 }, export.CodeSizeMismatch},
87 {"a short body", want, func() *http.Request {
88 r := putBody(bytes.NewReader(data[:5]))
89 r.ContentLength = -1
90 return r
91 }, export.CodeSizeMismatch},
92 {"a body longer than declared", want, func() *http.Request {
93 r := putBody(io.MultiReader(bytes.NewReader(data), endless{}))
94 r.ContentLength = -1
95 return r
96 }, export.CodeSizeMismatch},
97 {"other bytes of the same size", want, func() *http.Request {
98 return putBody(bytes.NewReader(bytes.ToUpper(data)))
99 }, export.CodeHashMismatch},
100 {"a name that is no hash", export.Body{Name: "x", SHA256: "../../outside", Size: int64(len(data))}, func() *http.Request {
101 return putBody(bytes.NewReader(data))
102 }, export.CodeBadManifest},
103 }
104 for _, c := range refusals {
105 t.Run(c.name, func(t *testing.T) {
106 parent := t.TempDir()
107 dir := filepath.Join(parent, "bodies")
108 if err := os.Mkdir(dir, 0o755); err != nil {
109 t.Fatal(err)
110 }
111 _, err := export.StageBody(dir, c.want, limit, c.req())
112 wantCode(t, err, c.code)
113 if left := entries(t, dir); len(left) != 0 {
114 t.Errorf("the refusal left %v in the staging directory", left)
115 }
116 if left := entries(t, parent); len(left) != 1 {
117 t.Errorf("the refusal wrote %v beside the staging directory", left)
118 }
119 })
120 }
121}
122
123func entries(t *testing.T, dir string) []string {
124 t.Helper()
125 list, err := os.ReadDir(dir)
126 if err != nil {
127 t.Fatal(err)
128 }
129 var names []string
130 for _, e := range list {
131 names = append(names, e.Name())
132 }
133 return names
134}