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

     1  package teams
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/keybase/client/go/kbtest"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/keybase1"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // Functions to help with testing the sigchain player where the inputs are
    14  // sigchain links that are not necessarily created during normal client
    15  // operation. Even though Keybase client doesn't generate such sigs, the player
    16  // should be resilient against bad sigs error out, not putting team into bad
    17  // state.
    18  
    19  // NOTE: We are using "precheck" method here for two reasons:
    20  
    21  // 1) We don't have to send the sigs to the server, making tests more efficient.
    22  
    23  // 2) Usually these sigs would fail server verification as well. In this case
    24  // we want to focus solely on sigchain player - it's fine if they do fail after
    25  // serverside check, but that doesn't mean that similar check should not exist
    26  // on the client side.
    27  
    28  func makeTestSCTeamSection(team *Team) SCTeamSection {
    29  	return SCTeamSection{
    30  		ID:       SCTeamID(team.ID),
    31  		Implicit: team.IsImplicit(),
    32  		Public:   team.IsPublic(),
    33  	}
    34  }
    35  
    36  func appendSigToState(t *testing.T, team *Team, state *TeamSigChainState,
    37  	linkType libkb.LinkType, section SCTeamSection, me keybase1.UserVersion,
    38  	merkleRoot *libkb.MerkleRoot) (*TeamSigChainState, error) {
    39  
    40  	if state == nil {
    41  		state = team.chain()
    42  	}
    43  
    44  	// Always make a copy here, call site shouldn't have to worry about that
    45  	// when e.g. attempting to append multiple links to one base state to
    46  	// excercise different errors.
    47  	state = state.DeepCopyToPtr()
    48  
    49  	sigMultiItem, _, err := team.sigTeamItemRaw(context.Background(), section,
    50  		linkType, state.GetLatestSeqno()+1, state.GetLatestLinkID(), merkleRoot)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	newState, err := precheckLinksToState(context.Background(),
    56  		team.G(), []libkb.SigMultiItem{sigMultiItem}, state, me)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return newState, nil
    62  }
    63  
    64  // setupTestForPrechecks sets up a test context, creates a user, creates a
    65  // team, and returns (tc, team, UV). Caller should call tc.Cleanup() after end
    66  // of the test, e.g. through defer: `defer tc.Cleanup()`
    67  func setupTestForPrechecks(t *testing.T, implicitTeam bool) (tc libkb.TestContext, team *Team, me keybase1.UserVersion) {
    68  	tc = SetupTest(t, "team", 1)
    69  
    70  	fus, err := kbtest.CreateAndSignupFakeUser("tprc", tc.G)
    71  	require.NoError(t, err)
    72  
    73  	if implicitTeam {
    74  		team, _, _, err = LookupOrCreateImplicitTeam(context.Background(), tc.G, fus.Username, false /* public */)
    75  		require.NoError(t, err)
    76  	} else {
    77  		teamname := createTeam(tc)
    78  		team, err = Load(context.Background(), tc.G, keybase1.LoadTeamArg{
    79  			Name:      teamname,
    80  			NeedAdmin: true,
    81  		})
    82  		require.NoError(t, err)
    83  	}
    84  
    85  	// Prepare key-manager if we plan to do ChangeMembership links or similar.
    86  	_, err = team.getKeyManager(context.TODO())
    87  	require.NoError(t, err)
    88  
    89  	me = tc.G.ActiveDevice.UserVersion()
    90  
    91  	return tc, team, me
    92  }
    93  
    94  func requirePrecheckError(t *testing.T, err error) {
    95  	require.Error(t, err)
    96  	require.IsType(t, PrecheckAppendError{}, err)
    97  }