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

     1  package ephemeral
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/keybase/client/go/kbtest"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/gregor1"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func ephemeralKeyTestSetup(t *testing.T) (libkb.TestContext, libkb.MetaContext, *kbtest.FakeUser) {
    15  	tc := libkb.SetupTest(t, "ephemeral", 2)
    16  
    17  	mctx := libkb.NewMetaContextForTest(tc)
    18  	NewEphemeralStorageAndInstall(mctx)
    19  
    20  	user, err := kbtest.CreateAndSignupFakeUser("t", tc.G)
    21  	require.NoError(t, err)
    22  	err = mctx.G().GetEKLib().KeygenIfNeeded(mctx)
    23  	require.NoError(t, err)
    24  
    25  	return tc, mctx, user
    26  }
    27  
    28  func TestTimeConversions(t *testing.T) {
    29  	// In this package, we assume that keybase1.TimeFromSeconds(t).UnixSeconds()
    30  	// is the exact same integer as t. Test that this is true.
    31  	now := time.Now().Unix()
    32  	require.Equal(t, now, keybase1.TimeFromSeconds(now).UnixSeconds())
    33  }
    34  
    35  func verifyUserEK(t *testing.T, metadata keybase1.UserEkMetadata, ek keybase1.UserEk) {
    36  	seed := UserEKSeed(ek.Seed)
    37  	keypair := seed.DeriveDHKey()
    38  	require.Equal(t, metadata.Kid, keypair.GetKID())
    39  }
    40  
    41  func verifyTeamEK(t *testing.T, teamEKMetadata keybase1.TeamEkMetadata,
    42  	ek keybase1.TeamEphemeralKey) {
    43  	typ, err := ek.KeyType()
    44  	require.NoError(t, err)
    45  	require.Equal(t, keybase1.TeamEphemeralKeyType_TEAM, typ)
    46  	teamEK := ek.Team()
    47  
    48  	seed := TeamEKSeed(teamEK.Seed)
    49  	keypair := seed.DeriveDHKey()
    50  	require.Equal(t, teamEKMetadata.Kid, keypair.GetKID())
    51  }
    52  
    53  func TestEphemeralCloneError(t *testing.T) {
    54  	tc, mctx, _ := ephemeralKeyTestSetup(t)
    55  	defer tc.Cleanup()
    56  
    57  	g := tc.G
    58  	teamID := createTeam(tc)
    59  
    60  	ekLib := g.GetEKLib()
    61  	teamEK1, created, err := ekLib.GetOrCreateLatestTeamEK(mctx, teamID)
    62  	require.NoError(t, err)
    63  	require.True(t, created)
    64  
    65  	// delete all our deviceEKs and make sure the error comes back as a cloning
    66  	// error since we simulate the cloned state.
    67  	libkb.CreateClonedDevice(tc, mctx)
    68  	deviceEKStorage := g.GetDeviceEKStorage()
    69  	s := deviceEKStorage.(*DeviceEKStorage)
    70  	allDevicEKs, err := s.GetAll(mctx)
    71  	require.NoError(t, err)
    72  	for _, dek := range allDevicEKs {
    73  		err = s.Delete(mctx, dek.Metadata.Generation, "")
    74  		require.NoError(t, err)
    75  	}
    76  	_, err = g.GetTeamEKBoxStorage().Get(mctx, teamID, teamEK1.Generation(), nil)
    77  	require.Error(t, err)
    78  	require.IsType(t, EphemeralKeyError{}, err)
    79  	ekErr := err.(EphemeralKeyError)
    80  	require.Contains(t, ekErr.HumanError(), DeviceCloneErrMsg)
    81  }
    82  
    83  func TestEphemeralDeviceProvisionedAfterContent(t *testing.T) {
    84  	tc, mctx, _ := ephemeralKeyTestSetup(t)
    85  	defer tc.Cleanup()
    86  
    87  	g := tc.G
    88  	teamID := createTeam(tc)
    89  
    90  	ekLib := g.GetEKLib()
    91  	teamEK1, created, err := ekLib.GetOrCreateLatestTeamEK(mctx, teamID)
    92  	require.NoError(t, err)
    93  	require.True(t, created)
    94  
    95  	deviceEKStorage := g.GetDeviceEKStorage()
    96  	s := deviceEKStorage.(*DeviceEKStorage)
    97  	allDevicEKs, err := s.GetAll(mctx)
    98  	require.NoError(t, err)
    99  	for _, dek := range allDevicEKs {
   100  		err = s.Delete(mctx, dek.Metadata.Generation, "")
   101  		require.NoError(t, err)
   102  	}
   103  
   104  	creationCtime := gregor1.ToTime(time.Now().Add(time.Hour * -100))
   105  	_, err = g.GetTeamEKBoxStorage().Get(mctx, teamID, teamEK1.Generation(), &creationCtime)
   106  	require.Error(t, err)
   107  	require.IsType(t, EphemeralKeyError{}, err)
   108  	ekErr := err.(EphemeralKeyError)
   109  	require.Contains(t, ekErr.HumanError(), DeviceAfterEKErrMsg)
   110  
   111  	// clear out cached error messages
   112  	g.GetEKLib().ClearCaches(mctx)
   113  	_, err = g.LocalDb.Nuke()
   114  	require.NoError(t, err)
   115  
   116  	// If no creation ctime is specified, we just get the default error message
   117  	_, err = g.GetTeamEKBoxStorage().Get(mctx, teamID, teamEK1.Generation(), nil)
   118  	require.Error(t, err)
   119  	require.IsType(t, EphemeralKeyError{}, err)
   120  	ekErr = err.(EphemeralKeyError)
   121  	require.Equal(t, DefaultHumanErrMsg, ekErr.HumanError())
   122  }
   123  
   124  func TestEphemeralPluralization(t *testing.T) {
   125  	humanMsg := humanMsgWithPrefix(DeviceAfterEKErrMsg)
   126  
   127  	pluralized := PluralizeErrorMessage(humanMsg, 0)
   128  	require.Equal(t, humanMsg, pluralized)
   129  
   130  	pluralized = PluralizeErrorMessage(humanMsg, 1)
   131  	require.Equal(t, humanMsg, pluralized)
   132  
   133  	pluralized = PluralizeErrorMessage(humanMsg, 2)
   134  	require.Equal(t, "2 exploding messages are not available, because this device was created after it was sent", pluralized)
   135  
   136  	pluralized = PluralizeErrorMessage(DefaultHumanErrMsg, 2)
   137  	require.Equal(t, "2 exploding messages are not available", pluralized)
   138  }