github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/pkg/gzip/gunzip_test.go (about)

     1  // Copyright 2017-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gzip
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"testing"
    11  )
    12  
    13  func Test_Decompress(t *testing.T) {
    14  	type args struct {
    15  		r         io.Reader
    16  		blocksize int
    17  		processes int
    18  	}
    19  	tests := []struct {
    20  		name    string
    21  		args    args
    22  		wantW   []byte
    23  		wantErr bool
    24  	}{
    25  		{
    26  			name: "Basic Decompress",
    27  			args: args{
    28  				r:         bytes.NewReader([]byte("\x1f\x8b\b\x00\x00\tn\x88\x02\xff\nI-.Q\x80\x13\x00\x00\x00\x00\xff\xff\x01\x00\x00\xff\xffG?\xfc\xcc\x0e\x00\x00\x00")),
    29  				blocksize: 128,
    30  				processes: 1,
    31  			},
    32  			wantW:   []byte("Test Test Test"),
    33  			wantErr: false,
    34  		},
    35  		{
    36  			name: "Zeros",
    37  			args: args{
    38  				r:         bytes.NewReader([]byte("\x1f\x8b\b\x00\x00\tn\x88\x02\xff2 \x1d\x00\x00\x00\x00\xff\xff\x01\x00\x00\xff\xffR6\xe3\xeb3\x00\x00\x00")),
    39  				blocksize: 128,
    40  				processes: 1,
    41  			},
    42  			wantW:   []byte("000000000000000000000000000000000000000000000000000"),
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name: "Nil",
    47  			args: args{
    48  				r:         bytes.NewReader([]byte(nil)),
    49  				blocksize: 128,
    50  				processes: 1,
    51  			},
    52  			wantErr: true,
    53  		},
    54  		{
    55  			name: "Corrupt input",
    56  			args: args{
    57  				r:         bytes.NewReader([]byte("\x1f\x8b\b\x00\x00\t\x88\x04\xff\x00\x00\x00\xff\xff\x01\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00")),
    58  				blocksize: 128,
    59  				processes: 1,
    60  			},
    61  			wantErr: true,
    62  		},
    63  		{
    64  			name: "Invalid header trailing garbage",
    65  			args: args{
    66  				r:         bytes.NewReader([]byte("\x1f\x8b\b\x00\x00\tn\x88\x04\xff\x00\x00\x00\xff\xff\x01\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")),
    67  				blocksize: 128,
    68  				processes: 1,
    69  			},
    70  			wantErr: true,
    71  		},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			w := &bytes.Buffer{}
    76  			if err := Decompress(tt.args.r, w, tt.args.blocksize, tt.args.processes); (err != nil) != tt.wantErr {
    77  				t.Errorf("Decompress() error = %v, wantErr %v", err, tt.wantErr)
    78  				return
    79  			}
    80  			gotW := w.Bytes()
    81  			if !bytes.Equal(gotW, tt.wantW) {
    82  				t.Errorf("Decompress() = %q, want %q", gotW, tt.wantW)
    83  			}
    84  		})
    85  	}
    86  }