github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/user_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 "time" 10 11 "github.com/keybase/client/go/libkb" 12 keybase1 "github.com/keybase/client/go/protocol/keybase1" 13 "github.com/keybase/clockwork" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestLoadUserPlusKeysHasKeys(t *testing.T) { 18 tc := SetupEngineTest(t, "user") 19 defer tc.Cleanup() 20 21 CreateAndSignupFakeUserPaper(tc, "login") 22 me, err := libkb.LoadMe(libkb.NewLoadUserArg(tc.G)) 23 if err != nil { 24 t.Fatal(err) 25 } 26 up, err := libkb.LoadUserPlusKeys(context.TODO(), tc.G, me.GetUID(), "") 27 if err != nil { 28 t.Fatal(err) 29 } 30 if len(up.DeviceKeys) != 4 { 31 t.Errorf("num device keys: %d, expected 4", len(up.DeviceKeys)) 32 } 33 } 34 35 func TestLoadUserPlusKeysRevoked(t *testing.T) { 36 fakeClock := clockwork.NewFakeClockAt(time.Now()) 37 tc := SetupEngineTest(t, "login") 38 defer tc.Cleanup() 39 tc.G.SetClock(fakeClock) 40 41 fu := CreateAndSignupFakeUserPaper(tc, "login") 42 me, err := libkb.LoadMe(libkb.NewLoadUserArg(tc.G)) 43 if err != nil { 44 t.Fatal(err) 45 } 46 up, err := libkb.LoadUserPlusKeys(context.TODO(), tc.G, me.GetUID(), "") 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 if len(up.DeviceKeys) != 4 { 52 t.Errorf("device keys: %d, expected 4", len(up.DeviceKeys)) 53 } 54 if len(up.RevokedDeviceKeys) != 0 { 55 t.Errorf("revoked keys: %d, expected 0", len(up.RevokedDeviceKeys)) 56 } 57 58 devices, _ := getActiveDevicesAndKeys(tc, fu) 59 var paper *libkb.Device 60 for _, device := range devices { 61 if device.Type == keybase1.DeviceTypeV2_PAPER { 62 paper = device.Device 63 break 64 } 65 } 66 67 if err := doRevokeDevice(tc, fu, paper.ID, false, false); err != nil { 68 t.Fatal(err) 69 } 70 fakeClock.Advance(libkb.CachedUserTimeout + 2*time.Second) 71 72 up2, err := libkb.LoadUserPlusKeys(context.TODO(), tc.G, me.GetUID(), "") 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 if len(up2.DeviceKeys) != 2 { 78 t.Errorf("device keys: %d, expected 2", len(up2.DeviceKeys)) 79 } 80 if len(up2.RevokedDeviceKeys) != 2 { 81 t.Errorf("revoked keys: %d, expected 2", len(up2.RevokedDeviceKeys)) 82 } 83 } 84 85 // TestMerkleHashMetaAndFirstAppearedInKeyFamily tests new user & key family features: 86 // - FirstAppearedMerkleSeqnoUnverified in sig chain links 87 // - EldestSeqno in sig chain links 88 // - HashMeta in sig chain links 89 // 90 // We should be able to see these fields in sigchains and also propagated through 91 // to the KeyFamilies 92 func TestMerkleHashMetaAndFirstAppearedInKeyFamily(t *testing.T) { 93 tc := SetupEngineTest(t, "login") 94 defer tc.Cleanup() 95 CreateAndSignupFakeUser(tc, "login") 96 me, err := libkb.LoadMe(libkb.NewLoadUserArg(tc.G)) 97 require.NoError(t, err) 98 99 ckf := me.GetComputedKeyFamily() 100 checkKey := func(key libkb.GenericKey, cki libkb.ComputedKeyInfo, err error) { 101 require.NoError(t, err) 102 require.NotNil(t, key, "non-nil key") 103 require.Equal(t, len(cki.DelegatedAtHashMeta), 32, "needed a SHA256 hash for merkle hash_meta") 104 require.True(t, (cki.FirstAppearedUnverified > 0), "need a >0 merkle root first appeared in") 105 } 106 checkSibkey := func(kid keybase1.KID) { 107 checkKey(ckf.FindActiveSibkey(kid)) 108 } 109 checkSubkey := func(kid keybase1.KID) { 110 checkKey(ckf.FindActiveEncryptionSubkey(kid)) 111 } 112 113 for _, sibkey := range ckf.GetAllActiveSibkeys() { 114 checkSibkey(sibkey.GetKID()) 115 } 116 for _, subkey := range ckf.GetAllActiveSubkeys() { 117 checkSubkey(subkey.GetKID()) 118 } 119 } 120 121 func assertPostedHighSkipSeqno(t *testing.T, tc libkb.TestContext, name string, seqno int) { 122 u, err := libkb.LoadUser(libkb.NewLoadUserByNameArg(tc.G, name)) 123 if err != nil { 124 t.Fatal(err) 125 } 126 127 highSkip := u.GetLastLink().GetHighSkip() 128 require.Equal(t, highSkip.Seqno, keybase1.Seqno(seqno)) 129 } 130 131 func TestBlankUserHighSkip(t *testing.T) { 132 tc := SetupEngineTest(t, "user") 133 defer tc.Cleanup() 134 135 i := CreateAndSignupFakeUser(tc, "login") 136 137 assertPostedHighSkipSeqno(t, tc, i.Username, 1) 138 } 139 140 func TestPaperUserHighSkip(t *testing.T) { 141 tc := SetupEngineTest(t, "user") 142 defer tc.Cleanup() 143 them, _ := createFakeUserWithNoKeys(tc) 144 145 i := CreateAndSignupFakeUserPaper(tc, "login") 146 assertPostedHighSkipSeqno(t, tc, i.Username, 4) 147 148 trackUser(tc, i, libkb.NewNormalizedUsername(them), libkb.GetDefaultSigVersion(tc.G)) 149 assertPostedHighSkipSeqno(t, tc, i.Username, 4) 150 151 eng := NewPaperKey(tc.G) 152 uis := libkb.UIs{ 153 LogUI: tc.G.UI.GetLogUI(), 154 LoginUI: &libkb.TestLoginUI{}, 155 SecretUI: &libkb.TestSecretUI{}, 156 } 157 m := NewMetaContextForTest(tc).WithUIs(uis) 158 if err := RunEngine2(m, eng); err != nil { 159 t.Fatal(err) 160 } 161 assertPostedHighSkipSeqno(t, tc, i.Username, 7) 162 }