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

     1  // Copyright 2015 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  	"context"
     8  	"testing"
     9  
    10  	"github.com/keybase/client/go/libkb"
    11  	"github.com/keybase/client/go/protocol/keybase1"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestSignupEnginePerUserKey(t *testing.T) {
    16  	subTestSignupEngine(t, true)
    17  }
    18  
    19  func TestPerUserKeySignupAndPullKeys(t *testing.T) {
    20  	tc := SetupEngineTest(t, "signup")
    21  	defer tc.Cleanup()
    22  	var err error
    23  
    24  	fu := CreateAndSignupFakeUser(tc, "se")
    25  
    26  	if err = AssertLoggedIn(tc); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	kr, err := libkb.NewPerUserKeyring(tc.G, fu.UID())
    31  	require.NoError(t, err)
    32  	ctx := context.Background()
    33  	mctx := libkb.NewMetaContext(ctx, tc.G)
    34  	err = kr.Sync(mctx)
    35  	require.NoError(t, err)
    36  	gen := keybase1.PerUserKeyGeneration(1)
    37  	require.Equal(t, kr.CurrentGeneration(), gen)
    38  
    39  	sigKey, err := kr.GetLatestSigningKey(mctx)
    40  	require.NoError(t, err)
    41  	require.NotNil(t, sigKey)
    42  	require.NotNil(t, sigKey.Private)
    43  
    44  	encKey, err := kr.GetEncryptionKeyByGeneration(mctx, keybase1.PerUserKeyGeneration(1))
    45  	require.NoError(t, err)
    46  	require.NotNil(t, encKey)
    47  	require.NotNil(t, encKey.Private)
    48  
    49  	encKey, err = kr.GetEncryptionKeyBySeqno(mctx, keybase1.Seqno(3))
    50  	require.NoError(t, err)
    51  	require.NotNil(t, encKey)
    52  	require.NotNil(t, encKey.Private)
    53  
    54  	_, err = kr.GetEncryptionKeyByGeneration(mctx, keybase1.PerUserKeyGeneration(2))
    55  	require.Error(t, err)
    56  
    57  	err = kr.Sync(mctx)
    58  	require.Nil(t, err)
    59  	require.Equal(t, kr.CurrentGeneration(), gen)
    60  }
    61  
    62  func TestPerUserKeySignupPlusPaper(t *testing.T) {
    63  	tc := SetupEngineTest(t, "signup")
    64  	defer tc.Cleanup()
    65  	var err error
    66  
    67  	fu := CreateAndSignupFakeUserPaper(tc, "se")
    68  
    69  	if err = AssertLoggedIn(tc); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	kr, err := libkb.NewPerUserKeyring(tc.G, fu.UID())
    74  	require.NoError(t, err)
    75  	ctx := context.Background()
    76  	mctx := libkb.NewMetaContext(ctx, tc.G)
    77  	err = kr.Sync(mctx)
    78  	require.NoError(t, err)
    79  
    80  	gen := keybase1.PerUserKeyGeneration(1)
    81  	require.Equal(t, kr.CurrentGeneration(), gen)
    82  
    83  	sigKey, err := kr.GetLatestSigningKey(mctx)
    84  	require.NoError(t, err)
    85  	require.NotNil(t, sigKey)
    86  	require.NotNil(t, sigKey.Private)
    87  
    88  	encKey, err := kr.GetEncryptionKeyByGeneration(mctx, keybase1.PerUserKeyGeneration(1))
    89  	require.NoError(t, err)
    90  	require.NotNil(t, encKey)
    91  	require.NotNil(t, encKey.Private)
    92  
    93  	encKey, err = kr.GetEncryptionKeyBySeqno(mctx, keybase1.Seqno(3))
    94  	require.NoError(t, err)
    95  	require.NotNil(t, encKey)
    96  	require.NotNil(t, encKey.Private)
    97  
    98  	_, err = kr.GetEncryptionKeyByGeneration(mctx, keybase1.PerUserKeyGeneration(2))
    99  	require.Error(t, err)
   100  
   101  	err = kr.Sync(mctx)
   102  	require.Nil(t, err)
   103  	require.Equal(t, kr.CurrentGeneration(), gen)
   104  }