github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/pkg/gzip/gunzip_test.go (about)

     1  package gzip
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  func Test_Decompress(t *testing.T) {
    10  	type args struct {
    11  		r         io.Reader
    12  		blocksize int
    13  		processes int
    14  	}
    15  	tests := []struct {
    16  		name    string
    17  		args    args
    18  		wantW   []byte
    19  		wantErr bool
    20  	}{
    21  		{
    22  			name: "Basic Decompress",
    23  			args: args{
    24  				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")),
    25  				blocksize: 128,
    26  				processes: 1,
    27  			},
    28  			wantW:   []byte("Test Test Test"),
    29  			wantErr: false,
    30  		},
    31  		{
    32  			name: "Zeros",
    33  			args: args{
    34  				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")),
    35  				blocksize: 128,
    36  				processes: 1,
    37  			},
    38  			wantW:   []byte("000000000000000000000000000000000000000000000000000"),
    39  			wantErr: false,
    40  		},
    41  		{
    42  			name: "Nil",
    43  			args: args{
    44  				r:         bytes.NewReader([]byte(nil)),
    45  				blocksize: 128,
    46  				processes: 1,
    47  			},
    48  			wantErr: true,
    49  		},
    50  		{
    51  			name: "Corrupt input",
    52  			args: args{
    53  				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")),
    54  				blocksize: 128,
    55  				processes: 1,
    56  			},
    57  			wantErr: true,
    58  		},
    59  		{
    60  			name: "Invalid header trailing garbage",
    61  			args: args{
    62  				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")),
    63  				blocksize: 128,
    64  				processes: 1,
    65  			},
    66  			wantErr: true,
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			w := &bytes.Buffer{}
    72  			if err := Decompress(tt.args.r, w, tt.args.blocksize, tt.args.processes); (err != nil) != tt.wantErr {
    73  				t.Errorf("Decompress() error = %v, wantErr %v", err, tt.wantErr)
    74  				return
    75  			}
    76  			gotW := w.Bytes()
    77  			if !bytes.Equal(gotW, tt.wantW) {
    78  				t.Errorf("Decompress() = %q, want %q", gotW, tt.wantW)
    79  			}
    80  		})
    81  	}
    82  }