| 1 | package fonts |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/slides/internal/slides/fonts/fontstest" |
| 9 | ) |
| 10 | |
| 11 | // Uploaded fonts reach Parse from the object store, so every count in |
| 12 | // a file is somebody else's number. These are the ones that drove a |
| 13 | // loop or an allocation past what the table's own bytes hold. |
| 14 | |
| 15 | // parseWithin parses data and fails the test if that takes longer than |
| 16 | // d: a parse that does not finish is the bug, whatever it returns. |
| 17 | func parseWithin(t *testing.T, data []byte, d time.Duration) error { |
| 18 | t.Helper() |
| 19 | done := make(chan error, 1) |
| 20 | go func() { |
| 21 | _, err := Parse(data) |
| 22 | done <- err |
| 23 | }() |
| 24 | select { |
| 25 | case err := <-done: |
| 26 | return err |
| 27 | case <-time.After(d): |
| 28 | t.Fatalf("Parse did not return within %v", d) |
| 29 | return nil |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // cmapWith is a cmap table with one Unicode subtable, sub, under |
| 34 | // platform/encoding. |
| 35 | func cmapWith(platform, encoding uint16, sub []byte) []byte { |
| 36 | head := make([]byte, 12) |
| 37 | binary.BigEndian.PutUint16(head[2:], 1) |
| 38 | binary.BigEndian.PutUint16(head[4:], platform) |
| 39 | binary.BigEndian.PutUint16(head[6:], encoding) |
| 40 | binary.BigEndian.PutUint32(head[8:], 12) |
| 41 | return append(head, sub...) |
| 42 | } |
| 43 | |
| 44 | // cmap12 is a format 12 subtable of groups {start, end, glyph}. |
| 45 | func cmap12(groups ...[3]uint32) []byte { |
| 46 | sub := make([]byte, 16+12*len(groups)) |
| 47 | binary.BigEndian.PutUint16(sub, 12) |
| 48 | binary.BigEndian.PutUint32(sub[4:], uint32(len(sub))) |
| 49 | binary.BigEndian.PutUint32(sub[12:], uint32(len(groups))) |
| 50 | for i, g := range groups { |
| 51 | for j, v := range g { |
| 52 | binary.BigEndian.PutUint32(sub[16+i*12+j*4:], v) |
| 53 | } |
| 54 | } |
| 55 | return sub |
| 56 | } |
| 57 | |
| 58 | // cmap4 is a format 4 subtable of segments {start, end}, each mapped |
| 59 | // by delta 0 and no range offset. |
| 60 | func cmap4(segs ...[2]uint16) []byte { |
| 61 | n := len(segs) |
| 62 | sub := make([]byte, 16+8*n) |
| 63 | binary.BigEndian.PutUint16(sub, 4) |
| 64 | binary.BigEndian.PutUint16(sub[2:], uint16(min(len(sub), 0xffff))) |
| 65 | binary.BigEndian.PutUint16(sub[6:], uint16(2*n)) |
| 66 | for i, s := range segs { |
| 67 | binary.BigEndian.PutUint16(sub[14+i*2:], s[1]) // endCode |
| 68 | binary.BigEndian.PutUint16(sub[16+2*n+i*2:], s[0]) // startCode |
| 69 | } |
| 70 | return sub |
| 71 | } |
| 72 | |
| 73 | func serifBytes() []byte { return Data(Face{Family: Serif}) } |
| 74 | |
| 75 | // A group ending at 0xFFFFFFFF wrapped the 32-bit counter to zero, and |
| 76 | // the loop never ended (Task 7 review). Codepoints past U+10FFFF are |
| 77 | // not Unicode; the font is refused. |
| 78 | func TestACmapGroupPastUnicodeIsRefused(t *testing.T) { |
| 79 | for _, g := range [][3]uint32{ |
| 80 | {0xffffffff, 0xffffffff, 1}, |
| 81 | {0xfffffff0, 0xffffffff, 1}, |
| 82 | {0x10ffff, 0x110000, 1}, |
| 83 | } { |
| 84 | data := fontstest.WithTable(t, serifBytes(), "cmap", cmapWith(3, 10, cmap12(g))) |
| 85 | if err := parseWithin(t, data, 5*time.Second); err == nil { |
| 86 | t.Errorf("group %#x..%#x parsed", g[0], g[1]) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Groups are bounded by the bytes present, but each could still span |
| 92 | // all of Unicode: ten thousand of them was ten billion iterations. |
| 93 | // Real groups are sorted and disjoint, so together they cover Unicode |
| 94 | // at most once; more is refused. |
| 95 | func TestCmapGroupsCoverUnicodeAtMostOnce(t *testing.T) { |
| 96 | groups := make([][3]uint32, 10000) |
| 97 | for i := range groups { |
| 98 | groups[i] = [3]uint32{0, 0x10ffff, 1} |
| 99 | } |
| 100 | data := fontstest.WithTable(t, serifBytes(), "cmap", cmapWith(3, 10, cmap12(groups...))) |
| 101 | if err := parseWithin(t, data, 5*time.Second); err == nil { |
| 102 | t.Error("overlapping groups covering Unicode ten thousand times parsed") |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // The same for format 4: 32,767 segments each spanning the BMP was two |
| 107 | // billion iterations. |
| 108 | func TestCmap4SegmentsCoverTheBMPAtMostOnce(t *testing.T) { |
| 109 | segs := make([][2]uint16, 32767) |
| 110 | for i := range segs { |
| 111 | segs[i] = [2]uint16{0, 0xfffe} |
| 112 | } |
| 113 | data := fontstest.WithTable(t, serifBytes(), "cmap", cmapWith(3, 1, cmap4(segs...))) |
| 114 | if err := parseWithin(t, data, 5*time.Second); err == nil { |
| 115 | t.Error("overlapping segments covering the BMP 32,767 times parsed") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // The twelve built-in faces still parse under the bounds, with every |
| 120 | // rune they mapped before. |
| 121 | func TestTheBoundsAdmitRealFonts(t *testing.T) { |
| 122 | for _, f := range All() { |
| 123 | font, err := Parse(Data(f)) |
| 124 | if err != nil { |
| 125 | t.Fatalf("%s: %v", f.File(), err) |
| 126 | } |
| 127 | if len(font.cmap) < 2000 { |
| 128 | t.Errorf("%s maps only %d runes", f.File(), len(font.cmap)) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Disjoint groups, each restarting at glyph 1 and as long as the font |
| 134 | // has glyphs — a few kilobytes — mapped every rune of Unicode to a real |
| 135 | // glyph: a map of tens of megabytes per face. No font this app draws |
| 136 | // with maps more than maxCmapRunes; one that does is refused. |
| 137 | func TestACmapMapsABoundedNumberOfRunes(t *testing.T) { |
| 138 | serif, err := Parse(serifBytes()) |
| 139 | if err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | span := uint32(serif.NumGlyphs) - 2 |
| 143 | var groups [][3]uint32 |
| 144 | for start := uint32(0); start+span <= 0x10ffff; start += span + 1 { |
| 145 | groups = append(groups, [3]uint32{start, start + span, 1}) |
| 146 | } |
| 147 | data := fontstest.WithTable(t, serifBytes(), "cmap", cmapWith(3, 10, cmap12(groups...))) |
| 148 | if font, err := Parse(data); err == nil { |
| 149 | t.Errorf("a cmap of %d runes from %d bytes of groups parsed", len(font.cmap), 12*len(groups)) |
| 150 | } |
| 151 | } |
| 152 | |