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

     1  package ephemeral
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestNewDeviceEK(t *testing.T) {
    11  	tc, mctx, _ := ephemeralKeyTestSetup(t)
    12  	defer tc.Cleanup()
    13  
    14  	merkleRootPtr, err := tc.G.GetMerkleClient().FetchRootFromServer(mctx, libkb.EphemeralKeyMerkleFreshness)
    15  	require.NoError(t, err)
    16  	merkleRoot := *merkleRootPtr
    17  
    18  	publishedMetadata, err := publishNewDeviceEK(mctx, merkleRoot)
    19  	require.NoError(t, err)
    20  
    21  	s := tc.G.GetDeviceEKStorage()
    22  	deviceEK, err := s.Get(mctx, publishedMetadata.Generation)
    23  	require.NoError(t, err)
    24  	// Clear out DeviceCtime since it won't be present in fetched data, it's
    25  	// only known locally.
    26  	require.NotEqual(t, 0, deviceEK.Metadata.DeviceCtime)
    27  	deviceEK.Metadata.DeviceCtime = 0
    28  	require.Equal(t, deviceEK.Metadata, publishedMetadata)
    29  
    30  	fetchedDevices, err := allActiveDeviceEKMetadata(mctx, merkleRoot)
    31  	require.NoError(t, err)
    32  
    33  	require.Equal(t, 1, len(fetchedDevices))
    34  	for _, fetchedDeviceMetadata := range fetchedDevices {
    35  		require.Equal(t, publishedMetadata, fetchedDeviceMetadata)
    36  	}
    37  	maxGeneration, err := s.MaxGeneration(mctx, false)
    38  	require.NoError(t, err)
    39  
    40  	require.EqualValues(t, maxGeneration, publishedMetadata.Generation)
    41  
    42  	// If we publish again, we increase the generation
    43  	publishedMetadata2, err := publishNewDeviceEK(mctx, merkleRoot)
    44  	require.NoError(t, err)
    45  	require.EqualValues(t, maxGeneration+1, publishedMetadata2.Generation)
    46  
    47  	rawStorage := NewDeviceEKStorage(mctx)
    48  	// Put our storage in a bad state by deleting the maxGeneration
    49  	err = rawStorage.Delete(mctx, maxGeneration+1, "")
    50  	require.NoError(t, err)
    51  
    52  	// If we publish in a bad local state, we can successfully get the
    53  	// maxGeneration from the server and continue
    54  	publishedMetadata3, err := publishNewDeviceEK(mctx, merkleRoot)
    55  	require.NoError(t, err)
    56  	require.EqualValues(t, maxGeneration+2, publishedMetadata3.Generation)
    57  }
    58  
    59  // TODO: test cases chat verify we can detect invalid signatures and bad metadata