| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Mixed versions (publishing a site at 16): at 16 every publish frame and |
| 14 | // field goes out whole; at 15 a publish dispatch and a publish check are |
| 15 | // refused outright (an older runner is never asked to publish, and an |
| 16 | // older instance never asked to allow it), and a report's publish result |
| 17 | // and a hello's sites capability are dropped, so nothing a protocol 15 |
| 18 | // peer decodes strictly carries them. |
| 19 | func TestEncodeBelowProtocol16DropsPublish(t *testing.T) { |
| 20 | if wire.ProtocolPublish != wire.Protocol16 || wire.ProtocolCurrent != wire.ProtocolDesign { |
| 21 | t.Fatalf("publish %d, current %d", wire.ProtocolPublish, wire.ProtocolCurrent) |
| 22 | } |
| 23 | publish := 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-sample", Into: "main"}} |
| 24 | dispatch, _ := wire.NewFrame("d", wire.FrameDispatch, publish) |
| 25 | check, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s", Attempt: 1, Purpose: wire.LeasePurposePublish, Digest: strings.Repeat("a", 64)}) |
| 26 | result := wire.StepReport{TaskID: "tk", StepID: "s", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}} |
| 27 | built := result |
| 28 | built.Publish = &wire.PublishResult{Phase: wire.PublishBuilt, Digest: strings.Repeat("a", 64), Files: 2, Bytes: 40} |
| 29 | report, _ := wire.NewFrame("r", wire.FrameStep, built) |
| 30 | plainReport, _ := wire.NewFrame("r", wire.FrameStep, result) |
| 31 | hello := wire.Hello{Protocol: wire.Protocol16, Repos: []string{}, Capabilities: &wire.Capabilities{Sites: true, Tags: []string{wire.TagLinux}}} |
| 32 | helloFrame, _ := wire.NewFrame("h", wire.FrameHello, hello) |
| 33 | |
| 34 | for _, f := range []wire.Frame{report, helloFrame} { |
| 35 | if same, err := wire.Encode(f, wire.Protocol16); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 36 | t.Fatalf("%s changed at 16: %v", f.Type, err) |
| 37 | } |
| 38 | } |
| 39 | // Since places (31) every publish dispatch and check is bound to a |
| 40 | // place, which an older peer would ignore: both are refused below 31. |
| 41 | for _, f := range []wire.Frame{dispatch, check} { |
| 42 | for _, p := range []int{wire.Protocol30, wire.Protocol16} { |
| 43 | if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { |
| 44 | t.Fatalf("a publish %s encoded at %d: %v", f.Type, p, err) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | for _, f := range []wire.Frame{dispatch, check} { |
| 49 | for _, p := range []int{wire.Protocol15, wire.Protocol14, wire.Protocol9, wire.Protocol1} { |
| 50 | if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { |
| 51 | t.Fatalf("%s %s encoded at %d: %v", f.Type, f.Data, p, err) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | got, err := wire.Encode(report, wire.Protocol15) |
| 56 | if err != nil || !bytes.Equal(got.Data, plainReport.Data) { |
| 57 | t.Fatalf("report at 15: %v\n got %s\nwant %s", err, got.Data, plainReport.Data) |
| 58 | } |
| 59 | got, err = wire.Encode(helloFrame, wire.Protocol15) |
| 60 | if err != nil || strings.Contains(string(got.Data), `"sites"`) { |
| 61 | t.Fatalf("hello at 15 keeps sites: %v %s", err, got.Data) |
| 62 | } |
| 63 | if !hello.Capabilities.Sites { |
| 64 | t.Fatal("encoding changed the caller's hello") |
| 65 | } |
| 66 | // A non-publish dispatch never carries a target to an older runner. |
| 67 | stray := publish |
| 68 | stray.Specialist = wire.SpecialistReview |
| 69 | strayFrame, _ := wire.NewFrame("d", wire.FrameDispatch, stray) |
| 70 | got, err = wire.Encode(strayFrame, wire.Protocol15) |
| 71 | var back map[string]json.RawMessage |
| 72 | if err != nil || json.Unmarshal(got.Data, &back) != nil || back["publish"] != nil { |
| 73 | t.Fatalf("a review dispatch kept its publish target at 15: %v %s", err, got.Data) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // ValidSiteApp: site- and a slug of lower-case letters and digits in |
| 78 | // hyphen-separated runs, at most Carloku's 32 characters; nothing that |
| 79 | // could read as a flag, a path, another account or a second argument. |
| 80 | func TestValidSiteApp(t *testing.T) { |
| 81 | for _, ok := range []string{"site-sample", "site-a", "site-quick-site-2", "site-" + strings.Repeat("a", 27)} { |
| 82 | if !wire.ValidSiteApp(ok) { |
| 83 | t.Fatalf("refused %q", ok) |
| 84 | } |
| 85 | } |
| 86 | for _, bad := range []string{"", "site-", "sample", "tmp-sample", "site--x", "site-x-", "site-X", "site-a b", "site-a\n", "site-a/b", "site-../x", "--help", "site-x --account other", "site-ñ", "site-" + strings.Repeat("a", 28), " site-a", "site-a;rm"} { |
| 87 | if wire.ValidSiteApp(bad) { |
| 88 | t.Fatalf("accepted %q", bad) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |