github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/device_history_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 "testing" 8 9 "github.com/keybase/client/go/libkb" 10 keybase1 "github.com/keybase/client/go/protocol/keybase1" 11 ) 12 13 func TestDeviceHistoryBasic(t *testing.T) { 14 tc := SetupEngineTest(t, "devhist") 15 defer tc.Cleanup() 16 17 CreateAndSignupFakeUserPaper(tc, "dhst") 18 19 eng := NewDeviceHistorySelf(tc.G) 20 m := NewMetaContextForTest(tc) 21 if err := RunEngine2(m, eng); err != nil { 22 t.Fatal(err) 23 } 24 devs := eng.Devices() 25 if len(devs) != 2 { 26 t.Errorf("num devices: %d, expected 2", len(devs)) 27 } 28 29 var desktop keybase1.DeviceDetail 30 var paper keybase1.DeviceDetail 31 32 for _, d := range devs { 33 switch d.Device.Type { 34 case keybase1.DeviceTypeV2_PAPER: 35 paper = d 36 case keybase1.DeviceTypeV2_DESKTOP: 37 desktop = d 38 default: 39 t.Fatalf("unexpected device type %s", d.Device.Type) 40 } 41 } 42 43 // paper's provisioner should be desktop 44 if paper.Provisioner == nil { 45 t.Fatal("paper device has no provisioner") 46 } 47 if paper.Provisioner.DeviceID != desktop.Device.DeviceID { 48 t.Errorf("paper provisioned id: %s, expected %s", paper.Provisioner.DeviceID, desktop.Device.DeviceID) 49 t.Logf("desktop: %+v", desktop) 50 t.Logf("paper: %+v", paper) 51 } 52 53 // Check that LastUsedTime is set (since we're fetching our own device history) 54 for _, d := range devs { 55 if d.Device.LastUsedTime == 0 { 56 t.Fatal("last used time not set") 57 } 58 } 59 } 60 61 func TestDeviceHistoryRevoked(t *testing.T) { 62 tc := SetupEngineTest(t, "devhist") 63 defer tc.Cleanup() 64 65 u := CreateAndSignupFakeUserPaper(tc, "dhst") 66 67 eng := NewDeviceHistorySelf(tc.G) 68 m := NewMetaContextForTest(tc) 69 if err := RunEngine2(m, eng); err != nil { 70 t.Fatal(err) 71 } 72 73 var desktop keybase1.DeviceDetail 74 var paper keybase1.DeviceDetail 75 76 for _, d := range eng.Devices() { 77 switch d.Device.Type { 78 case keybase1.DeviceTypeV2_PAPER: 79 paper = d 80 case keybase1.DeviceTypeV2_DESKTOP: 81 desktop = d 82 default: 83 t.Fatalf("unexpected device type %s", d.Device.Type) 84 } 85 } 86 87 // paper's provisioner should be desktop 88 if paper.Provisioner == nil { 89 t.Fatal("paper device has no provisioner") 90 } 91 if paper.Provisioner.DeviceID != desktop.Device.DeviceID { 92 t.Errorf("paper provisioned id: %s, expected %s", paper.Provisioner.DeviceID, desktop.Device.DeviceID) 93 t.Logf("desktop: %+v", desktop) 94 t.Logf("paper: %+v", paper) 95 } 96 97 // revoke the paper device 98 uis := libkb.UIs{ 99 SecretUI: u.NewSecretUI(), 100 LogUI: tc.G.UI.GetLogUI(), 101 } 102 m = NewMetaContextForTest(tc).WithUIs(uis) 103 reng := NewRevokeDeviceEngine(tc.G, RevokeDeviceEngineArgs{ID: paper.Device.DeviceID}) 104 if err := RunEngine2(m, reng); err != nil { 105 t.Fatal(err) 106 } 107 108 // get history after revoke 109 eng = NewDeviceHistorySelf(tc.G) 110 if err := RunEngine2(m, eng); err != nil { 111 t.Fatal(err) 112 } 113 114 var desktop2 keybase1.DeviceDetail 115 var paper2 keybase1.DeviceDetail 116 117 for _, d := range eng.Devices() { 118 switch d.Device.Type { 119 case keybase1.DeviceTypeV2_PAPER: 120 paper2 = d 121 case keybase1.DeviceTypeV2_DESKTOP: 122 desktop2 = d 123 default: 124 t.Fatalf("unexpected device type %s", d.Device.Type) 125 } 126 } 127 128 // paper's provisioner should (still) be desktop 129 if paper2.Provisioner == nil { 130 t.Fatal("paper device has no provisioner") 131 } 132 if paper2.Provisioner.DeviceID != desktop2.Device.DeviceID { 133 t.Errorf("paper provisioned id: %s, expected %s", paper2.Provisioner.DeviceID, desktop2.Device.DeviceID) 134 t.Logf("desktop: %+v", desktop2) 135 t.Logf("paper: %+v", paper2) 136 } 137 138 if paper2.RevokedAt == nil { 139 t.Fatal("paper device RevokedAt is nil") 140 } 141 if paper2.RevokedBy.IsNil() { 142 t.Fatal("paper device RevokedBy is nil") 143 } 144 if paper2.RevokedByDevice == nil { 145 t.Fatal("paper device RevokedByDevice is nil") 146 } 147 if paper2.RevokedByDevice.DeviceID != desktop.Device.DeviceID { 148 t.Fatalf("paper revoked by wrong device, %s != %s", paper2.RevokedByDevice.DeviceID, 149 desktop.Device.DeviceID) 150 } 151 if paper2.RevokedByDevice.Name != desktop.Device.Name { 152 t.Fatalf("paper revoked by wrong device, %s != %s", paper2.RevokedByDevice.Name, 153 desktop.Device.Name) 154 } 155 } 156 157 func TestDeviceHistoryPGP(t *testing.T) { 158 tc := SetupEngineTest(t, "devhist") 159 u1 := createFakeUserWithPGPOnly(t, tc) 160 t.Log("Created fake synced pgp user") 161 Logout(tc) 162 tc.Cleanup() 163 164 // redo SetupEngineTest to get a new home directory...should look like a new device. 165 tc = SetupEngineTest(t, "devhist") 166 defer tc.Cleanup() 167 168 uis := libkb.UIs{ 169 ProvisionUI: newTestProvisionUIPassphrase(), 170 LoginUI: &libkb.TestLoginUI{Username: u1.Username}, 171 LogUI: tc.G.UI.GetLogUI(), 172 SecretUI: u1.NewSecretUI(), 173 GPGUI: &gpgtestui{}, 174 } 175 eng := NewLogin(tc.G, keybase1.DeviceTypeV2_DESKTOP, "", keybase1.ClientType_CLI) 176 m := NewMetaContextForTest(tc).WithUIs(uis) 177 if err := RunEngine2(m, eng); err != nil { 178 t.Fatal(err) 179 } 180 181 heng := NewDeviceHistorySelf(tc.G) 182 if err := RunEngine2(m, heng); err != nil { 183 t.Fatal(err) 184 } 185 devs := heng.Devices() 186 if len(devs) != 1 { 187 t.Errorf("num devices: %d, expected 1", len(devs)) 188 } 189 }