forked from extrame/xls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsst_test.go
More file actions
140 lines (122 loc) · 5.63 KB
/
Copy pathsst_test.go
File metadata and controls
140 lines (122 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package xls
import (
"bytes"
"encoding/binary"
"testing"
)
func writeBIFFRecord(buf *bytes.Buffer, id uint16, data []byte) {
binary.Write(buf, binary.LittleEndian, id)
binary.Write(buf, binary.LittleEndian, uint16(len(data)))
buf.Write(data)
}
func makeBOFData() []byte {
var b bytes.Buffer
binary.Write(&b, binary.LittleEndian, uint16(0x0600)) // Ver: BIFF8
binary.Write(&b, binary.LittleEndian, uint16(0x0005)) // Type: workbook globals
b.Write(make([]byte, 12)) // Id_make, Year, Flags, Min_ver
return b.Bytes()
}
// TestSSTContinueRichtext tests that richtext formatting runs spanning
// a record boundary don't corrupt subsequent SST entries.
// String 0: "ABC" with 2 formatting runs — chars fit in SST, runs overflow to CONTINUE.
// String 1: "DEF" in CONTINUE after the formatting runs.
func TestSSTContinueRichtext(t *testing.T) {
// SST record: SstInfo + string 0 header + "ABC" (no room for formatting runs)
var sstData bytes.Buffer
binary.Write(&sstData, binary.LittleEndian, uint32(2)) // Total
binary.Write(&sstData, binary.LittleEndian, uint32(2)) // Count
binary.Write(&sstData, binary.LittleEndian, uint16(3)) // string 0 char count
sstData.WriteByte(0x08) // flag: has richtext
binary.Write(&sstData, binary.LittleEndian, uint16(2)) // richtext_num = 2 runs
sstData.Write([]byte("ABC")) // char data (Latin1)
// 0 bytes left for 8-byte formatting runs → overflow to CONTINUE
// CONTINUE record: formatting runs + string 1
var contData bytes.Buffer
contData.Write(make([]byte, 8)) // 2 runs × 4 bytes (dummy)
binary.Write(&contData, binary.LittleEndian, uint16(3)) // string 1 char count
contData.WriteByte(0x00) // flag: plain Latin1
contData.Write([]byte("DEF"))
var stream bytes.Buffer
writeBIFFRecord(&stream, 0x0809, makeBOFData())
writeBIFFRecord(&stream, 0x00FC, sstData.Bytes())
writeBIFFRecord(&stream, 0x003C, contData.Bytes())
wb := &WorkBook{Formats: make(map[uint16]*Format)}
wb.Parse(bytes.NewReader(stream.Bytes()))
if len(wb.sst) != 2 {
t.Fatalf("expected 2 SST entries, got %d", len(wb.sst))
}
if wb.sst[0] != "ABC" {
t.Errorf("sst[0]: expected %q, got %q", "ABC", wb.sst[0])
}
if wb.sst[1] != "DEF" {
t.Errorf("sst[1]: expected %q, got %q", "DEF", wb.sst[1])
}
}
// TestSSTContinueRichtextPartial tests the case where richtext formatting
// runs are partially read in the SST record (boundary falls mid-formatting).
func TestSSTContinueRichtextPartial(t *testing.T) {
// SST record: SstInfo + string 0 header + "ABC" + 4 of 8 formatting bytes
var sstData bytes.Buffer
binary.Write(&sstData, binary.LittleEndian, uint32(2)) // Total
binary.Write(&sstData, binary.LittleEndian, uint32(2)) // Count
binary.Write(&sstData, binary.LittleEndian, uint16(3)) // string 0 char count
sstData.WriteByte(0x08) // flag: has richtext
binary.Write(&sstData, binary.LittleEndian, uint16(2)) // richtext_num = 2
sstData.Write([]byte("ABC")) // chars
sstData.Write(make([]byte, 4)) // partial: 4 of 8 formatting bytes
// CONTINUE: remaining 4 formatting bytes + string 1
var contData bytes.Buffer
contData.Write(make([]byte, 4)) // remaining formatting bytes
binary.Write(&contData, binary.LittleEndian, uint16(3)) // string 1 char count
contData.WriteByte(0x00) // flag
contData.Write([]byte("DEF"))
var stream bytes.Buffer
writeBIFFRecord(&stream, 0x0809, makeBOFData())
writeBIFFRecord(&stream, 0x00FC, sstData.Bytes())
writeBIFFRecord(&stream, 0x003C, contData.Bytes())
wb := &WorkBook{Formats: make(map[uint16]*Format)}
wb.Parse(bytes.NewReader(stream.Bytes()))
if len(wb.sst) != 2 {
t.Fatalf("expected 2 SST entries, got %d", len(wb.sst))
}
if wb.sst[0] != "ABC" {
t.Errorf("sst[0]: expected %q, got %q", "ABC", wb.sst[0])
}
if wb.sst[1] != "DEF" {
t.Errorf("sst[1]: expected %q, got %q", "DEF", wb.sst[1])
}
}
// TestSSTContinuePhonetic tests that phonetic data spanning a record
// boundary is properly skipped in the CONTINUE handler.
func TestSSTContinuePhonetic(t *testing.T) {
// SST record: SstInfo + string 0 with phonetic flag, chars fit but phonetic overflows
var sstData bytes.Buffer
binary.Write(&sstData, binary.LittleEndian, uint32(2)) // Total
binary.Write(&sstData, binary.LittleEndian, uint32(2)) // Count
binary.Write(&sstData, binary.LittleEndian, uint16(3)) // string 0 char count
sstData.WriteByte(0x04) // flag: has phonetic
binary.Write(&sstData, binary.LittleEndian, uint32(12)) // phonetic_size = 12 bytes
sstData.Write([]byte("ABC")) // chars
// 0 bytes for 12-byte phonetic data → overflow
// CONTINUE: phonetic data + string 1
var contData bytes.Buffer
contData.Write(make([]byte, 12)) // phonetic data (dummy)
binary.Write(&contData, binary.LittleEndian, uint16(3)) // string 1 char count
contData.WriteByte(0x00) // flag
contData.Write([]byte("DEF"))
var stream bytes.Buffer
writeBIFFRecord(&stream, 0x0809, makeBOFData())
writeBIFFRecord(&stream, 0x00FC, sstData.Bytes())
writeBIFFRecord(&stream, 0x003C, contData.Bytes())
wb := &WorkBook{Formats: make(map[uint16]*Format)}
wb.Parse(bytes.NewReader(stream.Bytes()))
if len(wb.sst) != 2 {
t.Fatalf("expected 2 SST entries, got %d", len(wb.sst))
}
if wb.sst[0] != "ABC" {
t.Errorf("sst[0]: expected %q, got %q", "ABC", wb.sst[0])
}
if wb.sst[1] != "DEF" {
t.Errorf("sst[1]: expected %q, got %q", "DEF", wb.sst[1])
}
}