2017-03-13 16:33:17 +00:00
|
|
|
package compression
|
2017-02-01 00:45:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDetectCompression(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
filename string
|
|
|
|
unimplemented bool
|
|
|
|
}{
|
|
|
|
{"fixtures/Hello.uncompressed", false},
|
|
|
|
{"fixtures/Hello.gz", false},
|
|
|
|
{"fixtures/Hello.bz2", false},
|
|
|
|
{"fixtures/Hello.xz", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
// The original stream is preserved.
|
|
|
|
for _, c := range cases {
|
|
|
|
originalContents, err := ioutil.ReadFile(c.filename)
|
|
|
|
require.NoError(t, err, c.filename)
|
|
|
|
|
|
|
|
stream, err := os.Open(c.filename)
|
|
|
|
require.NoError(t, err, c.filename)
|
|
|
|
defer stream.Close()
|
|
|
|
|
2017-03-13 16:33:17 +00:00
|
|
|
_, updatedStream, err := DetectCompression(stream)
|
2017-02-01 00:45:59 +00:00
|
|
|
require.NoError(t, err, c.filename)
|
|
|
|
|
|
|
|
updatedContents, err := ioutil.ReadAll(updatedStream)
|
|
|
|
require.NoError(t, err, c.filename)
|
|
|
|
assert.Equal(t, originalContents, updatedContents, c.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The correct decompressor is chosen, and the result is as expected.
|
|
|
|
for _, c := range cases {
|
|
|
|
stream, err := os.Open(c.filename)
|
|
|
|
require.NoError(t, err, c.filename)
|
|
|
|
defer stream.Close()
|
|
|
|
|
2017-03-13 16:33:17 +00:00
|
|
|
decompressor, updatedStream, err := DetectCompression(stream)
|
2017-02-01 00:45:59 +00:00
|
|
|
require.NoError(t, err, c.filename)
|
|
|
|
|
|
|
|
var uncompressedStream io.Reader
|
|
|
|
switch {
|
|
|
|
case decompressor == nil:
|
|
|
|
uncompressedStream = updatedStream
|
|
|
|
case c.unimplemented:
|
|
|
|
_, err := decompressor(updatedStream)
|
|
|
|
assert.Error(t, err)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
s, err := decompressor(updatedStream)
|
|
|
|
require.NoError(t, err)
|
|
|
|
uncompressedStream = s
|
|
|
|
}
|
|
|
|
|
|
|
|
uncompressedContents, err := ioutil.ReadAll(uncompressedStream)
|
|
|
|
require.NoError(t, err, c.filename)
|
|
|
|
assert.Equal(t, []byte("Hello"), uncompressedContents, c.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty input is handled reasonably.
|
2017-03-13 16:33:17 +00:00
|
|
|
decompressor, updatedStream, err := DetectCompression(bytes.NewReader([]byte{}))
|
2017-02-01 00:45:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, decompressor)
|
|
|
|
updatedContents, err := ioutil.ReadAll(updatedStream)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte{}, updatedContents)
|
|
|
|
|
|
|
|
// Error reading input
|
|
|
|
reader, writer := io.Pipe()
|
|
|
|
defer reader.Close()
|
2017-03-13 16:33:17 +00:00
|
|
|
writer.CloseWithError(errors.New("Expected error reading input in DetectCompression"))
|
|
|
|
_, _, err = DetectCompression(reader)
|
2017-02-01 00:45:59 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|