@@ -126,3 +126,52 @@ func TestNodeArgToID(t *testing.T) {
126126 t .Errorf ("address: got %d, want 99" , got )
127127 }
128128}
129+
130+ // TestCmdRecoveryRecoverInstallsNewIdentity covers the most destructive command:
131+ // keyless force-rotate via the registry, then install the new identity locally.
132+ func TestCmdRecoveryRecoverInstallsNewIdentity (t * testing.T ) {
133+ dir := t .TempDir ()
134+ newKey := filepath .Join (dir , "new.json" )
135+ id , err := crypto .GenerateIdentity ()
136+ if err != nil {
137+ t .Fatalf ("keygen: %v" , err )
138+ }
139+ if err := crypto .SaveIdentity (newKey , id ); err != nil {
140+ t .Fatalf ("save new key: %v" , err )
141+ }
142+ newPub := crypto .EncodePublicKey (id .PublicKey )
143+ idPath := filepath .Join (dir , "installed.json" )
144+
145+ r := newFakeRegistry (t )
146+ var gotPub , gotRecovery string
147+ r .on ("recover_identity" , func (req map [string ]interface {}) map [string ]interface {} {
148+ gotPub , _ = req ["new_public_key" ].(string )
149+ gotRecovery , _ = req ["recovery" ].(string )
150+ return map [string ]interface {}{"type" : "recover_identity_ok" , "ok" : true , "node_id" : float64 (99 )}
151+ })
152+ useRegistry (t , r )
153+
154+ prev := jsonOutput
155+ defer func () { jsonOutput = prev }()
156+ jsonOutput = true
157+ _ = captureStdout (t , func () {
158+ cmdRecovery ([]string {"recover" , "--node" , "99" , "--new-key" , newKey ,
159+ "--recovery" , "pilotrecover:v1:99:bmV3:Y29t:9999999999:nn:rec-v1" , "--recovery-sig" , "c2ln" ,
160+ "--identity" , idPath })
161+ })
162+
163+ if gotRecovery == "" {
164+ t .Fatal ("registry never received recover_identity" )
165+ }
166+ if gotPub != newPub {
167+ t .Errorf ("registry got new_public_key=%q, want the new-key pubkey %q" , gotPub , newPub )
168+ }
169+ // The recovered key must be installed at the daemon identity path.
170+ installed , err := crypto .LoadIdentity (idPath )
171+ if err != nil {
172+ t .Fatalf ("new identity not installed at %s: %v" , idPath , err )
173+ }
174+ if crypto .EncodePublicKey (installed .PublicKey ) != newPub {
175+ t .Errorf ("installed identity does not match the recovered key" )
176+ }
177+ }
0 commit comments