github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/hidden/hidden_test.go (about)

     1  package hidden
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/keybase/client/go/blindtree"
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  	"github.com/keybase/client/go/sig3"
    12  )
    13  
    14  func TestMakeHiddenRespFromTeamLeaf(t *testing.T) {
    15  
    16  	tails := make(map[keybase1.SeqType]sig3.Tail)
    17  	tails[keybase1.SeqType_TEAM_PRIVATE_HIDDEN] = sig3.Tail{ChainType: keybase1.SeqType_TEAM_PRIVATE_HIDDEN}
    18  	testSeqno := keybase1.Seqno(5)
    19  
    20  	tests := []struct {
    21  		desc        string
    22  		leaf        blindtree.BlindMerkleValue
    23  		shouldError bool
    24  		errType     libkb.HiddenMerkleErrorType
    25  		respType    libkb.MerkleHiddenResponseType
    26  	}{
    27  		{"Leaf without hidden chain", blindtree.BlindMerkleValueTeamV1(blindtree.TeamV1Value{}), true, libkb.HiddenMerkleErrorNoHiddenChainInLeaf, 0},
    28  		{"wrong leaf type", blindtree.BlindMerkleValue{ValueType: 71}, true, libkb.HiddenMerkleErrorInvalidLeafType, 0},
    29  		{"empty leaf type", blindtree.BlindMerkleValueEmpty(), false, 0, libkb.MerkleHiddenResponseTypeABSENCEPROOF},
    30  		{"valid leaf", blindtree.BlindMerkleValueTeamV1(blindtree.TeamV1Value{
    31  			Tails: map[keybase1.SeqType]sig3.Tail{keybase1.SeqType_TEAM_PRIVATE_HIDDEN: {ChainType: keybase1.SeqType_TEAM_PRIVATE_HIDDEN}},
    32  		}), false, 0, libkb.MerkleHiddenResponseTypeOK},
    33  		{"inconsistent leaf", blindtree.BlindMerkleValueTeamV1(blindtree.TeamV1Value{
    34  			Tails: map[keybase1.SeqType]sig3.Tail{keybase1.SeqType_TEAM_PRIVATE_HIDDEN: {ChainType: keybase1.SeqType_PUBLIC}},
    35  		}), true, libkb.HiddenMerkleErrorInconsistentLeaf, 0},
    36  	}
    37  
    38  	for _, test := range tests {
    39  		t.Run(test.desc, func(t *testing.T) {
    40  			hr, err := makeHiddenRespFromTeamLeaf(libkb.MetaContext{}, test.leaf, testSeqno)
    41  			if test.shouldError {
    42  				require.Error(t, err)
    43  				require.IsType(t, libkb.HiddenMerkleError{}, err)
    44  				require.Equal(t, err.(libkb.HiddenMerkleError).ErrorType(), test.errType)
    45  			} else {
    46  				require.NoError(t, err)
    47  				require.Equal(t, test.respType, hr.RespType)
    48  				require.Equal(t, testSeqno, hr.UncommittedSeqno)
    49  				if test.respType == libkb.MerkleHiddenResponseTypeOK {
    50  					require.Equal(t, keybase1.SeqType_TEAM_PRIVATE_HIDDEN, hr.CommittedHiddenTail.ChainType)
    51  				}
    52  			}
    53  		})
    54  	}
    55  
    56  }