oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "bytes"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "os"
10 "path/filepath"
11 "strings"
12 "sync"
13 "testing"
14
15 "amadan.net/oficina/mapa/internal/mapa"
16)
17
18// IMG2, the plaintext file scan for images (images spec §6), extending
19// gate 4. A PNG and a JPEG carry canaries in tEXt, EXIF, XMP and a
20// trailer after EOI, in the file name, and in the alt text. Afterwards
21// the database (WAL included) and the bucket hold no canary and no alt
22// text; no object starts with a PNG or JPEG magic number; the bucket
23// was told application/octet-stream; and every object is a multiple of
24// 64 KiB plus the seal's constant.
25
26const altCanary = "CANARYALT a whiteboard of salaries"
27
28// fakeS3 is a bucket over HTTP, path-style, as blobs.S3 speaks to an
29// endpoint: it keeps each object as a file and records each PUT's
30// Content-Type.
31type fakeS3 struct {
32 dir string
33 mutex sync.Mutex
34 types []string
35}
36
37func (bucket *fakeS3) ServeHTTP(writer http.ResponseWriter,
38 request *http.Request) {
39 path := filepath.Join(bucket.dir, filepath.Base(request.URL.Path))
40 switch request.Method {
41 case http.MethodPut:
42 raw, _ := io.ReadAll(request.Body)
43 bucket.mutex.Lock()
44 bucket.types = append(bucket.types,
45 request.Header.Get("Content-Type"))
46 bucket.mutex.Unlock()
47 _ = os.WriteFile(path, raw, 0o600)
48 case http.MethodGet:
49 raw, err := os.ReadFile(path)
50 if err != nil {
51 http.NotFound(writer, request)
52 return
53 }
54 _, _ = writer.Write(raw)
55 case http.MethodDelete:
56 _ = os.Remove(path)
57 writer.WriteHeader(http.StatusNoContent)
58 }
59}
60
61// s3Rig is a rig whose images go to a fake S3.
62func s3Rig(t *testing.T) (*rig, *fakeS3) {
63 t.Helper()
64 bucket := &fakeS3{dir: t.TempDir()}
65 server := httptest.NewServer(bucket)
66 t.Cleanup(server.Close)
67 // Through the config a CARLOS store arrives as, so the Put is the
68 // one production makes.
69 env := testEnv(Ana + "," + Bo)
70 env["CARLOS_STORE_BUCKET"], env["CARLOS_STORE_REGION"] = "mapa",
71 "eu-west-1"
72 env["CARLOS_STORE_ENDPOINT"] = server.URL
73 env["CARLOS_STORE_ACCESS_KEY"], env["CARLOS_STORE_SECRET_KEY"] = "key",
74 "secret"
75 delivered, err := mapa.LoadConfig(func(key string) string {
76 return env[key]
77 })
78 if err != nil || delivered.Images.Objects == nil {
79 t.Fatalf("the store was not bound: %v", err)
80 }
81 rig := newRigWith(t, func(config *mapa.Config, _ string) {
82 config.Images = delivered.Images
83 })
84 return rig, bucket
85}
86
87func fixture(t *testing.T, name string) []byte {
88 t.Helper()
89 raw, err := os.ReadFile(filepath.Join("..", "mapa", "imagefile",
90 "testdata", name))
91 if err != nil {
92 t.Fatal(err)
93 }
94 if !bytes.Contains(raw, []byte("CANARY-")) {
95 t.Fatalf("%s carries no canary", name)
96 }
97 return raw
98}
99
100func TestImagesReachTheDiskSealed(t *testing.T) {
101 rig, bucket := s3Rig(t)
102 var body []string
103 for _, name := range []string{"meta.png", "baseline.jpg"} {
104 reply := rig.sendFile(t, Ana, "/images", nil,
105 "CANARY-FILENAME-"+name, fixture(t, name))
106 if reply.Code != http.StatusOK {
107 t.Fatalf("%s: %d %s", name, reply.Code, reply.Body.String())
108 }
109 src := between(reply.Body.String(), `"src":"`, `"`)
110 body = append(body, "!["+altCanary+"]("+src+")")
111 }
112 rig.post(t, Ana, "/scratch", url.Values{"body": {
113 strings.Join(body, "\n\n")}})
114 if err := rig.db.Close(); err != nil {
115 t.Fatal(err)
116 }
117 looking := []string{"CANARY", altCanary}
118 for _, hit := range scan(t, rig.dir, looking) {
119 t.Errorf("plaintext in the database: %s", hit)
120 }
121 for _, hit := range scan(t, bucket.dir, looking) {
122 t.Errorf("plaintext in the bucket: %s", hit)
123 }
124 checkObjects(t, bucket)
125}
126
127// checkObjects holds each object to IMG2's shape.
128func checkObjects(t *testing.T, bucket *fakeS3) {
129 t.Helper()
130 entries, err := os.ReadDir(bucket.dir)
131 if err != nil || len(entries) != 2 {
132 t.Fatalf("%d objects: %v", len(entries), err)
133 }
134 for _, entry := range entries {
135 raw, _ := os.ReadFile(filepath.Join(bucket.dir, entry.Name()))
136 if bytes.HasPrefix(raw, []byte("\x89PNG")) ||
137 bytes.HasPrefix(raw, []byte{0xff, 0xd8, 0xff}) {
138 t.Errorf("%s starts with an image's magic number", entry.Name())
139 }
140 if (len(raw)-28)%(64<<10) != 0 {
141 t.Errorf("%s is %d bytes: not 64 KiB steps plus the seal",
142 entry.Name(), len(raw))
143 }
144 }
145 for _, kind := range bucket.types {
146 if kind != "application/octet-stream" {
147 t.Errorf("the bucket was told %q", kind)
148 }
149 }
150}
151