| 1 | package homeop |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 10 | ) |
| 11 | |
| 12 | func TestSignRequestRoundTrip(t *testing.T) { |
| 13 | k, _ := family.Generate() |
| 14 | a := Authorization{Role: Directory, Origin: "https://oficina.works", Installation: Token(), Registry: Token(), Device: Token(), Sequence: 2, Previous: Token(), JournalSequence: 7, OperationID: Token(), Action: DirectoryProvision, Issued: 100, Expires: 400} |
| 15 | req, err := SignRequest(k, a, ProvisionInput{InputBase: InputBase{1, DirectoryProvision, a.OperationID}, Generation: Token(), TeamSQID: "abc", App: "docs", Consumer: "x-docs", Audience: "https://x.docs.oficina.works"}) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | back, err := ReadRequest(req.Bytes) |
| 20 | if err != nil || back.Head != req.Head || back.Authorization != req.Authorization { |
| 21 | t.Fatalf("round trip: %v %+v", err, back) |
| 22 | } |
| 23 | if back.Authorization.Initial { |
| 24 | t.Fatal("sequence 2 is not initial") |
| 25 | } |
| 26 | // Canonical: re-encoding the decoded wrapper gives the same bytes. |
| 27 | var w wrapper |
| 28 | if Decode(req.Bytes, &w) != nil { |
| 29 | t.Fatal("decode") |
| 30 | } |
| 31 | re, _ := Encode(w) |
| 32 | if !bytes.Equal(re, req.Bytes) { |
| 33 | t.Fatal("not canonical") |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestSignRequestRefusesWhatHomeRefuses(t *testing.T) { |
| 38 | k, _ := family.Generate() |
| 39 | good := Authorization{Role: Credential, Origin: "https://id.oficina.works", Installation: Token(), Registry: Token(), Device: Token(), Sequence: 1, JournalSequence: 1, OperationID: Token(), Action: CredentialOwnerSeed, Issued: 100, Expires: 400} |
| 40 | for name, mutate := range map[string]func(*Authorization){ |
| 41 | "life over 300 s": func(a *Authorization) { a.Expires = a.Issued + 301 }, |
| 42 | "verb of other role": func(a *Authorization) { a.Action = DirectorySeedTeam }, |
| 43 | "previous at seq 1": func(a *Authorization) { a.Previous = Token() }, |
| 44 | "no previous at seq2": func(a *Authorization) { a.Sequence = 2 }, |
| 45 | "journal zero": func(a *Authorization) { a.JournalSequence = 0 }, |
| 46 | "origin with path": func(a *Authorization) { a.Origin = "https://id.oficina.works/x" }, |
| 47 | } { |
| 48 | a := good |
| 49 | mutate(&a) |
| 50 | if _, err := SignRequest(k, a, InputBase{1, a.Action, a.OperationID}); err == nil { |
| 51 | t.Errorf("%s: signed", name) |
| 52 | } |
| 53 | } |
| 54 | if _, err := SignRequest(k, good, InputBase{1, good.Action, good.OperationID}); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestPlanCarriesEmptyAppsForInitialize(t *testing.T) { |
| 60 | k, _ := family.Generate() |
| 61 | raw, err := SignPlan(k, Plan{Role: Directory, Origin: "https://oficina.works", Installation: Token(), Registry: Token(), Device: Token(), PlanID: Token(), Kind: KindInitialize, Issued: 1, Expires: 900}) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | p, _ := EnvelopePayload(raw) |
| 66 | if !strings.Contains(string(p), `"apps":[]`) || strings.Contains(string(p), "label") || strings.Contains(string(p), "owner") { |
| 67 | t.Fatalf("plan payload %s", p) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestPlanPrefix(t *testing.T) { |
| 72 | id := base64.RawURLEncoding.EncodeToString(append([]byte{0xde, 0xad, 0xbe, 0xef}, make([]byte, 28)...)) |
| 73 | if got := PlanPrefix(id); got != "deadbeef" { |
| 74 | t.Fatal(got) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestManifestDigestIsOrderIndependent(t *testing.T) { |
| 79 | a := ManifestDigest(map[string]string{"docs": "https://docs.oficina.works", "alta": "https://alta.oficina.works"}) |
| 80 | b := ManifestDigest(map[string]string{"alta": "https://alta.oficina.works", "docs": "https://docs.oficina.works"}) |
| 81 | if a != b || !CanonicalToken(a) { |
| 82 | t.Fatal(a, b) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Home's Plan puts existing_team after owner and before issued_at, and |
| 87 | // omits it when empty (internal/operator/plan.go). Home re-encodes what it |
| 88 | // decodes and compares bytes, so the position is part of the contract. |
| 89 | func TestPlanExistingTeamEncodesLikeHome(t *testing.T) { |
| 90 | k, _ := family.Generate() |
| 91 | plan := Plan{Role: Directory, Origin: "https://oficina.works", Installation: Token(), Registry: Token(), Device: Token(), PlanID: Token(), Kind: KindCreateTeam, Apps: []string{"docs"}, Label: "oficina", DisplayName: "Oficina", ExistingTeam: "Uk", Issued: 1, Expires: 900} |
| 92 | raw, err := SignPlan(k, plan) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | p, _ := EnvelopePayload(raw) |
| 97 | if !strings.Contains(string(p), `"display_name":"Oficina","existing_team":"Uk","issued_at"`) { |
| 98 | t.Fatalf("plan payload %s", p) |
| 99 | } |
| 100 | // The credential role's plan carries an owner instead: still after it. |
| 101 | owned := Plan{Role: Credential, Origin: "https://id.oficina.works", Installation: Token(), Registry: Token(), Device: Token(), PlanID: Token(), Kind: KindCreateTeam, Apps: []string{"docs"}, Owner: "o@x.test", ExistingTeam: "Uk", Issued: 1, Expires: 900} |
| 102 | raw, _ = SignPlan(k, owned) |
| 103 | p, _ = EnvelopePayload(raw) |
| 104 | if !strings.Contains(string(p), `"owner":"o@x.test","existing_team":"Uk","issued_at"`) { |
| 105 | t.Fatalf("credential plan payload %s", p) |
| 106 | } |
| 107 | plan.ExistingTeam = "" |
| 108 | raw, _ = SignPlan(k, plan) |
| 109 | p, _ = EnvelopePayload(raw) |
| 110 | if strings.Contains(string(p), "existing_team") { |
| 111 | t.Fatalf("empty existing_team encoded: %s", p) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestCanonicalOrigin(t *testing.T) { |
| 116 | for _, app := range []string{"docs", "sheets", "slides", "memoria", "calendar", "calls"} { |
| 117 | if got := CanonicalOrigin(app); got != "https://"+app+".oficina.works" { |
| 118 | t.Errorf("%s: %q", app, got) |
| 119 | } |
| 120 | } |
| 121 | for _, app := range []string{"", "alta", "fichas", "Docs", "docs.oficina.works"} { |
| 122 | if got := CanonicalOrigin(app); got != "" { |
| 123 | t.Errorf("%q: %q", app, got) |
| 124 | } |
| 125 | } |
| 126 | if ExistingActivationPolicy != "oficina/existing-app-activation/v1" || ExistingActivationPolicy == ActivationPolicy { |
| 127 | t.Fatal(ExistingActivationPolicy) |
| 128 | } |
| 129 | } |
| 130 | |