| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // TestEncodeBelowProtocol13DropsResearchResults (slice R3, mixed |
| 15 | // versions): the hello's detected addresses and a step report's Research |
| 16 | // payload are dropped below 13 and nothing else changes; a research_file |
| 17 | // chunk is never spoken there. |
| 18 | func TestEncodeBelowProtocol13DropsResearchResults(t *testing.T) { |
| 19 | hello := wire.Hello{Generation: 3, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}, Protocol: wire.ProtocolResearch, |
| 20 | Capabilities: &wire.Capabilities{Tags: []string{wire.TagLinux}, Research: &wire.ResearchCapability{Addresses: []string{"81.2.69.7", "2a01:4f8::7"}}}} |
| 21 | f, _ := wire.NewFrame("h", wire.FrameHello, hello) |
| 22 | if same, err := wire.Encode(f, wire.ProtocolResearch); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 23 | t.Fatalf("hello changed at 13: %v", err) |
| 24 | } |
| 25 | if !strings.Contains(string(f.Data), `"research":{"addresses":["81.2.69.7","2a01:4f8::7"]}`) { |
| 26 | t.Fatalf("the addresses on the wire: %s", f.Data) |
| 27 | } |
| 28 | old, err := wire.Encode(f, wire.ProtocolPreviewHost) |
| 29 | if err != nil || strings.Contains(string(old.Data), "research") || strings.Contains(string(old.Data), "81.2.69.7") { |
| 30 | t.Fatalf("hello at 11: %s %v", old.Data, err) |
| 31 | } |
| 32 | hello.Capabilities.Research = nil |
| 33 | plain, _ := wire.NewFrame("h", wire.FrameHello, hello) |
| 34 | if !bytes.Equal(old.Data, plain.Data) { |
| 35 | t.Fatalf("more than the addresses changed:\n%s\n%s", old.Data, plain.Data) |
| 36 | } |
| 37 | |
| 38 | rep := wire.StepReport{TaskID: "tk1", StepID: "s_1", Attempt: 1, State: wire.StepDone, Termination: true, |
| 39 | Research: &wire.ResearchResult{Findings: json.RawMessage(`{"summary":"s","facts":[],"images":[],"gaps":[]}`), Fetches: []wire.ResearchFetch{{SourceID: "s1", Origin: wire.ResearchOriginPerson, URL: "https://shop.example.com/", Status: 200}}, Images: []wire.ResearchImage{}}} |
| 40 | g, _ := wire.NewFrame("r", wire.FrameStep, rep) |
| 41 | if same, err := wire.Encode(g, wire.ProtocolResearch); err != nil || !bytes.Equal(same.Data, g.Data) { |
| 42 | t.Fatalf("report changed at 13: %v", err) |
| 43 | } |
| 44 | got, err := wire.Encode(g, wire.ProtocolPreviewHost) |
| 45 | rep.Research = nil |
| 46 | bare, _ := wire.NewFrame("r", wire.FrameStep, rep) |
| 47 | if err != nil || !bytes.Equal(got.Data, bare.Data) { |
| 48 | t.Fatalf("report at 11: %s %v", got.Data, err) |
| 49 | } |
| 50 | |
| 51 | chunk, _ := wire.NewFrame("f", wire.FrameResearchFile, wire.ResearchFile{TaskID: "tk1", StepID: "s_1", Attempt: 1, ImageID: "img1", Index: 0, Total: 1, Size: 3, SHA256: strings.Repeat("a", 64), Data: "YWJj"}) |
| 52 | if !chunk.Durable() { |
| 53 | t.Fatal("research_file is not durable") |
| 54 | } |
| 55 | if same, err := wire.Encode(chunk, wire.ProtocolResearch); err != nil || !bytes.Equal(same.Data, chunk.Data) { |
| 56 | t.Fatalf("research_file changed at 13: %v", err) |
| 57 | } |
| 58 | for _, p := range []int{wire.ProtocolMetered, wire.ProtocolPreviewHost, wire.ProtocolPreview, wire.Protocol2} { |
| 59 | if _, err := wire.Encode(chunk, p); !errors.Is(err, wire.ErrProtocol) { |
| 60 | t.Fatalf("research_file at %d: %v", p, err) |
| 61 | } |
| 62 | } |
| 63 | if _, body, err := wire.ParseFrame(mustJSON(t, chunk)); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } else if _, ok := body.(*wire.ResearchFile); !ok { |
| 66 | t.Fatalf("decoded as %T", body) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func mustJSON(t *testing.T, v any) []byte { |
| 71 | t.Helper() |
| 72 | raw, err := json.Marshal(v) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | return raw |
| 77 | } |
| 78 | |
| 79 | // TestResearchPrefixes: Confirm records the address itself, and an IPv6 |
| 80 | // address's /64 too; nothing for an address that is not a site address. |
| 81 | func TestResearchPrefixes(t *testing.T) { |
| 82 | for addr, want := range map[string][]string{ |
| 83 | "81.2.69.7": {"81.2.69.7/32"}, |
| 84 | "2a01:4f8:1:2::7": {"2a01:4f8:1:2::7/128", "2a01:4f8:1:2::/64"}, |
| 85 | "10.0.0.1": nil, |
| 86 | "127.0.0.1": nil, |
| 87 | "fe80::1": nil, |
| 88 | "::ffff:81.2.69.7": nil, |
| 89 | "81.2.69.007": nil, |
| 90 | "2A01:4F8::7": nil, |
| 91 | "not an address": nil, |
| 92 | "81.2.69.7/32": nil, |
| 93 | "[2a01:4f8::7]:443": nil, |
| 94 | "2a01:4f8::7%eth0": nil, |
| 95 | "": nil, |
| 96 | "0.0.0.0": nil, |
| 97 | "255.255.255.255": nil, |
| 98 | "ff02::1": nil, |
| 99 | "192.168.1.1": nil, |
| 100 | "fd00::1": nil, |
| 101 | "169.254.169.254": nil, |
| 102 | "2a01:4f8:ffff::abcd": {"2a01:4f8:ffff::abcd/128", "2a01:4f8:ffff::/64"}, |
| 103 | // Not public egress (Astra security finding 2): the tailnet and |
| 104 | // CGNAT, documentation, benchmark and transition ranges. |
| 105 | "100.100.1.2": nil, |
| 106 | "100.64.0.1": nil, |
| 107 | "203.0.113.7": nil, |
| 108 | "198.18.0.1": nil, |
| 109 | "2001:db8::7": nil, |
| 110 | "2002:a00:1::1": nil, |
| 111 | "fd7a:115c::1": nil, |
| 112 | "192.0.0.9": nil, |
| 113 | } { |
| 114 | if got := wire.ResearchPrefixes(addr); !slices.Equal(got, want) { |
| 115 | t.Errorf("%q: %v, want %v", addr, got, want) |
| 116 | } |
| 117 | } |
| 118 | if !wire.ValidSHA256(strings.Repeat("0", 64)) || wire.ValidSHA256(strings.Repeat("A", 64)) || wire.ValidSHA256("abc") { |
| 119 | t.Fatal("ValidSHA256") |
| 120 | } |
| 121 | if f := wire.NoFindings(); f.Summary != "" || len(f.Facts) != 0 || f.Facts == nil || f.Images == nil || !slices.Equal(f.Gaps, []string{"Research ended without findings"}) { |
| 122 | t.Fatalf("NoFindings %+v", f) |
| 123 | } |
| 124 | if raw, _ := json.Marshal(wire.NoFindings()); string(raw) != `{"summary":"","facts":[],"images":[],"gaps":["Research ended without findings"]}` { |
| 125 | t.Fatalf("no findings on the wire: %s", raw) |
| 126 | } |
| 127 | } |
| 128 | |