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