Skip to content

Commit

Permalink
add error check when adding a part to a reader
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Feb 13, 2019
1 parent be5626c commit a6af251
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
4 changes: 3 additions & 1 deletion reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ func (r *Reader) loadPackage() error {
}
part := &Part{Name: fileName, ContentType: cType, Relationships: rels.findRelationship(fileName)}
r.Files = append(r.Files, &File{part, file.Size(), file})
r.p.add(part)
if err = r.p.add(part); err != nil {
return err
}
}
}
r.p.contentTypes = *ct
Expand Down
56 changes: 56 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,62 @@ func Test_newReader(t *testing.T) {
}
}

func Test_newReader_File(t *testing.T) {
tests := []struct {
name string
files []archiveFile
want int
}{
{"duplicated", []archiveFile{
newMockFile(
"[Content_Types].xml",
ioutil.NopCloser(bytes.NewBufferString(new(cTypeBuilder).withDefault("image/png", "png").String())),
nil,
),
newMockFile("pictures/photo.png", ioutil.NopCloser(bytes.NewBufferString("")), nil),
newMockFile("pictures/photo.png", ioutil.NopCloser(bytes.NewBufferString("")), nil),
}, 112},
{"doubleDots", []archiveFile{
newMockFile(
"[Content_Types].xml",
ioutil.NopCloser(bytes.NewBufferString(new(cTypeBuilder).withDefault("image/png", "png").String())),
nil,
),
newMockFile("pictures/../photo.png", ioutil.NopCloser(bytes.NewBufferString("")), nil),
}, 110},
{"oneDot", []archiveFile{
newMockFile(
"[Content_Types].xml",
ioutil.NopCloser(bytes.NewBufferString(new(cTypeBuilder).withDefault("image/png", "png").String())),
nil,
),
newMockFile("pictures/../photo.png", ioutil.NopCloser(bytes.NewBufferString("")), nil),
}, 110},
{"emptySegment", []archiveFile{
newMockFile(
"[Content_Types].xml",
ioutil.NopCloser(bytes.NewBufferString(new(cTypeBuilder).withDefault("image/png", "png").String())),
nil,
),
newMockFile("//pictures/photo.png", ioutil.NopCloser(bytes.NewBufferString("")), nil),
}, 103},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := new(mockArchive)
a.On("Files").Return(tt.files)
_, err := newReader(a)
if err == nil {
t.Error("newReader() wantError")
return
}
if got := err.(*Error).Code(); got != tt.want {
t.Errorf("newReader() = %v, want %v", got, tt.want)
}
})
}
}

func Test_newReader_ContentType(t *testing.T) {
invalidType := `<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
Expand Down

0 comments on commit a6af251

Please sign in to comment.