Download this file
| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // crypto/rand.Text's RFC 4648 alphabet contains neither lowercase c nor _. |
| 11 | // Every persisted legacy card borrows its message's ID; Coordinator cards |
| 12 | // borrow their turn's ID, and native questions have a native: prefix. |
| 13 | func TestLegacyCardIDsCannotUseThinPrefix(t *testing.T) { |
| 14 | const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" |
| 15 | for i := 0; i < 256; i++ { |
| 16 | id := rand.Text() |
| 17 | if strings.HasPrefix(id, "c_") || strings.ContainsAny(id, "c_") { |
| 18 | t.Fatal("legacy ID overlaps thin namespace") |
| 19 | } |
| 20 | for _, r := range id { |
| 21 | if !strings.ContainsRune(alphabet, r) { |
| 22 | t.Fatal("legacy generator alphabet changed") |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | // Pin the actual generators as well, so a generator replacement must |
| 27 | // explicitly revisit the shared-route namespace contract. |
| 28 | for _, file := range []string{"store.go", "coordinator.go"} { |
| 29 | raw, err := os.ReadFile(file) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if !strings.Contains(string(raw), "ID: rand.Text()") { |
| 34 | t.Fatal(file, "changed its legacy ID generator") |
| 35 | } |
| 36 | } |
| 37 | raw, err := os.ReadFile("cards.go") |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if !strings.Contains(string(raw), `ID: "native:" + q.ID`) { |
| 42 | t.Fatal("native question alias prefix changed") |
| 43 | } |
| 44 | } |
| 45 | |