github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/blindtree/values_test.go (about)

     1  package blindtree
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/keybase/client/go/msgpack"
     8  	"github.com/keybase/client/go/protocol/keybase1"
     9  	"github.com/keybase/client/go/sig3"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestEncodeMerkleValues(t *testing.T) {
    15  	fakeTail := sig3.Tail{Seqno: keybase1.Seqno(2), Hash: [32]byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}}
    16  	fakeTeamValue := TeamV1Value{
    17  		Tails: map[keybase1.SeqType]sig3.Tail{keybase1.SeqType_TEAM_PRIVATE_HIDDEN: fakeTail},
    18  	}
    19  
    20  	encodingTests := []struct {
    21  		Value        interface{}
    22  		EncodedValue BlindMerkleValue
    23  		Type         BlindMerkleValueType
    24  	}{
    25  		{"Ciao", BlindMerkleValueStringForTesting("Ciao"), ValueTypeStringForTesting},
    26  		{"Bong", BlindMerkleValueStringForTesting("Bong"), ValueTypeStringForTesting},
    27  		{"", BlindMerkleValueStringForTesting(""), ValueTypeStringForTesting},
    28  		{fakeTeamValue, BlindMerkleValueTeamV1(fakeTeamValue), ValueTypeTeamV1},
    29  		{nil, BlindMerkleValueEmpty(), ValueTypeEmpty},
    30  	}
    31  
    32  	for _, et := range encodingTests {
    33  		t.Run(fmt.Sprintf("%v", et.Value), func(t *testing.T) {
    34  			require.Equal(t, et.EncodedValue.ValueType, et.Type)
    35  			require.EqualValues(t, et.Value, et.EncodedValue.InnerValue)
    36  
    37  			enc, err := msgpack.EncodeCanonical(et.EncodedValue)
    38  			require.NoError(t, err)
    39  			t.Logf("Encoded: %X", enc)
    40  
    41  			var dec BlindMerkleValue
    42  			err = msgpack.Decode(&dec, enc)
    43  			require.NoError(t, err)
    44  			require.Equal(t, et.Type, dec.ValueType)
    45  
    46  			switch et.Type {
    47  			case ValueTypeStringForTesting:
    48  				s, ok := dec.InnerValue.(string)
    49  				require.True(t, ok, "Got type %T", dec.InnerValue)
    50  				require.EqualValues(t, et.Value, s)
    51  			case ValueTypeTeamV1:
    52  				s, ok := dec.InnerValue.(TeamV1Value)
    53  				require.True(t, ok, "Got type %T", dec.InnerValue)
    54  				require.EqualValues(t, et.Value, s)
    55  			case ValueTypeEmpty:
    56  				require.Nil(t, dec.InnerValue)
    57  			default:
    58  				t.Errorf("The test does not suppor type %v", et.Type)
    59  			}
    60  		})
    61  	}
    62  }