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

     1  // Copyright 2017 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package engine
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPerUserKeyUpkeep(t *testing.T) {
    14  	tc := SetupEngineTest(t, "pukup")
    15  	defer tc.Cleanup()
    16  
    17  	fu := CreateAndSignupFakeUserPaper(tc, "pukup")
    18  	upkeep := func() *PerUserKeyUpkeep {
    19  		arg := &PerUserKeyUpkeepArgs{}
    20  		eng := NewPerUserKeyUpkeep(tc.G, arg)
    21  		m := NewMetaContextForTestWithLogUI(tc)
    22  		err := RunEngine2(m, eng)
    23  		require.NoError(t, err)
    24  		return eng
    25  	}
    26  	require.False(t, upkeep().DidRollKey, "don't roll")
    27  
    28  	t.Logf("revoke paper key")
    29  	revokeAnyPaperKey(tc, fu)
    30  
    31  	require.False(t, upkeep().DidRollKey, "don't roll after revoke-other")
    32  
    33  	t.Logf("provision second device")
    34  	tcY, cleanup := provisionNewDeviceKex(&tc, fu)
    35  	defer cleanup()
    36  
    37  	t.Logf("second device deprovisions itself")
    38  	{
    39  		eng := NewDeprovisionEngine(tcY.G, fu.Username, true /* doRevoke */, libkb.LogoutOptions{})
    40  		uis := libkb.UIs{
    41  			LogUI:    tcY.G.UI.GetLogUI(),
    42  			SecretUI: fu.NewSecretUI(),
    43  		}
    44  		m := libkb.NewMetaContextTODO(tcY.G).WithUIs(uis)
    45  		err := RunEngine2(m, eng)
    46  		require.NoError(t, err, "deprovision")
    47  	}
    48  
    49  	t.Logf("load self to bust the upak cache")
    50  	// Upkeep hits the cache. It's ok that upkeep doesn't notice a deprovision
    51  	// right away. Bust the upak cache as a way of simulating time passing
    52  	// for the sake of this test.
    53  	loadArg := libkb.NewLoadUserArg(tc.G).
    54  		WithUID(fu.UID()).
    55  		WithSelf(true).
    56  		WithForcePoll(true). // <-
    57  		WithPublicKeyOptional()
    58  	_, _, err := tc.G.GetUPAKLoader().LoadV2(loadArg)
    59  	require.NoError(t, err)
    60  
    61  	require.True(t, upkeep().DidRollKey, "roll after deprovision")
    62  
    63  	require.False(t, upkeep().DidRollKey, "don't roll after just rolled")
    64  }
    65  
    66  // This engine no-ops when the user has no PUKs.
    67  func TestPerUserKeyUpkeepNoPUK(t *testing.T) {
    68  	tc := SetupEngineTest(t, "pukup")
    69  	defer tc.Cleanup()
    70  	tc.Tp.DisableUpgradePerUserKey = true
    71  
    72  	_ = CreateAndSignupFakeUser(tc, "pukup")
    73  
    74  	upkeep := func() *PerUserKeyUpkeep {
    75  		arg := &PerUserKeyUpkeepArgs{}
    76  		eng := NewPerUserKeyUpkeep(tc.G, arg)
    77  		m := NewMetaContextForTestWithLogUI(tc)
    78  		err := RunEngine2(m, eng)
    79  		require.NoError(t, err)
    80  		return eng
    81  	}
    82  	require.False(t, upkeep().DidRollKey, "no puk, no roll")
    83  
    84  	checkPerUserKeyCountLocal(&tc, 0)
    85  	checkPerUserKeyCount(&tc, 0)
    86  }