| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | func placesHello() wire.Hello { |
| 13 | return wire.Hello{Protocol: wire.ProtocolCurrent, Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Capabilities: &wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagLinux}, |
| 14 | Tools: &wire.ToolHealth{Scanner: wire.ToolHealthy, Carlos: wire.CarlosOK, CarlosVersion: "v0.43.3"}, |
| 15 | Reach: &wire.Reach{Logins: []wire.ReachLogin{{Console: wire.FlagshipConsole, Accounts: []wire.ReachAccount{{Name: "paulca", Sqid: "kmnpq"}}}}, Deploys: []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "kmnpq", App: "site-nearys-k3x9qa", Name: "eq-nearys-k3x9qa-ab12", Fingerprint: "0123456789abcdef"}, {Console: wire.FlagshipConsole, Account: "kmnpq", App: "site-nearys-k3x9qa", Name: "eq-nearys-k3x9qa-zz00", Lost: true}}}}} |
| 16 | } |
| 17 | |
| 18 | // Protocol 31 (places): at 31 a hello's reach and carlos health, a |
| 19 | // place-bound publish dispatch, the publish and mint checks and the place |
| 20 | // tools go out whole. Below it the hello's fields are dropped, and a |
| 21 | // publish dispatch, a publish or mint check and a place tool's call are |
| 22 | // refused: an older runner would deploy to its own fixed account, an |
| 23 | // older instance would decide a check without the destination. |
| 24 | func TestEncodeBelowProtocol31DropsPlaces(t *testing.T) { |
| 25 | if wire.ProtocolPlaces != wire.Protocol34 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 { |
| 26 | t.Fatalf("places %d, current %d", wire.ProtocolPlaces, wire.ProtocolCurrent) |
| 27 | } |
| 28 | hello := placesHello() |
| 29 | helloFrame, _ := wire.NewFrame("h", wire.FrameHello, hello) |
| 30 | dispatch, _ := wire.NewFrame("d", wire.FrameDispatch, wire.DispatchCommand{Person: "p", TaskID: "tk", StepID: "s", Attempt: 1, Specialist: wire.SpecialistPublish, Findings: []wire.Finding{}, CandidateSHA: strings.Repeat("e", 40), Publish: &wire.PublishDispatch{App: "site-nearys-k3x9qa", Into: "main", Console: wire.FlagshipConsole, Account: "kmnpq", AccountName: "paulca", Place: "pl_1", PlaceRevision: 1, BindingRevision: 1, Destination: wire.SiteDestination(wire.FlagshipConsole, "kmnpq", "paulca", "site-nearys-k3x9qa"), Mode: wire.SiteSetup, DeployName: "eq-nearys-k3x9qa-ab12"}}) |
| 31 | check, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s", Attempt: 1, Purpose: wire.LeasePurposePublish, Digest: strings.Repeat("a", 64), Destination: strings.Repeat("b", 64)}) |
| 32 | mint, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s", Attempt: 1, Purpose: wire.LeasePurposeMint, Destination: strings.Repeat("b", 64)}) |
| 33 | var calls []wire.Frame |
| 34 | for _, name := range wire.PlacesTools { |
| 35 | f, _ := wire.NewFrame("t", wire.FrameToolCall, wire.ToolCall{TurnID: "t", CallID: "c", Name: name, Args: []byte(`{}`)}) |
| 36 | calls = append(calls, f) |
| 37 | } |
| 38 | for _, f := range append([]wire.Frame{helloFrame, dispatch, check, mint}, calls...) { |
| 39 | if same, err := wire.Encode(f, wire.Protocol34); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 40 | t.Fatalf("%s changed at 31: %v", f.Type, err) |
| 41 | } |
| 42 | } |
| 43 | plainCheck, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s", Attempt: 1, Purpose: wire.LeasePurposePublish, Digest: strings.Repeat("a", 64)}) |
| 44 | for _, f := range append([]wire.Frame{dispatch, check, plainCheck, mint}, calls...) { |
| 45 | if _, err := wire.Encode(f, wire.Protocol33); !errors.Is(err, wire.ErrProtocol) { |
| 46 | t.Fatalf("%s %s encoded at 30: %v", f.Type, f.Data, err) |
| 47 | } |
| 48 | } |
| 49 | got, err := wire.Encode(helloFrame, wire.Protocol33) |
| 50 | if err != nil || strings.Contains(string(got.Data), `"reach"`) || strings.Contains(string(got.Data), `"carlos`) || !strings.Contains(string(got.Data), `"scanner":"ok"`) { |
| 51 | t.Fatalf("hello at 30: %v %s", err, got.Data) |
| 52 | } |
| 53 | if hello.Capabilities.Reach == nil || hello.Capabilities.Tools.Carlos != wire.CarlosOK { |
| 54 | t.Fatal("encoding changed the caller's hello") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // A runner's reach is bounded and validated like every capability: host |
| 59 | // names, account names and sqids of their shapes, nothing twice, and a |
| 60 | // carlos health that is a fixed state with a version exactly when one was |
| 61 | // read; ok only at or above the minimum. |
| 62 | func TestValidReachAndCarlosHealth(t *testing.T) { |
| 63 | good := placesHello().Capabilities |
| 64 | if err := wire.ValidCapabilities(*good); err != nil { |
| 65 | t.Fatalf("refused %v", err) |
| 66 | } |
| 67 | mutate := func(f func(c *wire.Capabilities)) wire.Capabilities { |
| 68 | c := *placesHello().Capabilities |
| 69 | r := *c.Reach |
| 70 | r.Logins = append([]wire.ReachLogin{}, r.Logins...) |
| 71 | r.Logins[0].Accounts = append([]wire.ReachAccount{}, r.Logins[0].Accounts...) |
| 72 | r.Deploys = append([]wire.ReachDeploy{}, r.Deploys...) |
| 73 | c.Reach = &r |
| 74 | tools := *c.Tools |
| 75 | c.Tools = &tools |
| 76 | f(&c) |
| 77 | return c |
| 78 | } |
| 79 | for name, bad := range map[string]wire.Capabilities{ |
| 80 | "origin not host": mutate(func(c *wire.Capabilities) { c.Reach.Logins[0].Console = "https:" }), |
| 81 | "host with port": mutate(func(c *wire.Capabilities) { c.Reach.Logins[0].Console = "console.carloku.com:8443" }), |
| 82 | "bad sqid": mutate(func(c *wire.Capabilities) { c.Reach.Logins[0].Accounts[0].Sqid = "abcio" }), |
| 83 | "bad name": mutate(func(c *wire.Capabilities) { c.Reach.Logins[0].Accounts[0].Name = "-x" }), |
| 84 | "account twice": mutate(func(c *wire.Capabilities) { |
| 85 | c.Reach.Logins[0].Accounts = append(c.Reach.Logins[0].Accounts, c.Reach.Logins[0].Accounts[0]) |
| 86 | }), |
| 87 | "console twice": mutate(func(c *wire.Capabilities) { c.Reach.Logins = append(c.Reach.Logins, c.Reach.Logins[0]) }), |
| 88 | "deploy twice": mutate(func(c *wire.Capabilities) { c.Reach.Deploys = append(c.Reach.Deploys, c.Reach.Deploys[0]) }), |
| 89 | "deploy not site": mutate(func(c *wire.Capabilities) { c.Reach.Deploys[0].App = "tmp-x" }), |
| 90 | "held, no print": mutate(func(c *wire.Capabilities) { c.Reach.Deploys[0].Fingerprint = "" }), |
| 91 | "lost with print": mutate(func(c *wire.Capabilities) { c.Reach.Deploys[1].Fingerprint = "0123456789abcdef" }), |
| 92 | "bad deploy name": mutate(func(c *wire.Capabilities) { c.Reach.Deploys[0].Name = "Eq" }), |
| 93 | "too many logins": mutate(func(c *wire.Capabilities) { c.Reach.Logins = manyLogins(wire.MaxReachLogins + 1) }), |
| 94 | "ok but old": mutate(func(c *wire.Capabilities) { c.Tools.CarlosVersion = "v0.32.2" }), |
| 95 | "ok, no version": mutate(func(c *wire.Capabilities) { c.Tools.CarlosVersion = "" }), |
| 96 | "missing, version": mutate(func(c *wire.Capabilities) { c.Tools.Carlos, c.Tools.CarlosVersion = wire.CarlosMissing, "v0.43.3" }), |
| 97 | "unknown state": mutate(func(c *wire.Capabilities) { c.Tools.Carlos = "fine" }), |
| 98 | "raw version": mutate(func(c *wire.Capabilities) { |
| 99 | c.Tools.Carlos, c.Tools.CarlosVersion = wire.CarlosOutdated, "carlos v0.32.2" |
| 100 | }), |
| 101 | } { |
| 102 | if wire.ValidCapabilities(bad) == nil { |
| 103 | t.Errorf("%s accepted", name) |
| 104 | } |
| 105 | } |
| 106 | for name, ok := range map[string]wire.Capabilities{ |
| 107 | "minimum": mutate(func(c *wire.Capabilities) { c.Tools.CarlosVersion = wire.MinCarlos }), |
| 108 | "outdated": mutate(func(c *wire.Capabilities) { c.Tools.Carlos, c.Tools.CarlosVersion = wire.CarlosOutdated, "v0.32.2" }), |
| 109 | "unreadable": mutate(func(c *wire.Capabilities) { c.Tools.Carlos, c.Tools.CarlosVersion = wire.CarlosOutdated, "" }), |
| 110 | "keymail name": mutate(func(c *wire.Capabilities) { c.Reach.Logins[0].Accounts[0].Name = "paul@keymail.dev" }), |
| 111 | "most logins": mutate(func(c *wire.Capabilities) { c.Reach.Logins = manyLogins(wire.MaxReachLogins) }), |
| 112 | "no carlos at all": mutate(func(c *wire.Capabilities) { c.Tools.Carlos, c.Tools.CarlosVersion = "", "" }), |
| 113 | } { |
| 114 | if err := wire.ValidCapabilities(ok); err != nil { |
| 115 | t.Errorf("%s refused: %v", name, err) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func manyLogins(n int) []wire.ReachLogin { |
| 121 | out := make([]wire.ReachLogin, n) |
| 122 | for i := range out { |
| 123 | out[i] = wire.ReachLogin{Console: "console" + string(rune('a'+i)) + ".example.com", Accounts: []wire.ReachAccount{}} |
| 124 | } |
| 125 | return out |
| 126 | } |
| 127 | |
| 128 | // The ids and hashes code derives: stable, distinct per input, in their |
| 129 | // fixed shapes. |
| 130 | func TestPlaceIdentities(t *testing.T) { |
| 131 | a, b := wire.CandidateID(wire.FlagshipConsole, "kmnpq"), wire.CandidateID(wire.TitoConsole, "kmnpq") |
| 132 | if !wire.ValidCandidateID(a) || a == b || a != wire.CandidateID(wire.FlagshipConsole, "kmnpq") { |
| 133 | t.Fatalf("candidate ids %q %q", a, b) |
| 134 | } |
| 135 | d := wire.SiteDestination(wire.FlagshipConsole, "kmnpq", "paulca", "site-nearys-k3x9qa") |
| 136 | for _, other := range []string{wire.SiteDestination(wire.TitoConsole, "kmnpq", "paulca", "site-nearys-k3x9qa"), wire.SiteDestination(wire.FlagshipConsole, "kmnpr", "paulca", "site-nearys-k3x9qa"), wire.SiteDestination(wire.FlagshipConsole, "kmnpq", "oficina", "site-nearys-k3x9qa"), wire.SiteDestination(wire.FlagshipConsole, "kmnpq", "paulca", "site-nearys-k3x9qb")} { |
| 137 | if other == d { |
| 138 | t.Fatal("a destination hash ignores a part") |
| 139 | } |
| 140 | } |
| 141 | if !wire.ValidDestination(d) || wire.ValidDestination(d[:63]) { |
| 142 | t.Fatal("destination shape") |
| 143 | } |
| 144 | for v, want := range map[string]bool{"v0.43.0": true, "v0.43.3": true, "v1.0.0": true, "v0.42.9": false, "v0.32.2": false, "0.43.3": false, "v0.43": false, "": false} { |
| 145 | if wire.CarlosAtLeast(v, wire.MinCarlos) != want { |
| 146 | t.Errorf("CarlosAtLeast(%q) != %t", v, want) |
| 147 | } |
| 148 | } |
| 149 | if wire.ConsoleLabel(wire.FlagshipConsole) != "Carloku" || wire.ConsoleLabel(wire.TitoConsole) != "Tito" || wire.ConsoleLabel("console.example.com") != "console.example.com" { |
| 150 | t.Fatal("console labels") |
| 151 | } |
| 152 | if !wire.ValidDeployName("eq-nearys-k3x9qa-ab12") || wire.ValidDeployName("eq x") || wire.ValidDeployName(strings.Repeat("a", 33)) { |
| 153 | t.Fatal("deploy name shape") |
| 154 | } |
| 155 | } |
| 156 | |