oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3import (
4 "bytes"
5 "encoding/base64"
6 "encoding/json"
7 "log/slog"
8 "strings"
9 "testing"
10)
11
12// minimalConfig is a Config with every startup refusal already
13// satisfied outside development, so a test can flip exactly the one
14// field it cares about (here, Relay) and see only that field's effect.
15func minimalConfig(t *testing.T) Config {
16 t.Helper()
17 return Config{
18 Env: "production",
19 Origin: "https://meet.example",
20 InstanceKey: "calls-test-instance-key",
21 DEK: base64.StdEncoding.EncodeToString(make([]byte, 32)),
22 Mailer: failingSender{},
23 AllowedAddresses: []string{"paul@example.com"},
24 // The fake's host, as buildForTest names it: rastrillo v0.45
25 // dials a nil list on a transport keymailtest cannot stand under.
26 KeymailServers: []string{keymailHost},
27 }
28}
29
30// relayVector is the base64 CARLOS_RELAY value for one region, using
31// relay's own platform vector key and username suffix (relay_test.go)
32// so a leak of either into a log line is unmistakable.
33func relayVector(t *testing.T) string {
34 t.Helper()
35 const vectorKey = "uMhwW4P/9O5dQUIs91t2CCHGhwJbeiM0xQMQ2G0CJLU="
36 regions := []map[string]any{{
37 "region": "eu-west-1",
38 "urls": []string{"stun:relay.eu-west-1.oncarlos.com:3478"},
39 "key": vectorKey,
40 "username_suffix": "abc:meet:0123456789abcdef:k1",
41 }}
42 b, err := json.Marshal(regions)
43 if err != nil {
44 t.Fatal(err)
45 }
46 return base64.StdEncoding.EncodeToString(b)
47}
48
49func TestAMalformedRelayRefusesToStartOutsideDevelopment(t *testing.T) {
50 cfg := minimalConfig(t)
51 cfg.Env = "production"
52 cfg.Relay = "%%%not-base64"
53 _, err := resolve(cfg)
54 if err == nil || !strings.Contains(err.Error(), "CARLOS_RELAY") {
55 t.Fatalf("want a refusal naming the variable, got %v", err)
56 }
57 // Not just any refusal that happens to name the variable: this must
58 // be config.go's own wording, so a case where relay.Parse's error
59 // text alone satisfied the assertion above (and resolve silently
60 // stopped wrapping it) would still be caught.
61 if !strings.Contains(err.Error(), "is present but malformed") {
62 t.Fatalf("want config.go's own refusal wording, got %v", err)
63 }
64 if strings.Contains(err.Error(), "%%%") {
65 t.Fatal("the refusal echoes the value")
66 }
67}
68
69// TestAMalformedRelayBuiltFromTheVectorNeverLeaksIt is the config-layer
70// regression test for relay's Task 1 item 1: element 0's "no urls"
71// error used to embed the region field verbatim, so a raw value whose
72// region field held the platform vector key leaked it straight into
73// resolve's refusal. The key and the username_suffix's incarnation
74// segment are planted throughout the one region below so any of the
75// three fields' checks reintroducing an echo trips this test.
76func TestAMalformedRelayBuiltFromTheVectorNeverLeaksIt(t *testing.T) {
77 const vectorKey = "uMhwW4P/9O5dQUIs91t2CCHGhwJbeiM0xQMQ2G0CJLU="
78 cfg := minimalConfig(t)
79 cfg.Env = "production"
80 regions := []map[string]any{{
81 "region": vectorKey,
82 "urls": []string{},
83 "key": vectorKey,
84 "username_suffix": "abc:meet:0123456789abcdef:k1",
85 }}
86 b, err := json.Marshal(regions)
87 if err != nil {
88 t.Fatal(err)
89 }
90 cfg.Relay = base64.StdEncoding.EncodeToString(b)
91 _, err = resolve(cfg)
92 if err == nil {
93 t.Fatal("want a refusal")
94 }
95 if strings.Contains(err.Error(), vectorKey) || strings.Contains(err.Error(), "0123456789abcdef") {
96 t.Fatalf("the refusal echoes the vector: %v", err)
97 }
98}
99
100func TestAMalformedRelayIsIgnoredWithAWarningInDevelopment(t *testing.T) {
101 var buf bytes.Buffer
102 cfg := minimalConfig(t)
103 cfg.Env = EnvDevelopment
104 cfg.Logger = slog.New(slog.NewTextHandler(&buf, nil))
105 cfg.Relay = "%%%not-base64"
106 out, err := resolve(cfg)
107 if err != nil {
108 t.Fatal(err)
109 }
110 if out.relay != nil || out.relayConfigured {
111 t.Fatal("a malformed relay must mean no relay in development")
112 }
113 if !strings.Contains(buf.String(), "CARLOS_RELAY") || strings.Contains(buf.String(), "%%%") {
114 t.Fatalf("want one warning naming the variable and not the value, got %q", buf.String())
115 }
116}
117
118func TestAGoodRelayIsParsedAndNeverLogged(t *testing.T) {
119 var buf bytes.Buffer
120 cfg := minimalConfig(t)
121 cfg.Logger = slog.New(slog.NewTextHandler(&buf, nil))
122 cfg.Relay = relayVector(t) // base64 of the one-region contract with the vector key
123 out, err := resolve(cfg)
124 if err != nil {
125 t.Fatal(err)
126 }
127 if len(out.relay) != 1 || !out.relayConfigured {
128 t.Fatalf("parsed len=%d relayConfigured=%v", len(out.relay), out.relayConfigured)
129 }
130 if strings.Contains(buf.String(), "uMhwW4P") || strings.Contains(buf.String(), "0123456789abcdef") {
131 t.Fatal("the key or the suffix reached a log line")
132 }
133}
134
135func TestNoRelayIsTheFloor(t *testing.T) {
136 cfg := minimalConfig(t)
137 cfg.Relay = ""
138 out, err := resolve(cfg)
139 if err != nil {
140 t.Fatal(err)
141 }
142 if out.relay != nil || out.relayConfigured {
143 t.Fatal("absent must mean the floor")
144 }
145}
146