Browse Source

Merge pull request #143 from tealeg/error-on-invalid-zip-contents

Error on invalid zip contents
Shawn Milochik 10 years ago
parent
commit
1382b007e8
4 changed files with 20 additions and 0 deletions
  1. 6 0
      lib.go
  2. 14 0
      lib_test.go
  3. BIN
      testdocs/badfile_noWorkbookRels.xlsx
  4. BIN
      testdocs/badfile_noWorksheets.xlsx

+ 6 - 0
lib.go

@@ -845,10 +845,16 @@ func ReadZipReader(r *zip.Reader) (*File, error) {
 			}
 		}
 	}
+	if workbookRels == nil {
+		return nil, fmt.Errorf("xl/_rels/workbook.xml.rels not found in input xlsx.")
+	}
 	sheetXMLMap, err = readWorkbookRelationsFromZipFile(workbookRels)
 	if err != nil {
 		return nil, err
 	}
+	if len(worksheets) == 0 {
+		return nil, fmt.Errorf("Input xlsx contains no worksheets.")
+	}
 	file.worksheets = worksheets
 	reftable, err = readSharedStringsFromZipFile(sharedStrings)
 	if err != nil {

+ 14 - 0
lib_test.go

@@ -13,6 +13,20 @@ type LibSuite struct{}
 
 var _ = Suite(&LibSuite{})
 
+// Attempting to open a file without workbook.xml.rels returns an error.
+func (l *LibSuite) TestReadZipReaderWithFileWithNoWorkbookRels(c *C) {
+	_, err := OpenFile("./testdocs/badfile_noWorkbookRels.xlsx")
+	c.Assert(err, NotNil)
+	c.Assert(err.Error(), Equals, "xl/_rels/workbook.xml.rels not found in input xlsx.")
+}
+
+// Attempting to open a file with no worksheets returns an error.
+func (l *LibSuite) TestReadZipReaderWithFileWithNoWorksheets(c *C) {
+	_, err := OpenFile("./testdocs/badfile_noWorksheets.xlsx")
+	c.Assert(err, NotNil)
+	c.Assert(err.Error(), Equals, "Input xlsx contains no worksheets.")
+}
+
 // which they are contained from the XLSX file, even when the
 // worksheet files have arbitrary, non-numeric names.
 func (l *LibSuite) TestReadWorkbookRelationsFromZipFileWithFunnyNames(c *C) {

BIN
testdocs/badfile_noWorkbookRels.xlsx


BIN
testdocs/badfile_noWorksheets.xlsx