github.com/okex/exchain@v1.8.0/libs/tendermint/types/deltas_test.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"math"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestDelta(t *testing.T) {
    14  	d := &Deltas{
    15  		Payload: DeltaPayload{
    16  			ABCIRsp:        []byte("abci"),
    17  			DeltasBytes:    []byte("detal"),
    18  			WatchBytes:     []byte("watch"),
    19  			WasmWatchBytes: []byte("wasm watch"),
    20  		},
    21  		Height:       1,
    22  		CompressType: 2,
    23  	}
    24  
    25  	marshaled, err := d.Marshal()
    26  	require.NoError(t, err)
    27  
    28  	unmarshaled := &Deltas{}
    29  	err = unmarshaled.Unmarshal(marshaled)
    30  	require.NoError(t, err)
    31  
    32  	assert.True(t, bytes.Equal(unmarshaled.ABCIRsp(), d.ABCIRsp()), "ABCIRsp not equal")
    33  	assert.True(t, bytes.Equal(unmarshaled.DeltasBytes(), d.DeltasBytes()), "DeltasBytes not equal")
    34  	assert.True(t, bytes.Equal(unmarshaled.WatchBytes(), d.WatchBytes()), "WatchBytes not equal")
    35  	assert.True(t, bytes.Equal(unmarshaled.WasmWatchBytes(), d.WasmWatchBytes()), "WasmWatchBytes not equal")
    36  	assert.True(t, unmarshaled.Height == d.Height, "Height not equal")
    37  	assert.True(t, unmarshaled.CompressType == d.CompressType, "CompressType not equal")
    38  }
    39  
    40  func TestDeltas_MarshalUnMarshal(t *testing.T) {
    41  	payload := DeltaPayload{ABCIRsp: []byte("ABCIRsp"), DeltasBytes: []byte("DeltasBytes"), WatchBytes: []byte("WatchBytes")}
    42  	type fields struct {
    43  		Height          int64
    44  		Version         int
    45  		Payload         DeltaPayload
    46  		CompressType    int
    47  		CompressFlag    int
    48  		marshalElapsed  time.Duration
    49  		compressElapsed time.Duration
    50  		hashElapsed     time.Duration
    51  	}
    52  	tests := []struct {
    53  		name    string
    54  		fields  fields
    55  		wantErr bool
    56  	}{
    57  		{"no compress", fields{Height: 1, Version: 1, Payload: payload}, false},
    58  		{"compress", fields{Height: 1, Version: 1, Payload: payload, CompressType: 1}, false},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			d := &Deltas{
    63  				Height:          tt.fields.Height,
    64  				Payload:         tt.fields.Payload,
    65  				CompressType:    tt.fields.CompressType,
    66  				CompressFlag:    tt.fields.CompressFlag,
    67  				marshalElapsed:  tt.fields.marshalElapsed,
    68  				compressElapsed: tt.fields.compressElapsed,
    69  				hashElapsed:     tt.fields.hashElapsed,
    70  			}
    71  			got, err := d.Marshal()
    72  			if (err != nil) != tt.wantErr {
    73  				t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr)
    74  				return
    75  			}
    76  
    77  			err = d.Unmarshal(got)
    78  			assert.Nil(t, err)
    79  		})
    80  	}
    81  }
    82  
    83  func TestDeltas_Validate(t *testing.T) {
    84  	payload := DeltaPayload{ABCIRsp: []byte("ABCIRsp"), DeltasBytes: []byte("DeltasBytes"), WatchBytes: []byte("WatchBytes")}
    85  	noABCIRsp := DeltaPayload{ABCIRsp: nil, DeltasBytes: []byte("DeltasBytes"), WatchBytes: []byte("WatchBytes")}
    86  	noDeltaBytes := DeltaPayload{ABCIRsp: []byte("ABCIRsp"), DeltasBytes: nil, WatchBytes: []byte("WatchBytes")}
    87  	noWD := DeltaPayload{ABCIRsp: []byte("ABCIRsp"), DeltasBytes: []byte("DeltasBytes"), WatchBytes: nil}
    88  
    89  	type fields struct {
    90  		Height          int64
    91  		Version         int
    92  		Payload         DeltaPayload
    93  		CompressType    int
    94  		CompressFlag    int
    95  		marshalElapsed  time.Duration
    96  		compressElapsed time.Duration
    97  		hashElapsed     time.Duration
    98  	}
    99  	type args struct {
   100  		height int64
   101  	}
   102  	tests := []struct {
   103  		name   string
   104  		fields fields
   105  		args   args
   106  		want   bool
   107  	}{
   108  		{"normal case", fields{Height: 1, Version: DeltaVersion, Payload: payload}, args{1}, true},
   109  		{"no ABCIRsp", fields{Height: 1, Version: DeltaVersion, Payload: noABCIRsp}, args{1}, false},
   110  		{"no deltaBytes", fields{Height: 1, Version: DeltaVersion, Payload: noDeltaBytes}, args{1}, false},
   111  		{"no watchData", fields{Height: 1, Version: DeltaVersion, Payload: noWD}, args{1}, !FastQuery},
   112  		{"wrong height", fields{Height: 1, Version: DeltaVersion, Payload: payload}, args{2}, false},
   113  	}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			dds := &Deltas{
   117  				Height:          tt.fields.Height,
   118  				Payload:         tt.fields.Payload,
   119  				CompressType:    tt.fields.CompressType,
   120  				CompressFlag:    tt.fields.CompressFlag,
   121  				marshalElapsed:  tt.fields.marshalElapsed,
   122  				compressElapsed: tt.fields.compressElapsed,
   123  				hashElapsed:     tt.fields.hashElapsed,
   124  			}
   125  			if got := dds.Validate(tt.args.height); got != tt.want {
   126  				t.Errorf("Validate() = %v, want %v", got, tt.want)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func TestDeltaPayloadAmino(t *testing.T) {
   133  	var testCases = []DeltaPayload{
   134  		{},
   135  		{
   136  			ABCIRsp:        []byte("ABCIResp"),
   137  			DeltasBytes:    []byte("DeltasBytes"),
   138  			WatchBytes:     []byte("WatchBytes"),
   139  			WasmWatchBytes: []byte("WasmWatchBytes"),
   140  		},
   141  		{
   142  			ABCIRsp:        []byte{},
   143  			DeltasBytes:    []byte{},
   144  			WatchBytes:     []byte{},
   145  			WasmWatchBytes: []byte{},
   146  		},
   147  	}
   148  	for _, testCase := range testCases {
   149  		expectBz, err := cdc.MarshalBinaryBare(testCase)
   150  		require.NoError(t, err)
   151  
   152  		actulaBz, err := testCase.MarshalToAmino(cdc)
   153  		require.NoError(t, err)
   154  		require.Equal(t, expectBz, actulaBz)
   155  		require.Equal(t, len(expectBz), testCase.AminoSize(cdc))
   156  
   157  		var expectValue DeltaPayload
   158  		err = cdc.UnmarshalBinaryBare(expectBz, &expectValue)
   159  		require.NoError(t, err)
   160  
   161  		var actualValue DeltaPayload
   162  		err = actualValue.UnmarshalFromAmino(cdc, expectBz)
   163  		require.NoError(t, err)
   164  
   165  		require.Equal(t, expectValue, actualValue)
   166  	}
   167  
   168  	{
   169  		bz := []byte{1<<3 | 2, 0, 2<<3 | 2, 0, 3<<3 | 2, 0, 4<<3 | 2, 0}
   170  		var expectValue DeltaPayload
   171  		err := cdc.UnmarshalBinaryBare(bz, &expectValue)
   172  		require.NoError(t, err)
   173  
   174  		var actualValue DeltaPayload
   175  		err = actualValue.UnmarshalFromAmino(cdc, bz)
   176  		require.NoError(t, err)
   177  
   178  		require.Equal(t, expectValue, actualValue)
   179  	}
   180  }
   181  
   182  func TestDeltasMessageAmino(t *testing.T) {
   183  	var testCases = []DeltasMessage{
   184  		{},
   185  		{
   186  			Metadata:     []byte("Metadata"),
   187  			MetadataHash: []byte("MetadataHash"),
   188  			Height:       12345,
   189  			CompressType: 1234,
   190  			From:         "from",
   191  		},
   192  		{
   193  			Metadata:     []byte{},
   194  			MetadataHash: []byte{},
   195  		},
   196  		{
   197  			Height:       math.MaxInt64,
   198  			CompressType: math.MaxInt,
   199  		},
   200  		{
   201  			Height:       math.MinInt64,
   202  			CompressType: math.MinInt,
   203  		},
   204  	}
   205  	utInitValue := DeltasMessage{
   206  		Metadata:     []byte("Metadata"),
   207  		MetadataHash: []byte("MetadataHash"),
   208  		Height:       12345,
   209  		CompressType: 1234,
   210  		From:         "from",
   211  	}
   212  
   213  	for _, testCase := range testCases {
   214  		expectBz, err := cdc.MarshalBinaryBare(testCase)
   215  		require.NoError(t, err)
   216  
   217  		actulaBz, err := testCase.MarshalToAmino(cdc)
   218  		require.NoError(t, err)
   219  		require.Equal(t, expectBz, actulaBz)
   220  		require.Equal(t, len(expectBz), testCase.AminoSize(cdc))
   221  
   222  		var expectValue = utInitValue
   223  		err = cdc.UnmarshalBinaryBare(expectBz, &expectValue)
   224  		require.NoError(t, err)
   225  
   226  		var actualValue = utInitValue
   227  		err = actualValue.UnmarshalFromAmino(cdc, expectBz)
   228  		require.NoError(t, err)
   229  
   230  		require.Equal(t, expectValue, actualValue)
   231  	}
   232  
   233  	{
   234  		bz := []byte{1<<3 | 2, 0, 2<<3 | 2, 0, 3 << 3, 0, 4 << 3, 0, 5<<3 | 2, 0}
   235  		var expectValue = utInitValue
   236  		err := cdc.UnmarshalBinaryBare(bz, &expectValue)
   237  		require.NoError(t, err)
   238  
   239  		var actualValue = utInitValue
   240  		err = actualValue.UnmarshalFromAmino(cdc, bz)
   241  		require.NoError(t, err)
   242  
   243  		require.Equal(t, expectValue, actualValue)
   244  	}
   245  }