From 4a9ddc999762f095bfc49e7d5aaba09bdd795de9 Mon Sep 17 00:00:00 2001 From: Gabriel Ochsenhofer Date: Thu, 28 Feb 2019 09:01:44 -0300 Subject: [PATCH] more tests --- README.md | 2 +- pkg/bspatch/bspatch.go | 2 +- pkg/bspatch/bspatch_test.go | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f9a6f8a..a44ce2e 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ Pure Go implementation of [bsdiff](http://www.daemonology.net/bsdiff/) 4. [![GoDoc](https://godoc.org/github.com/gabstv/go-bsdiff?status.svg)](https://godoc.org/github.com/gabstv/go-bsdiff) [![Go Report Card](https://goreportcard.com/badge/github.com/gabstv/go-bsdiff)](https://goreportcard.com/report/github.com/gabstv/go-bsdiff) [![Build Status](https://travis-ci.org/gabstv/go-bsdiff.svg?branch=master)](https://travis-ci.org/gabstv/go-bsdiff) -[![codecov](https://codecov.io/gh/gabstv/go-bsdiff/branch/master/graph/badge.svg)](https://codecov.io/gh/gabstv/go-bsdiff) [![Coverage Status](https://coveralls.io/repos/github/gabstv/go-bsdiff/badge.svg?branch=master)](https://coveralls.io/github/gabstv/go-bsdiff?branch=master) + bsdiff and bspatch are tools for building and applying patches to binary files. By using suffix sorting (specifically, Larsson and Sadakane's [qsufsort](http://www.larsson.dogma.net/ssrev-tr.pdf)) and taking advantage of how executable files change. diff --git a/pkg/bspatch/bspatch.go b/pkg/bspatch/bspatch.go index 1538f53..c5d0a4d 100644 --- a/pkg/bspatch/bspatch.go +++ b/pkg/bspatch/bspatch.go @@ -118,7 +118,7 @@ func patchb(oldfile, patch []byte) ([]byte, error) { newsize = offtin(header[24:]) if bzctrllen < 0 || bzdatalen < 0 || newsize < 0 { - return nil, fmt.Errorf("corrupt patch bzctrllen %v bzdatalen %v newsize %v", bzctrllen, bzdatalen, newsize) + return nil, fmt.Errorf("corrupt patch (bzctrllen %v bzdatalen %v newsize %v)", bzctrllen, bzdatalen, newsize) } // Close patch file and re-open it via libbzip2 at the right places diff --git a/pkg/bspatch/bspatch_test.go b/pkg/bspatch/bspatch_test.go index da26efa..7c80ba4 100644 --- a/pkg/bspatch/bspatch_test.go +++ b/pkg/bspatch/bspatch_test.go @@ -3,6 +3,7 @@ package bspatch import ( "bytes" "encoding/binary" + "fmt" "io/ioutil" "os" "testing" @@ -171,3 +172,54 @@ func TestFile(t *testing.T) { os.Remove(t1n) os.Remove(tpp) } + +type corruptReader int + +func (r *corruptReader) Read(p []byte) (n int, err error) { + return 0, fmt.Errorf("testing") +} + +func TestReaderError(t *testing.T) { + cr := corruptReader(0) + b0 := bytes.NewReader([]byte{0, 0, 0, 0, 0, 1, 2, 3, 4, 5}) + b1 := new(bytes.Buffer) + if err := Reader(&cr, b1, b0); err == nil { + t.Fail() + } + if err := Reader(b0, b1, &cr); err == nil { + t.Fail() + } +} + +func TestCorruptHeader(t *testing.T) { + corruptPatch := []byte{ + 0x41, 0x53, 0x44, 0x49, 0x46, 0x46, 0x34, 0x30, + 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + } + _, err := Bytes(corruptPatch, corruptPatch[:30]) + if err == nil { + t.Fatal("header should be corrupt") + } + if err.Error()[:13] != "corrupt patch" { + t.Fatal("header should be corrupt (2)") + } + _, err = Bytes(corruptPatch, corruptPatch) + if err == nil { + t.Fatal("header should be corrupt (3)") + } + if err.Error() != "corrupt patch (header BSDIFF40)" { + t.Fatal("header should be corrupt (4)") + } + corruptPatch[0] = 0x42 + corruptLen := []byte{100, 0, 0, 0, 0, 0, 0, 128} + copy(corruptPatch[8:], corruptLen) + _, err = Bytes(corruptPatch, corruptPatch) + if err == nil { + t.Fatal("header should be corrupt (5)") + } + if err.Error()[:15] != "corrupt patch (" { + t.Fatal("header should be corrupt (6)") + } +}