| 1 | package edgesfile |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestParseReadsTheWireShape(t *testing.T) { |
| 6 | f, err := Parse([]byte(`{"v":1,"box":"annie","eip":"1.1.1.1","region":"eu-west-1",` + |
| 7 | `"generation":"2026-08-27T09:00:00Z","hosts":{"a.test":{"role":"home","platform":true},` + |
| 8 | `"b.test":{"role":"home","alias":true},` + |
| 9 | `"*.c.test":{"role":"wildcard","wildcard":true}}}`)) |
| 10 | if err != nil { |
| 11 | t.Fatalf("parse: %v", err) |
| 12 | } |
| 13 | if f.Box != "annie" || f.EIP != "1.1.1.1" || f.Region != "eu-west-1" { |
| 14 | t.Fatalf("identity fields = %+v", f) |
| 15 | } |
| 16 | if !f.Hosts["a.test"].Platform || f.Hosts["a.test"].Alias { |
| 17 | t.Fatalf("a.test = %+v, want platform-class route", f.Hosts["a.test"]) |
| 18 | } |
| 19 | if !f.Hosts["b.test"].Alias || f.Hosts["b.test"].Platform { |
| 20 | t.Fatalf("b.test = %+v, want a claim-derived alias", f.Hosts["b.test"]) |
| 21 | } |
| 22 | // A delegated wildcard parent (#305) decodes with its mark and its own |
| 23 | // role. The reader must carry the role through verbatim rather than |
| 24 | // normalising it: "wildcard" is the cert half alone, and a reader that |
| 25 | // silently read it as RoleHome would dial a box that does not serve the |
| 26 | // catch-all. |
| 27 | if w := f.Hosts["*.c.test"]; !w.Wildcard || w.Role != "wildcard" { |
| 28 | t.Fatalf("*.c.test = %+v, want a cert-only delegated parent", w) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // V < 1 (no version field at all) is not a file this reader understands; a |
| 33 | // HIGHER version is accepted, because the schema grows additively and a newer |
| 34 | // box's file still carries box/eip/hosts verbatim. Mirrors the contract |
| 35 | // internal/edge/homemap.go and internal/steering already hold. |
| 36 | func TestParseRefusesUnversionedAndAcceptsNewer(t *testing.T) { |
| 37 | if _, err := Parse([]byte(`{"box":"annie","eip":"1.1.1.1"}`)); err == nil { |
| 38 | t.Fatal("an unversioned file must be refused") |
| 39 | } |
| 40 | if _, err := Parse([]byte(`{"v":9,"box":"annie","eip":"1.1.1.1"}`)); err != nil { |
| 41 | t.Fatalf("a newer version must still parse: %v", err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestKeyIsThePrefixedBoxName(t *testing.T) { |
| 46 | if got := Key("annie"); got != "control/edges/annie.json" { |
| 47 | t.Fatalf("Key = %q", got) |
| 48 | } |
| 49 | } |
| 50 | |