Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabstv committed Feb 28, 2019
1 parent 37cfba2 commit 4a9ddc9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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)
<!--[![codecov](https://codecov.io/gh/gabstv/go-bsdiff/branch/master/graph/badge.svg)](https://codecov.io/gh/gabstv/go-bsdiff)-->

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.

Expand Down
2 changes: 1 addition & 1 deletion pkg/bspatch/bspatch.go
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions pkg/bspatch/bspatch_test.go
Expand Up @@ -3,6 +3,7 @@ package bspatch
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -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)")
}
}

0 comments on commit 4a9ddc9

Please sign in to comment.