|
|
@ -228,14 +228,18 @@ func TestOpenReader(t *testing.T) {
|
|
|
|
_, err = OpenReader(bytes.NewReader(oleIdentifier), Options{Password: "password", UnzipXMLSizeLimit: UnzipSizeLimit + 1})
|
|
|
|
_, err = OpenReader(bytes.NewReader(oleIdentifier), Options{Password: "password", UnzipXMLSizeLimit: UnzipSizeLimit + 1})
|
|
|
|
assert.EqualError(t, err, ErrWorkbookFileFormat.Error())
|
|
|
|
assert.EqualError(t, err, ErrWorkbookFileFormat.Error())
|
|
|
|
|
|
|
|
|
|
|
|
// Test open workbook with unsupported charset internal calculation chain
|
|
|
|
// Prepare unusual workbook, made the specified internal XML parts missing
|
|
|
|
preset := func(filePath string) *bytes.Buffer {
|
|
|
|
// or contain unsupported charset
|
|
|
|
|
|
|
|
preset := func(filePath string, notExist bool) *bytes.Buffer {
|
|
|
|
source, err := zip.OpenReader(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
source, err := zip.OpenReader(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
zw := zip.NewWriter(buf)
|
|
|
|
zw := zip.NewWriter(buf)
|
|
|
|
for _, item := range source.File {
|
|
|
|
for _, item := range source.File {
|
|
|
|
// The following statements can be simplified as zw.Copy(item) in go1.17
|
|
|
|
// The following statements can be simplified as zw.Copy(item) in go1.17
|
|
|
|
|
|
|
|
if notExist && item.Name == filePath {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
writer, err := zw.Create(item.Name)
|
|
|
|
writer, err := zw.Create(item.Name)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
readerCloser, err := item.Open()
|
|
|
|
readerCloser, err := item.Open()
|
|
|
@ -243,21 +247,33 @@ func TestOpenReader(t *testing.T) {
|
|
|
|
_, err = io.Copy(writer, readerCloser)
|
|
|
|
_, err = io.Copy(writer, readerCloser)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fi, err := zw.Create(filePath)
|
|
|
|
if !notExist {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
fi, err := zw.Create(filePath)
|
|
|
|
_, err = fi.Write(MacintoshCyrillicCharset)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = fi.Write(MacintoshCyrillicCharset)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
assert.NoError(t, zw.Close())
|
|
|
|
assert.NoError(t, zw.Close())
|
|
|
|
return buf
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test open workbook with unsupported charset internal XML parts
|
|
|
|
for _, defaultXMLPath := range []string{
|
|
|
|
for _, defaultXMLPath := range []string{
|
|
|
|
defaultXMLPathCalcChain,
|
|
|
|
defaultXMLPathCalcChain,
|
|
|
|
defaultXMLPathStyles,
|
|
|
|
defaultXMLPathStyles,
|
|
|
|
defaultXMLPathWorkbookRels,
|
|
|
|
defaultXMLPathWorkbookRels,
|
|
|
|
} {
|
|
|
|
} {
|
|
|
|
_, err = OpenReader(preset(defaultXMLPath))
|
|
|
|
_, err = OpenReader(preset(defaultXMLPath, false))
|
|
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
|
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test open workbook without internal XML parts
|
|
|
|
|
|
|
|
for _, defaultXMLPath := range []string{
|
|
|
|
|
|
|
|
defaultXMLPathCalcChain,
|
|
|
|
|
|
|
|
defaultXMLPathStyles,
|
|
|
|
|
|
|
|
defaultXMLPathWorkbookRels,
|
|
|
|
|
|
|
|
} {
|
|
|
|
|
|
|
|
_, err = OpenReader(preset(defaultXMLPath, true))
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test open spreadsheet with unzip size limit
|
|
|
|
// Test open spreadsheet with unzip size limit
|
|
|
|
_, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipSizeLimit: 100})
|
|
|
|
_, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipSizeLimit: 100})
|
|
|
|