github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/ioutils/ioutil_test.go (about)

     1  package ioutils
     2  
     3  import (
     4  	"bytes"
     5  	"compress/gzip"
     6  	"errors"
     7  	"io"
     8  	"io/ioutil"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/fibonacci-chain/fbc/x/wasm/types"
    16  )
    17  
    18  func TestUncompress(t *testing.T) {
    19  	wasmRaw, err := ioutil.ReadFile("../keeper/testdata/hackatom.wasm")
    20  	require.NoError(t, err)
    21  
    22  	wasmGzipped, err := ioutil.ReadFile("../keeper/testdata/hackatom.wasm.gzip")
    23  	require.NoError(t, err)
    24  
    25  	const maxSize = 400_000
    26  
    27  	specs := map[string]struct {
    28  		src       []byte
    29  		expError  error
    30  		expResult []byte
    31  	}{
    32  		"handle wasm uncompressed": {
    33  			src:       wasmRaw,
    34  			expResult: wasmRaw,
    35  		},
    36  		"handle wasm compressed": {
    37  			src:       wasmGzipped,
    38  			expResult: wasmRaw,
    39  		},
    40  		"handle nil slice": {
    41  			src:       nil,
    42  			expResult: nil,
    43  		},
    44  		"handle short unidentified": {
    45  			src:       []byte{0x1, 0x2},
    46  			expResult: []byte{0x1, 0x2},
    47  		},
    48  		"handle input slice exceeding limit": {
    49  			src:      []byte(strings.Repeat("a", maxSize+1)),
    50  			expError: types.ErrLimit,
    51  		},
    52  		"handle input slice at limit": {
    53  			src:       []byte(strings.Repeat("a", maxSize)),
    54  			expResult: []byte(strings.Repeat("a", maxSize)),
    55  		},
    56  		"handle gzip identifier only": {
    57  			src:      gzipIdent,
    58  			expError: io.ErrUnexpectedEOF,
    59  		},
    60  		"handle broken gzip": {
    61  			src:      append(gzipIdent, byte(0x1)),
    62  			expError: io.ErrUnexpectedEOF,
    63  		},
    64  		"handle incomplete gzip": {
    65  			src:      wasmGzipped[:len(wasmGzipped)-5],
    66  			expError: io.ErrUnexpectedEOF,
    67  		},
    68  		"handle limit gzip output": {
    69  			src:       asGzip(bytes.Repeat([]byte{0x1}, maxSize)),
    70  			expResult: bytes.Repeat([]byte{0x1}, maxSize),
    71  		},
    72  		"handle big gzip output": {
    73  			src:      asGzip(bytes.Repeat([]byte{0x1}, maxSize+1)),
    74  			expError: types.ErrLimit,
    75  		},
    76  		"handle other big gzip output": {
    77  			src:      asGzip(bytes.Repeat([]byte{0x1}, 2*maxSize)),
    78  			expError: types.ErrLimit,
    79  		},
    80  	}
    81  	for msg, spec := range specs {
    82  		t.Run(msg, func(t *testing.T) {
    83  			r, err := Uncompress(spec.src, maxSize)
    84  			require.True(t, errors.Is(spec.expError, err), "exp %v got %+v", spec.expError, err)
    85  			if spec.expError != nil {
    86  				return
    87  			}
    88  			assert.Equal(t, spec.expResult, r)
    89  		})
    90  	}
    91  }
    92  
    93  func asGzip(src []byte) []byte {
    94  	var buf bytes.Buffer
    95  	zipper := gzip.NewWriter(&buf)
    96  	if _, err := io.Copy(zipper, bytes.NewReader(src)); err != nil {
    97  		panic(err)
    98  	}
    99  	if err := zipper.Close(); err != nil {
   100  		panic(err)
   101  	}
   102  	return buf.Bytes()
   103  }