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

     1  package ioutils
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func GetTestData() ([]byte, []byte, []byte, error) {
    11  	wasmCode, err := ioutil.ReadFile("../keeper/testdata/hackatom.wasm")
    12  	if err != nil {
    13  		return nil, nil, nil, err
    14  	}
    15  
    16  	gzipData, err := GzipIt(wasmCode)
    17  	if err != nil {
    18  		return nil, nil, nil, err
    19  	}
    20  
    21  	someRandomStr := []byte("hello world")
    22  
    23  	return wasmCode, someRandomStr, gzipData, nil
    24  }
    25  
    26  func TestIsWasm(t *testing.T) {
    27  	wasmCode, someRandomStr, gzipData, err := GetTestData()
    28  	require.NoError(t, err)
    29  
    30  	t.Log("should return false for some random string data")
    31  	require.False(t, IsWasm(someRandomStr))
    32  	t.Log("should return false for gzip data")
    33  	require.False(t, IsWasm(gzipData))
    34  	t.Log("should return true for exact wasm")
    35  	require.True(t, IsWasm(wasmCode))
    36  }
    37  
    38  func TestIsGzip(t *testing.T) {
    39  	wasmCode, someRandomStr, gzipData, err := GetTestData()
    40  	require.NoError(t, err)
    41  
    42  	require.False(t, IsGzip(wasmCode))
    43  	require.False(t, IsGzip(someRandomStr))
    44  	require.True(t, IsGzip(gzipData))
    45  }
    46  
    47  func TestGzipIt(t *testing.T) {
    48  	wasmCode, someRandomStr, _, err := GetTestData()
    49  	originalGzipData := []byte{
    50  		31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 87, 40, 207, 47, 202, 73, 1,
    51  		4, 0, 0, 255, 255, 133, 17, 74, 13, 11, 0, 0, 0,
    52  	}
    53  
    54  	require.NoError(t, err)
    55  
    56  	t.Log("gzip wasm with no error")
    57  	_, err = GzipIt(wasmCode)
    58  	require.NoError(t, err)
    59  
    60  	t.Log("gzip of a string should return exact gzip data")
    61  	strToGzip, err := GzipIt(someRandomStr)
    62  
    63  	require.True(t, IsGzip(strToGzip))
    64  	require.NoError(t, err)
    65  	require.Equal(t, originalGzipData, strToGzip)
    66  }