oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package console
2
3import (
4 "crypto/ecdh"
5 "crypto/rand"
6 "encoding/base64"
7 "strings"
8 "testing"
9)
10
11func TestSealOpensForItsBindingOnly(t *testing.T) {
12 priv, _ := ecdh.P256().GenerateKey(rand.Reader)
13 pub := base64.StdEncoding.EncodeToString(priv.PublicKey().Bytes())
14 v1, err := Seal(pub, "", "docs", []byte("dek"))
15 if err != nil {
16 t.Fatal(err)
17 }
18 if got, err := Open(priv, "anything", "else", v1); err != nil || string(got) != "dek" {
19 t.Fatalf("v1: %q %v", got, err)
20 }
21 v2, err := Seal(pub, "bds", "docs", []byte("dek"))
22 if err != nil {
23 t.Fatal(err)
24 }
25 raw, _ := base64.StdEncoding.DecodeString(v2)
26 if !strings.HasPrefix(string(raw), "CSB2") || len(raw) != 4+65+12+3+16 {
27 t.Fatalf("v2 layout: %d bytes", len(raw))
28 }
29 if got, err := Open(priv, "bds", "docs", v2); err != nil || string(got) != "dek" {
30 t.Fatalf("v2: %q %v", got, err)
31 }
32 if _, err := Open(priv, "bds", "sheets", v2); err == nil {
33 t.Fatal("a v2 envelope opened for another app")
34 }
35 // A v2 envelope with its prefix cut off is not a valid v1 one.
36 if _, err := Open(priv, "", "", base64.StdEncoding.EncodeToString(raw[4:])); err == nil {
37 t.Fatal("downgrade opened")
38 }
39 if _, err := Seal(pub, "", "docs", nil); err == nil {
40 t.Fatal("sealed an empty secret")
41 }
42}
43
44func TestPlatformManaged(t *testing.T) {
45 for _, k := range []string{"CARLOS_SMTP_PASS", "DOCS_SMTP_FROM", "CARLOS_MAIL_FROM", "DOCS_MAIL_FROM", "CARLOS_MAIL", "CARLOS_RELAY", "CARLOS_MAIL_EVENTS_KEY", "SLIDES_SCAN_CALLBACK_SECRET", "CARLOS_STORE_ACCESS_KEY"} {
46 if !PlatformManaged(k) {
47 t.Errorf("%s not platform-managed", k)
48 }
49 }
50 for _, k := range []string{"DOCS_ORIGIN", "SMTP_HOSTNAME", "CALLS_DEK", "DOCS_SMTP", "MEMORIA_OWNERS", "CARLOS_MAILER", "MAIL_FROM"} {
51 if PlatformManaged(k) {
52 t.Errorf("%s platform-managed", k)
53 }
54 }
55}
56